#[track_caller]
pub fn panic_with_diff(expected: &str, found: &str) {
let chunks = dissimilar::diff(expected, found);
let diff = format_chunks(chunks);
panic!(
r#"
Snapshot comparison failed. Run the test again with UPDATE_EXPECT=1 in the environment to update the snapshot.
===== EXPECTED ====
{expected}
====== FOUND ======
{found}
======= DIFF ======
{diff}
"#
);
}
fn format_chunks(chunks: Vec<dissimilar::Chunk<'_>>) -> String {
let mut buf = String::new();
for chunk in chunks {
let formatted = match chunk {
dissimilar::Chunk::Equal(text) => text.into(),
dissimilar::Chunk::Delete(text) => format!("\x1b[41m{text}\x1b[0m"),
dissimilar::Chunk::Insert(text) => format!("\x1b[42m{text}\x1b[0m"),
};
buf.push_str(&formatted);
}
buf
}