--- /dev/null
+""" type_helper_unittest.py: unit tests for type_helpers
+
+Tests classes and functions in type_helpers
+
+Should be run from python2 and python3!
+
+For help see :py:mod:`unittest`
+
+.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com
+"""
+
+import unittest
+
+from type_helpers import is_unicode, isstr
+from sys import version_info
+
+
+is_py2 = version_info.major == 2
+
+class TypeHelperTester(unittest.TestCase):
+
+ def test_is_py2_or_py3(self):
+ """ test that python version is 2 or 3
+
+ when implementing type_helpers, there was no py4 and no idea what it
+ might be like. Also will probably fail for python v1
+ """
+ self.assertIn(version_info.major, (2, 3))
+
+ def test_is_unicode(self):
+ """ tests function is_unicode """
+
+ self.assertFalse(is_unicode(1))
+ self.assertFalse(is_unicode([]))
+ self.assertFalse(is_unicode(unittest.TestCase))
+ self.assertFalse(is_unicode(type(unittest.TestCase)))
+
+ if is_py2:
+ self.assertTrue(is_unicode(u'bla'))
+ self.assertTrue(eval("is_unicode(ur'bla')")) # not allowed in py3!
+ self.assertFalse(is_unicode('bla'))
+ self.assertFalse(is_unicode(r'bla'))
+ else:
+ self.assertTrue(is_unicode('bla'))
+ self.assertTrue(is_unicode(r'bla'))
+ self.assertFalse(is_unicode(b'bla'))
+ self.assertFalse(is_unicode(br'bla'))
+
+ def test_isstr(self):
+ """ test function isstr """
+
+ tests = [
+ ('abc', True), (u'abc', True), (r'abc', True),
+ (1, False), (['a', 'b', 'c'], False), (('a', 'b', 'c'), False)]
+ if not is_py2:
+ tests.append((b'abc', False)) # b'' does not exist in py2
+
+ for test_var, expected_result in tests:
+ self.assertEqual(isstr(test_var), expected_result,
+ 'isstr of var {0} (which is of type {1}) returned'
+ '{2} but expected {3}!'
+ .format(test_var, type(test_var), isstr(test_var),
+ expected_result))
+
+
+if __name__ == '__main__':
+ unittest.main()
"""
Helpers for type checking and conversion, like isstr(x), is_file_obj(x)
+Provides abstraction from difference between PY2 and PY3
+
.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com
"""
return isinstance(var, basestring)
+def is_unicode(var):
+ """ returns true for unicode strings
+
+ py2: return True for type unicode but not type str
+ py3: return True for type str but not type bytes
+ """
+
+ if PY2:
+ return isinstance(var, unicode)
+ else:
+ return isinstance(var, str)
+
+
def is_file_obj(var):
""" determines whether given input is the result of 'open(file_name)'
return isinstance(var, IOBase)
-def test_isstr():
- """ test function isstr
-
- TODO: should probably be moved to some unittest or so ...
- """
-
- tests = [
- ('abc', True), (u'abc', True), (r'abc', True),
- (1, False), (['a', 'b', 'c'], False), (('a', 'b', 'c'), False)]
- if PY3:
- tests.append((b'abc', False)) # b'' does not exist in py2
-
- for test_var, expected_result in tests:
- assert isstr(test_var) == expected_result, 'isstr of var {0} ' \
- '(which is of type {1}) returned {2} but expected {3}!'.format(
- test_var, type(test_var), isstr(test_var), expected_result)
- print('all string tests finished successfully')
-# end: test_isstr
-
-
def main():
""" Main function, called when running file as script
print('is python3')
else:
print('is python2')
-
- # test_isstr()
# end: function main