Skip to content

Commit 3edfc91

Browse files
authored
Fix salad card bug (#87)
* 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
1 parent da95965 commit 3edfc91

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/plugin/action/card.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ impl Card {
7979
// saturating add is here unnecessary because the board is finite and never larger than usize::MAX
8080
self.move_to_field(current, state, other.position + 1, remaining_cards)?;
8181
}
82-
Card::EatSalad => current.eat_salad(state)?,
82+
Card::EatSalad => {
83+
if current.salads == 0 {
84+
return Err(HUIError::new_err(
85+
"You can only play this card if you have lettuce left",
86+
));
87+
}
88+
89+
current.eat_salad(state)?
90+
}
8391
Card::SwapCarrots => {
8492
if current.position >= PluginConstants::LAST_LETTUCE_POSITION
8593
|| other.position >= PluginConstants::LAST_LETTUCE_POSITION

src/plugin/test/card_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,16 @@ mod tests {
147147
let result = invalid_card.perform(&mut state, vec![Card::EatSalad, Card::SwapCarrots]);
148148
assert!(result.is_err());
149149
}
150+
151+
#[test]
152+
fn test_no_salad_but_salad_card() {
153+
let mut state = create_test_game_state();
154+
let card = Card::EatSalad;
155+
let mut current_player = state.clone_current_player();
156+
current_player.salads = 0;
157+
current_player.cards = vec![card];
158+
state.update_player(current_player);
159+
let result = card.perform(&mut state, vec![]);
160+
assert!(result.is_err());
161+
}
150162
}

0 commit comments

Comments
 (0)