homework: dynamic insert / remove in devices and rooms
This commit is contained in:
@@ -28,6 +28,14 @@ impl House {
|
||||
self.rooms.get_mut(key)
|
||||
}
|
||||
|
||||
pub fn insert_room(&mut self, room: Room) -> Option<Room> {
|
||||
self.rooms.insert(room.get_name().to_string(), room)
|
||||
}
|
||||
|
||||
pub fn remove_room(&mut self, key: &str) -> Option<Room> {
|
||||
self.rooms.remove(key)
|
||||
}
|
||||
|
||||
pub fn print_status(&self) {
|
||||
println!("HOUSE '{}':", self.address);
|
||||
println!("{}", "=".repeat(32));
|
||||
@@ -102,4 +110,18 @@ mod tests {
|
||||
let house = create_test_house();
|
||||
assert!(house.get_room("absent").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_remove() {
|
||||
let mut house = create_test_house();
|
||||
let room = Room::new("empty", Box::new([]));
|
||||
|
||||
let result = house.insert_room(room);
|
||||
assert!(result.is_none());
|
||||
assert_eq!(house.rooms.len(), 3);
|
||||
|
||||
let Some(result) = house.remove_room("bedroom") else { unreachable!() };
|
||||
assert_eq!(result.get_name(), "bedroom");
|
||||
assert_eq!(house.rooms.len(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user