1 | import json |
||
2 | import unittest |
||
3 | from datetime import datetime |
||
4 | |||
5 | from coalib.output.JSONEncoder import create_json_encoder |
||
6 | |||
7 | |||
8 | class TestClass1(object): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
9 | |||
10 | def __init__(self): |
||
11 | self.a = 0 |
||
12 | |||
13 | |||
14 | class TestClass2(object): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
15 | |||
16 | def __init__(self): |
||
17 | self.a = 0 |
||
18 | self.b = TestClass1() |
||
19 | |||
20 | |||
21 | class TestClass3(object): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
22 | |||
23 | def __init__(self): |
||
24 | self.a = 0 |
||
25 | self.b = TestClass1() |
||
26 | |||
27 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
28 | def __getitem__(key): |
||
29 | return "val" |
||
30 | |||
31 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
32 | def keys(): |
||
33 | return ["key"] |
||
34 | |||
35 | |||
36 | class PropertiedClass(object): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
37 | |||
38 | def __init__(self): |
||
39 | self._a = 5 |
||
40 | |||
41 | @property |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
42 | def prop(self): |
||
43 | return self._a |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
44 | |||
45 | |||
46 | class JSONAbleClass(object): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
47 | |||
48 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
49 | def __json__(): |
||
50 | return ['dont', 'panic'] |
||
51 | |||
52 | |||
53 | class JSONEncoderTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
54 | JSONEncoder = create_json_encoder(use_relpath=True) |
||
55 | kw = {"cls": JSONEncoder, "sort_keys": True} |
||
56 | |||
57 | def test_builtins(self): |
||
58 | self.assertEquals('"test"', json.dumps("test", **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
59 | self.assertEquals('1', json.dumps(1, **self.kw)) |
||
60 | self.assertEquals('true', json.dumps(True, **self.kw)) |
||
61 | self.assertEquals('null', json.dumps(None, **self.kw)) |
||
62 | |||
63 | def test_iter(self): |
||
64 | self.assertEquals('[0, 1]', json.dumps([0, 1], **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
65 | self.assertEquals('[0, 1]', json.dumps((0, 1), **self.kw)) |
||
66 | self.assertEquals('[0, 1]', json.dumps(range(2), **self.kw)) |
||
67 | |||
68 | def test_dict(self): |
||
69 | self.assertEquals('{"0": 1}', json.dumps({0: 1}, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
70 | self.assertEquals('{"0": 1}', json.dumps({"0": 1}, **self.kw)) |
||
71 | self.assertEquals('{"0": "1"}', json.dumps({"0": "1"}, **self.kw)) |
||
72 | |||
73 | def test_time(self): |
||
74 | tf = datetime.today() |
||
75 | self.assertEquals('"' + tf.isoformat() + '"', |
||
76 | json.dumps(tf, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
77 | |||
78 | def test_class1(self): |
||
79 | tc1 = TestClass1() |
||
80 | self.assertEquals('{"a": 0}', json.dumps(tc1, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
81 | self.assertEquals('[{"a": 0}]', json.dumps([tc1], **self.kw)) |
||
82 | self.assertEquals('{"0": {"a": 0}}', json.dumps({0: tc1}, **self.kw)) |
||
83 | |||
84 | def test_class2(self): |
||
85 | tc2 = TestClass2() |
||
86 | self.assertEquals('{"a": 0, "b": {"a": 0}}', |
||
87 | json.dumps(tc2, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
88 | |||
89 | def test_class3(self): |
||
90 | tc3 = TestClass3() |
||
91 | self.assertEquals('{"key": "val"}', |
||
92 | json.dumps(tc3, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
93 | |||
94 | def test_propertied_class(self): |
||
95 | uut = PropertiedClass() |
||
96 | self.assertEqual('{"prop": 5}', json.dumps(uut, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
97 | |||
98 | def test_jsonable_class(self): |
||
99 | uut = JSONAbleClass() |
||
100 | self.assertEqual('["dont", "panic"]', json.dumps(uut, **self.kw)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
101 | |||
102 | def test_type_error(self): |
||
103 | with self.assertRaises(TypeError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
104 | json.dumps(1j, **self.kw) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
105 |