Skip to content

Commit

Permalink
Add unit test for LineSearcher
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Jan 24, 2025
1 parent 8acce27 commit b7c827d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use uuid::Uuid;
pub mod linear;
pub mod regular;
#[cfg(test)]
pub mod tests_linear;
#[cfg(test)]
pub mod tests_regular;
#[cfg(test)]
pub mod tests_values;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::search::{filter::SearchFilter, searchers::linear::LineSearcher};

#[cfg(test)]
const SAMPLES: &[&str] = &[
"[Info](1.3): a",
"[Warn](1.4): b",
"[Info](1.5): c",
"[Err](1.6): d",
"[err](1.6): d",
"[Info](1.7): e",
"[Info](1.8): f",
"[warn](1.4): b",
];

#[test]
fn test_linear() -> Result<(), std::io::Error> {
let cases: Vec<(SearchFilter, &[usize])> = vec![
(
SearchFilter::plain(r"[Err]")
.regex(false)
.ignore_case(true)
.word(false),
&[3, 4],
),
(
SearchFilter::plain(r"[err]")
.regex(false)
.ignore_case(true)
.word(false),
&[3, 4],
),
(
SearchFilter::plain(r"[err]")
.regex(false)
.ignore_case(false)
.word(false),
&[4],
),
(
SearchFilter::plain(r"\[Warn\]")
.regex(true)
.ignore_case(true)
.word(false),
&[1, 7],
),
(
SearchFilter::plain(r"warn")
.regex(true)
.ignore_case(false)
.word(false),
&[7],
),
];
for (filter, matches) in cases.into_iter() {
let searcher = LineSearcher::new(&filter)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))?;
for (n, smpl) in SAMPLES.iter().enumerate() {
if searcher.is_match(smpl) {
assert!(matches.contains(&n));
} else {
assert!(!matches.contains(&n));
}
}
}
Ok(())
}

0 comments on commit b7c827d

Please sign in to comment.