/Lib/json/tests/test_pass1.py

http://unladen-swallow.googlecode.com/ · Python · 76 lines · 71 code · 3 blank · 2 comment · 3 complexity · 19ac46570766fdaa9df8371f12f6310b MD5 · raw file

  1. from unittest import TestCase
  2. import json
  3. # from http://json.org/JSON_checker/test/pass1.json
  4. JSON = r'''
  5. [
  6. "JSON Test Pattern pass1",
  7. {"object with 1 member":["array with 1 element"]},
  8. {},
  9. [],
  10. -42,
  11. true,
  12. false,
  13. null,
  14. {
  15. "integer": 1234567890,
  16. "real": -9876.543210,
  17. "e": 0.123456789e-12,
  18. "E": 1.234567890E+34,
  19. "": 23456789012E666,
  20. "zero": 0,
  21. "one": 1,
  22. "space": " ",
  23. "quote": "\"",
  24. "backslash": "\\",
  25. "controls": "\b\f\n\r\t",
  26. "slash": "/ & \/",
  27. "alpha": "abcdefghijklmnopqrstuvwyz",
  28. "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
  29. "digit": "0123456789",
  30. "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
  31. "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
  32. "true": true,
  33. "false": false,
  34. "null": null,
  35. "array":[ ],
  36. "object":{ },
  37. "address": "50 St. James Street",
  38. "url": "http://www.JSON.org/",
  39. "comment": "// /* <!-- --",
  40. "# -- --> */": " ",
  41. " s p a c e d " :[1,2 , 3
  42. ,
  43. 4 , 5 , 6 ,7 ],
  44. "compact": [1,2,3,4,5,6,7],
  45. "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
  46. "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
  47. "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
  48. : "A key can be any string"
  49. },
  50. 0.5 ,98.6
  51. ,
  52. 99.44
  53. ,
  54. 1066
  55. ,"rosebud"]
  56. '''
  57. class TestPass1(TestCase):
  58. def test_parse(self):
  59. # test in/out equivalence and parsing
  60. res = json.loads(JSON)
  61. out = json.dumps(res)
  62. self.assertEquals(res, json.loads(out))
  63. try:
  64. json.dumps(res, allow_nan=False)
  65. except ValueError:
  66. pass
  67. else:
  68. self.fail("23456789012E666 should be out of range")