gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / simplejson / tests / test_fail.py @ 545
History | View | Annotate | Download (6.2 KB)
1 |
import sys |
---|---|
2 |
from unittest import TestCase |
3 |
|
4 |
import simplejson as json |
5 |
|
6 |
# 2007-10-05
|
7 |
JSONDOCS = [ |
8 |
# http://json.org/JSON_checker/test/fail1.json
|
9 |
'"A JSON payload should be an object or array, not a string."',
|
10 |
# http://json.org/JSON_checker/test/fail2.json
|
11 |
'["Unclosed array"',
|
12 |
# http://json.org/JSON_checker/test/fail3.json
|
13 |
'{unquoted_key: "keys must be quoted"}',
|
14 |
# http://json.org/JSON_checker/test/fail4.json
|
15 |
'["extra comma",]',
|
16 |
# http://json.org/JSON_checker/test/fail5.json
|
17 |
'["double extra comma",,]',
|
18 |
# http://json.org/JSON_checker/test/fail6.json
|
19 |
'[ , "<-- missing value"]',
|
20 |
# http://json.org/JSON_checker/test/fail7.json
|
21 |
'["Comma after the close"],',
|
22 |
# http://json.org/JSON_checker/test/fail8.json
|
23 |
'["Extra close"]]',
|
24 |
# http://json.org/JSON_checker/test/fail9.json
|
25 |
'{"Extra comma": true,}',
|
26 |
# http://json.org/JSON_checker/test/fail10.json
|
27 |
'{"Extra value after close": true} "misplaced quoted value"',
|
28 |
# http://json.org/JSON_checker/test/fail11.json
|
29 |
'{"Illegal expression": 1 + 2}',
|
30 |
# http://json.org/JSON_checker/test/fail12.json
|
31 |
'{"Illegal invocation": alert()}',
|
32 |
# http://json.org/JSON_checker/test/fail13.json
|
33 |
'{"Numbers cannot have leading zeroes": 013}',
|
34 |
# http://json.org/JSON_checker/test/fail14.json
|
35 |
'{"Numbers cannot be hex": 0x14}',
|
36 |
# http://json.org/JSON_checker/test/fail15.json
|
37 |
'["Illegal backslash escape: \\x15"]',
|
38 |
# http://json.org/JSON_checker/test/fail16.json
|
39 |
'[\\naked]',
|
40 |
# http://json.org/JSON_checker/test/fail17.json
|
41 |
'["Illegal backslash escape: \\017"]',
|
42 |
# http://json.org/JSON_checker/test/fail18.json
|
43 |
'[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
|
44 |
# http://json.org/JSON_checker/test/fail19.json
|
45 |
'{"Missing colon" null}',
|
46 |
# http://json.org/JSON_checker/test/fail20.json
|
47 |
'{"Double colon":: null}',
|
48 |
# http://json.org/JSON_checker/test/fail21.json
|
49 |
'{"Comma instead of colon", null}',
|
50 |
# http://json.org/JSON_checker/test/fail22.json
|
51 |
'["Colon instead of comma": false]',
|
52 |
# http://json.org/JSON_checker/test/fail23.json
|
53 |
'["Bad value", truth]',
|
54 |
# http://json.org/JSON_checker/test/fail24.json
|
55 |
"['single quote']",
|
56 |
# http://json.org/JSON_checker/test/fail25.json
|
57 |
'["\ttab\tcharacter\tin\tstring\t"]',
|
58 |
# http://json.org/JSON_checker/test/fail26.json
|
59 |
'["tab\\ character\\ in\\ string\\ "]',
|
60 |
# http://json.org/JSON_checker/test/fail27.json
|
61 |
'["line\nbreak"]',
|
62 |
# http://json.org/JSON_checker/test/fail28.json
|
63 |
'["line\\\nbreak"]',
|
64 |
# http://json.org/JSON_checker/test/fail29.json
|
65 |
'[0e]',
|
66 |
# http://json.org/JSON_checker/test/fail30.json
|
67 |
'[0e+]',
|
68 |
# http://json.org/JSON_checker/test/fail31.json
|
69 |
'[0e+-1]',
|
70 |
# http://json.org/JSON_checker/test/fail32.json
|
71 |
'{"Comma instead if closing brace": true,',
|
72 |
# http://json.org/JSON_checker/test/fail33.json
|
73 |
'["mismatch"}',
|
74 |
# http://code.google.com/p/simplejson/issues/detail?id=3
|
75 |
u'["A\u001FZ control characters in string"]',
|
76 |
# misc based on coverage
|
77 |
'{',
|
78 |
'{]',
|
79 |
'{"foo": "bar"]',
|
80 |
'{"foo": "bar"',
|
81 |
'nul',
|
82 |
'nulx',
|
83 |
'-',
|
84 |
'-x',
|
85 |
'-e',
|
86 |
'-e0',
|
87 |
'-Infinite',
|
88 |
'-Inf',
|
89 |
'Infinit',
|
90 |
'Infinite',
|
91 |
'NaM',
|
92 |
'NuN',
|
93 |
'falsy',
|
94 |
'fal',
|
95 |
'trug',
|
96 |
'tru',
|
97 |
'1e',
|
98 |
'1ex',
|
99 |
'1e-',
|
100 |
'1e-x',
|
101 |
] |
102 |
|
103 |
SKIPS = { |
104 |
1: "why not have a string payload?", |
105 |
18: "spec doesn't specify any nesting limitations", |
106 |
} |
107 |
|
108 |
class TestFail(TestCase): |
109 |
def test_failures(self): |
110 |
for idx, doc in enumerate(JSONDOCS): |
111 |
idx = idx + 1
|
112 |
if idx in SKIPS: |
113 |
json.loads(doc) |
114 |
continue
|
115 |
try:
|
116 |
json.loads(doc) |
117 |
except json.JSONDecodeError:
|
118 |
pass
|
119 |
else:
|
120 |
self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) |
121 |
|
122 |
def test_array_decoder_issue46(self): |
123 |
# http://code.google.com/p/simplejson/issues/detail?id=46
|
124 |
for doc in [u'[,]', '[,]']: |
125 |
try:
|
126 |
json.loads(doc) |
127 |
except json.JSONDecodeError:
|
128 |
e = sys.exc_info()[1]
|
129 |
self.assertEqual(e.pos, 1) |
130 |
self.assertEqual(e.lineno, 1) |
131 |
self.assertEqual(e.colno, 2) |
132 |
except Exception: |
133 |
e = sys.exc_info()[1]
|
134 |
self.fail("Unexpected exception raised %r %s" % (e, e)) |
135 |
else:
|
136 |
self.fail("Unexpected success parsing '[,]'") |
137 |
|
138 |
def test_truncated_input(self): |
139 |
test_cases = [ |
140 |
('', 'Expecting value', 0), |
141 |
('[', "Expecting value or ']'", 1), |
142 |
('[42', "Expecting ',' delimiter", 3), |
143 |
('[42,', 'Expecting value', 4), |
144 |
('["', 'Unterminated string starting at', 1), |
145 |
('["spam', 'Unterminated string starting at', 1), |
146 |
('["spam"', "Expecting ',' delimiter", 7), |
147 |
('["spam",', 'Expecting value', 8), |
148 |
('{', 'Expecting property name enclosed in double quotes', 1), |
149 |
('{"', 'Unterminated string starting at', 1), |
150 |
('{"spam', 'Unterminated string starting at', 1), |
151 |
('{"spam"', "Expecting ':' delimiter", 7), |
152 |
('{"spam":', 'Expecting value', 8), |
153 |
('{"spam":42', "Expecting ',' delimiter", 10), |
154 |
('{"spam":42,', 'Expecting property name enclosed in double quotes', |
155 |
11),
|
156 |
('"', 'Unterminated string starting at', 0), |
157 |
('"spam', 'Unterminated string starting at', 0), |
158 |
('[,', "Expecting value", 1), |
159 |
] |
160 |
for data, msg, idx in test_cases: |
161 |
try:
|
162 |
json.loads(data) |
163 |
except json.JSONDecodeError:
|
164 |
e = sys.exc_info()[1]
|
165 |
self.assertEqual(
|
166 |
e.msg[:len(msg)],
|
167 |
msg, |
168 |
"%r doesn't start with %r for %r" % (e.msg, msg, data))
|
169 |
self.assertEqual(
|
170 |
e.pos, idx, |
171 |
"pos %r != %r for %r" % (e.pos, idx, data))
|
172 |
except Exception: |
173 |
e = sys.exc_info()[1]
|
174 |
self.fail("Unexpected exception raised %r %s" % (e, e)) |
175 |
else:
|
176 |
self.fail("Unexpected success parsing '%r'" % (data,)) |