ยปCore Development>Code coverage>Lib/json/tests/test_fail.py

Python code coverage for Lib/json/tests/test_fail.py

#countcontent
1n/afrom unittest import TestCase
2n/a
3n/aimport json
4n/a
5n/a# Fri Dec 30 18:57:26 2005
6n/aJSONDOCS = [
7n/a # http://json.org/JSON_checker/test/fail1.json
8n/a '"A JSON payload should be an object or array, not a string."',
9n/a # http://json.org/JSON_checker/test/fail2.json
10n/a '["Unclosed array"',
11n/a # http://json.org/JSON_checker/test/fail3.json
12n/a '{unquoted_key: "keys must be quoted}',
13n/a # http://json.org/JSON_checker/test/fail4.json
14n/a '["extra comma",]',
15n/a # http://json.org/JSON_checker/test/fail5.json
16n/a '["double extra comma",,]',
17n/a # http://json.org/JSON_checker/test/fail6.json
18n/a '[ , "<-- missing value"]',
19n/a # http://json.org/JSON_checker/test/fail7.json
20n/a '["Comma after the close"],',
21n/a # http://json.org/JSON_checker/test/fail8.json
22n/a '["Extra close"]]',
23n/a # http://json.org/JSON_checker/test/fail9.json
24n/a '{"Extra comma": true,}',
25n/a # http://json.org/JSON_checker/test/fail10.json
26n/a '{"Extra value after close": true} "misplaced quoted value"',
27n/a # http://json.org/JSON_checker/test/fail11.json
28n/a '{"Illegal expression": 1 + 2}',
29n/a # http://json.org/JSON_checker/test/fail12.json
30n/a '{"Illegal invocation": alert()}',
31n/a # http://json.org/JSON_checker/test/fail13.json
32n/a '{"Numbers cannot have leading zeroes": 013}',
33n/a # http://json.org/JSON_checker/test/fail14.json
34n/a '{"Numbers cannot be hex": 0x14}',
35n/a # http://json.org/JSON_checker/test/fail15.json
36n/a '["Illegal backslash escape: \\x15"]',
37n/a # http://json.org/JSON_checker/test/fail16.json
38n/a '["Illegal backslash escape: \\\'"]',
39n/a # http://json.org/JSON_checker/test/fail17.json
40n/a '["Illegal backslash escape: \\017"]',
41n/a # http://json.org/JSON_checker/test/fail18.json
42n/a '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
43n/a # http://json.org/JSON_checker/test/fail19.json
44n/a '{"Missing colon" null}',
45n/a # http://json.org/JSON_checker/test/fail20.json
46n/a '{"Double colon":: null}',
47n/a # http://json.org/JSON_checker/test/fail21.json
48n/a '{"Comma instead of colon", null}',
49n/a # http://json.org/JSON_checker/test/fail22.json
50n/a '["Colon instead of comma": false]',
51n/a # http://json.org/JSON_checker/test/fail23.json
52n/a '["Bad value", truth]',
53n/a # http://json.org/JSON_checker/test/fail24.json
54n/a "['single quote']",
55n/a # http://code.google.com/p/simplejson/issues/detail?id=3
56n/a '["A\u001FZ control characters in string"]',
57n/a]
58n/a
59n/aSKIPS = {
60n/a 1: "why not have a string payload?",
61n/a 18: "spec doesn't specify any nesting limitations",
62n/a}
63n/a
64n/aclass TestFail(TestCase):
65n/a def test_failures(self):
66n/a for idx, doc in enumerate(JSONDOCS):
67n/a idx = idx + 1
68n/a if idx in SKIPS:
69n/a json.loads(doc)
70n/a continue
71n/a try:
72n/a json.loads(doc)
73n/a except ValueError:
74n/a pass
75n/a else:
76n/a self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
77n/a
78n/a def test_non_string_keys_dict(self):
79n/a data = {'a' : 1, (1, 2) : 2}
80n/a
81n/a #This is for c encoder
82n/a self.assertRaises(TypeError, json.dumps, data)
83n/a
84n/a #This is for python encoder
85n/a self.assertRaises(TypeError, json.dumps, data, indent=True)