From 03be0afeebbc163cd915f36e70e9ea970fe5f8e4 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Sun, 30 Sep 2018 21:37:15 +0100 Subject: [PATCH] tests: Add tests of mitogen.utils.cast() --- tests/utils_test.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/utils_test.py b/tests/utils_test.py index 4c2e2e0f..b2e0aa9e 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -2,6 +2,7 @@ import unittest2 +import mitogen.core import mitogen.master import mitogen.utils @@ -32,5 +33,64 @@ class WithRouterTest(unittest2.TestCase): self.assertFalse(router.broker._thread.isAlive()) +class Dict(dict): pass +class List(list): pass +class Tuple(tuple): pass +class Unicode(mitogen.core.UnicodeType): pass +class Bytes(mitogen.core.BytesType): pass + + +class CastTest(unittest2.TestCase): + def test_dict(self): + self.assertEqual(type(mitogen.utils.cast({})), dict) + self.assertEqual(type(mitogen.utils.cast(Dict())), dict) + + def test_nested_dict(self): + specimen = mitogen.utils.cast(Dict({'k': Dict({'k2': 'v2'})})) + self.assertEqual(type(specimen), dict) + self.assertEqual(type(specimen['k']), dict) + + def test_list(self): + self.assertEqual(type(mitogen.utils.cast([])), list) + self.assertEqual(type(mitogen.utils.cast(List())), list) + + def test_nested_list(self): + specimen = mitogen.utils.cast(List((0, 1, List((None,))))) + self.assertEqual(type(specimen), list) + self.assertEqual(type(specimen[2]), list) + + def test_tuple(self): + self.assertEqual(type(mitogen.utils.cast(())), list) + self.assertEqual(type(mitogen.utils.cast(Tuple())), list) + + def test_nested_tuple(self): + specimen = mitogen.utils.cast(Tuple((0, 1, Tuple((None,))))) + self.assertEqual(type(specimen), list) + self.assertEqual(type(specimen[2]), list) + + def assertUnchanged(self, v): + self.assertIs(mitogen.utils.cast(v), v) + + def test_passthrough(self): + self.assertUnchanged(0) + self.assertUnchanged(0.0) + self.assertUnchanged(float('inf')) + self.assertUnchanged(True) + self.assertUnchanged(False) + self.assertUnchanged(None) + + def test_unicode(self): + self.assertEqual(type(mitogen.utils.cast(u'')), mitogen.core.UnicodeType) + self.assertEqual(type(mitogen.utils.cast(Unicode())), mitogen.core.UnicodeType) + + def test_bytes(self): + self.assertEqual(type(mitogen.utils.cast(b'')), mitogen.core.BytesType) + self.assertEqual(type(mitogen.utils.cast(Bytes())), mitogen.core.BytesType) + + def test_unknown(self): + self.assertRaises(TypeError, mitogen.utils.cast, set()) + self.assertRaises(TypeError, mitogen.utils.cast, 4j) + + if __name__ == '__main__': unittest2.main()