Skip to content

Commit 25cedbc

Browse files
max-sixtyClaude
andauthored
Add support for named snapshots with redactions and debug expr (#756)
Co-authored-by: Claude <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent d18390d commit 25cedbc

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

insta/src/macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ macro_rules! _assert_serialized_snapshot {
253253
};
254254
$crate::_assert_snapshot_base!(transform=transform, $value $($arg)*);
255255
}};
256+
// If there's a name, redaction expressions, and debug_expr, capture and pass all to `_assert_snapshot_base`
257+
(format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?}, $debug_expr:expr $(,)?) => {{
258+
let transform = |value| {
259+
let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format);
260+
value
261+
};
262+
$crate::_assert_snapshot_base!(transform=transform, $name, $value, $debug_expr);
263+
}};
256264
// If there's a name and redaction expressions, capture and pass to `_assert_snapshot_base`
257265
(format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
258266
let transform = |value| {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: insta/tests/test_redaction.rs
3+
expression: This is a custom debug expression for the snapshot
4+
---
5+
id: "[id]"
6+
items:
7+
- one
8+
- two
9+
- three
10+
metadata:
11+
count: 42
12+
version: 123
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: insta/tests/test_redaction.rs
3+
expression: "&complex_obj"
4+
---
5+
id: "[id]"
6+
items:
7+
- one
8+
- two
9+
- three
10+
metadata:
11+
count: 42
12+
version: 123

insta/tests/test_redaction.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,68 @@ fn test_rounded_redaction() {
543543
}
544544
);
545545
}
546+
547+
#[cfg(feature = "yaml")]
548+
#[test]
549+
fn test_named_redacted_with_debug_expr() {
550+
// This test demonstrates the form with a name, redactions, and debug expression
551+
// | File, redacted, named, debug expr | `assert_yaml_snapshot!("name", expr, {"." => sorted_redaction()}, "debug_expr")` |
552+
553+
#[derive(Serialize, Debug)]
554+
pub struct ComplexObject {
555+
id: u32,
556+
items: Vec<String>,
557+
metadata: std::collections::HashMap<String, u32>,
558+
}
559+
560+
let mut metadata = std::collections::HashMap::new();
561+
metadata.insert("count".to_string(), 42);
562+
metadata.insert("version".to_string(), 123);
563+
564+
let complex_obj = ComplexObject {
565+
id: 12345,
566+
items: vec!["one".to_string(), "two".to_string(), "three".to_string()],
567+
metadata,
568+
};
569+
570+
// Now that we've added support for this form, we can test it directly
571+
assert_yaml_snapshot!(
572+
"named_redacted_debug_expr",
573+
&complex_obj,
574+
{
575+
".id" => "[id]",
576+
".metadata" => insta::sorted_redaction()
577+
},
578+
"This is a custom debug expression for the snapshot"
579+
);
580+
}
581+
582+
#[cfg(feature = "yaml")]
583+
#[test]
584+
fn test_named_redacted_supported_form() {
585+
#[derive(Serialize, Debug)]
586+
pub struct ComplexObject {
587+
id: u32,
588+
items: Vec<String>,
589+
metadata: std::collections::HashMap<String, u32>,
590+
}
591+
592+
let mut metadata = std::collections::HashMap::new();
593+
metadata.insert("count".to_string(), 42);
594+
metadata.insert("version".to_string(), 123);
595+
596+
let obj = ComplexObject {
597+
id: 12345,
598+
items: vec!["one".to_string(), "two".to_string(), "three".to_string()],
599+
metadata,
600+
};
601+
602+
assert_yaml_snapshot!(
603+
"named_redacted_supported",
604+
&obj,
605+
{
606+
".id" => "[id]",
607+
".metadata" => insta::sorted_redaction()
608+
}
609+
);
610+
}

0 commit comments

Comments
 (0)