Commits

Marc Abramowitz committed 6ad961c

Support testing in both Python 2 (one of the tests fails) and Python 3
(all tests pass).

  • Participants
  • Parent commits 77164fe
  • Branches issue-9

Comments (0)

Files changed (1)

File tests/test_string.py

+# -*- coding: utf-8 -*-
+
 import sys
 
 from scripttest.backwardscompat import string
     'funny "Unicode" characters.\n\nJust good ol\' ASCII text.',
 ])
 
-ascii_unicode = u''.join([
-    u'This is a "degenerate" unicode string with just ASCII characters',
-])
-
-utf8_str = ''.join([
-    'Bj\xc3\xb6rk Gu\xc3\xb0mundsd\xc3\xb3ttir ',
-    '[\xcb\x88pj\xc5\x93r\xcc\xa5k ', 
-    '\xcb\x88kv\xca\x8f\xc3\xb0m\xca\x8fnts\xcb\x8cto\xca\x8aht\xc9\xaar]',
-])
+if sys.version_info.major <= 2:
+    utf8_str = ''.join([
+        'Bj\xc3\xb6rk Gu\xc3\xb0mundsd\xc3\xb3ttir ',
+        '[\xcb\x88pj\xc5\x93r\xcc\xa5k ', 
+        '\xcb\x88kv\xca\x8f\xc3\xb0m\xca\x8fnts\xcb\x8cto\xca\x8aht\xc9\xaar]',
+    ])
+else:
+    utf8_str = 'Björk Guðmundsdóttir [ˈpjœr̥k ˈkvʏðmʏntsˌtoʊhtɪr]'
 
 
 def skip_test_if_not_python_2():
         raise SkipTest
 
 
+#-----------------------------------------
+# Python 2 tests
+#-----------------------------------------
+
+
 def test_python_2_string_with_ascii_str():
     skip_test_if_not_python_2()
 
-    assert string(ascii_str) == ascii_str
+    assert isinstance(ascii_str, str)
+
+    result = string(ascii_str)
+
+    assert isinstance(result, unicode)
+    assert result == ascii_str
 
 
 def test_python_2_string_with_utf8_str():
     skip_test_if_not_python_2()
 
-    assert string(utf8_str) == utf8_str.decode('utf-8')
+    assert isinstance(utf8_str, str)
+
+    result = string(utf8_str)
+
+    assert isinstance(result, unicode)
+    assert result == utf8_str.decode('utf-8')
 
 
 def test_python_2_string_with_ascii_unicode():
     skip_test_if_not_python_2()
 
-    assert string(ascii_unicode) == ascii_unicode
+    ascii_unicode = ascii_str.decode('utf-8')
+    assert isinstance(ascii_unicode, unicode)
+
+    result = string(ascii_unicode)
+
+    assert isinstance(result, unicode)
+    assert result == ascii_unicode
 
 
 def test_python_2_string_with_utf8_unicode():
     skip_test_if_not_python_2()
 
     utf8_unicode = utf8_str.decode('utf-8')
+    assert isinstance(utf8_unicode, unicode)
 
-    assert string(utf8_unicode) == utf8_unicode
+    result = string(utf8_unicode)
 
+    assert isinstance(result, unicode)
+    assert result == utf8_unicode
+
+
+#-----------------------------------------
+# Python 3 tests
+#-----------------------------------------
+
+
+def test_python_3_string_with_ascii_bytes():
+    skip_test_if_not_python_3()
+
+    ascii_bytes = ascii_str.encode('utf-8')
+    assert isinstance(ascii_bytes, bytes)
+
+    result = string(ascii_bytes)
+
+    assert isinstance(result, str)
+    assert result == ascii_bytes.decode('utf-8')
+
+
+def test_python_3_string_with_utf8_bytes():
+    skip_test_if_not_python_3()
+
+    utf8_bytes = utf8_str.encode('utf-8')
+
+    assert isinstance(utf8_bytes, bytes)
+
+    result = string(utf8_bytes)
+
+    assert isinstance(result, str)
+    assert result == utf8_bytes.decode('utf-8')
+
+
+def test_python_3_string_with_ascii_str():
+    skip_test_if_not_python_3()
+
+    assert isinstance(ascii_str, str)
+
+    result = string(ascii_str)
+
+    assert isinstance(result, str)
+    assert result == ascii_str
+
+
+def test_python_3_string_with_utf8_str():
+    skip_test_if_not_python_3()
+
+    assert isinstance(utf8_str, str)
+
+    result = string(utf8_str)
+
+    assert isinstance(result, str)
+    assert result == utf8_str
+