ยปCore Development>Code coverage>Lib/test/test_json/test_dump.py

Python code coverage for Lib/test/test_json/test_dump.py

#countcontent
1n/afrom io import StringIO
2n/afrom test.test_json import PyTest, CTest
3n/a
4n/afrom test.support import bigmemtest, _1G
5n/a
6n/aclass TestDump:
7n/a def test_dump(self):
8n/a sio = StringIO()
9n/a self.json.dump({}, sio)
10n/a self.assertEqual(sio.getvalue(), '{}')
11n/a
12n/a def test_dumps(self):
13n/a self.assertEqual(self.dumps({}), '{}')
14n/a
15n/a def test_encode_truefalse(self):
16n/a self.assertEqual(self.dumps(
17n/a {True: False, False: True}, sort_keys=True),
18n/a '{"false": true, "true": false}')
19n/a self.assertEqual(self.dumps(
20n/a {2: 3.0, 4.0: 5, False: 1, 6: True}, sort_keys=True),
21n/a '{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
22n/a
23n/a # Issue 16228: Crash on encoding resized list
24n/a def test_encode_mutated(self):
25n/a a = [object()] * 10
26n/a def crasher(obj):
27n/a del a[-1]
28n/a self.assertEqual(self.dumps(a, default=crasher),
29n/a '[null, null, null, null, null]')
30n/a
31n/a # Issue 24094
32n/a def test_encode_evil_dict(self):
33n/a class D(dict):
34n/a def keys(self):
35n/a return L
36n/a
37n/a class X:
38n/a def __hash__(self):
39n/a del L[0]
40n/a return 1337
41n/a
42n/a def __lt__(self, o):
43n/a return 0
44n/a
45n/a L = [X() for i in range(1122)]
46n/a d = D()
47n/a d[1337] = "true.dat"
48n/a self.assertEqual(self.dumps(d, sort_keys=True), '{"1337": "true.dat"}')
49n/a
50n/a
51n/aclass TestPyDump(TestDump, PyTest): pass
52n/a
53n/aclass TestCDump(TestDump, CTest):
54n/a
55n/a # The size requirement here is hopefully over-estimated (actual
56n/a # memory consumption depending on implementation details, and also
57n/a # system memory management, since this may allocate a lot of
58n/a # small objects).
59n/a
60n/a @bigmemtest(size=_1G, memuse=1)
61n/a def test_large_list(self, size):
62n/a N = int(30 * 1024 * 1024 * (size / _1G))
63n/a l = [1] * N
64n/a encoded = self.dumps(l)
65n/a self.assertEqual(len(encoded), N * 3)
66n/a self.assertEqual(encoded[:1], "[")
67n/a self.assertEqual(encoded[-2:], "1]")
68n/a self.assertEqual(encoded[1:-2], "1, " * (N - 1))