1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
7 # As a special exception, if other files instantiate templates or use macros
8 # or inline functions from this file, or you compile this file and link it
9 # with other works to produce a work based on this file, this file
10 # does not by itself cause the resulting work to be covered
11 # by the GNU General Public License.
13 # However the source code for this file must still be made available
14 # in accordance with section (3) of the GNU General Public License.
16 # This exception does not invalidate any other reasons why a work based
17 # on this file might be covered by the GNU General Public License.
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
21 """ type_helper_unittest.py: unit tests for type_helpers
23 Tests classes and functions in type_helpers
25 Should be run from python2 and python3!
27 For help see :py:mod:`unittest`
32 from src.type_helpers import is_unicode, isstr
33 from sys import version_info
36 is_py2 = version_info.major == 2
38 class TypeHelperTester(unittest.TestCase):
40 def test_is_py2_or_py3(self):
41 """ test that python version is 2 or 3
43 when implementing type_helpers, there was no py4 and no idea what it
44 might be like. Also will probably fail for python v1
46 self.assertIn(version_info.major, (2, 3))
48 def test_is_unicode(self):
49 """ tests function is_unicode """
51 self.assertFalse(is_unicode(1))
52 self.assertFalse(is_unicode([]))
53 self.assertFalse(is_unicode(unittest.TestCase))
54 self.assertFalse(is_unicode(type(unittest.TestCase)))
57 self.assertTrue(is_unicode(u'bla'))
58 self.assertTrue(eval("is_unicode(ur'bla')")) # not allowed in py3!
59 self.assertFalse(is_unicode('bla'))
60 self.assertFalse(is_unicode(r'bla'))
62 self.assertTrue(is_unicode('bla'))
63 self.assertTrue(is_unicode(r'bla'))
64 self.assertFalse(is_unicode(b'bla'))
65 self.assertFalse(is_unicode(br'bla'))
68 """ test function isstr """
71 ('abc', True), (u'abc', True), (r'abc', True),
72 (1, False), (['a', 'b', 'c'], False), (('a', 'b', 'c'), False)]
74 tests.append((b'abc', False)) # b'' does not exist in py2
76 for test_var, expected_result in tests:
77 self.assertEqual(isstr(test_var), expected_result,
78 'isstr of var {0} (which is of type {1}) returned'
79 '{2} but expected {3}!'
80 .format(test_var, type(test_var), isstr(test_var),
84 if __name__ == '__main__':