1 from cStringIO import StringIO 2 from json.tests import PyTest, CTest 3 4 5 class TestDump(object): 6 def test_dump(self): 7 sio = StringIO() 8 self.json.dump({}, sio) 9 self.assertEqual(sio.getvalue(), '{}') 10 11 def test_dumps(self): 12 self.assertEqual(self.dumps({}), '{}') 13 14 def test_encode_truefalse(self): 15 self.assertEqual(self.dumps( 16 {True: False, False: True}, sort_keys=True), 17 '{"false": true, "true": false}') 18 self.assertEqual(self.dumps( 19 {2: 3.0, 4.0: 5L, False: 1, 6L: True}, sort_keys=True), 20 '{"false": 1, "2": 3.0, "4.0": 5, "6": true}') 21 22 # Issue 16228: Crash on encoding resized list 23 def test_encode_mutated(self): 24 a = [object()] * 10 25 def crasher(obj): 26 del a[-1] 27 self.assertEqual(self.dumps(a, default=crasher), 28 '[null, null, null, null, null]') 29 30 31 class TestPyDump(TestDump, PyTest): pass 32 class TestCDump(TestDump, CTest): pass 33