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

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

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