| 1 |
from json.tests import PyTest, CTest |
| 2 |
|
| 3 |
|
| 4 |
JSONDOCS = [ |
| 5 |
|
| 6 |
'"A JSON payload should be an object or array, not a string."', |
| 7 |
|
| 8 |
'["Unclosed array"', |
| 9 |
|
| 10 |
'{unquoted_key: "keys must be quoted"}', |
| 11 |
|
| 12 |
'["extra comma",]', |
| 13 |
|
| 14 |
'["double extra comma",,]', |
| 15 |
|
| 16 |
'[ , "<-- missing value"]', |
| 17 |
|
| 18 |
'["Comma after the close"],', |
| 19 |
|
| 20 |
'["Extra close"]]', |
| 21 |
|
| 22 |
'{"Extra comma": true,}', |
| 23 |
|
| 24 |
'{"Extra value after close": true} "misplaced quoted value"', |
| 25 |
|
| 26 |
'{"Illegal expression": 1 + 2}', |
| 27 |
|
| 28 |
'{"Illegal invocation": alert()}', |
| 29 |
|
| 30 |
'{"Numbers cannot have leading zeroes": 013}', |
| 31 |
|
| 32 |
'{"Numbers cannot be hex": 0x14}', |
| 33 |
|
| 34 |
'["Illegal backslash escape: \\x15"]', |
| 35 |
|
| 36 |
'[\\naked]', |
| 37 |
|
| 38 |
'["Illegal backslash escape: \\017"]', |
| 39 |
|
| 40 |
'[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', |
| 41 |
|
| 42 |
'{"Missing colon" null}', |
| 43 |
|
| 44 |
'{"Double colon":: null}', |
| 45 |
|
| 46 |
'{"Comma instead of colon", null}', |
| 47 |
|
| 48 |
'["Colon instead of comma": false]', |
| 49 |
|
| 50 |
'["Bad value", truth]', |
| 51 |
|
| 52 |
"['single quote']", |
| 53 |
|
| 54 |
'["\ttab\tcharacter\tin\tstring\t"]', |
| 55 |
|
| 56 |
'["tab\\ character\\ in\\ string\\ "]', |
| 57 |
|
| 58 |
'["line\nbreak"]', |
| 59 |
|
| 60 |
'["line\\\nbreak"]', |
| 61 |
|
| 62 |
'[0e]', |
| 63 |
|
| 64 |
'[0e+]', |
| 65 |
|
| 66 |
'[0e+-1]', |
| 67 |
|
| 68 |
'{"Comma instead if closing brace": true,', |
| 69 |
|
| 70 |
'["mismatch"}', |
| 71 |
|
| 72 |
u'["A\u001FZ control characters in string"]', |
| 73 |
] |
| 74 |
|
| 75 |
SKIPS = { |
| 76 |
1: "why not have a string payload?", |
| 77 |
18: "spec doesn't specify any nesting limitations", |
| 78 |
} |
| 79 |
|
| 80 |
class 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 |
|
| 98 |
self.assertRaises(TypeError, self.dumps, data) |
| 99 |
|
| 100 |
|
| 101 |
self.assertRaises(TypeError, self.dumps, data, indent=True) |
| 102 |
|
| 103 |
|
| 104 |
class TestPyFail(TestFail, PyTest): pass |
| 105 |
class TestCFail(TestFail, CTest): pass |