Skip to content

Commit a4231cc

Browse files
authored
Add test for find_snapshot_macro (#582)
Was trying to work out whether it extracted hashes, got a useful test as a side-effect
1 parent d609d7d commit a4231cc

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

cargo-insta/src/inline.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,51 @@ impl FilePatcher {
259259
visitor.1
260260
}
261261
}
262+
263+
#[cfg(test)]
264+
mod tests {
265+
use insta::assert_debug_snapshot;
266+
267+
use super::*;
268+
use std::path::PathBuf;
269+
270+
#[test]
271+
fn test_find_snapshot_macro() {
272+
let content = r######"
273+
use insta::assert_snapshot;
274+
275+
fn test_function() {
276+
assert_snapshot!("test\ntest", @r###"
277+
test
278+
test
279+
"###);
280+
}
281+
"######;
282+
283+
let file_patcher = FilePatcher {
284+
filename: PathBuf::new(),
285+
lines: content.lines().map(String::from).collect(),
286+
source: syn::parse_file(content).unwrap(),
287+
inline_snapshots: vec![],
288+
};
289+
290+
// The snapshot macro starts on line 5 (1-based index)
291+
let snapshot = file_patcher.find_snapshot_macro(5).unwrap();
292+
293+
// Extract the snapshot content
294+
let snapshot_content: Vec<String> =
295+
file_patcher.lines[snapshot.start.0..=snapshot.end.0].to_vec();
296+
297+
assert_debug_snapshot!(snapshot_content, @r####"
298+
[
299+
" assert_snapshot!(\"test\\ntest\", @r###\"",
300+
" test",
301+
" test",
302+
" \"###);",
303+
]
304+
"####);
305+
306+
// Assert the indentation
307+
assert_debug_snapshot!(snapshot.indentation, @"4");
308+
}
309+
}

0 commit comments

Comments
 (0)