/Lib/json/tests/test_indent.py

http://unladen-swallow.googlecode.com/ · Python · 41 lines · 34 code · 7 blank · 0 comment · 0 complexity · 03b71c00fc8534a70847e9667b69e482 MD5 · raw file

  1. from unittest import TestCase
  2. import json
  3. import textwrap
  4. class TestIndent(TestCase):
  5. def test_indent(self):
  6. h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
  7. {'nifty': 87}, {'field': 'yes', 'morefield': False} ]
  8. expect = textwrap.dedent("""\
  9. [
  10. [
  11. "blorpie"
  12. ],
  13. [
  14. "whoops"
  15. ],
  16. [],
  17. "d-shtaeou",
  18. "d-nthiouh",
  19. "i-vhbjkhnth",
  20. {
  21. "nifty": 87
  22. },
  23. {
  24. "field": "yes",
  25. "morefield": false
  26. }
  27. ]""")
  28. d1 = json.dumps(h)
  29. d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
  30. h1 = json.loads(d1)
  31. h2 = json.loads(d2)
  32. self.assertEquals(h1, h)
  33. self.assertEquals(h2, h)
  34. self.assertEquals(d2, expect)