Skip to content

Commit

Permalink
Add proposal for + syntax for inline snapshots
Browse files Browse the repository at this point in the history
Quick demo; took 10 min. Would need a substantial discussion, docs, etc.

In particular, older versions of `cargo-insta` would have surprising behavior.

But I think it's a good idea; allows for `cargo fmt` to work on the inline snapshots.
  • Loading branch information
max-sixty committed Sep 22, 2024
1 parent caa1e8f commit cf27826
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cargo-insta/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ impl FilePatcher {
}

match tokens[tokens.len() - 2] {
TokenTree::Punct(ref punct) if punct.as_char() == '@' => {}
TokenTree::Punct(ref punct)
if punct.as_char() == '@' || punct.as_char() == '+' => {}
_ => {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions insta/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ macro_rules! _assert_snapshot_base {
$($arg),*
)
};
// Or using the new syntax
(transform=$transform:expr, $($arg:expr),*, !$snapshot:literal $(,)?) => {
$crate::_assert_snapshot_base!(
transform = $transform,
#[allow(clippy::needless_raw_string_hashes)]
$crate::_macro_support::ReferenceValue::Inline($snapshot),
$($arg),*
)
};
// If there's no debug_expr, use the stringified value, call self.
(transform=$transform:expr, $name:expr, $value:expr $(,)?) => {
$crate::_assert_snapshot_base!(transform = $transform, $name, $value, stringify!($value))
Expand Down
10 changes: 10 additions & 0 deletions insta/tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ fn test_single_line() {
assert_snapshot!("Testing", @"Testing");
}

#[test]
fn test_single_line_plus() {
// New `+` syntax
assert_snapshot!("Testing", !"Testing");

// TODO: move to integration test: confirm this fails (it wouldn't fail with
// a `-` prefix, for example)
// assert_snapshot!("Testing", *5);
}

// We used to use the thread name for snapshot name detection. This is unreliable
// so this test now basically does exactly the same as `test_unnamed_single_line`.
#[test]
Expand Down

0 comments on commit cf27826

Please sign in to comment.