From fce1140ad11c08b50ee90315325dfb6b1c7d64c8 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 8 Feb 2024 14:09:13 +0100 Subject: [PATCH] test(ui): Split tests and improve documentation. --- .../src/room_list_service/filters/any.rs | 80 ++++++++++--------- .../src/room_list_service/filters/category.rs | 17 ++-- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/filters/any.rs b/crates/matrix-sdk-ui/src/room_list_service/filters/any.rs index c0bb094246d..dd92ddf73f7 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/filters/any.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/filters/any.rs @@ -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()); } } diff --git a/crates/matrix-sdk-ui/src/room_list_service/filters/category.rs b/crates/matrix-sdk-ui/src/room_list_service/filters/category.rs index 6fc07eb8a85..f7041ca14a3 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/filters/category.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/filters/category.rs @@ -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 @@ -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 { @@ -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)]