use super::{BoxedFilterFn, Filter};
pub fn new_filter(filters: Vec<BoxedFilterFn>) -> impl Filter {
move |room| -> bool { filters.iter().all(|filter| filter(room)) }
}
#[cfg(test)]
mod tests {
use std::ops::Not;
use matrix_sdk_test::async_test;
use ruma::room_id;
use super::{
super::{client_and_server_prelude, new_rooms},
*,
};
#[async_test]
async fn test_one_filter() {
let (client, server, sliding_sync) = client_and_server_prelude().await;
let [room] = new_rooms([room_id!("!a:b.c")], &client, &server, &sliding_sync).await;
{
let filter = |_: &_| true;
let all = new_filter(vec![Box::new(filter)]);
assert!(all(&room));
}
{
let filter = |_: &_| false;
let all = new_filter(vec![Box::new(filter)]);
assert!(all(&room).not());
}
}
#[async_test]
async fn test_two_filters() {
let (client, server, sliding_sync) = client_and_server_prelude().await;
let [room] = new_rooms([room_id!("!a:b.c")], &client, &server, &sliding_sync).await;
{
let filter1 = |_: &_| true;
let filter2 = |_: &_| true;
let all = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
assert!(all(&room));
}
{
let filter1 = |_: &_| true;
let filter2 = |_: &_| false;
let all = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
assert!(all(&room).not());
}
{
let filter1 = |_: &_| false;
let filter2 = |_: &_| true;
let all = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
assert!(all(&room).not());
}
{
let filter1 = |_: &_| false;
let filter2 = |_: &_| false;
let all = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
assert!(all(&room).not());
}
}
}