added tests, bug in say uno check

This commit is contained in:
mux 2025-01-19 02:23:45 +01:00
parent 552b09730f
commit 4d5b75ea82

View File

@ -39,6 +39,7 @@ pub enum CardColor {
YELLOW, YELLOW,
} }
#[derive(Debug)]
pub enum Direction { pub enum Direction {
CLOCKWISE, CLOCKWISE,
COUNTER_CLOCKWISE, COUNTER_CLOCKWISE,
@ -528,13 +529,13 @@ impl<S> GameState<'_, S> where S: Read + Write {
] { ] {
if ![CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR, CardValue::ZERO].contains(&value) { if ![CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR, CardValue::ZERO].contains(&value) {
for _ in 0..2 { for _ in 0..2 {
deck.push(Card { value: value, color: color }); deck.push(Card { value, color });
} }
} else if value == CardValue::ZERO{ } else if value == CardValue::ZERO{
deck.push(Card { value: value, color: color }); deck.push(Card { value, color });
} else if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&value) && color == CardColor::RED { } else if [CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR].contains(&value) && color == CardColor::RED {
for _ in 0..4 { for _ in 0..4 {
deck.push(Card { value: value, color: color }); deck.push(Card { value, color });
} }
} }
} }
@ -656,15 +657,10 @@ mod tests {
} }
fn get_card(card_value : CardValue, card_color : CardColor) -> Card{ fn get_card(card_value : CardValue, card_color : CardColor) -> Card{
let card_set = get_card_set(); Card {
for card in &card_set{ color : card_color,
if card_value == card.value && card_color == card.color { value: card_value,
return card.to_owned();
}
} }
let mut rng = rand::thread_rng();
let n: usize = rng.gen_range(0..card_set.len());
return card_set.get(n).unwrap().to_owned();
} }
fn get_test_gs<'a>() -> GameState<'a, VecDeque<u8>> { fn get_test_gs<'a>() -> GameState<'a, VecDeque<u8>> {
@ -775,7 +771,10 @@ mod tests {
#[test] #[test]
fn test_draw(){ fn test_draw(){
let mut gs = get_test_gs();
gs.game_init();
gs.draw("detlef", 5);
assert_eq!(gs.player_states.get(0).unwrap().cards.len(), 12);
} }
#[test] #[test]
@ -833,21 +832,69 @@ mod tests {
#[test] #[test]
fn test_next_turn_green_two_to_green_reverse(){ fn test_next_turn_green_two_to_green_reverse(){
let mut gs = get_test_gs();
gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::REVERSE, CardColor::GREEN));
assert_eq!(gs.next_turn(get_card(CardValue::REVERSE, CardColor::GREEN)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_card, get_card(CardValue::REVERSE, CardColor::GREEN));
assert_eq!(gs.current_direction, Direction::COUNTER_CLOCKWISE);
assert_eq!(gs.current_turn, 2);
} }
#[test] #[test]
fn test_next_turn_green_two_to_change_color(){ fn test_next_turn_green_two_to_change_color_blue(){
let mut gs = get_test_gs();
gs.game_init();
gs.current_card = get_card(CardValue::TWO, CardColor::GREEN);
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::CHANGE_COLOR, CardColor::BLUE));
assert_eq!(gs.next_turn(get_card(CardValue::CHANGE_COLOR, CardColor::BLUE)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_turn, 1);
assert_eq!(gs.current_card.color, CardColor::BLUE);
assert_eq!(gs.current_card, get_card(CardValue::CHANGE_COLOR, CardColor::BLUE));
} }
#[test] #[test]
fn test_next_turn_yellow_five_to_yello_three_last_card_no_uno(){ fn test_next_turn_yellow_five_to_yellow_three_last_card_no_uno(){
let mut gs = get_test_gs();
gs.game_init();
gs.current_card = get_card(CardValue::FIVE, CardColor::YELLOW);
gs.player_states.get_mut(0).unwrap().cards.clear();
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::THREE, CardColor::YELLOW));
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::CHANGE_COLOR, CardColor::BLUE));
assert_eq!(gs.player_states.get(0).unwrap().cards.len(), 2);
assert_eq!(gs.next_turn(get_card(CardValue::THREE, CardColor::YELLOW)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_card, get_card(CardValue::THREE, CardColor::YELLOW));
assert_eq!(gs.current_turn, 1);
//said_uno should be false by default
assert_eq!(gs.player_states.get(0).unwrap().said_uno, false);
assert_eq!(gs.player_states.get(0).unwrap().cards.len(), 2);
} }
#[test] #[test]
fn test_next_turn_yellow_five_to_yellow_three_last_card_yes_uno(){ fn test_next_turn_yellow_five_to_yellow_three_last_card_yes_uno(){
let mut gs = get_test_gs();
gs.game_init();
gs.current_card = get_card(CardValue::FIVE, CardColor::YELLOW);
gs.player_states.get_mut(0).unwrap().cards.clear();
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::THREE, CardColor::YELLOW));
gs.player_states.get_mut(0).unwrap().cards.push(get_card(CardValue::CHANGE_COLOR, CardColor::BLUE));
assert_eq!(gs.player_states.get(0).unwrap().cards.len(), 2);
gs.player_states.get_mut(0).unwrap().said_uno = true;
assert_eq!(gs.next_turn(get_card(CardValue::THREE, CardColor::YELLOW)), TURN_RESULT::NEXT_TURN);
assert_eq!(gs.current_card, get_card(CardValue::THREE, CardColor::YELLOW));
assert_eq!(gs.current_turn, 1);
assert_eq!(gs.player_states.get(0).unwrap().said_uno, true);
assert_eq!(gs.player_states.get(0).unwrap().cards.len(), 1);
}
#[test]
fn test_next_3_turns_3_plus_two(){
}
#[test]
fn test_next_turn_1_plus_two(){
} }
} }