summaryrefslogtreecommitdiff
path: root/simplejson/tests/test_fail.py
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2013-02-21 14:19:08 -0800
committerBob Ippolito <bob@redivi.com>2013-02-21 14:19:08 -0800
commit104b40fcf6aa39d9ba7b240c3c528d1f85e86ef2 (patch)
tree3cf08092fb2b95376a2b93f88e400c98c53872d8 /simplejson/tests/test_fail.py
parent44d7709a31f3a19f3d465411585ebb7be7fa2295 (diff)
downloadsimplejson-104b40fcf6aa39d9ba7b240c3c528d1f85e86ef2.tar.gz
improve truncated input error messages, use JSONDecodeError instead of StopIteration (#61)
Diffstat (limited to 'simplejson/tests/test_fail.py')
-rw-r--r--simplejson/tests/test_fail.py41
1 files changed, 40 insertions, 1 deletions
diff --git a/simplejson/tests/test_fail.py b/simplejson/tests/test_fail.py
index f458a4b..d882ec0 100644
--- a/simplejson/tests/test_fail.py
+++ b/simplejson/tests/test_fail.py
@@ -99,7 +99,6 @@ class TestFail(TestCase):
except json.JSONDecodeError:
pass
else:
- #self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
self.fail("Expected failure for fail%d.json: %r" % (idx, doc))
def test_array_decoder_issue46(self):
@@ -117,3 +116,43 @@ class TestFail(TestCase):
self.fail("Unexpected exception raised %r %s" % (e, e))
else:
self.fail("Unexpected success parsing '[,]'")
+
+ def test_truncated_input(self):
+ test_cases = [
+ ('', 'Expecting value', 0),
+ ('[', "Expecting value or ']'", 1),
+ ('[42', "Expecting ',' delimiter", 3),
+ ('[42,', 'Expecting value', 4),
+ ('["', 'Unterminated string starting at', 1),
+ ('["spam', 'Unterminated string starting at', 1),
+ ('["spam"', "Expecting ',' delimiter", 7),
+ ('["spam",', 'Expecting value', 8),
+ ('{', 'Expecting property name enclosed in double quotes', 1),
+ ('{"', 'Unterminated string starting at', 1),
+ ('{"spam', 'Unterminated string starting at', 1),
+ ('{"spam"', "Expecting ':' delimiter", 7),
+ ('{"spam":', 'Expecting value', 8),
+ ('{"spam":42', "Expecting ',' delimiter", 10),
+ ('{"spam":42,', 'Expecting property name enclosed in double quotes',
+ 11),
+ ('"', 'Unterminated string starting at', 0),
+ ('"spam', 'Unterminated string starting at', 0),
+ ('[,', "Expecting value", 1),
+ ]
+ for data, msg, idx in test_cases:
+ try:
+ json.loads(data)
+ except json.JSONDecodeError:
+ e = sys.exc_info()[1]
+ self.assertEqual(
+ e.msg[:len(msg)],
+ msg,
+ "%r doesn't start with %r for %r" % (e.msg, msg, data))
+ self.assertEqual(
+ e.pos, idx,
+ "pos %r != %r for %r" % (e.pos, idx, data))
+ except Exception:
+ e = sys.exc_info()[1]
+ self.fail("Unexpected exception raised %r %s" % (e, e))
+ else:
+ self.fail("Unexpected success parsing '%r'" % (data,))