1 | n/a | import sys |
---|
2 | n/a | from test.test_json import PyTest, CTest |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | class TestScanstring: |
---|
6 | n/a | def test_scanstring(self): |
---|
7 | n/a | scanstring = self.json.decoder.scanstring |
---|
8 | n/a | self.assertEqual( |
---|
9 | n/a | scanstring('"z\U0001d120x"', 1, True), |
---|
10 | n/a | ('z\U0001d120x', 5)) |
---|
11 | n/a | |
---|
12 | n/a | self.assertEqual( |
---|
13 | n/a | scanstring('"\\u007b"', 1, True), |
---|
14 | n/a | ('{', 8)) |
---|
15 | n/a | |
---|
16 | n/a | self.assertEqual( |
---|
17 | n/a | scanstring('"A JSON payload should be an object or array, not a string."', 1, True), |
---|
18 | n/a | ('A JSON payload should be an object or array, not a string.', 60)) |
---|
19 | n/a | |
---|
20 | n/a | self.assertEqual( |
---|
21 | n/a | scanstring('["Unclosed array"', 2, True), |
---|
22 | n/a | ('Unclosed array', 17)) |
---|
23 | n/a | |
---|
24 | n/a | self.assertEqual( |
---|
25 | n/a | scanstring('["extra comma",]', 2, True), |
---|
26 | n/a | ('extra comma', 14)) |
---|
27 | n/a | |
---|
28 | n/a | self.assertEqual( |
---|
29 | n/a | scanstring('["double extra comma",,]', 2, True), |
---|
30 | n/a | ('double extra comma', 21)) |
---|
31 | n/a | |
---|
32 | n/a | self.assertEqual( |
---|
33 | n/a | scanstring('["Comma after the close"],', 2, True), |
---|
34 | n/a | ('Comma after the close', 24)) |
---|
35 | n/a | |
---|
36 | n/a | self.assertEqual( |
---|
37 | n/a | scanstring('["Extra close"]]', 2, True), |
---|
38 | n/a | ('Extra close', 14)) |
---|
39 | n/a | |
---|
40 | n/a | self.assertEqual( |
---|
41 | n/a | scanstring('{"Extra comma": true,}', 2, True), |
---|
42 | n/a | ('Extra comma', 14)) |
---|
43 | n/a | |
---|
44 | n/a | self.assertEqual( |
---|
45 | n/a | scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, True), |
---|
46 | n/a | ('Extra value after close', 26)) |
---|
47 | n/a | |
---|
48 | n/a | self.assertEqual( |
---|
49 | n/a | scanstring('{"Illegal expression": 1 + 2}', 2, True), |
---|
50 | n/a | ('Illegal expression', 21)) |
---|
51 | n/a | |
---|
52 | n/a | self.assertEqual( |
---|
53 | n/a | scanstring('{"Illegal invocation": alert()}', 2, True), |
---|
54 | n/a | ('Illegal invocation', 21)) |
---|
55 | n/a | |
---|
56 | n/a | self.assertEqual( |
---|
57 | n/a | scanstring('{"Numbers cannot have leading zeroes": 013}', 2, True), |
---|
58 | n/a | ('Numbers cannot have leading zeroes', 37)) |
---|
59 | n/a | |
---|
60 | n/a | self.assertEqual( |
---|
61 | n/a | scanstring('{"Numbers cannot be hex": 0x14}', 2, True), |
---|
62 | n/a | ('Numbers cannot be hex', 24)) |
---|
63 | n/a | |
---|
64 | n/a | self.assertEqual( |
---|
65 | n/a | scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, True), |
---|
66 | n/a | ('Too deep', 30)) |
---|
67 | n/a | |
---|
68 | n/a | self.assertEqual( |
---|
69 | n/a | scanstring('{"Missing colon" null}', 2, True), |
---|
70 | n/a | ('Missing colon', 16)) |
---|
71 | n/a | |
---|
72 | n/a | self.assertEqual( |
---|
73 | n/a | scanstring('{"Double colon":: null}', 2, True), |
---|
74 | n/a | ('Double colon', 15)) |
---|
75 | n/a | |
---|
76 | n/a | self.assertEqual( |
---|
77 | n/a | scanstring('{"Comma instead of colon", null}', 2, True), |
---|
78 | n/a | ('Comma instead of colon', 25)) |
---|
79 | n/a | |
---|
80 | n/a | self.assertEqual( |
---|
81 | n/a | scanstring('["Colon instead of comma": false]', 2, True), |
---|
82 | n/a | ('Colon instead of comma', 25)) |
---|
83 | n/a | |
---|
84 | n/a | self.assertEqual( |
---|
85 | n/a | scanstring('["Bad value", truth]', 2, True), |
---|
86 | n/a | ('Bad value', 12)) |
---|
87 | n/a | |
---|
88 | n/a | def test_surrogates(self): |
---|
89 | n/a | scanstring = self.json.decoder.scanstring |
---|
90 | n/a | def assertScan(given, expect): |
---|
91 | n/a | self.assertEqual(scanstring(given, 1, True), |
---|
92 | n/a | (expect, len(given))) |
---|
93 | n/a | |
---|
94 | n/a | assertScan('"z\\ud834\\u0079x"', 'z\ud834yx') |
---|
95 | n/a | assertScan('"z\\ud834\\udd20x"', 'z\U0001d120x') |
---|
96 | n/a | assertScan('"z\\ud834\\ud834\\udd20x"', 'z\ud834\U0001d120x') |
---|
97 | n/a | assertScan('"z\\ud834x"', 'z\ud834x') |
---|
98 | n/a | assertScan('"z\\ud834\udd20x12345"', 'z\ud834\udd20x12345') |
---|
99 | n/a | assertScan('"z\\udd20x"', 'z\udd20x') |
---|
100 | n/a | assertScan('"z\ud834\udd20x"', 'z\ud834\udd20x') |
---|
101 | n/a | assertScan('"z\ud834\\udd20x"', 'z\ud834\udd20x') |
---|
102 | n/a | assertScan('"z\ud834x"', 'z\ud834x') |
---|
103 | n/a | |
---|
104 | n/a | def test_bad_escapes(self): |
---|
105 | n/a | scanstring = self.json.decoder.scanstring |
---|
106 | n/a | bad_escapes = [ |
---|
107 | n/a | '"\\"', |
---|
108 | n/a | '"\\x"', |
---|
109 | n/a | '"\\u"', |
---|
110 | n/a | '"\\u0"', |
---|
111 | n/a | '"\\u01"', |
---|
112 | n/a | '"\\u012"', |
---|
113 | n/a | '"\\uz012"', |
---|
114 | n/a | '"\\u0z12"', |
---|
115 | n/a | '"\\u01z2"', |
---|
116 | n/a | '"\\u012z"', |
---|
117 | n/a | '"\\u0x12"', |
---|
118 | n/a | '"\\u0X12"', |
---|
119 | n/a | '"\\ud834\\"', |
---|
120 | n/a | '"\\ud834\\u"', |
---|
121 | n/a | '"\\ud834\\ud"', |
---|
122 | n/a | '"\\ud834\\udd"', |
---|
123 | n/a | '"\\ud834\\udd2"', |
---|
124 | n/a | '"\\ud834\\uzdd2"', |
---|
125 | n/a | '"\\ud834\\udzd2"', |
---|
126 | n/a | '"\\ud834\\uddz2"', |
---|
127 | n/a | '"\\ud834\\udd2z"', |
---|
128 | n/a | '"\\ud834\\u0x20"', |
---|
129 | n/a | '"\\ud834\\u0X20"', |
---|
130 | n/a | ] |
---|
131 | n/a | for s in bad_escapes: |
---|
132 | n/a | with self.assertRaises(self.JSONDecodeError, msg=s): |
---|
133 | n/a | scanstring(s, 1, True) |
---|
134 | n/a | |
---|
135 | n/a | def test_overflow(self): |
---|
136 | n/a | with self.assertRaises(OverflowError): |
---|
137 | n/a | self.json.decoder.scanstring(b"xxx", sys.maxsize+1) |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | class TestPyScanstring(TestScanstring, PyTest): pass |
---|
141 | n/a | class TestCScanstring(TestScanstring, CTest): pass |
---|