This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 779
Expand file tree
/
Copy pathtest_json_parser.py
More file actions
125 lines (107 loc) · 2.82 KB
/
Copy pathtest_json_parser.py
File metadata and controls
125 lines (107 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import json
from typing import Any, List
import pytest
from taskweaver.utils import json_parser
obj_cases: List[Any] = [
["hello", "world"],
"any_str",
{
"test_key": {
"str_array": ["hello", "world", "test"],
"another_key": {},
"empty_array": [[[]], []],
},
},
[True, False, None],
[1, 2, 3],
123.345,
{"val": 123.345},
[
{
"a": {},
"b": {},
"c": {},
"d": {},
"e": {},
},
],
{
"test_key": {
"str_array": ["hello", "world", "test"],
"test another key": [
"hello",
"world",
1,
2.0,
True,
False,
None,
{
"test yet another key": "test value",
"test yet key 2": '\r\n\u1234\ffdfd\tfdfv\b"',
},
],
True: False,
},
},
]
@pytest.mark.parametrize("obj", obj_cases)
def test_json_parser(obj: Any):
dumped_str = json.dumps(obj)
# expect error with the JSON is incomplete
for i in range(len(dumped_str) - 1):
cur_incomplete_seg = dumped_str[:i]
try:
float(cur_incomplete_seg)
# skip incomplete number that is valid JSON as well
continue
except ValueError:
pass
with pytest.raises(json_parser.StreamJsonParserError):
json_parser.parse_json(cur_incomplete_seg)
# proper parsing exception should raise before this
raise Exception("Failed to parse incomplete JSON: " + cur_incomplete_seg)
obj = json_parser.parse_json(json.dumps(obj))
dumped_str2 = json.dumps(obj)
assert dumped_str == dumped_str2
str_cases: List[str] = [
' { "a": [ true, false, null ] } ',
" \r \n \t [ \r \n \t true, false, null \r \n \t ] \r \n \t ",
' \r \n \t "hello world" \r \n \t ',
]
@pytest.mark.parametrize("str_case", str_cases)
def test_json_parser_str(str_case: str):
obj = json.loads(str_case)
dumped_str = json.dumps(obj)
obj = json_parser.parse_json(str_case)
dumped_str2 = json.dumps(obj)
assert dumped_str == dumped_str2
bad_cases: List[str] = [
" - ",
"'abc'",
"\\a",
"{} {}",
"[[[]}]",
'""""',
"{'abc': 'def'}",
"[[[[{{{{0}}}}]]]]",
" ",
"",
"((((()))))",
'"\\"',
# incomplete json
'"abc',
"[1,2,3",
'{"abc',
'{"abc":',
'{"abc":}',
'{"abc":1',
"123,456,789",
"undefined",
"None",
"{true: false}",
]
@pytest.mark.parametrize("bad_case", bad_cases)
def test_json_parser_bad(bad_case: str):
with pytest.raises(json_parser.StreamJsonParserError):
json_parser.parse_json(bad_case)