Switch to snapshot tests for search/matches/mod.rs

This commit is contained in:
Loïc Lecrenier 2022-08-04 11:26:39 +02:00
parent d2e01528a6
commit 051f24f674

View File

@ -573,15 +573,18 @@ mod tests {
let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World."; let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no crop should return complete text with highlighted matches. // no crop should return complete text with highlighted matches.
assert_eq!(&matcher.format(format_options), "Natalie risk her future to build a <em>world</em> with <em>the</em> boy she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>."); insta::assert_snapshot!(
matcher.format(format_options),
@"Natalie risk her future to build a <em>world</em> with <em>the</em> boy she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>."
);
// Text containing some matches. // Text containing some matches.
let text = "Natalie risk her future to build a world with the boy she loves."; let text = "Natalie risk her future to build a world with the boy she loves.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no crop should return complete text with highlighted matches. // no crop should return complete text with highlighted matches.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"Natalie risk her future to build a <em>world</em> with <em>the</em> boy she loves." @"Natalie risk her future to build a <em>world</em> with <em>the</em> boy she loves."
); );
} }
@ -602,19 +605,28 @@ mod tests {
let text = "Ŵôřlḑôle"; let text = "Ŵôřlḑôle";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no crop should return complete text with highlighted matches. // no crop should return complete text with highlighted matches.
assert_eq!(&matcher.format(format_options), "<em>Ŵôřlḑ</em>ôle"); insta::assert_snapshot!(
matcher.format(format_options),
@"<em>Ŵôřlḑ</em>ôle"
);
// Text containing unicode match. // Text containing unicode match.
let text = "Ŵôřlḑ"; let text = "Ŵôřlḑ";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no crop should return complete text with highlighted matches. // no crop should return complete text with highlighted matches.
assert_eq!(&matcher.format(format_options), "<em>Ŵôřlḑ</em>"); insta::assert_snapshot!(
matcher.format(format_options),
@"<em>Ŵôřlḑ</em>"
);
// Text containing unicode match. // Text containing unicode match.
let text = "Westfália"; let text = "Westfália";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no crop should return complete text with highlighted matches. // no crop should return complete text with highlighted matches.
assert_eq!(&matcher.format(format_options), "<em>Westfáli</em>a"); insta::assert_snapshot!(
matcher.format(format_options),
@"<em>Westfáli</em>a"
);
} }
#[test] #[test]
@ -628,83 +640,89 @@ mod tests {
// empty text. // empty text.
let text = ""; let text = "";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
assert_eq!(&matcher.format(format_options), ""); insta::assert_snapshot!(
matcher.format(format_options),
@""
);
// text containing only separators. // text containing only separators.
let text = ":-)"; let text = ":-)";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
assert_eq!(&matcher.format(format_options), ":-)"); insta::assert_snapshot!(
matcher.format(format_options),
@":-)"
);
// Text without any match. // Text without any match.
let text = "A quick brown fox can not jump 32 feet, right? Brr, it is cold!"; let text = "A quick brown fox can not jump 32 feet, right? Brr, it is cold!";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no highlight should return 10 first words with a marker at the end. // no highlight should return 10 first words with a marker at the end.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"A quick brown fox can not jump 32 feet, right…" @"A quick brown fox can not jump 32 feet, right…"
); );
// Text without any match starting by a separator. // Text without any match starting by a separator.
let text = "(A quick brown fox can not jump 32 feet, right? Brr, it is cold!)"; let text = "(A quick brown fox can not jump 32 feet, right? Brr, it is cold!)";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no highlight should return 10 first words with a marker at the end. // no highlight should return 10 first words with a marker at the end.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"(A quick brown fox can not jump 32 feet, right…" @"(A quick brown fox can not jump 32 feet, right…"
); );
// Test phrase propagation // Test phrase propagation
let text = "Natalie risk her future. Split The World is a book written by Emily Henry. I never read it."; let text = "Natalie risk her future. Split The World is a book written by Emily Henry. I never read it.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// should crop the phrase instead of croping around the match. // should crop the phrase instead of croping around the match.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"… Split The World is a book written by Emily Henry…", @"… Split The World is a book written by Emily Henry…"
); );
// Text containing some matches. // Text containing some matches.
let text = "Natalie risk her future to build a world with the boy she loves."; let text = "Natalie risk her future to build a world with the boy she loves.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no highlight should return 10 last words with a marker at the start. // no highlight should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…future to build a world with the boy she loves…" @"…future to build a world with the boy she loves…"
); );
// Text containing all matches. // Text containing all matches.
let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World."; let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// no highlight should return 10 last words with a marker at the start. // no highlight should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…she loves. Emily Henry: The Love That Split The World." @"…she loves. Emily Henry: The Love That Split The World."
); );
// Text containing a match unordered and a match ordered. // Text containing a match unordered and a match ordered.
let text = "The world split void void void void void void void void void split the world void void"; let text = "The world split void void void void void void void void void split the world void void";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// crop should return 10 last words with a marker at the start. // crop should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…void void void void void split the world void void" @"…void void void void void split the world void void"
); );
// Text containing matches with diferent density. // Text containing matches with diferent density.
let text = "split void the void void world void void void void void void void void void void split the world void void"; let text = "split void the void void world void void void void void void void void void void split the world void void";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// crop should return 10 last words with a marker at the start. // crop should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…void void void void void split the world void void" @"…void void void void void split the world void void"
); );
// Text containing matches with same word. // Text containing matches with same word.
let text = "split split split split split split void void void void void void void void void void split the world void void"; let text = "split split split split split split void void void void void void void void void void split the world void void";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// crop should return 10 last words with a marker at the start. // crop should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…void void void void void split the world void void" @"…void void void void void split the world void void"
); );
} }
@ -719,44 +737,53 @@ mod tests {
// empty text. // empty text.
let text = ""; let text = "";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
assert_eq!(&matcher.format(format_options), ""); insta::assert_snapshot!(
matcher.format(format_options),
@""
);
// text containing only separators. // text containing only separators.
let text = ":-)"; let text = ":-)";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
assert_eq!(&matcher.format(format_options), ":-)"); insta::assert_snapshot!(
matcher.format(format_options),
@":-)"
);
// Text without any match. // Text without any match.
let text = "A quick brown fox can not jump 32 feet, right? Brr, it is cold!"; let text = "A quick brown fox can not jump 32 feet, right? Brr, it is cold!";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// both should return 10 first words with a marker at the end. // both should return 10 first words with a marker at the end.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"A quick brown fox can not jump 32 feet, right…" @"A quick brown fox can not jump 32 feet, right…"
); );
// Text containing some matches. // Text containing some matches.
let text = "Natalie risk her future to build a world with the boy she loves."; let text = "Natalie risk her future to build a world with the boy she loves.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// both should return 10 last words with a marker at the start and highlighted matches. // both should return 10 last words with a marker at the start and highlighted matches.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…future to build a <em>world</em> with <em>the</em> boy she loves…" @"…future to build a <em>world</em> with <em>the</em> boy she loves…"
); );
// Text containing all matches. // Text containing all matches.
let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World."; let text = "Natalie risk her future to build a world with the boy she loves. Emily Henry: The Love That Split The World.";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// both should return 10 last words with a marker at the start and highlighted matches. // both should return 10 last words with a marker at the start and highlighted matches.
assert_eq!(&matcher.format(format_options), "…she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>."); insta::assert_snapshot!(
matcher.format(format_options),
@"…she loves. Emily Henry: <em>The</em> Love That <em>Split</em> <em>The</em> <em>World</em>."
);
// Text containing a match unordered and a match ordered. // Text containing a match unordered and a match ordered.
let text = "The world split void void void void void void void void void split the world void void"; let text = "The world split void void void void void void void void void split the world void void";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// crop should return 10 last words with a marker at the start. // crop should return 10 last words with a marker at the start.
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"…void void void void void <em>split</em> <em>the</em> <em>world</em> void void" @"…void void void void void <em>split</em> <em>the</em> <em>world</em> void void"
); );
} }
@ -773,19 +800,28 @@ mod tests {
let format_options = FormatOptions { highlight: false, crop: Some(2) }; let format_options = FormatOptions { highlight: false, crop: Some(2) };
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// because crop size < query size, partially format matches. // because crop size < query size, partially format matches.
assert_eq!(&matcher.format(format_options), "…split the…"); insta::assert_snapshot!(
matcher.format(format_options),
@"…split the…"
);
// set a smaller crop size // set a smaller crop size
let format_options = FormatOptions { highlight: false, crop: Some(1) }; let format_options = FormatOptions { highlight: false, crop: Some(1) };
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// because crop size < query size, partially format matches. // because crop size < query size, partially format matches.
assert_eq!(&matcher.format(format_options), "…split…"); insta::assert_snapshot!(
matcher.format(format_options),
@"…split…"
);
// set crop size to 0 // set crop size to 0
let format_options = FormatOptions { highlight: false, crop: Some(0) }; let format_options = FormatOptions { highlight: false, crop: Some(0) };
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
// because crop size is 0, crop is ignored. // because crop size is 0, crop is ignored.
assert_eq!(&matcher.format(format_options), "void void split the world void void."); insta::assert_snapshot!(
matcher.format(format_options),
@"void void split the world void void."
);
} }
#[test] #[test]
@ -820,11 +856,9 @@ mod tests {
let text = "the do or die can't be he do and or isn't he"; let text = "the do or die can't be he do and or isn't he";
let mut matcher = builder.build(text); let mut matcher = builder.build(text);
assert_eq!( insta::assert_snapshot!(
&matcher.format(format_options), matcher.format(format_options),
"_the_ _do_ _or_ die can't be he _do_ and or isn'_t_ _he_", @"_the_ _do_ _or_ die can't be he _do_ and or isn'_t_ _he_"
"matches: {:?}",
&matcher.matches
); );
} }
} }