From: Scott Worley Date: Mon, 17 Jul 2023 05:46:07 +0000 (-0700) Subject: Hands X-Git-Url: http://git.scottworley.com/pluta-lesnura/commitdiff_plain/b255b7c6916b4cd069e6e807d952aae314e713d8?ds=sidebyside Hands --- 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()); + } }