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