Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow debug expressions in redaction macros #455

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to insta and cargo-insta are documented here.

## 1.38.0

- Redaction macros allow `debug` expressions. #455

- `Filters` is now constructible from `IntoIterator`. #400

- Change `std` macro calls to be fully qualified. This fixes issues where
Expand Down
36 changes: 24 additions & 12 deletions insta/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,44 @@ 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`
//
Expand All @@ -267,7 +280,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 @@ -283,13 +296,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 insta/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 insta/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: ""
8 changes: 8 additions & 0 deletions insta/tests/snapshots/test_redaction__name.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/test_redaction.rs
expression: "&user"
---
id: "[id]"
username: john_doe
email: john@example.com
extra: ""
8 changes: 8 additions & 0 deletions insta/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 insta/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