• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import textwrap
2from test.test_json import PyTest, CTest
3
4
5class TestSeparators:
6    def test_separators(self):
7        h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
8             {'nifty': 87}, {'field': 'yes', 'morefield': False} ]
9
10        expect = textwrap.dedent("""\
11        [
12          [
13            "blorpie"
14          ] ,
15          [
16            "whoops"
17          ] ,
18          [] ,
19          "d-shtaeou" ,
20          "d-nthiouh" ,
21          "i-vhbjkhnth" ,
22          {
23            "nifty" : 87
24          } ,
25          {
26            "field" : "yes" ,
27            "morefield" : false
28          }
29        ]""")
30
31
32        d1 = self.dumps(h)
33        d2 = self.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))
34
35        h1 = self.loads(d1)
36        h2 = self.loads(d2)
37
38        self.assertEqual(h1, h)
39        self.assertEqual(h2, h)
40        self.assertEqual(d2, expect)
41
42    def test_illegal_separators(self):
43        h = {1: 2, 3: 4}
44        self.assertRaises(TypeError, self.dumps, h, separators=(b', ', ': '))
45        self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': '))
46        self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': '))
47
48
49class TestPySeparators(TestSeparators, PyTest): pass
50class TestCSeparators(TestSeparators, CTest): pass
51