5
from autogpt.core.utils.json_utils import json_loads
7
_JSON_FIXABLE: list[tuple[str, str]] = [
9
('{"name": "John Doe" "age": 30,}', '{"name": "John Doe", "age": 30}'),
10
("[1, 2 3]", "[1, 2, 3]"),
12
('{"name": "John Doe", "age": 30,}', '{"name": "John Doe", "age": 30}'),
13
("[1, 2, 3,]", "[1, 2, 3]"),
14
# Extra comma in object
15
('{"name": "John Doe",, "age": 30}', '{"name": "John Doe", "age": 30}'),
17
('{"name": "John Doe",\n"age": 30}', '{"name": "John Doe", "age": 30}'),
18
("[1, 2,\n3]", "[1, 2, 3]"),
19
# Missing closing brace or bracket
20
('{"name": "John Doe", "age": 30', '{"name": "John Doe", "age": 30}'),
21
("[1, 2, 3", "[1, 2, 3]"),
23
("[+1, ---2, .5, +-4.5, 123.]", "[1, -2, 0.5, -4.5, 123]"),
24
('{"bin": 0b1001, "hex": 0x1A, "oct": 0o17}', '{"bin": 9, "hex": 26, "oct": 15}'),
27
'[1, 2 3, "yes" true, false null, 25, {"obj": "var"}',
28
'[1, 2, 3, "yes", true, false, null, 25, {"obj": "var"}]',
32
'```json\n{"name": "John Doe", "age": 30}\n```',
33
'{"name": "John Doe", "age": 30}',
37
'{"name":"John Doe" "age": 30\n "empty": "","address": '
39
'{"city": "New York", "state": "NY"},'
40
'"skills": ["Python" "C++", "Java",""],',
41
'{"name": "John Doe", "age": 30, "empty": "", "address": '
42
'{"city": "New York", "state": "NY"}, '
43
'"skills": ["Python", "C++", "Java", ""]}',
47
'{"name": "John Doe", "age": 30, "address": '
48
'{"city": "New York", "state": "NY"}, '
49
'"skills": ["Python", "C++", "Java"]}',
50
'{"name": "John Doe", "age": 30, "address": '
51
'{"city": "New York", "state": "NY"}, '
52
'"skills": ["Python", "C++", "Java"]}',
58
('"Hello, World!"', '"Hello, World!"'),
63
_JSON_UNFIXABLE: list[tuple[str, str]] = [
64
# Broken booleans and null
65
("[TRUE, False, NULL]", "[true, false, null]"),
66
# Missing values in array
67
("[1, , 3]", "[1, 3]"),
68
# Leading zeros (are treated as octal)
69
("[0023, 015]", "[23, 15]"),
71
('{"name": John Doe}', '{"name": "John Doe"}'),
72
# Missing opening braces or bracket
73
('"name": "John Doe"}', '{"name": "John Doe"}'),
74
("1, 2, 3]", "[1, 2, 3]"),
78
@pytest.fixture(params=_JSON_FIXABLE)
79
def fixable_json(request: pytest.FixtureRequest) -> tuple[str, str]:
83
@pytest.fixture(params=_JSON_UNFIXABLE)
84
def unfixable_json(request: pytest.FixtureRequest) -> tuple[str, str]:
88
def test_json_loads_fixable(fixable_json: tuple[str, str]):
89
assert json_loads(fixable_json[0]) == json.loads(fixable_json[1])
92
def test_json_loads_unfixable(unfixable_json: tuple[str, str]):
93
assert json_loads(unfixable_json[0]) != json.loads(unfixable_json[1])