Convert python test scripts to be Python 2 and 3 compatible

Unicode testing
This commit is contained in:
William S Fulton 2020-08-15 00:09:31 +01:00
commit 66df0bd224
6 changed files with 28 additions and 30 deletions

View file

@ -7,20 +7,20 @@ class B(A):
A.__init__(self, string) A.__init__(self, string)
def get_first(self): def get_first(self):
return A.get_first(self) + u" world!" return A.get_first(self) + " world!"
def process_text(self, s): def process_text(self, s):
self.smem = s self.smem = s
b = B(u"hello") b = B("hello")
b.get(0) b.get(0)
if b.get_first() != u"hello world!": if b.get_first() != "hello world!":
raise RuntimeError("b.get_first(): {}".format(b.get_first())) raise RuntimeError("b.get_first(): {}".format(b.get_first()))
b.call_process_func() b.call_process_func()
if b.smem != u"hello": if b.smem != "hello":
raise RuntimeError("smem: {}".format(smem)) raise RuntimeError("smem: {}".format(smem))

View file

@ -1,28 +1,28 @@
from li_cwstring import * from li_cwstring import *
if count(u"ab\0ab\0ab\0", 0) != 3: if count("ab\0ab\0ab\0", 0) != 3:
raise RuntimeError raise RuntimeError
if test1() != u"Hello World": if test1() != "Hello World":
raise RuntimeError raise RuntimeError
if test2() != u" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_": if test2() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_":
raise RuntimeError raise RuntimeError
if test3("hello") != u"hello-suffix": if test3("hello") != "hello-suffix":
raise RuntimeError raise RuntimeError
if test4("hello") != u"hello-suffix": if test4("hello") != "hello-suffix":
raise RuntimeError raise RuntimeError
if test5(4) != u"xxxx": if test5(4) != "xxxx":
raise RuntimeError raise RuntimeError
if test6(10) != u"xxxxx": if test6(10) != "xxxxx":
raise RuntimeError raise RuntimeError
if test7() != u"Hello world!": if test7() != "Hello world!":
raise RuntimeError raise RuntimeError
if test8() != u" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_": if test8() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_":
raise RuntimeError raise RuntimeError

View file

@ -5,7 +5,7 @@ a = A()
o = wostringstream() o = wostringstream()
o << a << u" " << 2345 << u" " << 1.435 << wends o << a << " " << 2345 << " " << 1.435 << wends
if o.str() != "A class 2345 1.435\0": if o.str() != "A class 2345 1.435\0":
raise RuntimeError("str failed: \"%s\"".format(o.str())) raise RuntimeError("str failed: \"%s\"".format(o.str()))

View file

@ -1,10 +1,10 @@
import li_std_wstring_inherit import li_std_wstring_inherit
import sys import sys
x = u"hello" x = "hello"
s = li_std_wstring_inherit.wstring(u"he") s = li_std_wstring_inherit.wstring("he")
s = s + u"llo" s = s + "llo"
if s != x: if s != x:
raise RuntimeError("bad string mapping {} {}".format(s, x)) raise RuntimeError("bad string mapping {} {}".format(s, x))
@ -33,12 +33,12 @@ if not li_std_wstring_inherit.is_python_builtin():
b = li_std_wstring_inherit.B("hi") b = li_std_wstring_inherit.B("hi")
b.name = li_std_wstring_inherit.wstring(u"hello") b.name = li_std_wstring_inherit.wstring("hello")
if b.name != "hello": if b.name != "hello":
raise RuntimeError("bad string mapping") raise RuntimeError("bad string mapping")
b.a = li_std_wstring_inherit.A("hello") b.a = li_std_wstring_inherit.A("hello")
if b.a != u"hello": if b.a != "hello":
raise RuntimeError("bad string mapping") raise RuntimeError("bad string mapping")

View file

@ -5,10 +5,10 @@ def check_equal(a, b):
if a != b: if a != b:
raise RuntimeError("failed {} {}".format(a, b)) raise RuntimeError("failed {} {}".format(a, b))
h = u"h" h = "h"
check_equal(li_std_wstring.test_wcvalue(h), h) check_equal(li_std_wstring.test_wcvalue(h), h)
x = u"abc" x = "abc"
check_equal(li_std_wstring.test_ccvalue(x), x) check_equal(li_std_wstring.test_ccvalue(x), x)
check_equal(li_std_wstring.test_cvalue(x), x) check_equal(li_std_wstring.test_cvalue(x), x)
@ -72,7 +72,7 @@ except TypeError:
# Check surrogateescape # Check surrogateescape
if sys.version_info[0:2] > (3, 1): if sys.version_info[0:2] > (3, 1):
x = u"h\udce9llo" # surrogate escaped representation of C char*: "h\xe9llo" x = "h\udce9llo" # surrogate escaped representation of C char*: "h\xe9llo"
if li_std_wstring.non_utf8_c_str() != x: if li_std_wstring.non_utf8_c_str() != x:
raise RuntimeError("surrogateescape not working") raise RuntimeError("surrogateescape not working")
if li_std_wstring.size_wstring(x) != 5 and len(x) != 5: if li_std_wstring.size_wstring(x) != 5 and len(x) != 5:

View file

@ -2,12 +2,8 @@ import sys
import unicode_strings import unicode_strings
# The 'u' string prefix isn't valid in Python 3.0 - 3.2 and is redundant
# in 3.3+. Since this file is run through 2to3 before testing, though,
# mark this as a unicode string in 2.x so it'll become a str in 3.x.
test_string = u"h\udce9llo w\u00f6rld"
if sys.version_info[0:2] >= (3, 1): if sys.version_info[0:2] >= (3, 1):
test_string = "h\udce9llo w\u00f6rld"
if unicode_strings.non_utf8_c_str() != test_string: if unicode_strings.non_utf8_c_str() != test_string:
raise ValueError("Test comparison mismatch") raise ValueError("Test comparison mismatch")
if unicode_strings.non_utf8_std_string() != test_string: if unicode_strings.non_utf8_std_string() != test_string:
@ -22,15 +18,17 @@ if sys.version_info[0:2] < (3, 0):
check(unicode_strings.charstring("hello1"), "hello1") check(unicode_strings.charstring("hello1"), "hello1")
check(unicode_strings.charstring(str(u"hello2")), "hello2") check(unicode_strings.charstring(str(u"hello2")), "hello2")
check(unicode_strings.charstring(u"hello3"), "hello3") check(unicode_strings.charstring(u"hello3"), "hello3")
check(unicode_strings.charstring(unicode("hello4")), "hello4") check(unicode_strings.charstring(str("hello4")), "hello4")
unicode_strings.charstring(u"hell\xb05") unicode_strings.charstring(u"hell\xb05")
unicode_strings.charstring(u"hell\u00f66") unicode_strings.charstring(u"hell\u00f66")
low_surrogate_string = u"\udcff"
else:
low_surrogate_string = "\udcff"
low_surrogate_string = u"\udcff"
try: try:
unicode_strings.instring(low_surrogate_string) unicode_strings.instring(low_surrogate_string)
# Will succeed with Python 2 # Will succeed with Python 2
except TypeError, e: except TypeError as e:
# Python 3 will fail the PyUnicode_AsUTF8String conversion resulting in a TypeError. # Python 3 will fail the PyUnicode_AsUTF8String conversion resulting in a TypeError.
# The real error is actually: # The real error is actually:
# UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 0: surrogates not allowed # UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 0: surrogates not allowed