Ejemplo n.º 1
0
def test_compared_to_stdlib_random_text(input_text):
    try:
        json.loads(input_text)
    except json.JSONDecodeError:
        stdlib_raises = True
    else:
        stdlib_raises = False

    try:
        oj_result = oj.loads(input_text)
    except oj.JSONDecodeError as exc:
        oj_raises = True
        oj_result = exc
    else:
        oj_raises = False

    assert stdlib_raises == oj_raises, oj_result
Ejemplo n.º 2
0
def test_compared_to_stdlib_corrupted_json(input_json):
    # Randomly removing a character isn't guaranteed to make the input invalid (e.g.,
    # if we happen to remove a character from the middle of a string), so it's possible
    # that parsing the input shouldn't raise an exception. Therefore, we only care that
    # oj.loads(x) raises an exception iff json.loads(x) raises an exception for all
    # strings x.
    try:
        json.loads(input_json)
    except json.JSONDecodeError:
        stdlib_raises = True
    else:
        stdlib_raises = False

    try:
        oj_result = oj.loads(input_json)
    except JSONDecodeError as exc:
        oj_raises = True
        oj_result = exc
    else:
        oj_raises = False

    assert stdlib_raises == oj_raises, oj_result
Ejemplo n.º 3
0
def test_loads_object():
    assert oj.loads('{"key1": 1, "key2": "val"}') == {"key1": 1, "key2": "val"}
Ejemplo n.º 4
0
def test_loads_empty_object():
    assert oj.loads("{}") == {}
Ejemplo n.º 5
0
def test_loads_nested_list():
    assert oj.loads('[true, [1, "string"]]') == [True, [1, "string"]]
Ejemplo n.º 6
0
def test_loads_list():
    assert oj.loads("[true, 0.5, null]") == [True, 0.5, None]
Ejemplo n.º 7
0
def test_loads_empty_list():
    assert oj.loads("[]") == []
Ejemplo n.º 8
0
def test_lex_nested_list():
    lst = "[true, [false, null]]"
    assert oj.loads(lst) == [True, [False, None]]
Ejemplo n.º 9
0
def test_compared_to_stdlib_success(input_json):
    assert_json_equal(oj.loads(input_json), json.loads(input_json))
Ejemplo n.º 10
0
def test_float_positive(num):
    assert oj.loads(num) == float(num)
Ejemplo n.º 11
0
def test_integer_positive(num):
    assert oj.loads(num) == int(num)
Ejemplo n.º 12
0
def test_string_with_escapes():
    string_literal = r'"string with a \\ (backslash) and \" (quote)"'
    assert oj.loads(
        string_literal) == r'string with a \ (backslash) and " (quote)'
Ejemplo n.º 13
0
def test_string_positive(string_literal):
    assert oj.loads(string_literal) == string_literal[1:-1]
Ejemplo n.º 14
0
def test_empty_string():
    assert oj.loads('""') == ""
Ejemplo n.º 15
0
def test_null_positive():
    assert oj.loads("null") is None
Ejemplo n.º 16
0
def test_rejects_non_string_keys():
    with pytest.raises(JSONDecodeError):
        assert oj.loads('{5: "val"}')
Ejemplo n.º 17
0
def test_bool_positive(boolean):
    literal = str(boolean).lower()
    assert oj.loads(literal) == boolean
Ejemplo n.º 18
0
def test_list():
    lst = "[true, false, null]"
    assert oj.loads(lst) == [True, False, None]