ยปCore Development>Code coverage>Lib/test/test_json/test_enum.py

# Python code coverage for Lib/test/test_json/test_enum.py

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