use insta::{assert_snapshot, with_settings, Comparator, Snapshot};
struct WhitespaceInsensitiveComparator;
impl Comparator for WhitespaceInsensitiveComparator {
fn matches(&self, reference: &Snapshot, test: &Snapshot) -> bool {
match (reference.as_text(), test.as_text()) {
(Some(a), Some(b)) => {
let a_normalized: String = a.to_string().split_whitespace().collect();
let b_normalized: String = b.to_string().split_whitespace().collect();
a_normalized == b_normalized
}
_ => false,
}
}
fn dyn_clone(&self) -> Box<dyn Comparator> {
Box::new(WhitespaceInsensitiveComparator)
}
}
#[test]
fn custom_comparator_matches() {
let comparator = Box::new(WhitespaceInsensitiveComparator);
with_settings!({comparator => comparator}, {
assert_snapshot!("hello world", @"hello world");
});
}
macro_rules! assert_whitespace_insensitive {
($($body:tt)*) => {
::insta::with_settings!(
{comparator => Box::new(crate::WhitespaceInsensitiveComparator)},
{
::insta::assert_snapshot!($($body)*);
});
};
}
#[test]
fn custom_macro_passes() {
assert_whitespace_insensitive!("hello world", @"hello world");
}