test_fail.py 6.2 KB

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