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