Skip to content

Commit

Permalink
Allow debug expressions in redaction macros
Browse files Browse the repository at this point in the history
It's can be a bit confusing when some macros have features which others don't have. So this adds debug expressions to the redaction macros.

(Though they do get quite long; possibly we want to recommend something like `description` in place of `debug` throughout?)
  • Loading branch information
max-sixty committed Mar 5, 2024
1 parent 3cb9934 commit 7f160b5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {{
Expand All @@ -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),*);
}};
}

Expand All @@ -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)
)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/snapshots/insta__test__name.snap
Original file line number Diff line number Diff line change
@@ -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: ""
8 changes: 8 additions & 0 deletions src/snapshots/insta__test__name_expr.snap
Original file line number Diff line number Diff line change
@@ -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: ""
9 changes: 9 additions & 0 deletions tests/snapshots/test_redaction__name.snap.new
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/test_redaction.rs
assertion_line: 78
expression: "&user"
---
id: "[id]"
username: john_doe
email: john@example.com
extra: ""
8 changes: 8 additions & 0 deletions tests/snapshots/test_redaction__name_expr.snap
Original file line number Diff line number Diff line change
@@ -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: ""
38 changes: 38 additions & 0 deletions tests/test_redaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 7f160b5

Please sign in to comment.