2
from unittest import TestCase
4
import simplejson as json
5
import simplejson.decoder
7
class TestScanString(TestCase):
8
def test_py_scanstring(self):
9
self._test_scanstring(simplejson.decoder.py_scanstring)
11
def test_c_scanstring(self):
12
if not simplejson.decoder.c_scanstring:
14
self._test_scanstring(simplejson.decoder.c_scanstring)
16
def _test_scanstring(self, scanstring):
18
scanstring('"z\\ud834\\udd20x"', 1, None, True),
19
(u'z\U0001d120x', 16))
21
if sys.maxunicode == 65535:
23
scanstring(u'"z\U0001d120x"', 1, None, True),
27
scanstring(u'"z\U0001d120x"', 1, None, True),
31
scanstring('"\\u007b"', 1, None, True),
35
scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
36
(u'A JSON payload should be an object or array, not a string.', 60))
39
scanstring('["Unclosed array"', 2, None, True),
40
(u'Unclosed array', 17))
43
scanstring('["extra comma",]', 2, None, True),
47
scanstring('["double extra comma",,]', 2, None, True),
48
(u'double extra comma', 21))
51
scanstring('["Comma after the close"],', 2, None, True),
52
(u'Comma after the close', 24))
55
scanstring('["Extra close"]]', 2, None, True),
59
scanstring('{"Extra comma": true,}', 2, None, True),
63
scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
64
(u'Extra value after close', 26))
67
scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
68
(u'Illegal expression', 21))
71
scanstring('{"Illegal invocation": alert()}', 2, None, True),
72
(u'Illegal invocation', 21))
75
scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
76
(u'Numbers cannot have leading zeroes', 37))
79
scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
80
(u'Numbers cannot be hex', 24))
83
scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
87
scanstring('{"Missing colon" null}', 2, None, True),
88
(u'Missing colon', 16))
91
scanstring('{"Double colon":: null}', 2, None, True),
92
(u'Double colon', 15))
95
scanstring('{"Comma instead of colon", null}', 2, None, True),
96
(u'Comma instead of colon', 25))
99
scanstring('["Colon instead of comma": false]', 2, None, True),
100
(u'Colon instead of comma', 25))
103
scanstring('["Bad value", truth]', 2, None, True),
106
def test_issue3623(self):
107
self.assertRaises(ValueError, json.decoder.scanstring, "xxx", 1,
109
self.assertRaises(UnicodeDecodeError,
110
json.encoder.encode_basestring_ascii, "xx\xff")
112
def test_overflow(self):
113
# Python 2.5 does not have maxsize
114
maxsize = getattr(sys, 'maxsize', sys.maxint)
115
self.assertRaises(OverflowError, json.decoder.scanstring, "xxx",