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