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

feat: add assert_compact_debug_snapshot #514

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions insta/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@ macro_rules! assert_debug_snapshot {
};
}

/// Asserts a `Debug` snapshot in compact format.
///
/// The value needs to implement the `fmt::Debug` trait. This is useful for
/// simple values that do not implement the `Serialize` trait, but does not
/// permit redactions.
///
/// Debug is called with `"{:?}"`, which means this does not use pretty-print.
#[macro_export]
macro_rules! assert_compact_debug_snapshot {
($($arg:tt)*) => {
$crate::_assert_snapshot_base!(transform=|v| std::format!("{:?}", v), $($arg)*)
};
}

// A helper macro which takes a closure as `transform`, and runs the closure on
// the value. This allows us to implement other macros with a small wrapper. All
// snapshot macros eventually call this macro.
Expand Down
8 changes: 7 additions & 1 deletion insta/tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use insta::assert_yaml_snapshot;
#[cfg(feature = "json")]
use insta::{assert_compact_json_snapshot, assert_json_snapshot};

use insta::{assert_debug_snapshot, assert_snapshot};
use insta::{assert_compact_debug_snapshot, assert_debug_snapshot, assert_snapshot};
use std::thread;

#[test]
Expand Down Expand Up @@ -281,6 +281,12 @@ fn test_compact_json() {
"###);
}

#[test]
fn test_compact_debug() {
assert_compact_debug_snapshot!((1..30).collect::<Vec<_>>(), @"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]");
assert_compact_debug_snapshot!((1..34).collect::<Vec<_>>(), @"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]");
}

#[test]
#[should_panic = "Insta does not allow inline snapshot assertions in loops"]
fn test_inline_test_in_loop() {
Expand Down
Loading