summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2023-04-03 01:47:57 -0400
committerGitHub <noreply@github.com>2023-04-03 01:47:57 -0400
commit8814bc53e3392e085279ac94b43f4242452241f2 (patch)
tree64cb736ca23c661495f137aab877bebb5c4c7c07
parent827ef0eec420960d04bd45131a3378c9939a6d18 (diff)
other: add test for multiple regexes in filter (#1082)
-rw-r--r--src/app/filter.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/src/app/filter.rs b/src/app/filter.rs
index b9695268..10629d94 100644
--- a/src/app/filter.rs
+++ b/src/app/filter.rs
@@ -9,11 +9,13 @@ impl Filter {
/// Whether the filter should keep the entry or reject it.
#[inline]
pub(crate) fn keep_entry(&self, value: &str) -> bool {
- self.list
- .iter()
- .find(|regex| regex.is_match(value))
- .map(|_| !self.is_list_ignored)
- .unwrap_or(self.is_list_ignored)
+ if self.list.iter().any(|regex| regex.is_match(value)) {
+ // If a match is found, then if we wanted to ignore if we match, return false. If we want
+ // to keep if we match, return true. Thus, return the inverse of `is_list_ignored`.
+ !self.is_list_ignored
+ } else {
+ self.is_list_ignored
+ }
}
}
@@ -58,5 +60,37 @@ mod test {
.collect::<Vec<_>>(),
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"]
+ );
}
}