• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from json.tests import PyTest, CTest
2
3# 2007-10-05
4JSONDOCS = [
5    # http://json.org/JSON_checker/test/fail1.json
6    '"A JSON payload should be an object or array, not a string."',
7    # http://json.org/JSON_checker/test/fail2.json
8    '["Unclosed array"',
9    # http://json.org/JSON_checker/test/fail3.json
10    '{unquoted_key: "keys must be quoted"}',
11    # http://json.org/JSON_checker/test/fail4.json
12    '["extra comma",]',
13    # http://json.org/JSON_checker/test/fail5.json
14    '["double extra comma",,]',
15    # http://json.org/JSON_checker/test/fail6.json
16    '[   , "<-- missing value"]',
17    # http://json.org/JSON_checker/test/fail7.json
18    '["Comma after the close"],',
19    # http://json.org/JSON_checker/test/fail8.json
20    '["Extra close"]]',
21    # http://json.org/JSON_checker/test/fail9.json
22    '{"Extra comma": true,}',
23    # http://json.org/JSON_checker/test/fail10.json
24    '{"Extra value after close": true} "misplaced quoted value"',
25    # http://json.org/JSON_checker/test/fail11.json
26    '{"Illegal expression": 1 + 2}',
27    # http://json.org/JSON_checker/test/fail12.json
28    '{"Illegal invocation": alert()}',
29    # http://json.org/JSON_checker/test/fail13.json
30    '{"Numbers cannot have leading zeroes": 013}',
31    # http://json.org/JSON_checker/test/fail14.json
32    '{"Numbers cannot be hex": 0x14}',
33    # http://json.org/JSON_checker/test/fail15.json
34    '["Illegal backslash escape: \\x15"]',
35    # http://json.org/JSON_checker/test/fail16.json
36    '[\\naked]',
37    # http://json.org/JSON_checker/test/fail17.json
38    '["Illegal backslash escape: \\017"]',
39    # http://json.org/JSON_checker/test/fail18.json
40    '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
41    # http://json.org/JSON_checker/test/fail19.json
42    '{"Missing colon" null}',
43    # http://json.org/JSON_checker/test/fail20.json
44    '{"Double colon":: null}',
45    # http://json.org/JSON_checker/test/fail21.json
46    '{"Comma instead of colon", null}',
47    # http://json.org/JSON_checker/test/fail22.json
48    '["Colon instead of comma": false]',
49    # http://json.org/JSON_checker/test/fail23.json
50    '["Bad value", truth]',
51    # http://json.org/JSON_checker/test/fail24.json
52    "['single quote']",
53    # http://json.org/JSON_checker/test/fail25.json
54    '["\ttab\tcharacter\tin\tstring\t"]',
55    # http://json.org/JSON_checker/test/fail26.json
56    '["tab\\   character\\   in\\  string\\  "]',
57    # http://json.org/JSON_checker/test/fail27.json
58    '["line\nbreak"]',
59    # http://json.org/JSON_checker/test/fail28.json
60    '["line\\\nbreak"]',
61    # http://json.org/JSON_checker/test/fail29.json
62    '[0e]',
63    # http://json.org/JSON_checker/test/fail30.json
64    '[0e+]',
65    # http://json.org/JSON_checker/test/fail31.json
66    '[0e+-1]',
67    # http://json.org/JSON_checker/test/fail32.json
68    '{"Comma instead if closing brace": true,',
69    # http://json.org/JSON_checker/test/fail33.json
70    '["mismatch"}',
71    # http://code.google.com/p/simplejson/issues/detail?id=3
72    u'["A\u001FZ control characters in string"]',
73]
74
75SKIPS = {
76    1: "why not have a string payload?",
77    18: "spec doesn't specify any nesting limitations",
78}
79
80class TestFail(object):
81    def test_failures(self):
82        for idx, doc in enumerate(JSONDOCS):
83            idx = idx + 1
84            if idx in SKIPS:
85                self.loads(doc)
86                continue
87            try:
88                self.loads(doc)
89            except ValueError:
90                pass
91            else:
92                self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
93
94    def test_non_string_keys_dict(self):
95        data = {'a' : 1, (1, 2) : 2}
96
97        #This is for c encoder
98        self.assertRaises(TypeError, self.dumps, data)
99
100        #This is for python encoder
101        self.assertRaises(TypeError, self.dumps, data, indent=True)
102
103
104class TestPyFail(TestFail, PyTest): pass
105class TestCFail(TestFail, CTest): pass
106