From b255b7c6916b4cd069e6e807d952aae314e713d8 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Sun, 16 Jul 2023 22:46:07 -0700 Subject: [PATCH 1/1] Hands --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 4496f80..e2604dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,6 +115,27 @@ impl Library { } } +#[cfg(test)] +#[derive(Default)] +struct Hand { + cards: Vec, +} +#[cfg(test)] +impl Hand { + fn add(&mut self, card: Card) { + self.cards.push(card); + } + fn remove(&mut self, card: Card) -> Result<(), &'static str> { + let i = self + .cards + .iter() + .position(|&e| e == card) + .ok_or("That card is not in your hand")?; + self.cards.swap_remove(i); + Ok(()) + } +} + #[cfg(test)] mod tests { use super::*; @@ -161,4 +182,14 @@ mod tests { assert_eq!(lib.draw(&mut dis), Some(Card(8))); assert_eq!(lib.draw(&mut dis), None); } + + #[test] + fn test_hand() { + let mut h = Hand::default(); + assert!(h.remove(Card(4)).is_err()); + h.add(Card(4)); + assert!(h.remove(Card(3)).is_err()); + assert!(h.remove(Card(4)).is_ok()); + assert!(h.remove(Card(4)).is_err()); + } } -- 2.44.1