diff --git a/tests/test_convert_anything_to_str.py b/tests/test_convert_anything_to_str.py new file mode 100755 index 0000000..b59e273 --- /dev/null +++ b/tests/test_convert_anything_to_str.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 +# Possible test commands: +# PYTHONPATH=. python2 tests/test_convert_anything_to_str.py +# PYTHONPATH=. python3 tests/test_convert_anything_to_str.py +# python2 -m unittest discover +# python3 -m unittest discover +"""Test case for XSConsoleLang.convert_anything_to_str()""" + +import sys +import unittest + +from XSConsoleLang import convert_anything_to_str + + +class TestIPAddress(unittest.TestCase): + def test_convert_anything_to_str(self): + expected_string = "Török" + encoded_iso8859 = b'T\xf6r\xf6k' + encoded_as_utf8 = b"T\xc3\xb6r\xc3\xb6k" + + a = convert_anything_to_str(encoded_iso8859, "iso-8859-1") + print(a) + assert expected_string == convert_anything_to_str(encoded_as_utf8) + assert expected_string == convert_anything_to_str(encoded_iso8859, "iso-8859-1") + + print(expected_string.encode("iso-8859-1")) + + if sys.version_info.major < 3: # For Py2, on Py3, str == unicode + unicode_str = encoded_as_utf8.decode("utf-8") + assert str(type(unicode_str)) == "" + assert expected_string == convert_anything_to_str(unicode_str) + + assert expected_string == convert_anything_to_str(expected_string) + + # Special case for then call location check the result for None: + assert convert_anything_to_str(None) == None + + # Test cases for str(arg) + assert convert_anything_to_str(42) == "42" + assert convert_anything_to_str(42.2) == "42.2" + assert convert_anything_to_str(Exception("True")) == "True" + assert convert_anything_to_str(True) == "True" + assert convert_anything_to_str(False) == "False" + + +if __name__ == "__main__": + unittest.main()