Fix: GameTest

This commit is contained in:
MatteoPellegrino05
2026-04-30 16:08:50 +02:00
parent fbe511678b
commit dfb2a51816
@@ -55,7 +55,7 @@ class GameTest {
private void giveOptionalEffectToAllPlayers(Player... players) { private void giveOptionalEffectToAllPlayers(Player... players) {
for (Player player : players) { for (Player player : players) {
player.buildingCards.add(new BuildingCard(12, 1, 1, 0)); player.buildingCards.add(new BuildingCard(12, 1, 1, 1));
} }
} }
@@ -90,7 +90,23 @@ class GameTest {
} }
} else { } else {
fail("Current player has no drawable cards, although the game is still resolving actions."); fail(
"Current player has no drawable cards, although the game is still resolving actions.\n" +
"Current player: " + current + "\n" +
"Round: " + game.getCurrentState().getRound() + "\n" +
"Stage: " + game.getCurrentState().getGameStage() + "\n" +
"Slot: " + (game.getCurrentState().getSlot() == null
? "null"
: game.getCurrentState().getSlot().getSlotId()) + "\n" +
"NLower: " + game.getCurrentState().getNLower() + "\n" +
"NUpper: " + game.getCurrentState().getNUpper() + "\n" +
"Lower tribe size: " + game.getLowerListTribeCards().size() + "\n" +
"Upper tribe size: " + game.getUpperListTribeCards().size() + "\n" +
"Lower building size: " + game.getLowerListBuilding().size() + "\n" +
"Upper building size: " + game.getUpperListBuilding().size() + "\n" +
"First lower non-event: " + firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) + "\n" +
"First upper non-event: " + firstNonEventIndexOrMinusOne(game.getUpperListTribeCards())
);
} }
} }
@@ -116,7 +132,7 @@ class GameTest {
private static class FinalTestBuildingCard extends BuildingCard { private static class FinalTestBuildingCard extends BuildingCard {
FinalTestBuildingCard() { FinalTestBuildingCard() {
super(12, 1, 1, 0); super(12, 1, 1, 1);
} }
@Override @Override
@@ -674,7 +690,7 @@ class GameTest {
assertNotNull(board); assertNotNull(board);
board.lowerListBuilding.clear(); board.lowerListBuilding.clear();
board.lowerListBuilding.add(new BuildingCard(12, 1, 1, 0)); board.lowerListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100); current.addFood(100);
@@ -731,6 +747,7 @@ class GameTest {
} }
@Test @Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void pickOptionalBuildingCardShouldReturnFalseIfPlayerCannotPay() { void pickOptionalBuildingCardShouldReturnFalseIfPlayerCannotPay() {
Game game = new Game(3); Game game = new Game(3);
@@ -744,9 +761,18 @@ class GameTest {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current); assertNotNull(current);
if (!game.getUpperListBuilding().isEmpty()) { while (current.getFoodValue() > 0) {
assertFalse(game.PickOptionalBuildingCard(current, 0)); current.removeFood(1);
} }
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.upperListBuilding.clear();
board.upperListBuilding.add(new BuildingCard(12, 1, 1, 1));
assertEquals(0, current.getFoodValue());
assertFalse(game.PickOptionalBuildingCard(current, 0));
} }
@Test @Test
@@ -835,7 +861,7 @@ class GameTest {
assertNotNull(board); assertNotNull(board);
board.upperListBuilding.clear(); board.upperListBuilding.clear();
board.upperListBuilding.add(new BuildingCard(12, 1, 1, 0)); board.upperListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100); current.addFood(100);
@@ -856,7 +882,7 @@ class GameTest {
assertNotNull(board); assertNotNull(board);
board.upperListBuilding.clear(); board.upperListBuilding.clear();
board.upperListBuilding.add(new BuildingCard(12, 1, 1, 0)); board.upperListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100); current.addFood(100);
@@ -944,8 +970,8 @@ class GameTest {
} }
@Test @Test
@Timeout(value = 2, unit = TimeUnit.SECONDS) @Timeout(value = 10, unit = TimeUnit.SECONDS)
void shouldEndAfterTenRounds() { void shouldCompleteFullGameThroughRealFlow() {
for (int nPlayers : new int[]{2, 3, 4, 5}) { for (int nPlayers : new int[]{2, 3, 4, 5}) {
Game game = new Game(nPlayers); Game game = new Game(nPlayers);
@@ -957,54 +983,42 @@ class GameTest {
assertEquals(1, game.getCurrentState().getRound()); assertEquals(1, game.getCurrentState().getRound());
assertNotNull(game.getCurrentState().getCurrentPlayer()); assertNotNull(game.getCurrentState().getCurrentPlayer());
while (game.getCurrentState().getGameStage() != GameStages.ENDED) { for (int expectedRound = 1; expectedRound < 10; expectedRound++) {
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
int roundBefore = game.getCurrentState().getRound(); assertEquals(expectedRound, game.getCurrentState().getRound());
playOneFullRound(game); playOneFullRound(game);
if (roundBefore < 10) { assertEquals(
assertEquals( GameStages.SLOT_CHOICE,
GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage(),
game.getCurrentState().getGameStage(), "After round " + expectedRound + ", the game should return to SLOT_CHOICE."
"After a non-final round, the game must return to SLOT_CHOICE." );
);
assertEquals( assertEquals(
roundBefore + 1, expectedRound + 1,
game.getCurrentState().getRound(), game.getCurrentState().getRound(),
"The round number must increase by one after each completed round." "The round should increase after completing round " + expectedRound + "."
); );
assertNotNull( assertNotNull(game.getCurrentState().getCurrentPlayer());
game.getCurrentState().getCurrentPlayer(),
"At the beginning of the next round there must be a current player."
);
} else {
assertEquals(
GameStages.ENDED,
game.getCurrentState().getGameStage(),
"After round 10, the game must end."
);
assertEquals(
10,
game.getCurrentState().getRound(),
"The game must end at round 10."
);
}
} }
assertEquals(10, game.getCurrentState().getRound());
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
playOneFullRound(game);
assertEquals( assertEquals(
GameStages.ENDED, GameStages.ENDED,
game.getCurrentState().getGameStage(), game.getCurrentState().getGameStage(),
"The game must end after round 10 with " + nPlayers + " players." "The game should end after completing round 10 with " + nPlayers + " players."
); );
assertEquals( assertEquals(
10, 10,
game.getCurrentState().getRound(), game.getCurrentState().getRound(),
"The game must end at round 10 with " + nPlayers + " players." "The game should end at round 10 with " + nPlayers + " players."
); );
} }
} }
@@ -1031,7 +1045,7 @@ class GameTest {
List<Player> players = addPlayers(game, 3, "single_optional_"); List<Player> players = addPlayers(game, 3, "single_optional_");
players.get(0).buildingCards.add(new BuildingCard(12, 1, 1, 0)); players.get(0).buildingCards.add(new BuildingCard(12, 1, 1, 1));
int roundBefore = game.getCurrentState().getRound(); int roundBefore = game.getCurrentState().getRound();
@@ -1110,5 +1124,64 @@ class GameTest {
); );
} }
@Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void pickOptionalTribeCardShouldRejectEventCard() {
Game game = new Game(3);
Player p1 = new Player("p1");
Player p2 = new Player("p2");
Player p3 = new Player("p3");
assertTrue(game.addPlayer(p1));
assertTrue(game.addPlayer(p2));
assertTrue(game.addPlayer(p3));
giveOptionalEffectToAllPlayers(p1, p2, p3);
completeSlotChoice(game);
resolveActionsUntilOptionalCardEffect(game);
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.upperListTribe.add(0,
new it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance(1, 3));
assertTrue(board.upperListTribe.get(0).IsEventCard());
assertFalse(game.PickOptionalTribeCardByIndex(current, 0));
}
@Test
void drawLowerBuildingCardShouldRejectWrongPlayer() {
Game game = new Game(3);
Player p1 = new Player("p1");
Player p2 = new Player("p2");
Player p3 = new Player("p3");
assertTrue(game.addPlayer(p1));
assertTrue(game.addPlayer(p2));
assertTrue(game.addPlayer(p3));
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'D');
assertNotNull(current);
Player wrongPlayer = current.equals(p1) ? p2 : p1;
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.lowerListBuilding.clear();
board.lowerListBuilding.add(new BuildingCard(12, 1, 1, 1));
wrongPlayer.addFood(100);
assertFalse(game.DrawLowerBuildingCardByIndex(wrongPlayer, 0));
}
} }