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