📸 assert-snap
A snapshot testing and assertion library for Rust that supports flexible, regex-based dynamic data redactions and detailed unified diff output.
✨ Features
- 🎯
assert_snap!: Assert any types implementingDisplay. - 🔍
assert_debug_snap!: Assert any types implementingDebug. - 🛡️ Regex Redactions: Inline rules to scrub volatile data (timestamps, UUIDs, secret tokens, memory addresses) before checking assertions.
- 📊 Unified Diff: Built-in diff printing powered by
similaron assertion failures (enabled via thedifffeature, default ON).
🚀 Quickstart
Basic String Assertion (assert_snap!)
use assert_snap;
let actual_output = "User connected successfully";
let expected_snapshot = "User connected successfully";
assert_snap!;
Debug Assertion (assert_debug_snap!)
use assert_debug_snap;
let user = User ;
assert_debug_snap!;
🛡️ Redacting Dynamic Data
Dynamic values like generated IDs, secret keys, or timestamps can cause snapshot test instability. assert-snap allows appending redaction rules directly inside the macro invocation.
Basic Redaction Syntax
Format: "regex_pattern" => "replacement" (redacts all matches)
use assert_snap;
let actual = "Response time: 142ms, status: 200";
let expected = "Response time: [DURATION], status: 200";
assert_snap!;
Redactions with Limits
Format: [limit] "regex_pattern" => "replacement"
By default (or with [0]), all matches are redacted. Specifying a limit (e.g., [1]) restricts redaction to that maximum number of matches:
use assert_snap;
let actual = "token: secret_abc, session: secret_xyz";
let expected = "token: ****, session: secret_xyz";
assert_snap!;
Multiple Redaction Rules
You can combine multiple redaction rules separated by commas:
use assert_debug_snap;
let session = Session ;
assert_debug_snap!;
⚙️ Cargo Features
| Feature | Default | Description |
|---|---|---|
diff |
Enabled | Prints a unified diff using the similar crate when an assertion fails. |
To disable default features (such as diff to minimize dependencies):
[]
= { = "0.0.0", = false }
📜 License
This project is dual-licensed under:
You may choose either license at your discretion.
🤝 Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual-licensed as above, without any additional terms or conditions.