From c235d6320e3c6985b0eaa0c37f8e2917a1989327 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Sun, 22 Sep 2024 11:18:47 -0700 Subject: [PATCH] Add a test and comments for selecting indentation (#606) --- cargo-insta/src/inline.rs | 5 ++++- insta/tests/test_inline.rs | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cargo-insta/src/inline.rs b/cargo-insta/src/inline.rs index b0ee5150..5aee2f49 100644 --- a/cargo-insta/src/inline.rs +++ b/cargo-insta/src/inline.rs @@ -149,19 +149,22 @@ impl FilePatcher { impl Visitor { fn scan_nested_macros(&mut self, tokens: &[TokenTree]) { for idx in 0..tokens.len() { + // Look for the start of a macro (potential snapshot location) if let Some(TokenTree::Ident(_)) = tokens.get(idx) { if let Some(TokenTree::Punct(ref punct)) = tokens.get(idx + 1) { if punct.as_char() == '!' { if let Some(TokenTree::Group(ref group)) = tokens.get(idx + 2) { + // Found a macro, determine its indentation let indentation = scan_for_path_start(tokens, idx); + // Extract tokens from the macro arguments let tokens: Vec<_> = group.stream().into_iter().collect(); + // Try to extract a snapshot, passing the calculated indentation self.try_extract_snapshot(&tokens, indentation); } } } } } - for token in tokens { // recurse into groups if let TokenTree::Group(group) = token { diff --git a/insta/tests/test_inline.rs b/insta/tests/test_inline.rs index 93f70126..bdee1fed 100644 --- a/insta/tests/test_inline.rs +++ b/insta/tests/test_inline.rs @@ -304,3 +304,14 @@ fn test_inline_snapshot_whitespace() { "###); } + +#[test] +fn test_indentation() { + assert_snapshot!("aaa\nbbb\nccc\nddd", @r" + aaa + bbb + ccc + ddd + " + ); +}