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