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