|
import sys |
|
from unittest import TestCase |
|
|
|
import hjson as json |
|
|
|
|
|
JSONDOCS = [ |
|
|
|
|
|
|
|
'["Unclosed array"', |
|
|
|
|
|
|
|
|
|
|
|
'["double extra comma",,]', |
|
|
|
'[ , "<-- missing value"]', |
|
|
|
'["Comma after the close"],', |
|
|
|
'["Extra close"]]', |
|
|
|
|
|
|
|
'{"Extra value after close": true} "misplaced quoted value"', |
|
|
|
'{"Illegal expression": 1 + 2}', |
|
|
|
'{"Illegal invocation": alert()}', |
|
|
|
'{"Numbers cannot have leading zeroes": 013}', |
|
|
|
'{"Numbers cannot be hex": 0x14}', |
|
|
|
'["Illegal backslash escape: \\x15"]', |
|
|
|
'[\\naked]', |
|
|
|
'["Illegal backslash escape: \\017"]', |
|
|
|
|
|
|
|
'{"Missing colon" null}', |
|
|
|
'{"Double colon":: null}', |
|
|
|
'{"Comma instead of colon", null}', |
|
|
|
'["Colon instead of comma": false]', |
|
|
|
'["Bad value", truth]', |
|
|
|
|
|
|
|
'["\ttab\tcharacter\tin\tstring\t"]', |
|
|
|
'["tab\\ character\\ in\\ string\\ "]', |
|
|
|
'["line\nbreak"]', |
|
|
|
'["line\\\nbreak"]', |
|
|
|
'[0e]', |
|
|
|
'[0e+]', |
|
|
|
'[0e+-1]', |
|
|
|
'{"Comma instead if closing brace": true,', |
|
|
|
'["mismatch"}', |
|
|
|
u'["A\u001FZ control characters in string"]', |
|
|
|
'{', |
|
'{]', |
|
'{"foo": "bar"]', |
|
'{"foo": "bar"', |
|
] |
|
|
|
class TestFail(TestCase): |
|
def test_failures(self): |
|
for idx, doc in enumerate(JSONDOCS): |
|
idx = idx + 1 |
|
try: |
|
json.loads(doc) |
|
except json.HjsonDecodeError: |
|
pass |
|
else: |
|
self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) |
|
|
|
def test_array_decoder_issue46(self): |
|
|
|
for doc in [u'[,]', '[,]']: |
|
try: |
|
json.loads(doc) |
|
except json.HjsonDecodeError: |
|
pass |
|
except Exception: |
|
e = sys.exc_info()[1] |
|
self.fail("Unexpected exception raised %r %s" % (e, e)) |
|
else: |
|
self.fail("Unexpected success parsing '[,]'") |
|
|
|
def test_truncated_input(self): |
|
test_cases = [ |
|
('[', "End of input while parsing an array", 1), |
|
|
|
('[42,', 'Expecting value', 4), |
|
('["', 'Unterminated string starting at', 1), |
|
('["spam', 'Unterminated string starting at', 1), |
|
|
|
('["spam",', 'Expecting value', 8), |
|
('{', 'Bad key name (eof)', 1), |
|
('{"', 'Unterminated string starting at', 1), |
|
('{"spam', 'Unterminated string starting at', 1), |
|
('{"spam"', "Expecting ':' delimiter", 7), |
|
('{"spam":', 'Expecting value', 8), |
|
|
|
('{"spam":42,', 'Bad key name (eof)', 11), |
|
('"', 'Unterminated string starting at', 0), |
|
('"spam', 'Unterminated string starting at', 0), |
|
('[,', "Found a punctuator character", 1), |
|
] |
|
for data, msg, idx in test_cases: |
|
try: |
|
json.loads(data) |
|
except json.HjsonDecodeError: |
|
e = sys.exc_info()[1] |
|
self.assertEqual( |
|
e.msg[:len(msg)], |
|
msg, |
|
"%r doesn't start with %r for %r" % (e.msg, msg, data)) |
|
self.assertEqual( |
|
e.pos, idx, |
|
"pos %r != %r for %r" % (e.pos, idx, data)) |
|
except Exception: |
|
e = sys.exc_info()[1] |
|
self.fail("Unexpected exception raised %r %s" % (e, e)) |
|
else: |
|
self.fail("Unexpected success parsing '%r'" % (data,)) |
|
|