Skip to content

Commit

Permalink
Add a test and comments for selecting indentation (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty authored Sep 22, 2024
1 parent e0d1224 commit c235d63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cargo-insta/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions insta/tests/test_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,14 @@ fn test_inline_snapshot_whitespace() {
"###);
}

#[test]
fn test_indentation() {
assert_snapshot!("aaa\nbbb\nccc\nddd", @r"
aaa
bbb
ccc
ddd
"
);
}

0 comments on commit c235d63

Please sign in to comment.