Write some tests - fixed 2 errors in the only 2 "real" lines of code I wrote...
This commit is contained in:
@@ -109,7 +109,7 @@ impl fmt::Display for Card {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug, PartialEq)]
|
||||||
pub enum StackingError {
|
pub enum StackingError {
|
||||||
#[error("Trying to stack the same coloured suit")]
|
#[error("Trying to stack the same coloured suit")]
|
||||||
SameColour,
|
SameColour,
|
||||||
@@ -120,12 +120,12 @@ pub enum StackingError {
|
|||||||
impl Card {
|
impl Card {
|
||||||
pub fn can_be_placed_on_top(&self, top: Card) -> Result<(), StackingError> {
|
pub fn can_be_placed_on_top(&self, top: Card) -> Result<(), StackingError> {
|
||||||
// Can't be the same Colour
|
// Can't be the same Colour
|
||||||
if self.suit.colour() != top.suit.colour() {
|
if self.suit.colour() == top.suit.colour() {
|
||||||
return Err(StackingError::SameColour);
|
return Err(StackingError::SameColour);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Needs to be adjascent
|
// Needs to be adjacent
|
||||||
if self.value == Value::King || self.value.indexed_values() != top.value.indexed_values() + 1 {
|
if self.value == Value::King || self.value.indexed_values() + 1 != top.value.indexed_values() {
|
||||||
return Err(StackingError::NotAdjacent(self.to_string(), top.to_string()));
|
return Err(StackingError::NotAdjacent(self.to_string(), top.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +139,38 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn basic_card_stacking() {
|
||||||
let result = add(2, 2);
|
let testing_card = Card {
|
||||||
assert_eq!(result, 4);
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user