@@ -27,14 +27,20 @@ macro_rules! generate_string_bytes_and_cstr_tests {
2727 assert_eq!( ascii. len( ) , 5 ) ;
2828
2929 let ascii_nul = <$T>:: try_from_bytes( b"Hello\0 " , Encoding :: Ascii ) ;
30- assert_eq!( ascii_nul, None , "intermediate NUL byte is not valid ASCII" ) ; // at end, but still not NUL terminator.
30+ let ascii_nul = ascii_nul. expect_err( "intermediate NUL byte is not valid ASCII" ) ; // at end, but still not NUL terminator.
31+ assert_eq!(
32+ ascii_nul. to_string( ) ,
33+ "intermediate NUL byte in ASCII string"
34+ ) ;
3135
3236 let latin1 = <$T>:: try_from_bytes( b"/\xF0 \xF5 \xBE " , Encoding :: Ascii ) ;
33- assert_eq!( latin1, None , "Latin-1 is *not* valid ASCII" ) ;
37+ let latin1 = latin1. expect_err( "Latin-1 is *not* valid ASCII" ) ;
38+ assert_eq!( latin1. to_string( ) , "invalid ASCII" ) ;
3439
3540 let utf8 =
3641 <$T>:: try_from_bytes( b"\xF6 \xF0 \x9F \x8D \x8E \xF0 \x9F \x92 \xA1 " , Encoding :: Ascii ) ;
37- assert_eq!( utf8, None , "UTF-8 is *not* valid ASCII" ) ;
42+ let utf8 = utf8. expect_err( "UTF-8 is *not* valid ASCII" ) ;
43+ assert_eq!( utf8. to_string( ) , "invalid ASCII" ) ;
3844 }
3945
4046 #[ itest]
@@ -45,10 +51,12 @@ macro_rules! generate_string_bytes_and_cstr_tests {
4551 assert_eq!( ascii. len( ) , 5 ) ;
4652
4753 let latin1 = <$T>:: try_from_cstr( c"/ðõ¾" , Encoding :: Ascii ) ;
48- assert_eq!( latin1, None , "Latin-1 is *not* valid ASCII" ) ;
54+ let latin1 = latin1. expect_err( "Latin-1 is *not* valid ASCII" ) ;
55+ assert_eq!( latin1. to_string( ) , "invalid ASCII" ) ;
4956
5057 let utf8 = <$T>:: try_from_cstr( c"ö🍎A💡" , Encoding :: Ascii ) ;
51- assert_eq!( utf8, None , "UTF-8 is *not* valid ASCII" ) ;
58+ let utf8 = utf8. expect_err( "UTF-8 is *not* valid ASCII" ) ;
59+ assert_eq!( utf8. to_string( ) , "invalid ASCII" ) ;
5260 }
5361
5462 #[ itest]
@@ -64,9 +72,10 @@ macro_rules! generate_string_bytes_and_cstr_tests {
6472 assert_eq!( latin1. len( ) , 4 ) ;
6573
6674 let latin1_nul = <$T>:: try_from_bytes( b"/\0 \xF0 \xF5 \xBE " , Encoding :: Latin1 ) ;
75+ let latin1_nul = latin1_nul. expect_err( "intermediate NUL byte is not valid Latin-1" ) ;
6776 assert_eq!(
68- latin1_nul, None ,
69- "intermediate NUL byte is not valid Latin-1"
77+ latin1_nul. to_string ( ) ,
78+ "intermediate NUL byte in Latin-1 string "
7079 ) ;
7180
7281 // UTF-8 -> Latin-1: always succeeds, even if result is garbage, since every byte is a valid Latin-1 character.
@@ -107,7 +116,12 @@ macro_rules! generate_string_bytes_and_cstr_tests {
107116 assert_eq!( ascii. len( ) , 5 ) ;
108117
109118 let latin1 = <$T>:: try_from_bytes( b"/\xF0 \xF5 \xBE " , Encoding :: Utf8 ) ;
110- assert_eq!( latin1, None , "Latin-1 is *not* valid UTF-8" ) ;
119+ let latin1 = latin1. expect_err( "Latin-1 is *not* valid UTF-8" ) ;
120+ // Note: depends on exact output of std's Utf8Error; might need format!() if that changes.
121+ assert_eq!(
122+ latin1. to_string( ) ,
123+ "invalid UTF-8: invalid utf-8 sequence of 1 bytes from index 1"
124+ ) ;
111125
112126 let utf8 = <$T>:: try_from_bytes(
113127 b"\xC3 \xB6 \xF0 \x9F \x8D \x8E \x41 \xF0 \x9F \x92 \xA1 " ,
@@ -118,7 +132,11 @@ macro_rules! generate_string_bytes_and_cstr_tests {
118132 assert_eq!( utf8. len( ) , 4 ) ;
119133
120134 let utf8_nul = <$T>:: try_from_bytes( b"\xC3 \0 A" , Encoding :: Utf8 ) ;
121- assert_eq!( utf8_nul, None , "intermediate NUL byte is not valid UTF-8" ) ;
135+ let utf8_nul = utf8_nul. expect_err( "intermediate NUL byte is not valid UTF-8" ) ;
136+ assert_eq!(
137+ utf8_nul. to_string( ) ,
138+ "invalid UTF-8: invalid utf-8 sequence of 1 bytes from index 0"
139+ ) ;
122140 }
123141
124142 #[ itest]
0 commit comments