@@ -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,13 +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'β₯'
38
-
43
+ if hasattr (m , "has_u8string" ) :
44
+ assert m .u8_char8_Z () == 'Z'
39
45
40
46
def test_single_char_arguments ():
41
47
"""Tests failures for passing invalid inputs to char-accepting functions"""
@@ -92,6 +98,16 @@ def toobig_message(r):
92
98
assert m .ord_wchar (u'aa' )
93
99
assert str (excinfo .value ) == toolong_message
94
100
101
+ if hasattr (m , "has_u8string" ) :
102
+ assert m .ord_char8 (u'a' ) == 0x61 # simple ASCII
103
+ assert m .ord_char8_lv (u'b' ) == 0x62
104
+ assert m .ord_char8 (u'Γ©' ) == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
105
+ with pytest .raises (ValueError ) as excinfo :
106
+ assert m .ord_char8 (u'Δ' ) == 0x100 # requires 2 bytes, doesn't fit in a char
107
+ assert str (excinfo .value ) == toobig_message (0x100 )
108
+ with pytest .raises (ValueError ) as excinfo :
109
+ assert m .ord_char8 (u'ab' )
110
+ assert str (excinfo .value ) == toolong_message
95
111
96
112
def test_bytes_to_string ():
97
113
"""Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
@@ -116,10 +132,15 @@ def test_string_view(capture):
116
132
assert m .string_view_chars ("Hi π" ) == [72 , 105 , 32 , 0xf0 , 0x9f , 0x8e , 0x82 ]
117
133
assert m .string_view16_chars ("Hi π" ) == [72 , 105 , 32 , 0xd83c , 0xdf82 ]
118
134
assert m .string_view32_chars ("Hi π" ) == [72 , 105 , 32 , 127874 ]
135
+ if hasattr (m , "has_u8string" ) :
136
+ assert m .string_view8_chars ("Hi" ) == [72 , 105 ]
137
+ assert m .string_view8_chars ("Hi π" ) == [72 , 105 , 32 , 0xf0 , 0x9f , 0x8e , 0x82 ]
119
138
120
139
assert m .string_view_return () == "utf8 secret π"
121
140
assert m .string_view16_return () == "utf16 secret π"
122
141
assert m .string_view32_return () == "utf32 secret π"
142
+ if hasattr (m , "has_u8string" ) :
143
+ assert m .string_view8_return () == "utf8 secret π"
123
144
124
145
with capture :
125
146
m .string_view_print ("Hi" )
@@ -132,6 +153,14 @@ def test_string_view(capture):
132
153
utf16 π 8
133
154
utf32 π 7
134
155
"""
156
+ if hasattr (m , "has_u8string" ) :
157
+ with capture :
158
+ m .string_view8_print ("Hi" )
159
+ m .string_view8_print ("utf8 π" )
160
+ assert capture == """
161
+ Hi 2
162
+ utf8 π 9
163
+ """
135
164
136
165
with capture :
137
166
m .string_view_print ("Hi, ascii" )
@@ -144,6 +173,14 @@ def test_string_view(capture):
144
173
Hi, utf16 π 12
145
174
Hi, utf32 π 11
146
175
"""
176
+ if hasattr (m , "has_u8string" ) :
177
+ with capture :
178
+ m .string_view8_print ("Hi, ascii" )
179
+ m .string_view8_print ("Hi, utf8 π" )
180
+ assert capture == """
181
+ Hi, ascii 9
182
+ Hi, utf8 π 13
183
+ """
147
184
148
185
149
186
def test_integer_casting ():
0 commit comments