diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2013-08-10 20:01:45 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2013-08-10 20:01:45 (GMT) |
commit | a4998a70416c27730e75c0a4225ee2c3552b1618 (patch) | |
tree | 5f04a47e0c77fdc0d0e05ff4a09d9a3e3c42702e /Lib/test/test_json | |
parent | fbcf4d78c881176345483111503c4bc7765d4ff8 (diff) | |
download | cpython-a4998a70416c27730e75c0a4225ee2c3552b1618.zip cpython-a4998a70416c27730e75c0a4225ee2c3552b1618.tar.gz cpython-a4998a70416c27730e75c0a4225ee2c3552b1618.tar.bz2 |
Close #18264: int- and float-derived enums now converted to int or float.
Diffstat (limited to 'Lib/test/test_json')
-rw-r--r-- | Lib/test/test_json/test_enum.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Lib/test/test_json/test_enum.py b/Lib/test/test_json/test_enum.py new file mode 100644 index 0000000..66c6480 --- /dev/null +++ b/Lib/test/test_json/test_enum.py @@ -0,0 +1,81 @@ +from enum import Enum, IntEnum +from test.test_json import PyTest, CTest + +SMALL = 1 +BIG = 1<<32 +HUGE = 1<<64 +REALLY_HUGE = 1<<96 + +class BigNum(IntEnum): + small = SMALL + big = BIG + huge = HUGE + really_huge = REALLY_HUGE + +E = 2.718281 +PI = 3.141593 +TAU = 2 * PI + +class FloatNum(float, Enum): + e = E + pi = PI + tau = TAU + +class TestEnum: + + def test_floats(self): + for enum in FloatNum: + self.assertEqual(self.dumps(enum), repr(enum.value)) + self.assertEqual(float(self.dumps(enum)), enum) + self.assertEqual(self.loads(self.dumps(enum)), enum) + + def test_ints(self): + for enum in BigNum: + self.assertEqual(self.dumps(enum), str(enum.value)) + self.assertEqual(int(self.dumps(enum)), enum) + self.assertEqual(self.loads(self.dumps(enum)), enum) + + def test_list(self): + self.assertEqual( + self.dumps(list(BigNum)), + str([SMALL, BIG, HUGE, REALLY_HUGE]), + ) + self.assertEqual(self.dumps(list(FloatNum)), str([E, PI, TAU])) + + def test_dict_keys(self): + s, b, h, r = BigNum + e, p, t = FloatNum + d = { + s:'tiny', b:'large', h:'larger', r:'largest', + e:"Euler's number", p:'pi', t:'tau', + } + nd = self.loads(self.dumps(d)) + self.assertEqual(nd[str(SMALL)], 'tiny') + self.assertEqual(nd[str(BIG)], 'large') + self.assertEqual(nd[str(HUGE)], 'larger') + self.assertEqual(nd[str(REALLY_HUGE)], 'largest') + self.assertEqual(nd[repr(E)], "Euler's number") + self.assertEqual(nd[repr(PI)], 'pi') + self.assertEqual(nd[repr(TAU)], 'tau') + + def test_dict_values(self): + d = dict( + tiny=BigNum.small, + large=BigNum.big, + larger=BigNum.huge, + largest=BigNum.really_huge, + e=FloatNum.e, + pi=FloatNum.pi, + tau=FloatNum.tau, + ) + nd = self.loads(self.dumps(d)) + self.assertEqual(nd['tiny'], SMALL) + self.assertEqual(nd['large'], BIG) + self.assertEqual(nd['larger'], HUGE) + self.assertEqual(nd['largest'], REALLY_HUGE) + self.assertEqual(nd['e'], E) + self.assertEqual(nd['pi'], PI) + self.assertEqual(nd['tau'], TAU) + +class TestPyEnum(TestEnum, PyTest): pass +class TestCEnum(TestEnum, CTest): pass |