@@ -15,6 +15,8 @@ def test_unicode_conversion():
1515 assert m .good_utf16_string () == u"bβ½ππz"
1616 assert m .good_utf32_string () == u"aππβ½z"
1717 assert m .good_wchar_string () == u"aβΈπz"
18+ if hasattr (m , "has_u8string" ):
19+ assert m .good_utf8_u8string () == u"Say utf8β½ π π"
1820
1921 with pytest .raises (UnicodeDecodeError ):
2022 m .bad_utf8_string ()
@@ -29,12 +31,17 @@ def test_unicode_conversion():
2931 if hasattr (m , "bad_wchar_string" ):
3032 with pytest .raises (UnicodeDecodeError ):
3133 m .bad_wchar_string ()
34+ if hasattr (m , "has_u8string" ):
35+ with pytest .raises (UnicodeDecodeError ):
36+ m .bad_utf8_u8string ()
3237
3338 assert m .u8_Z () == 'Z'
3439 assert m .u8_eacute () == u'Γ©'
3540 assert m .u16_ibang () == u'β½'
3641 assert m .u32_mathbfA () == u'π'
3742 assert m .wchar_heart () == u'β₯'
43+ if hasattr (m , "has_u8string" ):
44+ assert m .u8_char8_Z () == 'Z'
3845
3946
4047def test_single_char_arguments ():
@@ -92,6 +99,17 @@ def toobig_message(r):
9299 assert m .ord_wchar (u'aa' )
93100 assert str (excinfo .value ) == toolong_message
94101
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+
95113
96114def test_bytes_to_string ():
97115 """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
@@ -116,10 +134,15 @@ def test_string_view(capture):
116134 assert m .string_view_chars ("Hi π" ) == [72 , 105 , 32 , 0xf0 , 0x9f , 0x8e , 0x82 ]
117135 assert m .string_view16_chars ("Hi π" ) == [72 , 105 , 32 , 0xd83c , 0xdf82 ]
118136 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 ]
119140
120141 assert m .string_view_return () == "utf8 secret π"
121142 assert m .string_view16_return () == "utf16 secret π"
122143 assert m .string_view32_return () == "utf32 secret π"
144+ if hasattr (m , "has_u8string" ):
145+ assert m .string_view8_return () == "utf8 secret π"
123146
124147 with capture :
125148 m .string_view_print ("Hi" )
@@ -132,6 +155,14 @@ def test_string_view(capture):
132155 utf16 π 8
133156 utf32 π 7
134157 """
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+ """
135166
136167 with capture :
137168 m .string_view_print ("Hi, ascii" )
@@ -144,6 +175,14 @@ def test_string_view(capture):
144175 Hi, utf16 π 12
145176 Hi, utf32 π 11
146177 """
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+ """
147186
148187
149188def test_integer_casting ():
0 commit comments