Unit test for search_rooms
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
projectmoon 2021-05-28 21:13:19 +00:00
parent 0ca7ad4db0
commit 7f0bdc1e82
1 changed files with 26 additions and 1 deletions

View File

@ -10,7 +10,7 @@ use std::convert::TryFrom;
/// Holds matrix room ID and display name as strings, for use with /// Holds matrix room ID and display name as strings, for use with
/// searching. See search_for_room. /// searching. See search_for_room.
#[derive(Clone, Debug)] #[derive(Clone, Debug, Eq, PartialEq)]
struct RoomNameAndId { struct RoomNameAndId {
id: String, id: String,
name: String, name: String,
@ -42,6 +42,7 @@ fn search_for_room<'a>(
rooms_for_user: &'a [RoomNameAndId], rooms_for_user: &'a [RoomNameAndId],
query: &str, query: &str,
) -> Option<&'a RoomNameAndId> { ) -> Option<&'a RoomNameAndId> {
//Lowest score is the best match.
rooms_for_user rooms_for_user
.iter() .iter()
.find(|room| room.id == query) .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]);
}
}