Fix: GameTest

This commit is contained in:
MatteoPellegrino05
2026-04-30 17:07:27 +02:00
parent dfb2a51816
commit fbc77533e3
@@ -1,6 +1,5 @@
package it.polimi.ingsw.gc14.Model; package it.polimi.ingsw.gc14.Model;
import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard; import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character; import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
@@ -14,6 +13,7 @@ import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@Timeout(value = 10, unit = TimeUnit.SECONDS)
class GameTest { class GameTest {
@@ -118,84 +118,6 @@ class GameTest {
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage()); assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
} }
private void setCurrentStateEra(Game game, int era) {
try {
java.lang.reflect.Field field = game.getCurrentState().getClass().getDeclaredField("Era");
field.setAccessible(true);
field.set(game.getCurrentState(), era);
} catch (Exception e) {
fail("Failed to set CurrentState era: " + e.getMessage());
}
}
private static class FinalTestBuildingCard extends BuildingCard {
FinalTestBuildingCard() {
super(12, 1, 1, 1);
}
@Override
public EffectType getEffectType() {
return EffectType.FINAL;
}
@Override
public void applyEffect(Player player) {
player.addPrestige(10);
}
}
private int slotIndexById(Game game, char slotId) {
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
List<Slot> slots = board.getSlotList();
for (int i = 0; i < slots.size(); i++) {
if (slots.get(i).getSlotId() == slotId) {
return i;
}
}
fail("No slot found with id: " + slotId);
return -1;
}
private Player completeSlotChoiceAndAdvanceToPlayerOnSlot(Game game, char slotId) {
Player targetPlayer = game.getCurrentState().getCurrentPlayer();
int targetSlotIndex = slotIndexById(game, slotId);
assertTrue(game.SlotChoiceByIndex(targetPlayer, targetSlotIndex));
Set<Integer> usedSlots = new HashSet<>();
usedSlots.add(targetSlotIndex);
int nextSlotIndex = 0;
while (game.getCurrentState().getGameStage() == GameStages.SLOT_CHOICE) {
while (usedSlots.contains(nextSlotIndex)) {
nextSlotIndex++;
}
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
assertTrue(game.SlotChoiceByIndex(current, nextSlotIndex));
usedSlots.add(nextSlotIndex);
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
&& !targetPlayer.equals(game.getCurrentState().getCurrentPlayer())) {
resolveOneMandatoryAction(game);
}
assertEquals(targetPlayer, game.getCurrentState().getCurrentPlayer());
return targetPlayer;
}
private void resolveOptionalPhaseIfPresent(Game game) { private void resolveOptionalPhaseIfPresent(Game game) {
while (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT) { while (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT) {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
@@ -571,27 +493,6 @@ class GameTest {
return -1; return -1;
} }
private it.polimi.ingsw.gc14.Model.GamePackage.Board getBoard(Game game) {
try {
java.lang.reflect.Field field = Game.class.getDeclaredField("board");
field.setAccessible(true);
return (it.polimi.ingsw.gc14.Model.GamePackage.Board) field.get(game);
} catch (Exception e) {
fail("Failed to access board field: " + e.getMessage());
return null;
}
}
private void invokePrivateMethod(Game game, String methodName) {
try {
java.lang.reflect.Method method = Game.class.getDeclaredMethod(methodName);
method.setAccessible(true);
method.invoke(game);
} catch (Exception e) {
fail("Failed to invoke private method " + methodName + ": " + e.getMessage());
}
}
@Test @Test
void getCurrentPlayerNumberShouldTrackAddedPlayers() { void getCurrentPlayerNumberShouldTrackAddedPlayers() {
Game game = new Game(3); Game game = new Game(3);
@@ -677,32 +578,6 @@ class GameTest {
} }
} }
@Test
void drawLowerBuildingCardShouldWorkWhenLowerBuildingExists() {
Game game = new Game(3);
addPlayers(game, 3, "lower_building_");
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'D');
assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.lowerListBuilding.clear();
board.lowerListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100);
int foodBefore = current.getFoodValue();
int buildingsBefore = current.buildingCards.size();
assertTrue(game.DrawLowerBuildingCardByIndex(current, 0));
assertTrue(current.getFoodValue() < foodBefore);
assertEquals(buildingsBefore + 1, current.buildingCards.size());
assertEquals(1, game.getCurrentState().getNLower());
}
@Test @Test
void optionalMethodsShouldReturnFalseOutsideOptionalState() { void optionalMethodsShouldReturnFalseOutsideOptionalState() {
@@ -746,191 +621,6 @@ class GameTest {
} }
@Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void pickOptionalBuildingCardShouldReturnFalseIfPlayerCannotPay() {
Game game = new Game(3);
List<Player> players = addPlayers(game, 3, "optional_no_food_");
giveOptionalEffectToAllPlayers(players.get(0), players.get(1), players.get(2));
completeSlotChoice(game);
resolveActionsUntilOptionalCardEffect(game);
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
while (current.getFoodValue() > 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
void eventResolutionIgnoredOutsideItsStage() {
Game game = new Game(3);
assertEquals(GameStages.WAITING, game.getCurrentState().getGameStage());
invokePrivateMethod(game, "EventResolution");
assertEquals(GameStages.WAITING, game.getCurrentState().getGameStage());
}
@Test
void nextRoundIncreasesRound() {
Game game = new Game(3);
int roundBefore = game.getCurrentState().getRound();
invokePrivateMethod(game, "nextRound");
assertEquals(roundBefore + 1, game.getCurrentState().getRound());
}
@Test
void addObserverAndNotifyObserversShouldCallObserver() {
Game game = new Game(3);
final boolean[] notified = {false};
game.addObserver(updatedGame -> {
assertSame(game, updatedGame);
notified[0] = true;
});
invokePrivateMethod(game, "notifyObservers");
assertTrue(notified[0]);
}
@Test
void drawLowerTribeCardShouldReturnFalseWhenNoLowerDrawsAreAvailable() {
Game game = new Game(3);
addPlayers(game, 3, "no_lower_");
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'C');
assertNotNull(current);
int index = firstNonEventIndex(game.getLowerListTribeCards());
assertEquals(0, game.getCurrentState().getNLower());
assertFalse(game.DrawLowerTribeCardByIndex(current, index));
}
@Test
void drawLowerTribeCardShouldReturnFalseForEventCard() {
Game game = new Game(3);
addPlayers(game, 3, "lower_event_");
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'B');
assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.lowerListTribe.add(0,
new it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance(1, 3));
assertTrue(board.lowerListTribe.get(0).IsEventCard());
assertFalse(game.DrawLowerTribeCardByIndex(current, 0));
}
@Test
void drawUpperBuildingCardShouldReturnFalseWhenNoUpperDrawsAreAvailable() {
Game game = new Game(3);
addPlayers(game, 3, "no_upper_building_");
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'B');
assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.upperListBuilding.clear();
board.upperListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100);
assertEquals(0, game.getCurrentState().getNUpper());
assertFalse(game.DrawUpperBuildingCardByIndex(current, 0));
}
@Test
void drawUpperBuildingCardShouldBuyBuildingAndDecreaseUpperDraws() {
Game game = new Game(3);
addPlayers(game, 3, "upper_buy_true_");
Player current = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'F');
assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game);
assertNotNull(board);
board.upperListBuilding.clear();
board.upperListBuilding.add(new BuildingCard(12, 1, 1, 1));
current.addFood(100);
int foodBefore = current.getFoodValue();
int buildingsBefore = current.buildingCards.size();
assertTrue(game.DrawUpperBuildingCardByIndex(current, 0));
assertTrue(current.getFoodValue() < foodBefore);
assertEquals(buildingsBefore + 1, current.buildingCards.size());
assertEquals(1, game.getCurrentState().getNUpper());
}
@Test
void nextRoundUpdatesEra() {
Game game = new Game(3);
setCurrentStateEra(game, 0);
assertEquals(0, game.getCurrentState().getEra());
invokePrivateMethod(game, "nextRound");
assertEquals(1, game.getCurrentState().getEra());
}
@Test
void endGameShouldApplyFinalBuildingEffectsAndSetEndedStage() {
Game game = new Game(3);
Player player = new Player("final_player");
Player p2 = new Player("p2");
Player p3 = new Player("p3");
assertTrue(game.addPlayer(player));
assertTrue(game.addPlayer(p2));
assertTrue(game.addPlayer(p3));
player.buildingCards.add(new FinalTestBuildingCard());
int prestigeBefore = player.getPrestigeValue();
invokePrivateMethod(game, "endGame");
assertEquals(prestigeBefore + 10, player.getPrestigeValue());
assertEquals(GameStages.ENDED, game.getCurrentState().getGameStage());
}
@Test @Test
@Timeout(value = 2, unit = TimeUnit.SECONDS) @Timeout(value = 2, unit = TimeUnit.SECONDS)
void shouldNotCrashWhenNoPlayerHasOptionalEffect() { void shouldNotCrashWhenNoPlayerHasOptionalEffect() {
@@ -970,7 +660,7 @@ class GameTest {
} }
@Test @Test
@Timeout(value = 10, unit = TimeUnit.SECONDS) @Timeout(value = 20, unit = TimeUnit.SECONDS)
void shouldCompleteFullGameThroughRealFlow() { 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);
@@ -1090,54 +780,11 @@ class GameTest {
} }
@Test @Test
@Timeout(value = 2, unit = TimeUnit.SECONDS) void pickOptionalBuildingCardShouldReturnFalseIfPlayerCannotPay() {
void fourPlayerGameShouldAllowPlayingSlotG() {
Game game = new Game(4);
addPlayers(game, 4, "slot_g_");
Player playerOnG = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'G');
assertNotNull(playerOnG);
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(playerOnG, game.getCurrentState().getCurrentPlayer());
assertEquals('G', game.getCurrentState().getSlot().getSlotId());
assertEquals(1, game.getCurrentState().getNLower());
assertEquals(2, game.getCurrentState().getNUpper());
resolveOneMandatoryAction(game);
assertEquals(0, game.getCurrentState().getNLower());
assertEquals(2, game.getCurrentState().getNUpper());
resolveOneMandatoryAction(game);
assertEquals(0, game.getCurrentState().getNLower());
assertEquals(1, game.getCurrentState().getNUpper());
resolveOneMandatoryAction(game);
assertFalse(
game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
&& playerOnG.equals(game.getCurrentState().getCurrentPlayer())
&& game.getCurrentState().getSlot().getSlotId() == 'G',
"After resolving all actions of slot G, the game must not still be resolving slot G for the same player."
);
}
@Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void pickOptionalTribeCardShouldRejectEventCard() {
Game game = new Game(3); Game game = new Game(3);
List<Player> players = addPlayers(game, 3, "no_food_");
Player p1 = new Player("p1"); giveOptionalEffectToAllPlayers(players.get(0), players.get(1), players.get(2));
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); completeSlotChoice(game);
resolveActionsUntilOptionalCardEffect(game); resolveActionsUntilOptionalCardEffect(game);
@@ -1145,43 +792,18 @@ class GameTest {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current); assertNotNull(current);
it.polimi.ingsw.gc14.Model.GamePackage.Board board = getBoard(game); while (current.getFoodValue() > 0) {
assertNotNull(board); assertTrue(current.removeFood(1));
}
board.upperListTribe.add(0, assertEquals(0, current.getFoodValue());
new it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance(1, 3));
assertTrue(board.upperListTribe.get(0).IsEventCard()); assertFalse(
assertFalse(game.PickOptionalTribeCardByIndex(current, 0)); game.getUpperListBuilding().isEmpty(),
"There must be at least one upper building card to test that the player cannot buy it."
);
assertFalse(game.PickOptionalBuildingCard(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));
}
} }