mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-26 14:10:19 +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.
|
/// 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"]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue