summaryrefslogtreecommitdiff
path: root/Examples/test-suite/python/li_std_wstring_runme.py
blob: c6210e2de9dbba28988373582b68f1e5d382c81f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import li_std_wstring
import sys

def check_equal(a, b):
    if a != b:
        raise RuntimeError("failed {} {}".format(a, b))

h = u"h"
check_equal(li_std_wstring.test_wcvalue(h), h)

x = u"abc"
check_equal(li_std_wstring.test_ccvalue(x), x)
check_equal(li_std_wstring.test_cvalue(x), x)

check_equal(li_std_wstring.test_wchar_overload(x), x)
check_equal(li_std_wstring.test_wchar_overload(), None)

li_std_wstring.test_pointer(None)
li_std_wstring.test_const_pointer(None)

try:
    li_std_wstring.test_value(None)
    raise RuntimeError("NULL check failed")
except TypeError as e:
    pass

try:
      li_std_wstring.test_reference(None)
      raise RuntimeError("NULL check failed")
except ValueError as e:
    if "invalid null reference" not in str(e):
        raise RuntimeError("Missing text {}".format(e))
try:
    li_std_wstring.test_const_reference(None)
    raise RuntimeError("NULL check failed")
except ValueError as e:
    if "invalid null reference" not in str(e):
        raise RuntimeError("Missing text {}".format(e))

x = "hello"
check_equal(li_std_wstring.test_const_reference(x), x)

s = "abc"
if not li_std_wstring.test_equal_abc(s):
    raise RuntimeError("Not equal {}".format(s))

try:
    li_std_wstring.test_throw
except RuntimeError as e:
    check_equal(e.message, "throwing test_throw")

x = "abc\0def"
check_equal(li_std_wstring.test_value(x), x)
check_equal(li_std_wstring.test_ccvalue(x), "abc")
check_equal(li_std_wstring.test_wchar_overload(x), "abc")

################### Python specific

# Byte strings only converted in Python 2
if sys.version_info[0:2] < (3, 0):
    x = b"hello there"
    if li_std_wstring.test_value(x) != x:
        raise RuntimeError("bad string mapping")

# Invalid utf-8 in a byte string fails in all versions
x = b"h\xe9llo"
try:
    li_std_wstring.test_value(x)
    raise RuntimeError("TypeError not thrown")
except TypeError:
    pass

# Check surrogateescape
if sys.version_info[0:2] > (3, 1):
    x = u"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:
        raise RuntimeError("Unexpected length")