From 7f0bdc1e822fd2554281d5da35ce4dd3d25b1abf Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 28 May 2021 21:13:19 +0000 Subject: [PATCH] Unit test for search_rooms --- src/commands/rooms.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/commands/rooms.rs b/src/commands/rooms.rs index 5591b8b..7f368c0 100644 --- a/src/commands/rooms.rs +++ b/src/commands/rooms.rs @@ -10,7 +10,7 @@ use std::convert::TryFrom; /// Holds matrix room ID and display name as strings, for use with /// searching. See search_for_room. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] struct RoomNameAndId { id: String, name: String, @@ -42,6 +42,7 @@ fn search_for_room<'a>( rooms_for_user: &'a [RoomNameAndId], query: &str, ) -> Option<&'a RoomNameAndId> { + //Lowest score is the best match. rooms_for_user .iter() .find(|room| room.id == query) @@ -151,3 +152,27 @@ impl Command for SetRoomCommand { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] + async fn set_room_prefers_room_id_over_name() { + let rooms = vec![ + RoomNameAndId { + id: "roomid".to_string(), + name: "room_name".to_string(), + }, + RoomNameAndId { + id: "anotherone".to_string(), + name: "roomid".to_string(), + }, + ]; + + let found_room = search_for_room(&rooms, "roomid"); + + assert!(found_room.is_some()); + assert_eq!(found_room.unwrap(), &rooms[0]); + } +}