diff --git a/src/macros.rs b/src/macros.rs index 0c2024b3..8d32303b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -222,30 +222,42 @@ macro_rules! assert_compact_json_snapshot { #[doc(hidden)] #[macro_export] macro_rules! _assert_serialized_snapshot { - // If there are redaction expressions and an inline snapshot, capture - // the redactions expressions and pass to `_assert_snapshot_base` + // If there are redaction expressions and an inline snapshot, capture the + // redactions expressions and pass to `_assert_snapshot_base` // // Note that if we could unify the Inline & File represenations of snapshots // redactions we could unify some of these branches. (format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),*$(,)?}, @$snapshot:literal) => {{ let transform = |value| { - let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, Inline); - value + $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, Inline) }; $crate::_assert_snapshot_base!(transform=transform, $value, @$snapshot); }}; - // If there are redaction expressions and no name, add a auto-generated name, call self + // If there are redaction expressions and no name, add a auto-generated + // name, call self + // + // Note that we can't just pass all exprs before the `match` and let + // `_assert_snapshot_base` handle them, we need the below few branches + // copying the logic; since there can be ambiguity with a `match` + // expression. (format=$format:ident, $value:expr, $(match ..)? {$($k:expr => $v:expr),*$(,)?}) => {{ $crate::_assert_serialized_snapshot!(format=$format, $crate::_macro_support::AutoName, $value, {$($k => $v),*}); }}; - // If there are redaction expressions, capture and pass to `_assert_snapshot_base` + // If there are redaction expressions and no debug_expr, capture and pass to `_assert_snapshot_base` (format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),*$(,)?}) => {{ let transform = |value| { - let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, File); - value + $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, File) }; $crate::_assert_snapshot_base!(transform=transform, $name, $value); }}; + // If there are redaction expressions, capture and pass to + // `_assert_snapshot_base`. + (format=$format:ident, $name:expr, $value:expr, $debug_expr:expr, $(match ..)? {$($k:expr => $v:expr),*$(,)?}) => {{ + let transform = |value| { + $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format, File) + }; + $crate::_assert_snapshot_base!(transform=transform, $name, $value, $debug_expr); + }}; // If there's an inline snapshot, capture serialization function and pass to // `_assert_snapshot_base`, specifying `Inline` (format=$format:ident, $($arg:expr),*, @$snapshot:literal) => {{ @@ -264,7 +276,7 @@ macro_rules! _assert_serialized_snapshot { $crate::_macro_support::SerializationFormat::$format, $crate::_macro_support::SnapshotLocation::File )}; - $crate::_assert_snapshot_base!(transform = transform, $($arg),*); + $crate::_assert_snapshot_base!(transform=transform, $($arg),*); }}; } @@ -280,13 +292,12 @@ macro_rules! _prepare_snapshot_for_redaction { $crate::_macro_support::Redaction::from($v) ),)* ]; - let value = $crate::_macro_support::serialize_value_redacted( + $crate::_macro_support::serialize_value_redacted( &$value, &vec, $crate::_macro_support::SerializationFormat::$format, $crate::_macro_support::SnapshotLocation::$location - ); - (vec, value) + ) } } } diff --git a/src/snapshots/insta__test__name.snap b/src/snapshots/insta__test__name.snap new file mode 100644 index 00000000..edf809e1 --- /dev/null +++ b/src/snapshots/insta__test__name.snap @@ -0,0 +1,8 @@ +--- +source: src/test.rs +expression: "&User {\n id: 42,\n username: \"john_doe\".to_string(),\n email: Email(\"john@example.com\".to_string()),\n extra: \"\".to_string(),\n }" +--- +id: "[id]" +username: john_doe +email: john@example.com +extra: "" diff --git a/src/snapshots/insta__test__name_expr.snap b/src/snapshots/insta__test__name_expr.snap new file mode 100644 index 00000000..edf809e1 --- /dev/null +++ b/src/snapshots/insta__test__name_expr.snap @@ -0,0 +1,8 @@ +--- +source: src/test.rs +expression: "&User {\n id: 42,\n username: \"john_doe\".to_string(),\n email: Email(\"john@example.com\".to_string()),\n extra: \"\".to_string(),\n }" +--- +id: "[id]" +username: john_doe +email: john@example.com +extra: "" diff --git a/tests/snapshots/test_redaction__name.snap.new b/tests/snapshots/test_redaction__name.snap.new new file mode 100644 index 00000000..9e319114 --- /dev/null +++ b/tests/snapshots/test_redaction__name.snap.new @@ -0,0 +1,9 @@ +--- +source: tests/test_redaction.rs +assertion_line: 78 +expression: "&user" +--- +id: "[id]" +username: john_doe +email: john@example.com +extra: "" diff --git a/tests/snapshots/test_redaction__name_expr.snap b/tests/snapshots/test_redaction__name_expr.snap new file mode 100644 index 00000000..f839a48f --- /dev/null +++ b/tests/snapshots/test_redaction__name_expr.snap @@ -0,0 +1,8 @@ +--- +source: tests/test_redaction.rs +expression: "&User {\n id: 42,\n username: \"john_doe\".to_string(),\n email: Email(\"john@example.com\".to_string()),\n extra: \"\".to_string(),\n }" +--- +id: "[id]" +username: john_doe +email: john@example.com +extra: "" diff --git a/tests/test_redaction.rs b/tests/test_redaction.rs index 912452e4..04688786 100644 --- a/tests/test_redaction.rs +++ b/tests/test_redaction.rs @@ -56,6 +56,44 @@ fn test_with_random_value() { }); } +#[cfg(feature = "yaml")] +#[test] +fn test_name_expr() { + let user = User { + id: 42, + username: "john_doe".to_string(), + email: Email("john@example.com".to_string()), + extra: "".to_string(), + }; + + // unnamed + assert_yaml_snapshot!( + &user, + match .. { + ".id" => "[id]", + } + ); + + // named, no debug expr + assert_yaml_snapshot!( + "name", + &user, + match .. { + ".id" => "[id]", + } + ); + + // named, debug expr + assert_yaml_snapshot!( + "name", + &user, + "debug expr", + match .. { + ".id" => "[id]", + } + ); +} + #[cfg(feature = "yaml")] #[test] fn test_with_random_value_inline_callback() {