Skip to content

Commit

Permalink
Fix salad card bug (#87)
Browse files Browse the repository at this point in the history
* fix: eat salad card usable without salads

* add: test for fix

* add: a modified logic for testing

* fix: broken test

* remove: the modifed the test logic
  • Loading branch information
YoEnte authored Jan 20, 2025
1 parent da95965 commit 3edfc91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/plugin/action/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ impl Card {
// saturating add is here unnecessary because the board is finite and never larger than usize::MAX
self.move_to_field(current, state, other.position + 1, remaining_cards)?;
}
Card::EatSalad => current.eat_salad(state)?,
Card::EatSalad => {
if current.salads == 0 {
return Err(HUIError::new_err(
"You can only play this card if you have lettuce left",
));
}

current.eat_salad(state)?
}
Card::SwapCarrots => {
if current.position >= PluginConstants::LAST_LETTUCE_POSITION
|| other.position >= PluginConstants::LAST_LETTUCE_POSITION
Expand Down
12 changes: 12 additions & 0 deletions src/plugin/test/card_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,16 @@ mod tests {
let result = invalid_card.perform(&mut state, vec![Card::EatSalad, Card::SwapCarrots]);
assert!(result.is_err());
}

#[test]
fn test_no_salad_but_salad_card() {
let mut state = create_test_game_state();
let card = Card::EatSalad;
let mut current_player = state.clone_current_player();
current_player.salads = 0;
current_player.cards = vec![card];
state.update_player(current_player);
let result = card.perform(&mut state, vec![]);
assert!(result.is_err());
}
}

0 comments on commit 3edfc91

Please sign in to comment.