test_separators.py 942 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import textwrap
  2. from unittest import TestCase
  3. import simplejson as json
  4. class TestSeparators(TestCase):
  5. def test_separators(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=' ', sort_keys=True, separators=(' ,', ' : '))
  30. h1 = json.loads(d1)
  31. h2 = json.loads(d2)
  32. self.assertEqual(h1, h)
  33. self.assertEqual(h2, h)
  34. self.assertEqual(d2, expect)