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