Add: GameControllerTest coverage for totem choice and player reconnection
This commit is contained in:
@@ -5,6 +5,8 @@ import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||
import it.polimi.ingsw.gc14.Model.Player;
|
||||
import it.polimi.ingsw.gc14.Model.Totems;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
@@ -15,6 +17,30 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class GameControllerTest {
|
||||
|
||||
private void completeTotemChoice(Game game, GameController controller) {
|
||||
assertEquals(GameStages.TOTEM_CHOICE, game.getCurrentState().getGameStage());
|
||||
|
||||
while (game.getCurrentState().getGameStage() == GameStages.TOTEM_CHOICE) {
|
||||
Player current = game.getCurrentState().getCurrentPlayer();
|
||||
assertNotNull(current);
|
||||
|
||||
List<Totems> availableTotems = game.getAvailableTotems();
|
||||
assertFalse(availableTotems.isEmpty());
|
||||
|
||||
Totems selectedTotem = availableTotems.get(0);
|
||||
|
||||
assertTrue(controller.TotemChoice(
|
||||
current.getUserName(),
|
||||
selectedTotem.name()
|
||||
));
|
||||
|
||||
assertEquals(selectedTotem, current.totem);
|
||||
}
|
||||
|
||||
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
|
||||
assertNotNull(game.getCurrentState().getCurrentPlayer());
|
||||
}
|
||||
|
||||
private Game createStartedGame() {
|
||||
Game game = new Game(3);
|
||||
GameController controller = new GameController(game);
|
||||
@@ -23,6 +49,8 @@ class GameControllerTest {
|
||||
assertTrue(controller.addPlayer("Marco"));
|
||||
assertTrue(controller.addPlayer("Luca"));
|
||||
|
||||
completeTotemChoice(game, controller);
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
@@ -113,12 +141,17 @@ class GameControllerTest {
|
||||
|
||||
if (!game.getLowerListBuilding().isEmpty()) {
|
||||
current.addFood(100);
|
||||
assertTrue(controller.drawLowerBuildingCard(username, 0));
|
||||
return;
|
||||
|
||||
if (controller.drawLowerBuildingCard(username, 0)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(controller.SkipNoDrawable(username));
|
||||
return;
|
||||
if (game.getCurrentState().getNUpper() == 0
|
||||
|| game.getUpperListTribeCards().stream().allMatch(TribeCard::IsEventCard)) {
|
||||
assertTrue(controller.SkipNoDrawable(username));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (game.getCurrentState().getNUpper() > 0) {
|
||||
@@ -131,12 +164,17 @@ class GameControllerTest {
|
||||
|
||||
if (!game.getUpperListBuilding().isEmpty()) {
|
||||
current.addFood(100);
|
||||
assertTrue(controller.drawUpperBuildingCard(username, 0));
|
||||
return;
|
||||
|
||||
if (controller.drawUpperBuildingCard(username, 0)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(controller.SkipNoDrawable(username));
|
||||
return;
|
||||
if (game.getCurrentState().getNLower() == 0
|
||||
|| game.getLowerListTribeCards().stream().allMatch(TribeCard::IsEventCard)) {
|
||||
assertTrue(controller.SkipNoDrawable(username));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fail("Current player has no remaining draw actions.");
|
||||
@@ -173,10 +211,74 @@ class GameControllerTest {
|
||||
assertNotNull(game.getPlayerByUsername("Marco"));
|
||||
assertNotNull(game.getPlayerByUsername("Luca"));
|
||||
|
||||
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
|
||||
assertEquals(GameStages.TOTEM_CHOICE, game.getCurrentState().getGameStage());
|
||||
assertFalse(controller.addPlayer("Extra"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void totemChoiceShouldAssignTotemsAndStartSlotChoice() {
|
||||
Game game = new Game(3);
|
||||
GameController controller = new GameController(game);
|
||||
|
||||
assertTrue(controller.addPlayer("Giorgio"));
|
||||
assertTrue(controller.addPlayer("Marco"));
|
||||
assertTrue(controller.addPlayer("Luca"));
|
||||
|
||||
completeTotemChoice(game, controller);
|
||||
|
||||
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
|
||||
|
||||
assertNotNull(game.getPlayerByUsername("Giorgio").totem);
|
||||
assertNotNull(game.getPlayerByUsername("Marco").totem);
|
||||
assertNotNull(game.getPlayerByUsername("Luca").totem);
|
||||
}
|
||||
|
||||
@Test
|
||||
void totemChoiceShouldReturnFalseForWrongPlayer() {
|
||||
Game game = new Game(3);
|
||||
GameController controller = new GameController(game);
|
||||
|
||||
assertTrue(controller.addPlayer("Giorgio"));
|
||||
assertTrue(controller.addPlayer("Marco"));
|
||||
assertTrue(controller.addPlayer("Luca"));
|
||||
|
||||
Player current = game.getCurrentState().getCurrentPlayer();
|
||||
assertNotNull(current);
|
||||
|
||||
String wrongUsername =
|
||||
current.getUserName().equals("Giorgio") ? "Marco" : "Giorgio";
|
||||
|
||||
Totems selectedTotem = game.getAvailableTotems().get(0);
|
||||
|
||||
assertFalse(controller.TotemChoice(wrongUsername, selectedTotem.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disconnectedPlayerShouldDelegateToModel() {
|
||||
Game game = createStartedGame();
|
||||
GameController controller = new GameController(game);
|
||||
|
||||
Player current = game.getCurrentState().getCurrentPlayer();
|
||||
assertNotNull(current);
|
||||
|
||||
assertTrue(controller.DisconnectedPlayer(current.getUserName()));
|
||||
|
||||
assertTrue(game.disconnetedPlayers.containsKey(current));
|
||||
assertTrue(game.disconnetedPlayers.get(current));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconnectPlayerShouldDelegateToModel() {
|
||||
Game game = createStartedGame();
|
||||
GameController controller = new GameController(game);
|
||||
|
||||
Player current = game.getCurrentState().getCurrentPlayer();
|
||||
assertNotNull(current);
|
||||
|
||||
assertTrue(controller.DisconnectedPlayer(current.getUserName()));
|
||||
assertTrue(controller.ReconnectPlayer(current.getUserName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allMethodsShouldReturnFalseForUnknownUsername() {
|
||||
Game game = new Game(3);
|
||||
@@ -191,6 +293,9 @@ class GameControllerTest {
|
||||
assertFalse(controller.pickOptionalTribeCard("ghost", 0));
|
||||
assertFalse(controller.pickOptionalBuildingCard("ghost", 0));
|
||||
assertFalse(controller.noOptionalCard("ghost"));
|
||||
assertFalse(controller.TotemChoice("ghost", Totems.values()[0].name()));
|
||||
assertFalse(controller.DisconnectedPlayer("ghost"));
|
||||
assertFalse(controller.ReconnectPlayer("ghost"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -310,6 +415,8 @@ class GameControllerTest {
|
||||
assertFalse(controller.addPlayer("p2"));
|
||||
assertTrue(controller.addPlayer("p3"));
|
||||
|
||||
completeTotemChoice(game, controller);
|
||||
|
||||
Queue<Player> players = new LinkedList<>();
|
||||
|
||||
for (int i = 0; i < game.getNPlayers(); i++) {
|
||||
|
||||
Reference in New Issue
Block a user