ยปCore Development>Code coverage>Lib/test/test_json/test_scanstring.py

Python code coverage for Lib/test/test_json/test_scanstring.py

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