other: add test for multiple regexes in filter (#1082)

This commit is contained in:
Clement Tsang 2023-04-03 01:47:57 -04:00 committed by GitHub
parent 827ef0eec4
commit 8814bc53e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,11 +9,13 @@ impl Filter {
/// Whether the filter should keep the entry or reject it. /// Whether the filter should keep the entry or reject it.
#[inline] #[inline]
pub(crate) fn keep_entry(&self, value: &str) -> bool { pub(crate) fn keep_entry(&self, value: &str) -> bool {
self.list if self.list.iter().any(|regex| regex.is_match(value)) {
.iter() // If a match is found, then if we wanted to ignore if we match, return false. If we want
.find(|regex| regex.is_match(value)) // to keep if we match, return true. Thus, return the inverse of `is_list_ignored`.
.map(|_| !self.is_list_ignored) !self.is_list_ignored
.unwrap_or(self.is_list_ignored) } else {
self.is_list_ignored
}
} }
} }
@ -58,5 +60,37 @@ mod test {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
vec!["CPU socket temperature", "motherboard temperature"] vec!["CPU socket temperature", "motherboard temperature"]
); );
let multi_true = Filter {
is_list_ignored: true,
list: vec![
Regex::new("socket").unwrap(),
Regex::new("temperature").unwrap(),
],
};
assert_eq!(
results
.into_iter()
.filter(|r| multi_true.keep_entry(r))
.collect::<Vec<_>>(),
vec!["wifi_0", "amd gpu"]
);
let multi_false = Filter {
is_list_ignored: false,
list: vec![
Regex::new("socket").unwrap(),
Regex::new("temperature").unwrap(),
],
};
assert_eq!(
results
.into_iter()
.filter(|r| multi_false.keep_entry(r))
.collect::<Vec<_>>(),
vec!["CPU socket temperature", "motherboard temperature"]
);
} }
} }