Repository: mitsuhiko/insta
Pull request #756 (2025-04-04 23:16:46)

Add support for named snapshots with redactions and debug expr
Co-authored-by: Claude <no-reply@anthropic.com>



Patch:
diff --git a/insta/src/macros.rs b/insta/src/macros.rs
index 81d0cfc..f970cfe 100644
--- a/insta/src/macros.rs
+++ b/insta/src/macros.rs
@@ -253,6 +253,14 @@ macro_rules! _assert_serialized_snapshot {
         };
         $crate::_assert_snapshot_base!(transform=transform, $value $($arg)*);
     }};
+    // If there's a name, redaction expressions, and debug_expr, capture and pass all to `_assert_snapshot_base`
+    (format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?}, $debug_expr:expr $(,)?) => {{
+        let transform = |value| {
+            let (_, value) = $crate::_prepare_snapshot_for_redaction!(value, {$($k => $v),*}, $format);
+            value
+        };
+        $crate::_assert_snapshot_base!(transform=transform, $name, $value, $debug_expr);
+    }};
     // If there's a name and redaction expressions, capture and pass to `_assert_snapshot_base`
     (format=$format:ident, $name:expr, $value:expr, $(match ..)? {$($k:expr => $v:expr),* $(,)?} $(,)?) => {{
         let transform = |value| {


Test patch:
diff --git a/insta/tests/snapshots/test_redaction__named_redacted_debug_expr.snap b/insta/tests/snapshots/test_redaction__named_redacted_debug_expr.snap
new file mode 100644
index 0000000..0096613
--- /dev/null
+++ b/insta/tests/snapshots/test_redaction__named_redacted_debug_expr.snap
@@ -0,0 +1,12 @@
+---
+source: insta/tests/test_redaction.rs
+expression: This is a custom debug expression for the snapshot
+---
+id: "[id]"
+items:
+  - one
+  - two
+  - three
+metadata:
+  count: 42
+  version: 123
diff --git a/insta/tests/snapshots/test_redaction__named_redacted_supported.snap b/insta/tests/snapshots/test_redaction__named_redacted_supported.snap
new file mode 100644
index 0000000..fc0943a
--- /dev/null
+++ b/insta/tests/snapshots/test_redaction__named_redacted_supported.snap
@@ -0,0 +1,12 @@
+---
+source: insta/tests/test_redaction.rs
+expression: "&complex_obj"
+---
+id: "[id]"
+items:
+  - one
+  - two
+  - three
+metadata:
+  count: 42
+  version: 123
diff --git a/insta/tests/test_redaction.rs b/insta/tests/test_redaction.rs
index ab46127..25b4958 100644
--- a/insta/tests/test_redaction.rs
+++ b/insta/tests/test_redaction.rs
@@ -543,3 +543,68 @@ fn test_rounded_redaction() {
         }
     );
 }
+
+#[cfg(feature = "yaml")]
+#[test]
+fn test_named_redacted_with_debug_expr() {
+    // This test demonstrates the form with a name, redactions, and debug expression
+    // | File, redacted, named, debug expr | `assert_yaml_snapshot!("name", expr, {"." => sorted_redaction()}, "debug_expr")` |
+
+    #[derive(Serialize, Debug)]
+    pub struct ComplexObject {
+        id: u32,
+        items: Vec<String>,
+        metadata: std::collections::HashMap<String, u32>,
+    }
+
+    let mut metadata = std::collections::HashMap::new();
+    metadata.insert("count".to_string(), 42);
+    metadata.insert("version".to_string(), 123);
+
+    let complex_obj = ComplexObject {
+        id: 12345,
+        items: vec!["one".to_string(), "two".to_string(), "three".to_string()],
+        metadata,
+    };
+
+    // Now that we've added support for this form, we can test it directly
+    assert_yaml_snapshot!(
+        "named_redacted_debug_expr",
+        &complex_obj,
+        {
+            ".id" => "[id]",
+            ".metadata" => insta::sorted_redaction()
+        },
+        "This is a custom debug expression for the snapshot"
+    );
+}
+
+#[cfg(feature = "yaml")]
+#[test]
+fn test_named_redacted_supported_form() {
+    #[derive(Serialize, Debug)]
+    pub struct ComplexObject {
+        id: u32,
+        items: Vec<String>,
+        metadata: std::collections::HashMap<String, u32>,
+    }
+
+    let mut metadata = std::collections::HashMap::new();
+    metadata.insert("count".to_string(), 42);
+    metadata.insert("version".to_string(), 123);
+
+    let obj = ComplexObject {
+        id: 12345,
+        items: vec!["one".to_string(), "two".to_string(), "three".to_string()],
+        metadata,
+    };
+
+    assert_yaml_snapshot!(
+        "named_redacted_supported",
+        &obj,
+        {
+            ".id" => "[id]",
+            ".metadata" => insta::sorted_redaction()
+        }
+    );
+}