Skip to content

Commit

Permalink
add: test for fix
Browse files Browse the repository at this point in the history
modify: test board to accommodate old and new tests, because salad field on 1 was a bit misplaced
  • Loading branch information
YoEnte committed Jan 20, 2025
1 parent 0c440ac commit 769ec8e
Showing 1 changed file with 85 additions and 3 deletions.
88 changes: 85 additions & 3 deletions src/plugin/test/state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod tests {
game_state::GameState,
hare::{Hare, TeamEnum},
r#move::Move,
rules_engine::RulesEngine,
};

fn create_player(
Expand All @@ -31,13 +32,15 @@ mod tests {
fn create_board() -> Board {
Board::new(vec![
Field::Start,
Field::Salad,
Field::Carrots,
Field::Position2,
Field::Hare,
Field::Carrots,
Field::Market,
Field::Position1,
Field::Market,
Field::Carrots,
Field::Hare,
Field::Hedgehog,
Field::Salad,
Field::Goal,
])
}
Expand Down Expand Up @@ -80,4 +83,83 @@ mod tests {
vec![Card::HurryAhead, Card::FallBack, Card::EatSalad]
)))));
}

#[test]
fn test_correct_carrot_update() {
let state_depth_0 = GameState::new(
create_board(),
0,
create_player(TeamEnum::One, 0, vec![], 75, 0),
create_player(TeamEnum::Two, 0, vec![], 200, 0),
None,
);

// perform all poss moves for current player ("A")
let moves_depth_0 = state_depth_0.possible_moves();
for move_depth_0 in moves_depth_0.iter() {
let depth_1 = state_depth_0.perform_move(move_depth_0);
assert!(depth_1.is_ok());

match depth_1 {
Ok(state_depth_1) => {
let moves_depth_1 = state_depth_1.possible_moves();
let ref move_first_depth_1 = moves_depth_1[0];
let ref move_last_depth_1 = moves_depth_1[moves_depth_1.len() - 1];

// performed player "A" on Pos1 or Pos2 Field -> calculate next depth (player B)
let on_pos1 = state_depth_1.board.get_field(state_depth_1.clone_other_player().position) == Some(Field::Position1);
let on_pos2 = state_depth_1.board.get_field(state_depth_1.clone_other_player().position) == Some(Field::Position2);
if on_pos1 || on_pos2 {

let moved_distance = match &move_depth_0.action {
Action::Advance(advance) => advance.distance,
_ => 0,
};

let expected_carrots = state_depth_0.clone_current_player().carrots - RulesEngine::calculates_carrots(moved_distance);

// player "A" should be missing the exact carrot amount for the distance
assert_eq!(expected_carrots, state_depth_1.clone_other_player().carrots);

// first (shortest) poss move of player "B" gets performed -> A is (with this board) in front
let depth_2_first = state_depth_1.perform_move(move_first_depth_1);
assert!(depth_2_first.is_ok());
match depth_2_first {
Ok(state_depth_2_first) => {
// "A" got the 10 ten extra carrots if on pos1 field and in front
if on_pos1 {
assert_eq!(expected_carrots + 10, state_depth_2_first.clone_current_player().carrots);
}

// no carrots should have been added to "A" if on pos2 field and in front
if on_pos2 {
assert_eq!(expected_carrots, state_depth_2_first.clone_current_player().carrots);
}
}
Err(e) => println!("Error {e}")
}

// last (farthest) poss move of player "B" gets performed -> A is (with this board) behind
let depth_2_last = state_depth_1.perform_move(move_last_depth_1);
assert!(depth_2_last.is_ok());
match depth_2_last {
Ok(state_depth_2_last) => {
// no carrots should have been added to "A" if on pos1 field and behind
if on_pos1 {
assert_eq!(expected_carrots, state_depth_2_last.clone_current_player().carrots);
}

// "A" got the 30 ten extra carrots if on pos2 field and behind
if on_pos2 {
assert_eq!(expected_carrots + 30, state_depth_2_last.clone_current_player().carrots);
}
}
Err(e) => println!("Error {e}")
}
}
},
Err(e) => println!("Error {e}")
}
}
}
}

0 comments on commit 769ec8e

Please sign in to comment.