mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-22 12:13:06 +00:00
other: add test for multiple regexes in filter (#1082)
This commit is contained in:
parent
827ef0eec4
commit
8814bc53e3
1 changed files with 39 additions and 5 deletions
|
@ -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"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue