@@ -1194,6 +1194,42 @@ fn join_token_streams(streams: &[TokenStream]) -> String {
11941194 . join ( ", " )
11951195}
11961196
1197+ #[ derive( Clone ) ]
1198+ struct CustomAssertionMessage {
1199+ message_expr : String ,
1200+ step_display_expr : String ,
1201+ }
1202+
1203+ fn custom_assertion_message ( parts : & [ TokenStream ] , start : usize ) -> Option < CustomAssertionMessage > {
1204+ if parts. len ( ) <= start {
1205+ return None ;
1206+ }
1207+
1208+ let custom = join_token_streams ( & parts[ start..] ) ;
1209+ if custom. trim ( ) . is_empty ( ) {
1210+ return None ;
1211+ }
1212+
1213+ Some ( CustomAssertionMessage {
1214+ message_expr : format ! ( "format!(\" {{}}\" , format_args!({custom}))" ) ,
1215+ step_display_expr : format ! ( "format_args!({custom})" ) ,
1216+ } )
1217+ }
1218+
1219+ fn assertion_step_name ( base_name : & str , custom_message : Option < & CustomAssertionMessage > ) -> String {
1220+ match custom_message {
1221+ Some ( custom_message) => format ! (
1222+ "format!(\" {{}}: {{}}\" , {base_name}, {})" ,
1223+ custom_message. step_display_expr
1224+ ) ,
1225+ None => base_name. to_string ( ) ,
1226+ }
1227+ }
1228+
1229+ fn assertion_step_name_from_message_var ( base_name : & str ) -> String {
1230+ format ! ( "format!(\" {{}}: {{}}\" , {base_name}, __allure_assert_message)" )
1231+ }
1232+
11971233fn generate_assert_code ( kind : AssertMacroKind , args : TokenStream ) -> Option < String > {
11981234 let parts = split_macro_args ( args. clone ( ) ) ;
11991235 let condition = parts. first ( ) ?. clone ( ) ;
@@ -1202,32 +1238,30 @@ fn generate_assert_code(kind: AssertMacroKind, args: TokenStream) -> Option<Stri
12021238 }
12031239
12041240 let condition = condition. to_string ( ) ;
1205- let custom_message = if parts. len ( ) > 1 {
1206- let custom = join_token_streams ( & parts[ 1 ..] ) ;
1207- if custom. trim ( ) . is_empty ( ) {
1208- None
1209- } else {
1210- Some ( format ! ( "format!(\" {{}}\" , format_args!({custom}))" ) )
1211- }
1212- } else {
1213- None
1214- } ;
1241+ let custom_message = custom_assertion_message ( & parts, 1 ) ;
12151242 let message = custom_message
1243+ . as_ref ( )
1244+ . map ( |custom_message| custom_message. message_expr . clone ( ) )
12161245 . unwrap_or_else ( || format ! ( "format!(\" assertion failed: {{}}\" , stringify!({condition}))" ) ) ;
12171246 let name = format ! (
12181247 "concat!(\" {}!(\" , stringify!({condition}), \" )\" )" ,
12191248 kind. macro_name( )
12201249 ) ;
1250+ let pass_name = assertion_step_name ( & name, custom_message. as_ref ( ) ) ;
1251+ let fail_name = custom_message
1252+ . as_ref ( )
1253+ . map ( |_| assertion_step_name_from_message_var ( & name) )
1254+ . unwrap_or_else ( || name. clone ( ) ) ;
12211255
12221256 let mut instrumented = String :: new ( ) ;
12231257 instrumented. push_str ( "if " ) ;
12241258 instrumented. push_str ( & condition) ;
12251259 instrumented. push_str ( " { ::allure_cargotest::__private::record_assertion_pass(" ) ;
1226- instrumented. push_str ( & name ) ;
1260+ instrumented. push_str ( & pass_name ) ;
12271261 instrumented. push_str ( "); } else { let __allure_assert_message = " ) ;
12281262 instrumented. push_str ( & message) ;
12291263 instrumented. push_str ( "; ::allure_cargotest::__private::fail_assertion(" ) ;
1230- instrumented. push_str ( & name ) ;
1264+ instrumented. push_str ( & fail_name ) ;
12311265 instrumented. push_str (
12321266 ", __allure_assert_message, Some(\" false\" .to_string()), Some(\" true\" .to_string())); }" ,
12331267 ) ;
@@ -1257,20 +1291,20 @@ fn generate_assert_cmp_code(kind: AssertMacroKind, args: TokenStream) -> Option<
12571291 } else {
12581292 "format!(\" assertion `left == right` failed\\ n left: `{:?}`,\\ n right: `{:?}`\" , __allure_assert_left, __allure_assert_right)"
12591293 } ;
1260- let message = if parts. len ( ) > 2 {
1261- let custom = join_token_streams ( & parts[ 2 ..] ) ;
1262- if custom. trim ( ) . is_empty ( ) {
1263- default_message. to_string ( )
1264- } else {
1265- format ! ( "format!(\" {{}}\" , format_args!({custom}))" )
1266- }
1267- } else {
1268- default_message. to_string ( )
1269- } ;
1294+ let custom_message = custom_assertion_message ( & parts, 2 ) ;
1295+ let message = custom_message
1296+ . as_ref ( )
1297+ . map ( |custom_message| custom_message. message_expr . clone ( ) )
1298+ . unwrap_or_else ( || default_message. to_string ( ) ) ;
12701299 let name = format ! (
12711300 "concat!(\" {}!(\" , stringify!({left}), \" , \" , stringify!({right}), \" )\" )" ,
12721301 kind. macro_name( )
12731302 ) ;
1303+ let pass_name = assertion_step_name ( & name, custom_message. as_ref ( ) ) ;
1304+ let fail_name = custom_message
1305+ . as_ref ( )
1306+ . map ( |_| assertion_step_name_from_message_var ( & name) )
1307+ . unwrap_or_else ( || name. clone ( ) ) ;
12741308
12751309 let mut instrumented = String :: new ( ) ;
12761310 instrumented. push_str ( "match (&(" ) ;
@@ -1284,11 +1318,11 @@ fn generate_assert_cmp_code(kind: AssertMacroKind, args: TokenStream) -> Option<
12841318 instrumented. push_str (
12851319 " *__allure_assert_right { ::allure_cargotest::__private::record_assertion_pass(" ,
12861320 ) ;
1287- instrumented. push_str ( & name ) ;
1321+ instrumented. push_str ( & pass_name ) ;
12881322 instrumented. push_str ( "); } else { let __allure_assert_message = " ) ;
12891323 instrumented. push_str ( & message) ;
12901324 instrumented. push_str ( "; ::allure_cargotest::__private::fail_assertion(" ) ;
1291- instrumented. push_str ( & name ) ;
1325+ instrumented. push_str ( & fail_name ) ;
12921326 instrumented. push_str ( ", __allure_assert_message, Some(format!(\" {:?}\" , __allure_assert_left)), Some(format!(\" {:?}\" , __allure_assert_right))); } } }" ) ;
12931327
12941328 Some ( runtime_assert_code (
0 commit comments