Home | History | Annotate | Download | only in tests
      1 from json.tests import PyTest, CTest
      2 
      3 # Fri Dec 30 18:57:26 2005

      4 JSONDOCS = [
      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     '["Illegal backslash escape: \\\'"]',
     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://code.google.com/p/simplejson/issues/detail?id=3

     54     u'["A\u001FZ control characters in string"]',
     55 ]
     56 
     57 SKIPS = {
     58     1: "why not have a string payload?",
     59     18: "spec doesn't specify any nesting limitations",
     60 }
     61 
     62 class TestFail(object):
     63     def test_failures(self):
     64         for idx, doc in enumerate(JSONDOCS):
     65             idx = idx + 1
     66             if idx in SKIPS:
     67                 self.loads(doc)
     68                 continue
     69             try:
     70                 self.loads(doc)
     71             except ValueError:
     72                 pass
     73             else:
     74                 self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
     75 
     76     def test_non_string_keys_dict(self):
     77         data = {'a' : 1, (1, 2) : 2}
     78 
     79         #This is for c encoder

     80         self.assertRaises(TypeError, self.dumps, data)
     81 
     82         #This is for python encoder

     83         self.assertRaises(TypeError, self.dumps, data, indent=True)
     84 
     85 
     86 class TestPyFail(TestFail, PyTest): pass
     87 class TestCFail(TestFail, CTest): pass
     88