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

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

#countcontent
1n/aimport textwrap
2n/afrom io import StringIO
3n/afrom test.test_json import PyTest, CTest
4n/a
5n/a
6n/aclass TestIndent:
7n/a def test_indent(self):
8n/a h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
9n/a {'nifty': 87}, {'field': 'yes', 'morefield': False} ]
10n/a
11n/a expect = textwrap.dedent("""\
12n/a [
13n/a \t[
14n/a \t\t"blorpie"
15n/a \t],
16n/a \t[
17n/a \t\t"whoops"
18n/a \t],
19n/a \t[],
20n/a \t"d-shtaeou",
21n/a \t"d-nthiouh",
22n/a \t"i-vhbjkhnth",
23n/a \t{
24n/a \t\t"nifty": 87
25n/a \t},
26n/a \t{
27n/a \t\t"field": "yes",
28n/a \t\t"morefield": false
29n/a \t}
30n/a ]""")
31n/a
32n/a d1 = self.dumps(h)
33n/a d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
34n/a d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
35n/a d4 = self.dumps(h, indent=2, sort_keys=True)
36n/a d5 = self.dumps(h, indent='\t', sort_keys=True)
37n/a
38n/a h1 = self.loads(d1)
39n/a h2 = self.loads(d2)
40n/a h3 = self.loads(d3)
41n/a
42n/a self.assertEqual(h1, h)
43n/a self.assertEqual(h2, h)
44n/a self.assertEqual(h3, h)
45n/a self.assertEqual(d2, expect.expandtabs(2))
46n/a self.assertEqual(d3, expect)
47n/a self.assertEqual(d4, d2)
48n/a self.assertEqual(d5, d3)
49n/a
50n/a def test_indent0(self):
51n/a h = {3: 1}
52n/a def check(indent, expected):
53n/a d1 = self.dumps(h, indent=indent)
54n/a self.assertEqual(d1, expected)
55n/a
56n/a sio = StringIO()
57n/a self.json.dump(h, sio, indent=indent)
58n/a self.assertEqual(sio.getvalue(), expected)
59n/a
60n/a # indent=0 should emit newlines
61n/a check(0, '{\n"3": 1\n}')
62n/a # indent=None is more compact
63n/a check(None, '{"3": 1}')
64n/a
65n/a
66n/aclass TestPyIndent(TestIndent, PyTest): pass
67n/aclass TestCIndent(TestIndent, CTest): pass