c9cb289749e2f266ead248274190cf45fd02656b
[pyi2ncommon] / test / test_type_helpers.py
1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
3 #
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
6 #
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.
12 #
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.
15 #
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.
18 #
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
20
21 """ type_helper_unittest.py: unit tests for type_helpers
22
23 Tests classes and functions in type_helpers
24
25 Should be run from python2 and python3!
26
27 For help see :py:mod:`unittest`
28 """
29
30 import unittest
31
32 from src.type_helpers import is_unicode, isstr
33 from sys import version_info
34
35
36 is_py2 = version_info.major == 2
37
38 class TypeHelperTester(unittest.TestCase):
39
40     def test_is_py2_or_py3(self):
41         """ test that python version is 2 or 3
42
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
45         """
46         self.assertIn(version_info.major, (2, 3))
47
48     def test_is_unicode(self):
49         """ tests function is_unicode """
50
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)))
55
56         if is_py2:
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'))
61         else:
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'))
66
67     def test_isstr(self):
68         """ test function isstr """
69
70         tests = [
71             ('abc', True), (u'abc', True), (r'abc', True),
72             (1, False), (['a', 'b', 'c'], False), (('a', 'b', 'c'), False)]
73         if not is_py2:
74             tests.append((b'abc', False))  # b'' does not exist in py2
75
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),
81                                      expected_result))
82
83
84 if __name__ == '__main__':
85     unittest.main()