2022-12-17 08:32:48 -04:00
|
|
|
import collections
|
2013-08-08 09:03:45 -03:00
|
|
|
from test.test_json import PyTest, CTest
|
2008-05-08 11:29:10 -03:00
|
|
|
|
|
|
|
|
2011-05-14 00:47:51 -03:00
|
|
|
class TestDefault:
|
2008-05-08 11:29:10 -03:00
|
|
|
def test_default(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(
|
2011-05-14 00:47:51 -03:00
|
|
|
self.dumps(type, default=repr),
|
|
|
|
self.dumps(repr(type)))
|
|
|
|
|
2024-07-23 14:02:54 -03:00
|
|
|
def test_bad_default(self):
|
|
|
|
def default(obj):
|
|
|
|
if obj is NotImplemented:
|
|
|
|
raise ValueError
|
|
|
|
if obj is ...:
|
|
|
|
return NotImplemented
|
|
|
|
if obj is type:
|
|
|
|
return collections
|
|
|
|
return [...]
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError) as cm:
|
|
|
|
self.dumps(type, default=default)
|
|
|
|
self.assertEqual(cm.exception.__notes__,
|
|
|
|
['when serializing ellipsis object',
|
|
|
|
'when serializing list item 0',
|
|
|
|
'when serializing module object',
|
|
|
|
'when serializing type object'])
|
|
|
|
|
2022-12-17 08:32:48 -04:00
|
|
|
def test_ordereddict(self):
|
|
|
|
od = collections.OrderedDict(a=1, b=2, c=3, d=4)
|
|
|
|
od.move_to_end('b')
|
|
|
|
self.assertEqual(
|
|
|
|
self.dumps(od),
|
|
|
|
'{"a": 1, "c": 3, "d": 4, "b": 2}')
|
|
|
|
self.assertEqual(
|
|
|
|
self.dumps(od, sort_keys=True),
|
|
|
|
'{"a": 1, "b": 2, "c": 3, "d": 4}')
|
|
|
|
|
2011-05-14 00:47:51 -03:00
|
|
|
|
|
|
|
class TestPyDefault(TestDefault, PyTest): pass
|
|
|
|
class TestCDefault(TestDefault, CTest): pass
|