1 | n/a | import textwrap |
---|
2 | n/a | from test.test_json import PyTest, CTest |
---|
3 | n/a | |
---|
4 | n/a | |
---|
5 | n/a | class TestSeparators: |
---|
6 | n/a | def test_separators(self): |
---|
7 | n/a | h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', |
---|
8 | n/a | {'nifty': 87}, {'field': 'yes', 'morefield': False} ] |
---|
9 | n/a | |
---|
10 | n/a | expect = textwrap.dedent("""\ |
---|
11 | n/a | [ |
---|
12 | n/a | [ |
---|
13 | n/a | "blorpie" |
---|
14 | n/a | ] , |
---|
15 | n/a | [ |
---|
16 | n/a | "whoops" |
---|
17 | n/a | ] , |
---|
18 | n/a | [] , |
---|
19 | n/a | "d-shtaeou" , |
---|
20 | n/a | "d-nthiouh" , |
---|
21 | n/a | "i-vhbjkhnth" , |
---|
22 | n/a | { |
---|
23 | n/a | "nifty" : 87 |
---|
24 | n/a | } , |
---|
25 | n/a | { |
---|
26 | n/a | "field" : "yes" , |
---|
27 | n/a | "morefield" : false |
---|
28 | n/a | } |
---|
29 | n/a | ]""") |
---|
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 | |
---|
35 | n/a | h1 = self.loads(d1) |
---|
36 | n/a | h2 = self.loads(d2) |
---|
37 | n/a | |
---|
38 | n/a | self.assertEqual(h1, h) |
---|
39 | n/a | self.assertEqual(h2, h) |
---|
40 | n/a | self.assertEqual(d2, expect) |
---|
41 | n/a | |
---|
42 | n/a | def test_illegal_separators(self): |
---|
43 | n/a | h = {1: 2, 3: 4} |
---|
44 | n/a | self.assertRaises(TypeError, self.dumps, h, separators=(b', ', ': ')) |
---|
45 | n/a | self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': ')) |
---|
46 | n/a | self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': ')) |
---|
47 | n/a | |
---|
48 | n/a | |
---|
49 | n/a | class TestPySeparators(TestSeparators, PyTest): pass |
---|
50 | n/a | class TestCSeparators(TestSeparators, CTest): pass |
---|