Skip to content

Commit

Permalink
test(ui): Split tests and improve documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Feb 8, 2024
1 parent dc89c00 commit fce1140
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
80 changes: 44 additions & 36 deletions crates/matrix-sdk-ui/src/room_list_service/filters/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,66 @@ mod tests {
use super::new_filter;

#[test]
fn test_one_filter() {
fn test_one_filter_is_true() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

{
let filter = |_: &_| true;
let any = new_filter(vec![Box::new(filter)]);
let filter = |_: &_| true;
let any = new_filter(vec![Box::new(filter)]);

assert!(any(&room_list_entry));
}
assert!(any(&room_list_entry));
}

#[test]
fn test_one_filter_is_false() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

let filter = |_: &_| false;
let any = new_filter(vec![Box::new(filter)]);

assert!(any(&room_list_entry).not());
}

#[test]
fn test_two_filters_with_true_true() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

{
let filter = |_: &_| false;
let any = new_filter(vec![Box::new(filter)]);
let filter1 = |_: &_| true;
let filter2 = |_: &_| true;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);

assert!(any(&room_list_entry).not());
}
assert!(any(&room_list_entry));
}

#[test]
fn test_two_filters() {
fn test_two_filters_with_true_false() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

{
let filter1 = |_: &_| true;
let filter2 = |_: &_| true;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
let filter1 = |_: &_| true;
let filter2 = |_: &_| false;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);

assert!(any(&room_list_entry));
}
assert!(any(&room_list_entry));
}

{
let filter1 = |_: &_| true;
let filter2 = |_: &_| false;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
#[test]
fn test_two_filters_with_false_true() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

assert!(any(&room_list_entry));
}
let filter1 = |_: &_| false;
let filter2 = |_: &_| true;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);

{
let filter1 = |_: &_| false;
let filter2 = |_: &_| true;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
assert!(any(&room_list_entry));
}

assert!(any(&room_list_entry));
}
#[test]
fn test_two_filters_with_false_false() {
let room_list_entry = RoomListEntry::Filled(room_id!("!r0:bar.org").to_owned());

{
let filter1 = |_: &_| false;
let filter2 = |_: &_| false;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);
let filter1 = |_: &_| false;
let filter2 = |_: &_| false;
let any = new_filter(vec![Box::new(filter1), Box::new(filter2)]);

assert!(any(&room_list_entry).not());
}
assert!(any(&room_list_entry).not());
}
}
17 changes: 11 additions & 6 deletions crates/matrix-sdk-ui/src/room_list_service/filters/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use matrix_sdk::{Client, RoomListEntry};

use super::Filter;

/// An enum to represent whether a room is about “people” (1 or 2 users) or
/// “group” (more than 2 users).
/// An enum to represent whether a room is about “people” (strictly 2 users) or
/// “group” (1 or more than 2 users).
///
/// Ideally, we would only want to rely on the
/// [`matrix_sdk::BaseRoom::is_direct`] method, but the rules are a little bit
/// different for this high-level UI API.
///
/// This is implemented this way so that it's impossible to filter by “group”
/// and by “people” at the same time: these criteria are mutually
Expand Down Expand Up @@ -52,9 +56,10 @@ where
}
}

/// Create a new filter that will accept all filled or invalidated entries, but
/// filters out rooms that have no unread messages.
pub fn new_filter(client: &Client, expected_kind: RoomCategory) -> impl Filter {
/// Create a new filter that will accept all filled or invalidated entries, and
/// if the associated rooms fit in the `expected_category`. The category is
/// defined by [`RoomCategory`], see this type to learn more.
pub fn new_filter(client: &Client, expected_category: RoomCategory) -> impl Filter {
let client = client.clone();

let matcher = CategoryRoomMatcher {
Expand All @@ -66,7 +71,7 @@ pub fn new_filter(client: &Client, expected_kind: RoomCategory) -> impl Filter {
},
};

move |room_list_entry| -> bool { matcher.matches(room_list_entry, expected_kind) }
move |room_list_entry| -> bool { matcher.matches(room_list_entry, expected_category) }
}

#[cfg(test)]
Expand Down

0 comments on commit fce1140

Please sign in to comment.