diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2013-09-02 08:14:56 (GMT) |
---|---|---|
committer | Ethan Furman <ethan@stoneleaf.us> | 2013-09-02 08:14:56 (GMT) |
commit | a661f4531e2c8d189dc4a6c22ce74d2bfd18cbcd (patch) | |
tree | 7a690574a0985ca1a064c885930723a032957f69 | |
parent | 51bae475e8a2c13bba2b6550265b7c92078f704c (diff) | |
download | cpython-a661f4531e2c8d189dc4a6c22ce74d2bfd18cbcd.zip cpython-a661f4531e2c8d189dc4a6c22ce74d2bfd18cbcd.tar.gz cpython-a661f4531e2c8d189dc4a6c22ce74d2bfd18cbcd.tar.bz2 |
Close #18745: Improve enum tests in test_json for infinities and NaN.
-rw-r--r-- | Lib/test/test_json/test_enum.py | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/Lib/test/test_json/test_enum.py b/Lib/test/test_json/test_enum.py index 66c6480..10f4148 100644 --- a/Lib/test/test_json/test_enum.py +++ b/Lib/test/test_json/test_enum.py @@ -1,4 +1,5 @@ from enum import Enum, IntEnum +from math import isnan from test.test_json import PyTest, CTest SMALL = 1 @@ -21,6 +22,15 @@ class FloatNum(float, Enum): pi = PI tau = TAU +INF = float('inf') +NEG_INF = float('-inf') +NAN = float('nan') + +class WierdNum(float, Enum): + inf = INF + neg_inf = NEG_INF + nan = NAN + class TestEnum: def test_floats(self): @@ -29,6 +39,16 @@ class TestEnum: self.assertEqual(float(self.dumps(enum)), enum) self.assertEqual(self.loads(self.dumps(enum)), enum) + def test_weird_floats(self): + for enum, expected in zip(WierdNum, ('Infinity', '-Infinity', 'NaN')): + self.assertEqual(self.dumps(enum), expected) + if not isnan(enum): + self.assertEqual(float(self.dumps(enum)), enum) + self.assertEqual(self.loads(self.dumps(enum)), enum) + else: + self.assertTrue(isnan(float(self.dumps(enum)))) + self.assertTrue(isnan(self.loads(self.dumps(enum)))) + def test_ints(self): for enum in BigNum: self.assertEqual(self.dumps(enum), str(enum.value)) @@ -36,18 +56,28 @@ class TestEnum: 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])) + self.assertEqual(self.dumps(list(BigNum)), + str([SMALL, BIG, HUGE, REALLY_HUGE])) + self.assertEqual(self.loads(self.dumps(list(BigNum))), + list(BigNum)) + self.assertEqual(self.dumps(list(FloatNum)), + str([E, PI, TAU])) + self.assertEqual(self.loads(self.dumps(list(FloatNum))), + list(FloatNum)) + self.assertEqual(self.dumps(list(WierdNum)), + '[Infinity, -Infinity, NaN]') + self.assertEqual(self.loads(self.dumps(list(WierdNum)))[:2], + list(WierdNum)[:2]) + self.assertTrue(isnan(self.loads(self.dumps(list(WierdNum)))[2])) def test_dict_keys(self): s, b, h, r = BigNum e, p, t = FloatNum + i, j, n = WierdNum d = { s:'tiny', b:'large', h:'larger', r:'largest', e:"Euler's number", p:'pi', t:'tau', + i:'Infinity', j:'-Infinity', n:'NaN', } nd = self.loads(self.dumps(d)) self.assertEqual(nd[str(SMALL)], 'tiny') @@ -57,6 +87,9 @@ class TestEnum: self.assertEqual(nd[repr(E)], "Euler's number") self.assertEqual(nd[repr(PI)], 'pi') self.assertEqual(nd[repr(TAU)], 'tau') + self.assertEqual(nd['Infinity'], 'Infinity') + self.assertEqual(nd['-Infinity'], '-Infinity') + self.assertEqual(nd['NaN'], 'NaN') def test_dict_values(self): d = dict( @@ -67,6 +100,9 @@ class TestEnum: e=FloatNum.e, pi=FloatNum.pi, tau=FloatNum.tau, + i=WierdNum.inf, + j=WierdNum.neg_inf, + n=WierdNum.nan, ) nd = self.loads(self.dumps(d)) self.assertEqual(nd['tiny'], SMALL) @@ -76,6 +112,9 @@ class TestEnum: self.assertEqual(nd['e'], E) self.assertEqual(nd['pi'], PI) self.assertEqual(nd['tau'], TAU) + self.assertEqual(nd['i'], INF) + self.assertEqual(nd['j'], NEG_INF) + self.assertTrue(isnan(nd['n'])) class TestPyEnum(TestEnum, PyTest): pass class TestCEnum(TestEnum, CTest): pass |