jsonutil_test.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # encoding=utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import unittest
  5. from terroroftinytown.util.jsonutil import NativeStringJSONEncoder, \
  6. NativeStringJSONDecoder
  7. class Test(unittest.TestCase):
  8. def test_encode(self):
  9. self.assertEqual(
  10. '["5C5C7830305C5C61"]',
  11. json.dumps([r'\x00\a'], cls=NativeStringJSONEncoder)
  12. )
  13. self.assertEqual(
  14. '["5C7866305C553030303166346565"]',
  15. json.dumps(['ð📮'], cls=NativeStringJSONEncoder)
  16. )
  17. self.assertEqual(
  18. '["5C7831665C7831665C786666"]',
  19. json.dumps([b'\x1f\x1f\xff'], cls=NativeStringJSONEncoder)
  20. )
  21. def test_decode(self):
  22. self.assertEqual(
  23. [r'\x00\a'],
  24. json.loads(r'["5C5C7830305C5C61"]', cls=NativeStringJSONDecoder)
  25. )
  26. self.assertEqual(
  27. ['ð📮'],
  28. json.loads(r'["5C7866305C553030303166346565"]',
  29. cls=NativeStringJSONDecoder)
  30. )
  31. self.assertEqual(
  32. ['\x1f\x1f\xff'],
  33. json.loads(r'["5C7831665C7831665C786666"]',
  34. cls=NativeStringJSONDecoder)
  35. )