Example #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
Example #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
Example #3
0
def test_loads_object():
    assert oj.loads('{"key1": 1, "key2": "val"}') == {"key1": 1, "key2": "val"}
Example #4
0
def test_loads_empty_object():
    assert oj.loads("{}") == {}
Example #5
0
def test_loads_nested_list():
    assert oj.loads('[true, [1, "string"]]') == [True, [1, "string"]]
Example #6
0
def test_loads_list():
    assert oj.loads("[true, 0.5, null]") == [True, 0.5, None]
Example #7
0
def test_loads_empty_list():
    assert oj.loads("[]") == []
Example #8
0
def test_lex_nested_list():
    lst = "[true, [false, null]]"
    assert oj.loads(lst) == [True, [False, None]]
Example #9
0
def test_compared_to_stdlib_success(input_json):
    assert_json_equal(oj.loads(input_json), json.loads(input_json))
Example #10
0
def test_float_positive(num):
    assert oj.loads(num) == float(num)
Example #11
0
def test_integer_positive(num):
    assert oj.loads(num) == int(num)
Example #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)'
Example #13
0
def test_string_positive(string_literal):
    assert oj.loads(string_literal) == string_literal[1:-1]
Example #14
0
def test_empty_string():
    assert oj.loads('""') == ""
Example #15
0
def test_null_positive():
    assert oj.loads("null") is None
Example #16
0
def test_rejects_non_string_keys():
    with pytest.raises(JSONDecodeError):
        assert oj.loads('{5: "val"}')
Example #17
0
def test_bool_positive(boolean):
    literal = str(boolean).lower()
    assert oj.loads(literal) == boolean
Example #18
0
def test_list():
    lst = "[true, false, null]"
    assert oj.loads(lst) == [True, False, None]