@@ -15,6 +15,8 @@ def test_unicode_conversion():
15
15
assert m .good_utf16_string () == u"bβ½ππz"
16
16
assert m .good_utf32_string () == u"aππβ½z"
17
17
assert m .good_wchar_string () == u"aβΈπz"
18
+ if hasattr (m , "has_u8string" ):
19
+ assert m .good_utf8_u8string () == u"Say utf8β½ π π"
18
20
19
21
with pytest .raises (UnicodeDecodeError ):
20
22
m .bad_utf8_string ()
@@ -29,12 +31,17 @@ def test_unicode_conversion():
29
31
if hasattr (m , "bad_wchar_string" ):
30
32
with pytest .raises (UnicodeDecodeError ):
31
33
m .bad_wchar_string ()
34
+ if hasattr (m , "has_u8string" ):
35
+ with pytest .raises (UnicodeDecodeError ):
36
+ m .bad_utf8_u8string ()
32
37
33
38
assert m .u8_Z () == 'Z'
34
39
assert m .u8_eacute () == u'Γ©'
35
40
assert m .u16_ibang () == u'β½'
36
41
assert m .u32_mathbfA () == u'π'
37
42
assert m .wchar_heart () == u'β₯'
43
+ if hasattr (m , "has_u8string" ):
44
+ assert m .u8_char8_Z () == 'Z'
38
45
39
46
40
47
def test_single_char_arguments ():
@@ -92,6 +99,17 @@ def toobig_message(r):
92
99
assert m .ord_wchar (u'aa' )
93
100
assert str (excinfo .value ) == toolong_message
94
101
102
+ if hasattr (m , "has_u8string" ):
103
+ assert m .ord_char8 (u'a' ) == 0x61 # simple ASCII
104
+ assert m .ord_char8_lv (u'b' ) == 0x62
105
+ assert m .ord_char8 (u'Γ©' ) == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
106
+ with pytest .raises (ValueError ) as excinfo :
107
+ assert m .ord_char8 (u'Δ' ) == 0x100 # requires 2 bytes, doesn't fit in a char
108
+ assert str (excinfo .value ) == toobig_message (0x100 )
109
+ with pytest .raises (ValueError ) as excinfo :
110
+ assert m .ord_char8 (u'ab' )
111
+ assert str (excinfo .value ) == toolong_message
112
+
95
113
96
114
def test_bytes_to_string ():
97
115
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
@@ -116,10 +134,15 @@ def test_string_view(capture):
116
134
assert m .string_view_chars ("Hi π" ) == [72 , 105 , 32 , 0xf0 , 0x9f , 0x8e , 0x82 ]
117
135
assert m .string_view16_chars ("Hi π" ) == [72 , 105 , 32 , 0xd83c , 0xdf82 ]
118
136
assert m .string_view32_chars ("Hi π" ) == [72 , 105 , 32 , 127874 ]
137
+ if hasattr (m , "has_u8string" ):
138
+ assert m .string_view8_chars ("Hi" ) == [72 , 105 ]
139
+ assert m .string_view8_chars ("Hi π" ) == [72 , 105 , 32 , 0xf0 , 0x9f , 0x8e , 0x82 ]
119
140
120
141
assert m .string_view_return () == "utf8 secret π"
121
142
assert m .string_view16_return () == "utf16 secret π"
122
143
assert m .string_view32_return () == "utf32 secret π"
144
+ if hasattr (m , "has_u8string" ):
145
+ assert m .string_view8_return () == "utf8 secret π"
123
146
124
147
with capture :
125
148
m .string_view_print ("Hi" )
@@ -132,6 +155,14 @@ def test_string_view(capture):
132
155
utf16 π 8
133
156
utf32 π 7
134
157
"""
158
+ if hasattr (m , "has_u8string" ):
159
+ with capture :
160
+ m .string_view8_print ("Hi" )
161
+ m .string_view8_print ("utf8 π" )
162
+ assert capture == """
163
+ Hi 2
164
+ utf8 π 9
165
+ """
135
166
136
167
with capture :
137
168
m .string_view_print ("Hi, ascii" )
@@ -144,6 +175,14 @@ def test_string_view(capture):
144
175
Hi, utf16 π 12
145
176
Hi, utf32 π 11
146
177
"""
178
+ if hasattr (m , "has_u8string" ):
179
+ with capture :
180
+ m .string_view8_print ("Hi, ascii" )
181
+ m .string_view8_print ("Hi, utf8 π" )
182
+ assert capture == """
183
+ Hi, ascii 9
184
+ Hi, utf8 π 13
185
+ """
147
186
148
187
149
188
def test_integer_casting ():
0 commit comments