diff --git a/card_stuffs/src/lib.rs b/card_stuffs/src/lib.rs index a20896f..2fd07e3 100644 --- a/card_stuffs/src/lib.rs +++ b/card_stuffs/src/lib.rs @@ -109,7 +109,7 @@ impl fmt::Display for Card { } } -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum StackingError { #[error("Trying to stack the same coloured suit")] SameColour, @@ -120,12 +120,12 @@ pub enum StackingError { impl Card { pub fn can_be_placed_on_top(&self, top: Card) -> Result<(), StackingError> { // Can't be the same Colour - if self.suit.colour() != top.suit.colour() { + if self.suit.colour() == top.suit.colour() { return Err(StackingError::SameColour); } - // Needs to be adjascent - if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 { + // Needs to be adjacent + if self.value == Value::King || self.value.indexed_values() + 1 != top.value.indexed_values() { return Err(StackingError::NotAdjacent(self.to_string(), top.to_string())); } @@ -139,8 +139,38 @@ mod tests { use super::*; #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn basic_card_stacking() { + let testing_card = Card { + suit: Suit::Heart, + value: Value::Five, + }; + let bad_same_suit = Card { + suit: Suit::Heart, + value: Value::Six, + }; + assert_eq!(testing_card.can_be_placed_on_top(bad_same_suit), Err(StackingError::SameColour)); + let bad_same_colour = Card { + suit: Suit::Diamond, + value: Value::Six, + }; + assert_eq!(testing_card.can_be_placed_on_top(bad_same_colour), Err(StackingError::SameColour)); + let should_stack_card = Card { + suit: Suit::Club, + value: Value::Six, + }; + assert_eq!(testing_card.can_be_placed_on_top(should_stack_card), Ok(())); + let value_too_high = Card { + suit: Suit::Club, + value: Value::Seven, + }; + let not_adj_error = testing_card.can_be_placed_on_top(value_too_high); + if let Err(e) = not_adj_error { + match e { + StackingError::NotAdjacent(_, _) => assert!(true), + StackingError::SameColour => assert!(false, "Colour is different - incorrect error"), + } + } else { + assert!(false, "Cards are not adjacent - should be an error") + } } }