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
parent 36bb54f01d
commit 66df0bd224
6 changed files with 28 additions and 30 deletions

View File

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

View File

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

View File

@ -5,7 +5,7 @@ a = A()
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":
raise RuntimeError("str failed: \"%s\"".format(o.str()))

View File

@ -1,10 +1,10 @@
import li_std_wstring_inherit
import sys
x = u"hello"
x = "hello"
s = li_std_wstring_inherit.wstring(u"he")
s = s + u"llo"
s = li_std_wstring_inherit.wstring("he")
s = s + "llo"
if 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.name = li_std_wstring_inherit.wstring(u"hello")
b.name = li_std_wstring_inherit.wstring("hello")
if b.name != "hello":
raise RuntimeError("bad string mapping")
b.a = li_std_wstring_inherit.A("hello")
if b.a != u"hello":
if b.a != "hello":
raise RuntimeError("bad string mapping")

View File

@ -5,10 +5,10 @@ def check_equal(a, b):
if a != b:
raise RuntimeError("failed {} {}".format(a, b))
h = u"h"
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_cvalue(x), x)
@ -72,7 +72,7 @@ except TypeError:
# Check surrogateescape
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:
raise RuntimeError("surrogateescape not working")
if li_std_wstring.size_wstring(x) != 5 and len(x) != 5:

View File

@ -2,12 +2,8 @@ import sys
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):
test_string = "h\udce9llo w\u00f6rld"
if unicode_strings.non_utf8_c_str() != test_string:
raise ValueError("Test comparison mismatch")
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(str(u"hello2")), "hello2")
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\u00f66")
low_surrogate_string = u"\udcff"
else:
low_surrogate_string = "\udcff"
low_surrogate_string = u"\udcff"
try:
unicode_strings.instring(low_surrogate_string)
# Will succeed with Python 2
except TypeError, e:
except TypeError as e:
# Python 3 will fail the PyUnicode_AsUTF8String conversion resulting in a TypeError.
# The real error is actually:
# UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 0: surrogates not allowed