From 4d5b75ea82770e23c9d1f02b892dd9c115b9f7c8 Mon Sep 17 00:00:00 2001 From: mux Date: Sun, 19 Jan 2025 02:23:45 +0100 Subject: [PATCH] added tests, bug in say uno check --- src/uno_logic.rs | 81 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/src/uno_logic.rs b/src/uno_logic.rs index e2bc9cd..c7ae956 100644 --- a/src/uno_logic.rs +++ b/src/uno_logic.rs @@ -39,6 +39,7 @@ pub enum CardColor { YELLOW, } +#[derive(Debug)] pub enum Direction { CLOCKWISE, COUNTER_CLOCKWISE, @@ -528,13 +529,13 @@ impl GameState<'_, S> where S: Read + Write { ] { if ![CardValue::CHANGE_COLOR, CardValue::PLUS_FOUR, CardValue::ZERO].contains(&value) { for _ in 0..2 { - deck.push(Card { value: value, color: color }); + deck.push(Card { value, color }); } } 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 { 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{ - let card_set = get_card_set(); - for card in &card_set{ - if card_value == card.value && card_color == card.color { - return card.to_owned(); - } + Card { + color : card_color, + value: card_value, } - 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> { @@ -775,7 +771,10 @@ mod tests { #[test] 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] @@ -833,21 +832,69 @@ mod tests { #[test] 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] - 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] - 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] 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(){ } }