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

Delta Between Two Patch Sets: Lib/test/test_json/test_fail.py

Issue 19361: Specialize exceptions thrown by JSON parser
Left Patch Set: Created 4 years, 11 months ago
Right Patch Set: Created 4 years, 9 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_json/test_decode.py ('k') | Lib/test/test_json/test_scanstring.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 ('"spam",42', 'Extra data', 6), 183 ('"spam",42', 'Extra data', 6),
184 ] 184 ]
185 for data, msg, idx in test_cases: 185 for data, msg, idx in test_cases:
186 with self.assertRaises(self.JSONDecodeError) as cm: 186 with self.assertRaises(self.JSONDecodeError) as cm:
187 self.loads(data) 187 self.loads(data)
188 err = cm.exception 188 err = cm.exception
189 self.assertEqual(err.msg, msg) 189 self.assertEqual(err.msg, msg)
190 self.assertEqual(err.pos, idx) 190 self.assertEqual(err.pos, idx)
191 self.assertEqual(err.lineno, 1) 191 self.assertEqual(err.lineno, 1)
192 self.assertEqual(err.colno, idx + 1) 192 self.assertEqual(err.colno, idx + 1)
193 self.assertEqual(err.end, len(data)) 193 errmsg = '%s: line 1 column %d (char %d)' % (msg, idx + 1, idx)
194 self.assertEqual(err.endlineno, 1)
195 self.assertEqual(err.endcolno, len(data) + 1)
196 errmsg = ('%s: line 1 column %d - line 1 column %d'
197 ' (char %d - %d)' %
198 (msg, idx + 1, len(data) + 1, idx, len(data)))
199 self.assertRegex(str(err), '^' + re.escape(errmsg)) 194 self.assertRegex(str(err), '^' + re.escape(errmsg))
200 195
201 def test_linecol(self): 196 def test_linecol(self):
202 test_cases = [ 197 test_cases = [
203 ('!', 1, 1, 0), 198 ('!', 1, 1, 0),
204 (' !', 1, 2, 1), 199 (' !', 1, 2, 1),
205 ('\n!', 2, 1, 1), 200 ('\n!', 2, 1, 1),
206 ('\n \n\n !', 4, 6, 10), 201 ('\n \n\n !', 4, 6, 10),
207 ] 202 ]
208 for data, line, col, idx in test_cases: 203 for data, line, col, idx in test_cases:
209 with self.assertRaises(self.JSONDecodeError) as cm: 204 with self.assertRaises(self.JSONDecodeError) as cm:
210 self.loads(data) 205 self.loads(data)
211 err = cm.exception 206 err = cm.exception
212 self.assertEqual(err.msg, 'Expecting value') 207 self.assertEqual(err.msg, 'Expecting value')
213 self.assertEqual(err.pos, idx) 208 self.assertEqual(err.pos, idx)
214 self.assertEqual(err.lineno, line) 209 self.assertEqual(err.lineno, line)
215 self.assertEqual(err.colno, col) 210 self.assertEqual(err.colno, col)
216 errmsg = ('Expecting value: line %s column %d' 211 errmsg = ('Expecting value: line %s column %d'
217 ' (char %d)' % (line, col, idx)) 212 ' (char %d)' % (line, col, idx))
218 self.assertRegex(str(err), '^%s$' % re.escape(errmsg)) 213 self.assertRegex(str(err), '^%s$' % re.escape(errmsg))
219 214
220 class TestPyFail(TestFail, PyTest): pass 215 class TestPyFail(TestFail, PyTest): pass
221 class TestCFail(TestFail, CTest): pass 216 class TestCFail(TestFail, CTest): pass
LEFTRIGHT

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