Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(121084)

Side by Side Diff: Lib/test/test_json/test_fail.py

Issue 19361: Specialize exceptions thrown by JSON parser
Patch Set: Created 4 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_json/test_decode.py ('k') | Lib/test/test_json/test_scanstring.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from test.test_json import PyTest, CTest 1 from test.test_json import PyTest, CTest
2 import re 2 import re
3 3
4 # 2007-10-05 4 # 2007-10-05
5 JSONDOCS = [ 5 JSONDOCS = [
6 # http://json.org/JSON_checker/test/fail1.json 6 # http://json.org/JSON_checker/test/fail1.json
7 '"A JSON payload should be an object or array, not a string."', 7 '"A JSON payload should be an object or array, not a string."',
8 # http://json.org/JSON_checker/test/fail2.json 8 # http://json.org/JSON_checker/test/fail2.json
9 '["Unclosed array"', 9 '["Unclosed array"',
10 # http://json.org/JSON_checker/test/fail3.json 10 # http://json.org/JSON_checker/test/fail3.json
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 80
81 class TestFail: 81 class TestFail:
82 def test_failures(self): 82 def test_failures(self):
83 for idx, doc in enumerate(JSONDOCS): 83 for idx, doc in enumerate(JSONDOCS):
84 idx = idx + 1 84 idx = idx + 1
85 if idx in SKIPS: 85 if idx in SKIPS:
86 self.loads(doc) 86 self.loads(doc)
87 continue 87 continue
88 try: 88 try:
89 self.loads(doc) 89 self.loads(doc)
90 except ValueError: 90 except self.JSONDecodeError:
91 pass 91 pass
92 else: 92 else:
93 self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc)) 93 self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
94 94
95 def test_non_string_keys_dict(self): 95 def test_non_string_keys_dict(self):
96 data = {'a' : 1, (1, 2) : 2} 96 data = {'a' : 1, (1, 2) : 2}
97 97
98 #This is for c encoder 98 #This is for c encoder
99 self.assertRaises(TypeError, self.dumps, data) 99 self.assertRaises(TypeError, self.dumps, data)
100 100
(...skipping 16 matching lines...) Expand all
117 ('{"spam"', "Expecting ':' delimiter", 7), 117 ('{"spam"', "Expecting ':' delimiter", 7),
118 ('{"spam":', 'Expecting value', 8), 118 ('{"spam":', 'Expecting value', 8),
119 ('{"spam":42', "Expecting ',' delimiter", 10), 119 ('{"spam":42', "Expecting ',' delimiter", 10),
120 ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11), 120 ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11),
121 ] 121 ]
122 test_cases += [ 122 test_cases += [
123 ('"', 'Unterminated string starting at', 0), 123 ('"', 'Unterminated string starting at', 0),
124 ('"spam', 'Unterminated string starting at', 0), 124 ('"spam', 'Unterminated string starting at', 0),
125 ] 125 ]
126 for data, msg, idx in test_cases: 126 for data, msg, idx in test_cases:
127 self.assertRaisesRegex(ValueError, 127 with self.assertRaises(self.JSONDecodeError) as cm:
128 r'^{0}: line 1 column {1} \(char {2}\)'.format( 128 self.loads(data)
129 re.escape(msg), idx + 1, idx), 129 err = cm.exception
130 self.loads, data) 130 self.assertEqual(err.msg, msg)
131 self.assertEqual(err.pos, idx)
132 self.assertEqual(err.lineno, 1)
133 self.assertEqual(err.colno, idx + 1)
134 errmsg = ('%s: line 1 column %d (char %d)' %
135 (msg, idx + 1, idx))
136 self.assertRegex(str(err), '^' + re.escape(errmsg))
131 137
132 def test_unexpected_data(self): 138 def test_unexpected_data(self):
133 test_cases = [ 139 test_cases = [
134 ('[,', 'Expecting value', 1), 140 ('[,', 'Expecting value', 1),
135 ('{"spam":[}', 'Expecting value', 9), 141 ('{"spam":[}', 'Expecting value', 9),
136 ('[42:', "Expecting ',' delimiter", 3), 142 ('[42:', "Expecting ',' delimiter", 3),
137 ('[42 "spam"', "Expecting ',' delimiter", 4), 143 ('[42 "spam"', "Expecting ',' delimiter", 4),
138 ('[42,]', 'Expecting value', 4), 144 ('[42,]', 'Expecting value', 4),
139 ('{"spam":[42}', "Expecting ',' delimiter", 11), 145 ('{"spam":[42}', "Expecting ',' delimiter", 11),
140 ('["]', 'Unterminated string starting at', 1), 146 ('["]', 'Unterminated string starting at', 1),
141 ('["spam":', "Expecting ',' delimiter", 7), 147 ('["spam":', "Expecting ',' delimiter", 7),
142 ('["spam",]', 'Expecting value', 8), 148 ('["spam",]', 'Expecting value', 8),
143 ('{:', 'Expecting property name enclosed in double quotes', 1), 149 ('{:', 'Expecting property name enclosed in double quotes', 1),
144 ('{,', 'Expecting property name enclosed in double quotes', 1), 150 ('{,', 'Expecting property name enclosed in double quotes', 1),
145 ('{42', 'Expecting property name enclosed in double quotes', 1), 151 ('{42', 'Expecting property name enclosed in double quotes', 1),
146 ('[{]', 'Expecting property name enclosed in double quotes', 2), 152 ('[{]', 'Expecting property name enclosed in double quotes', 2),
147 ('{"spam",', "Expecting ':' delimiter", 7), 153 ('{"spam",', "Expecting ':' delimiter", 7),
148 ('{"spam"}', "Expecting ':' delimiter", 7), 154 ('{"spam"}', "Expecting ':' delimiter", 7),
149 ('[{"spam"]', "Expecting ':' delimiter", 8), 155 ('[{"spam"]', "Expecting ':' delimiter", 8),
150 ('{"spam":}', 'Expecting value', 8), 156 ('{"spam":}', 'Expecting value', 8),
151 ('[{"spam":]', 'Expecting value', 9), 157 ('[{"spam":]', 'Expecting value', 9),
152 ('{"spam":42 "ham"', "Expecting ',' delimiter", 11), 158 ('{"spam":42 "ham"', "Expecting ',' delimiter", 11),
153 ('[{"spam":42]', "Expecting ',' delimiter", 11), 159 ('[{"spam":42]', "Expecting ',' delimiter", 11),
154 ('{"spam":42,}', 'Expecting property name enclosed in double quotes' , 11), 160 ('{"spam":42,}', 'Expecting property name enclosed in double quotes' , 11),
155 ] 161 ]
156 for data, msg, idx in test_cases: 162 for data, msg, idx in test_cases:
157 self.assertRaisesRegex(ValueError, 163 with self.assertRaises(self.JSONDecodeError) as cm:
158 r'^{0}: line 1 column {1} \(char {2}\)'.format( 164 self.loads(data)
159 re.escape(msg), idx + 1, idx), 165 err = cm.exception
160 self.loads, data) 166 self.assertEqual(err.msg, msg)
167 self.assertEqual(err.pos, idx)
168 self.assertEqual(err.lineno, 1)
169 self.assertEqual(err.colno, idx + 1)
170 errmsg = ('%s: line 1 column %d (char %d)' %
171 (msg, idx + 1, idx))
172 self.assertRegex(str(err), '^' + re.escape(errmsg))
161 173
162 def test_extra_data(self): 174 def test_extra_data(self):
163 test_cases = [ 175 test_cases = [
164 ('[]]', 'Extra data', 2), 176 ('[]]', 'Extra data', 2),
165 ('{}}', 'Extra data', 2), 177 ('{}}', 'Extra data', 2),
166 ('[],[]', 'Extra data', 2), 178 ('[],[]', 'Extra data', 2),
167 ('{},{}', 'Extra data', 2), 179 ('{},{}', 'Extra data', 2),
168 ] 180 ]
169 test_cases += [ 181 test_cases += [
170 ('42,"spam"', 'Extra data', 2), 182 ('42,"spam"', 'Extra data', 2),
171 ('"spam",42', 'Extra data', 6), 183 ('"spam",42', 'Extra data', 6),
172 ] 184 ]
173 for data, msg, idx in test_cases: 185 for data, msg, idx in test_cases:
174 self.assertRaisesRegex(ValueError, 186 with self.assertRaises(self.JSONDecodeError) as cm:
175 r'^{0}: line 1 column {1} - line 1 column {2}' 187 self.loads(data)
176 r' \(char {3} - {4}\)'.format( 188 err = cm.exception
177 re.escape(msg), idx + 1, len(data) + 1, idx, len(data)), 189 self.assertEqual(err.msg, msg)
178 self.loads, data) 190 self.assertEqual(err.pos, idx)
191 self.assertEqual(err.lineno, 1)
192 self.assertEqual(err.colno, idx + 1)
193 errmsg = '%s: line 1 column %d (char %d)' % (msg, idx + 1, idx)
194 self.assertRegex(str(err), '^' + re.escape(errmsg))
179 195
180 def test_linecol(self): 196 def test_linecol(self):
181 test_cases = [ 197 test_cases = [
182 ('!', 1, 1, 0), 198 ('!', 1, 1, 0),
183 (' !', 1, 2, 1), 199 (' !', 1, 2, 1),
184 ('\n!', 2, 1, 1), 200 ('\n!', 2, 1, 1),
185 ('\n \n\n !', 4, 6, 10), 201 ('\n \n\n !', 4, 6, 10),
186 ] 202 ]
187 for data, line, col, idx in test_cases: 203 for data, line, col, idx in test_cases:
188 self.assertRaisesRegex(ValueError, 204 with self.assertRaises(self.JSONDecodeError) as cm:
189 r'^Expecting value: line {0} column {1}' 205 self.loads(data)
190 r' \(char {2}\)$'.format(line, col, idx), 206 err = cm.exception
191 self.loads, data) 207 self.assertEqual(err.msg, 'Expecting value')
208 self.assertEqual(err.pos, idx)
209 self.assertEqual(err.lineno, line)
210 self.assertEqual(err.colno, col)
211 errmsg = ('Expecting value: line %s column %d'
212 ' (char %d)' % (line, col, idx))
213 self.assertRegex(str(err), '^%s$' % re.escape(errmsg))
192 214
193 class TestPyFail(TestFail, PyTest): pass 215 class TestPyFail(TestFail, PyTest): pass
194 class TestCFail(TestFail, CTest): pass 216 class TestCFail(TestFail, CTest): pass
OLDNEW
« no previous file with comments | « Lib/test/test_json/test_decode.py ('k') | Lib/test/test_json/test_scanstring.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+