1 | n/a | from enum import Enum, IntEnum |
---|
2 | n/a | from math import isnan |
---|
3 | n/a | from test.test_json import PyTest, CTest |
---|
4 | n/a | |
---|
5 | n/a | SMALL = 1 |
---|
6 | n/a | BIG = 1<<32 |
---|
7 | n/a | HUGE = 1<<64 |
---|
8 | n/a | REALLY_HUGE = 1<<96 |
---|
9 | n/a | |
---|
10 | n/a | class BigNum(IntEnum): |
---|
11 | n/a | small = SMALL |
---|
12 | n/a | big = BIG |
---|
13 | n/a | huge = HUGE |
---|
14 | n/a | really_huge = REALLY_HUGE |
---|
15 | n/a | |
---|
16 | n/a | E = 2.718281 |
---|
17 | n/a | PI = 3.141593 |
---|
18 | n/a | TAU = 2 * PI |
---|
19 | n/a | |
---|
20 | n/a | class FloatNum(float, Enum): |
---|
21 | n/a | e = E |
---|
22 | n/a | pi = PI |
---|
23 | n/a | tau = TAU |
---|
24 | n/a | |
---|
25 | n/a | INF = float('inf') |
---|
26 | n/a | NEG_INF = float('-inf') |
---|
27 | n/a | NAN = float('nan') |
---|
28 | n/a | |
---|
29 | n/a | class WierdNum(float, Enum): |
---|
30 | n/a | inf = INF |
---|
31 | n/a | neg_inf = NEG_INF |
---|
32 | n/a | nan = NAN |
---|
33 | n/a | |
---|
34 | n/a | class TestEnum: |
---|
35 | n/a | |
---|
36 | n/a | def test_floats(self): |
---|
37 | n/a | for enum in FloatNum: |
---|
38 | n/a | self.assertEqual(self.dumps(enum), repr(enum.value)) |
---|
39 | n/a | self.assertEqual(float(self.dumps(enum)), enum) |
---|
40 | n/a | self.assertEqual(self.loads(self.dumps(enum)), enum) |
---|
41 | n/a | |
---|
42 | n/a | def test_weird_floats(self): |
---|
43 | n/a | for enum, expected in zip(WierdNum, ('Infinity', '-Infinity', 'NaN')): |
---|
44 | n/a | self.assertEqual(self.dumps(enum), expected) |
---|
45 | n/a | if not isnan(enum): |
---|
46 | n/a | self.assertEqual(float(self.dumps(enum)), enum) |
---|
47 | n/a | self.assertEqual(self.loads(self.dumps(enum)), enum) |
---|
48 | n/a | else: |
---|
49 | n/a | self.assertTrue(isnan(float(self.dumps(enum)))) |
---|
50 | n/a | self.assertTrue(isnan(self.loads(self.dumps(enum)))) |
---|
51 | n/a | |
---|
52 | n/a | def test_ints(self): |
---|
53 | n/a | for enum in BigNum: |
---|
54 | n/a | self.assertEqual(self.dumps(enum), str(enum.value)) |
---|
55 | n/a | self.assertEqual(int(self.dumps(enum)), enum) |
---|
56 | n/a | self.assertEqual(self.loads(self.dumps(enum)), enum) |
---|
57 | n/a | |
---|
58 | n/a | def test_list(self): |
---|
59 | n/a | self.assertEqual(self.dumps(list(BigNum)), |
---|
60 | n/a | str([SMALL, BIG, HUGE, REALLY_HUGE])) |
---|
61 | n/a | self.assertEqual(self.loads(self.dumps(list(BigNum))), |
---|
62 | n/a | list(BigNum)) |
---|
63 | n/a | self.assertEqual(self.dumps(list(FloatNum)), |
---|
64 | n/a | str([E, PI, TAU])) |
---|
65 | n/a | self.assertEqual(self.loads(self.dumps(list(FloatNum))), |
---|
66 | n/a | list(FloatNum)) |
---|
67 | n/a | self.assertEqual(self.dumps(list(WierdNum)), |
---|
68 | n/a | '[Infinity, -Infinity, NaN]') |
---|
69 | n/a | self.assertEqual(self.loads(self.dumps(list(WierdNum)))[:2], |
---|
70 | n/a | list(WierdNum)[:2]) |
---|
71 | n/a | self.assertTrue(isnan(self.loads(self.dumps(list(WierdNum)))[2])) |
---|
72 | n/a | |
---|
73 | n/a | def test_dict_keys(self): |
---|
74 | n/a | s, b, h, r = BigNum |
---|
75 | n/a | e, p, t = FloatNum |
---|
76 | n/a | i, j, n = WierdNum |
---|
77 | n/a | d = { |
---|
78 | n/a | s:'tiny', b:'large', h:'larger', r:'largest', |
---|
79 | n/a | e:"Euler's number", p:'pi', t:'tau', |
---|
80 | n/a | i:'Infinity', j:'-Infinity', n:'NaN', |
---|
81 | n/a | } |
---|
82 | n/a | nd = self.loads(self.dumps(d)) |
---|
83 | n/a | self.assertEqual(nd[str(SMALL)], 'tiny') |
---|
84 | n/a | self.assertEqual(nd[str(BIG)], 'large') |
---|
85 | n/a | self.assertEqual(nd[str(HUGE)], 'larger') |
---|
86 | n/a | self.assertEqual(nd[str(REALLY_HUGE)], 'largest') |
---|
87 | n/a | self.assertEqual(nd[repr(E)], "Euler's number") |
---|
88 | n/a | self.assertEqual(nd[repr(PI)], 'pi') |
---|
89 | n/a | self.assertEqual(nd[repr(TAU)], 'tau') |
---|
90 | n/a | self.assertEqual(nd['Infinity'], 'Infinity') |
---|
91 | n/a | self.assertEqual(nd['-Infinity'], '-Infinity') |
---|
92 | n/a | self.assertEqual(nd['NaN'], 'NaN') |
---|
93 | n/a | |
---|
94 | n/a | def test_dict_values(self): |
---|
95 | n/a | d = dict( |
---|
96 | n/a | tiny=BigNum.small, |
---|
97 | n/a | large=BigNum.big, |
---|
98 | n/a | larger=BigNum.huge, |
---|
99 | n/a | largest=BigNum.really_huge, |
---|
100 | n/a | e=FloatNum.e, |
---|
101 | n/a | pi=FloatNum.pi, |
---|
102 | n/a | tau=FloatNum.tau, |
---|
103 | n/a | i=WierdNum.inf, |
---|
104 | n/a | j=WierdNum.neg_inf, |
---|
105 | n/a | n=WierdNum.nan, |
---|
106 | n/a | ) |
---|
107 | n/a | nd = self.loads(self.dumps(d)) |
---|
108 | n/a | self.assertEqual(nd['tiny'], SMALL) |
---|
109 | n/a | self.assertEqual(nd['large'], BIG) |
---|
110 | n/a | self.assertEqual(nd['larger'], HUGE) |
---|
111 | n/a | self.assertEqual(nd['largest'], REALLY_HUGE) |
---|
112 | n/a | self.assertEqual(nd['e'], E) |
---|
113 | n/a | self.assertEqual(nd['pi'], PI) |
---|
114 | n/a | self.assertEqual(nd['tau'], TAU) |
---|
115 | n/a | self.assertEqual(nd['i'], INF) |
---|
116 | n/a | self.assertEqual(nd['j'], NEG_INF) |
---|
117 | n/a | self.assertTrue(isnan(nd['n'])) |
---|
118 | n/a | |
---|
119 | n/a | class TestPyEnum(TestEnum, PyTest): pass |
---|
120 | n/a | class TestCEnum(TestEnum, CTest): pass |
---|