ยปCore Development>Code coverage>Lib/test/json_tests/test_fail.py

Python code coverage for Lib/test/json_tests/test_fail.py

#countcontent
1n/afrom test.json_tests import PyTest, CTest
2n/aimport re
3n/a
4n/a# 2007-10-05
5n/aJSONDOCS = [
6n/a # http://json.org/JSON_checker/test/fail1.json
7n/a '"A JSON payload should be an object or array, not a string."',
8n/a # http://json.org/JSON_checker/test/fail2.json
9n/a '["Unclosed array"',
10n/a # http://json.org/JSON_checker/test/fail3.json
11n/a '{unquoted_key: "keys must be quoted"}',
12n/a # http://json.org/JSON_checker/test/fail4.json
13n/a '["extra comma",]',
14n/a # http://json.org/JSON_checker/test/fail5.json
15n/a '["double extra comma",,]',
16n/a # http://json.org/JSON_checker/test/fail6.json
17n/a '[ , "<-- missing value"]',
18n/a # http://json.org/JSON_checker/test/fail7.json
19n/a '["Comma after the close"],',
20n/a # http://json.org/JSON_checker/test/fail8.json
21n/a '["Extra close"]]',
22n/a # http://json.org/JSON_checker/test/fail9.json
23n/a '{"Extra comma": true,}',
24n/a # http://json.org/JSON_checker/test/fail10.json
25n/a '{"Extra value after close": true} "misplaced quoted value"',
26n/a # http://json.org/JSON_checker/test/fail11.json
27n/a '{"Illegal expression": 1 + 2}',
28n/a # http://json.org/JSON_checker/test/fail12.json
29n/a '{"Illegal invocation": alert()}',
30n/a # http://json.org/JSON_checker/test/fail13.json
31n/a '{"Numbers cannot have leading zeroes": 013}',
32n/a # http://json.org/JSON_checker/test/fail14.json
33n/a '{"Numbers cannot be hex": 0x14}',
34n/a # http://json.org/JSON_checker/test/fail15.json
35n/a '["Illegal backslash escape: \\x15"]',
36n/a # http://json.org/JSON_checker/test/fail16.json
37n/a '[\\naked]',
38n/a # http://json.org/JSON_checker/test/fail17.json
39n/a '["Illegal backslash escape: \\017"]',
40n/a # http://json.org/JSON_checker/test/fail18.json
41n/a '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
42n/a # http://json.org/JSON_checker/test/fail19.json
43n/a '{"Missing colon" null}',
44n/a # http://json.org/JSON_checker/test/fail20.json
45n/a '{"Double colon":: null}',
46n/a # http://json.org/JSON_checker/test/fail21.json
47n/a '{"Comma instead of colon", null}',
48n/a # http://json.org/JSON_checker/test/fail22.json
49n/a '["Colon instead of comma": false]',
50n/a # http://json.org/JSON_checker/test/fail23.json
51n/a '["Bad value", truth]',
52n/a # http://json.org/JSON_checker/test/fail24.json
53n/a "['single quote']",
54n/a # http://json.org/JSON_checker/test/fail25.json
55n/a '["\ttab\tcharacter\tin\tstring\t"]',
56n/a # http://json.org/JSON_checker/test/fail26.json
57n/a '["tab\\ character\\ in\\ string\\ "]',
58n/a # http://json.org/JSON_checker/test/fail27.json
59n/a '["line\nbreak"]',
60n/a # http://json.org/JSON_checker/test/fail28.json
61n/a '["line\\\nbreak"]',
62n/a # http://json.org/JSON_checker/test/fail29.json
63n/a '[0e]',
64n/a # http://json.org/JSON_checker/test/fail30.json
65n/a '[0e+]',
66n/a # http://json.org/JSON_checker/test/fail31.json
67n/a '[0e+-1]',
68n/a # http://json.org/JSON_checker/test/fail32.json
69n/a '{"Comma instead if closing brace": true,',
70n/a # http://json.org/JSON_checker/test/fail33.json
71n/a '["mismatch"}',
72n/a # http://code.google.com/p/simplejson/issues/detail?id=3
73n/a '["A\u001FZ control characters in string"]',
74n/a]
75n/a
76n/aSKIPS = {
77n/a 1: "why not have a string payload?",
78n/a 18: "spec doesn't specify any nesting limitations",
79n/a}
80n/a
81n/aclass TestFail:
82n/a def test_failures(self):
83n/a for idx, doc in enumerate(JSONDOCS):
84n/a idx = idx + 1
85n/a if idx in SKIPS:
86n/a self.loads(doc)
87n/a continue
88n/a try:
89n/a self.loads(doc)
90n/a except ValueError:
91n/a pass
92n/a else:
93n/a self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
94n/a
95n/a def test_non_string_keys_dict(self):
96n/a data = {'a' : 1, (1, 2) : 2}
97n/a
98n/a #This is for c encoder
99n/a self.assertRaises(TypeError, self.dumps, data)
100n/a
101n/a #This is for python encoder
102n/a self.assertRaises(TypeError, self.dumps, data, indent=True)
103n/a
104n/a def test_truncated_input(self):
105n/a test_cases = [
106n/a ('', 'Expecting value', 0),
107n/a ('[', 'Expecting value', 1),
108n/a ('[42', "Expecting ',' delimiter", 3),
109n/a ('[42,', 'Expecting value', 4),
110n/a ('["', 'Unterminated string starting at', 1),
111n/a ('["spam', 'Unterminated string starting at', 1),
112n/a ('["spam"', "Expecting ',' delimiter", 7),
113n/a ('["spam",', 'Expecting value', 8),
114n/a ('{', 'Expecting property name enclosed in double quotes', 1),
115n/a ('{"', 'Unterminated string starting at', 1),
116n/a ('{"spam', 'Unterminated string starting at', 1),
117n/a ('{"spam"', "Expecting ':' delimiter", 7),
118n/a ('{"spam":', 'Expecting value', 8),
119n/a ('{"spam":42', "Expecting ',' delimiter", 10),
120n/a ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11),
121n/a ]
122n/a test_cases += [
123n/a ('"', 'Unterminated string starting at', 0),
124n/a ('"spam', 'Unterminated string starting at', 0),
125n/a ]
126n/a for data, msg, idx in test_cases:
127n/a self.assertRaisesRegex(ValueError,
128n/a r'^{0}: line 1 column {1} \(char {2}\)'.format(
129n/a re.escape(msg), idx + 1, idx),
130n/a self.loads, data)
131n/a
132n/a def test_unexpected_data(self):
133n/a test_cases = [
134n/a ('[,', 'Expecting value', 1),
135n/a ('{"spam":[}', 'Expecting value', 9),
136n/a ('[42:', "Expecting ',' delimiter", 3),
137n/a ('[42 "spam"', "Expecting ',' delimiter", 4),
138n/a ('[42,]', 'Expecting value', 4),
139n/a ('{"spam":[42}', "Expecting ',' delimiter", 11),
140n/a ('["]', 'Unterminated string starting at', 1),
141n/a ('["spam":', "Expecting ',' delimiter", 7),
142n/a ('["spam",]', 'Expecting value', 8),
143n/a ('{:', 'Expecting property name enclosed in double quotes', 1),
144n/a ('{,', 'Expecting property name enclosed in double quotes', 1),
145n/a ('{42', 'Expecting property name enclosed in double quotes', 1),
146n/a ('[{]', 'Expecting property name enclosed in double quotes', 2),
147n/a ('{"spam",', "Expecting ':' delimiter", 7),
148n/a ('{"spam"}', "Expecting ':' delimiter", 7),
149n/a ('[{"spam"]', "Expecting ':' delimiter", 8),
150n/a ('{"spam":}', 'Expecting value', 8),
151n/a ('[{"spam":]', 'Expecting value', 9),
152n/a ('{"spam":42 "ham"', "Expecting ',' delimiter", 11),
153n/a ('[{"spam":42]', "Expecting ',' delimiter", 11),
154n/a ('{"spam":42,}', 'Expecting property name enclosed in double quotes', 11),
155n/a ]
156n/a for data, msg, idx in test_cases:
157n/a self.assertRaisesRegex(ValueError,
158n/a r'^{0}: line 1 column {1} \(char {2}\)'.format(
159n/a re.escape(msg), idx + 1, idx),
160n/a self.loads, data)
161n/a
162n/a def test_extra_data(self):
163n/a test_cases = [
164n/a ('[]]', 'Extra data', 2),
165n/a ('{}}', 'Extra data', 2),
166n/a ('[],[]', 'Extra data', 2),
167n/a ('{},{}', 'Extra data', 2),
168n/a ]
169n/a test_cases += [
170n/a ('42,"spam"', 'Extra data', 2),
171n/a ('"spam",42', 'Extra data', 6),
172n/a ]
173n/a for data, msg, idx in test_cases:
174n/a self.assertRaisesRegex(ValueError,
175n/a r'^{0}: line 1 column {1} - line 1 column {2}'
176n/a r' \(char {3} - {4}\)'.format(
177n/a re.escape(msg), idx + 1, len(data) + 1, idx, len(data)),
178n/a self.loads, data)
179n/a
180n/a def test_linecol(self):
181n/a test_cases = [
182n/a ('!', 1, 1, 0),
183n/a (' !', 1, 2, 1),
184n/a ('\n!', 2, 1, 1),
185n/a ('\n \n\n !', 4, 6, 10),
186n/a ]
187n/a for data, line, col, idx in test_cases:
188n/a self.assertRaisesRegex(ValueError,
189n/a r'^Expecting value: line {0} column {1}'
190n/a r' \(char {2}\)$'.format(line, col, idx),
191n/a self.loads, data)
192n/a
193n/aclass TestPyFail(TestFail, PyTest): pass
194n/aclass TestCFail(TestFail, CTest): pass