Debian Codegraph @

Lib/test/test_json/test_fail.py

raw
1 from test.test_json import PyTest, CTest
2
3 # 2007-10-05
4 JSONDOCS = [
5 # https://json.org/JSON_checker/test/fail1.json
6 '"A JSON payload should be an object or array, not a string."',
7 # https://json.org/JSON_checker/test/fail2.json
8 '["Unclosed array"',
9 # https://json.org/JSON_checker/test/fail3.json
10 '{unquoted_key: "keys must be quoted"}',
11 # https://json.org/JSON_checker/test/fail4.json
12 '["extra comma",]',
13 # https://json.org/JSON_checker/test/fail5.json
14 '["double extra comma",,]',
15 # https://json.org/JSON_checker/test/fail6.json
16 '[ , "<-- missing value"]',
17 # https://json.org/JSON_checker/test/fail7.json
18 '["Comma after the close"],',
19 # https://json.org/JSON_checker/test/fail8.json
20 '["Extra close"]]',
21 # https://json.org/JSON_checker/test/fail9.json
22 '{"Extra comma": true,}',
23 # https://json.org/JSON_checker/test/fail10.json
24 '{"Extra value after close": true} "misplaced quoted value"',
25 # https://json.org/JSON_checker/test/fail11.json
26 '{"Illegal expression": 1 + 2}',
27 # https://json.org/JSON_checker/test/fail12.json
28 '{"Illegal invocation": alert()}',
29 # https://json.org/JSON_checker/test/fail13.json
30 '{"Numbers cannot have leading zeroes": 013}',
31 # https://json.org/JSON_checker/test/fail14.json
32 '{"Numbers cannot be hex": 0x14}',
33 # https://json.org/JSON_checker/test/fail15.json
34 '["Illegal backslash escape: \\x15"]',
35 # https://json.org/JSON_checker/test/fail16.json
36 '[\\naked]',
37 # https://json.org/JSON_checker/test/fail17.json
38 '["Illegal backslash escape: \\017"]',
39 # https://json.org/JSON_checker/test/fail18.json
40 '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
41 # https://json.org/JSON_checker/test/fail19.json
42 '{"Missing colon" null}',
43 # https://json.org/JSON_checker/test/fail20.json
44 '{"Double colon":: null}',
45 # https://json.org/JSON_checker/test/fail21.json
46 '{"Comma instead of colon", null}',
47 # https://json.org/JSON_checker/test/fail22.json
48 '["Colon instead of comma": false]',
49 # https://json.org/JSON_checker/test/fail23.json
50 '["Bad value", truth]',
51 # https://json.org/JSON_checker/test/fail24.json
52 "['single quote']",
53 # https://json.org/JSON_checker/test/fail25.json
54 '["\ttab\tcharacter\tin\tstring\t"]',
55 # https://json.org/JSON_checker/test/fail26.json
56 '["tab\\ character\\ in\\ string\\ "]',
57 # https://json.org/JSON_checker/test/fail27.json
58 '["line\nbreak"]',
59 # https://json.org/JSON_checker/test/fail28.json
60 '["line\\\nbreak"]',
61 # https://json.org/JSON_checker/test/fail29.json
62 '[0e]',
63 # https://json.org/JSON_checker/test/fail30.json
64 '[0e+]',
65 # https://json.org/JSON_checker/test/fail31.json
66 '[0e+-1]',
67 # https://json.org/JSON_checker/test/fail32.json
68 '{"Comma instead if closing brace": true,',
69 # https://json.org/JSON_checker/test/fail33.json
70 '["mismatch"}',
71 # https://code.google.com/archive/p/simplejson/issues/3
72 '["A\u001FZ control characters in string"]',
73 ]
74
75 SKIPS = {
76 1: "why not have a string payload?",
77 18: "spec doesn't specify any nesting limitations",
78 }
79
80 class TestFail:
81 def test_failures(self):
82 for idx, doc in enumerate(JSONDOCS):
83 idx = idx + 1
84 if idx in SKIPS:
85 self.loads(doc)
86 continue
87 try:
88 self.loads(doc)
89 except self.JSONDecodeError:
90 pass
91 else:
92 self.fail(f"Expected failure for fail{idx}.json: {doc!r}")
93
94 def test_non_string_keys_dict(self):
95 data = {'a' : 1, (1, 2) : 2}
96 with self.assertRaisesRegex(TypeError,
97 'keys must be str, int, float, bool or None, not tuple'):
98 self.dumps(data)
99
100 def test_not_serializable(self):
101 import sys
102 with self.assertRaisesRegex(TypeError,
103 'Object of type module is not JSON serializable') as cm:
104 self.dumps(sys)
105 self.assertNotHasAttr(cm.exception, '__notes__')
106
107 with self.assertRaises(TypeError) as cm:
108 self.dumps([1, [2, 3, sys]])
109 self.assertEqual(cm.exception.__notes__,
110 ['when serializing list item 2',
111 'when serializing list item 1'])
112
113 with self.assertRaises(TypeError) as cm:
114 self.dumps((1, (2, 3, sys)))
115 self.assertEqual(cm.exception.__notes__,
116 ['when serializing tuple item 2',
117 'when serializing tuple item 1'])
118
119 with self.assertRaises(TypeError) as cm:
120 self.dumps({'a': {'b': sys}})
121 self.assertEqual(cm.exception.__notes__,
122 ["when serializing dict item 'b'",
123 "when serializing dict item 'a'"])
124
125 def test_truncated_input(self):
126 test_cases = [
127 ('', 'Expecting value', 0),
128 ('[', 'Expecting value', 1),
129 ('[42', "Expecting ',' delimiter", 3),
130 ('[42,', 'Expecting value', 4),
131 ('["', 'Unterminated string starting at', 1),
132 ('["spam', 'Unterminated string starting at', 1),
133 ('["spam"', "Expecting ',' delimiter", 7),
134 ('["spam",', 'Expecting value', 8),
135 ('{', 'Expecting property name enclosed in double quotes', 1),
136 ('{"', 'Unterminated string starting at', 1),
137 ('{"spam', 'Unterminated string starting at', 1),
138 ('{"spam"', "Expecting ':' delimiter", 7),
139 ('{"spam":', 'Expecting value', 8),
140 ('{"spam":42', "Expecting ',' delimiter", 10),
141 ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11),
142 ]
143 test_cases += [
144 ('"', 'Unterminated string starting at', 0),
145 ('"spam', 'Unterminated string starting at', 0),
146 ]
147 for data, msg, idx in test_cases:
148 with self.assertRaises(self.JSONDecodeError) as cm:
149 self.loads(data)
150 err = cm.exception
151 self.assertEqual(err.msg, msg)
152 self.assertEqual(err.pos, idx)
153 self.assertEqual(err.lineno, 1)
154 self.assertEqual(err.colno, idx + 1)
155 self.assertEqual(str(err),
156 '%s: line 1 column %d (char %d)' %
157 (msg, idx + 1, idx))
158
159 def test_unexpected_data(self):
160 test_cases = [
161 ('[,', 'Expecting value', 1),
162 ('{"spam":[}', 'Expecting value', 9),
163 ('[42:', "Expecting ',' delimiter", 3),
164 ('[42 "spam"', "Expecting ',' delimiter", 4),
165 ('[42,]', "Illegal trailing comma before end of array", 3),
166 ('{"spam":[42}', "Expecting ',' delimiter", 11),
167 ('["]', 'Unterminated string starting at', 1),
168 ('["spam":', "Expecting ',' delimiter", 7),
169 ('["spam",]', "Illegal trailing comma before end of array", 7),
170 ('{:', 'Expecting property name enclosed in double quotes', 1),
171 ('{,', 'Expecting property name enclosed in double quotes', 1),
172 ('{42', 'Expecting property name enclosed in double quotes', 1),
173 ('[{]', 'Expecting property name enclosed in double quotes', 2),
174 ('{"spam",', "Expecting ':' delimiter", 7),
175 ('{"spam"}', "Expecting ':' delimiter", 7),
176 ('[{"spam"]', "Expecting ':' delimiter", 8),
177 ('{"spam":}', 'Expecting value', 8),
178 ('[{"spam":]', 'Expecting value', 9),
179 ('{"spam":42 "ham"', "Expecting ',' delimiter", 11),
180 ('[{"spam":42]', "Expecting ',' delimiter", 11),
181 ('{"spam":42,}', "Illegal trailing comma before end of object", 10),
182 ('{"spam":42 , }', "Illegal trailing comma before end of object", 11),
183 ('[123 , ]', "Illegal trailing comma before end of array", 6),
184 ]
185 for data, msg, idx in test_cases:
186 with self.assertRaises(self.JSONDecodeError) as cm:
187 self.loads(data)
188 err = cm.exception
189 self.assertEqual(err.msg, msg)
190 self.assertEqual(err.pos, idx)
191 self.assertEqual(err.lineno, 1)
192 self.assertEqual(err.colno, idx + 1)
193 self.assertEqual(str(err),
194 '%s: line 1 column %d (char %d)' %
195 (msg, idx + 1, idx))
196
197 def test_extra_data(self):
198 test_cases = [
199 ('[]]', 'Extra data', 2),
200 ('{}}', 'Extra data', 2),
201 ('[],[]', 'Extra data', 2),
202 ('{},{}', 'Extra data', 2),
203 ]
204 test_cases += [
205 ('42,"spam"', 'Extra data', 2),
206 ('"spam",42', 'Extra data', 6),
207 ]
208 for data, msg, idx in test_cases:
209 with self.assertRaises(self.JSONDecodeError) as cm:
210 self.loads(data)
211 err = cm.exception
212 self.assertEqual(err.msg, msg)
213 self.assertEqual(err.pos, idx)
214 self.assertEqual(err.lineno, 1)
215 self.assertEqual(err.colno, idx + 1)
216 self.assertEqual(str(err),
217 '%s: line 1 column %d (char %d)' %
218 (msg, idx + 1, idx))
219
220 def test_linecol(self):
221 test_cases = [
222 ('!', 1, 1, 0),
223 (' !', 1, 2, 1),
224 ('\n!', 2, 1, 1),
225 ('\n \n\n !', 4, 6, 10),
226 ]
227 for data, line, col, idx in test_cases:
228 with self.assertRaises(self.JSONDecodeError) as cm:
229 self.loads(data)
230 err = cm.exception
231 self.assertEqual(err.msg, 'Expecting value')
232 self.assertEqual(err.pos, idx)
233 self.assertEqual(err.lineno, line)
234 self.assertEqual(err.colno, col)
235 self.assertEqual(str(err),
236 'Expecting value: line %s column %d (char %d)' %
237 (line, col, idx))
238
239 class TestPyFail(TestFail, PyTest): pass
240 class TestCFail(TestFail, CTest): pass