diff options
Diffstat (limited to 'game-engine/src/test/java')
35 files changed, 4079 insertions, 0 deletions
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java new file mode 100644 index 00000000..e504126d --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java @@ -0,0 +1,116 @@ +package org.luxons.sevenwonders.game; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.luxons.sevenwonders.game.api.CustomizableSettings; +import org.luxons.sevenwonders.game.api.HandCard; +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.api.PlayerTurnInfo; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.data.GameDefinitionLoader; +import org.luxons.sevenwonders.game.moves.Move; +import org.luxons.sevenwonders.game.moves.MoveType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +public class GameTest { + + @Test + public void testFullGame() { + int nbPlayers = 5; + Game game = createGame(nbPlayers); + + for (int age = 1; age <= 3; age++) { + playAge(nbPlayers, game, age); + } + } + + private void playAge(int nbPlayers, Game game, int age) { + for (int i = 0; i < 6; i++) { + playTurn(nbPlayers, game, age, 7 - i); + } + } + + private static Game createGame(int nbPlayers) { + CustomizableSettings settings = TestUtils.createCustomizableSettings(); + return new GameDefinitionLoader().getGameDefinition().initGame(0, settings, nbPlayers); + } + + private static void playTurn(int nbPlayers, Game game, int ageToCheck, int handSize) { + Collection<PlayerTurnInfo> turnInfos = game.getCurrentTurnInfo(); + assertEquals(nbPlayers, turnInfos.size()); + + Map<Integer, PlayerMove> sentMoves = new HashMap<>(turnInfos.size()); + for (PlayerTurnInfo turnInfo : turnInfos) { + assertEquals(ageToCheck, turnInfo.getCurrentAge()); + assertEquals(handSize, turnInfo.getHand().size()); + PlayerMove move = getFirstAvailableMove(turnInfo); + if (move != null) { + game.prepareMove(turnInfo.getPlayerIndex(), move); + sentMoves.put(turnInfo.getPlayerIndex(), move); + } + } + assertTrue(game.allPlayersPreparedTheirMove()); + Table table = game.playTurn(); + checkLastPlayedMoves(sentMoves, table); + } + + private static void checkLastPlayedMoves(Map<Integer, PlayerMove> sentMoves, Table table) { + for (Move move : table.getLastPlayedMoves()) { + PlayerMove sentMove = sentMoves.get(move.getPlayerIndex()); + assertNotNull(sentMove); + assertNotNull(move.getCard()); + assertEquals(sentMove.getCardName(), move.getCard().getName()); + assertSame(sentMove.getType(), move.getType()); + } + } + + private static PlayerMove getFirstAvailableMove(PlayerTurnInfo turnInfo) { + switch (turnInfo.getAction()) { + case PLAY: + case PLAY_2: + case PLAY_LAST: + return createPlayCardMove(turnInfo); + case PICK_NEIGHBOR_GUILD: + return createPickGuildMove(turnInfo); + case WAIT: + default: + return null; + } + } + + private static PlayerMove createPlayCardMove(PlayerTurnInfo turnInfo) { + for (HandCard handCard : turnInfo.getHand()) { + if (handCard.isFree()) { + return createMove(handCard, MoveType.PLAY); + } + } + return createMove(turnInfo.getHand().get(0), MoveType.DISCARD); + } + + private static PlayerMove createPickGuildMove(PlayerTurnInfo turnInfo) { + List<Card> neighbourGuilds = turnInfo.getNeighbourGuildCards(); + assertNotNull(neighbourGuilds); + String cardName = neighbourGuilds.isEmpty() ? null : neighbourGuilds.get(0).getName(); + PlayerMove move = new PlayerMove(); + move.setCardName(cardName); + move.setType(MoveType.COPY_GUILD); + return move; + } + + private static PlayerMove createMove(HandCard handCard, MoveType type) { + PlayerMove move = new PlayerMove(); + move.setCardName(handCard.getCard().getName()); + move.setType(type); + return move; + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/api/TableTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/api/TableTest.java new file mode 100644 index 00000000..71dbeb89 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/api/TableTest.java @@ -0,0 +1,49 @@ +package org.luxons.sevenwonders.game.api; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class TableTest { + + @DataPoints + public static int[] nbPlayers() { + return new int[] {2, 3, 4, 5, 6, 7, 8}; + } + + @Theory + public void getBoard_wrapLeft(int nbPlayers) { + assumeTrue(nbPlayers >= 2); + Table table = TestUtils.createTable(nbPlayers); + int last = nbPlayers - 1; + assertEquals(table.getBoard(last), table.getBoard(0, RelativeBoardPosition.LEFT)); + assertEquals(table.getBoard(0), table.getBoard(0, RelativeBoardPosition.SELF)); + assertEquals(table.getBoard(1), table.getBoard(0, RelativeBoardPosition.RIGHT)); + } + + @Theory + public void getBoard_wrapRight(int nbPlayers) { + assumeTrue(nbPlayers >= 2); + Table table = TestUtils.createTable(nbPlayers); + int last = nbPlayers - 1; + assertEquals(table.getBoard(last - 1), table.getBoard(last, RelativeBoardPosition.LEFT)); + assertEquals(table.getBoard(last), table.getBoard(last, RelativeBoardPosition.SELF)); + assertEquals(table.getBoard(0), table.getBoard(last, RelativeBoardPosition.RIGHT)); + } + + @Theory + public void getBoard_noWrap(int nbPlayers) { + assumeTrue(nbPlayers >= 3); + Table table = TestUtils.createTable(nbPlayers); + assertEquals(table.getBoard(0), table.getBoard(1, RelativeBoardPosition.LEFT)); + assertEquals(table.getBoard(1), table.getBoard(1, RelativeBoardPosition.SELF)); + assertEquals(table.getBoard(2), table.getBoard(1, RelativeBoardPosition.RIGHT)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java new file mode 100644 index 00000000..c54ff0b2 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -0,0 +1,214 @@ +package org.luxons.sevenwonders.game.boards; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board.InsufficientFundsException; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.effects.Effect; +import org.luxons.sevenwonders.game.effects.RawPointsIncrease; +import org.luxons.sevenwonders.game.effects.SpecialAbility; +import org.luxons.sevenwonders.game.effects.SpecialAbilityActivation; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; +import org.luxons.sevenwonders.game.scoring.PlayerScore; +import org.luxons.sevenwonders.game.scoring.ScoreCategory; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class BoardTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @DataPoints("gold") + public static int[] goldAmounts() { + return new int[]{-3, -1, 0, 1, 2, 3}; + } + + @DataPoints("nbCards") + public static int[] nbCards() { + return new int[]{0, 1, 2}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @DataPoints + public static Color[] colors() { + return Color.values(); + } + + @DataPoints + public static SpecialAbility[] specialAbilities() { + return SpecialAbility.values(); + } + + @Theory + public void initialGold_respectsSettings(@FromDataPoints("gold") int goldAmountInSettings) { + CustomizableSettings customSettings = TestUtils.createCustomizableSettings(); + customSettings.setInitialGold(goldAmountInSettings); + Settings settings = new Settings(5, customSettings); + Board board = new Board(TestUtils.createWonder(), 0, settings); + assertEquals(goldAmountInSettings, board.getGold()); + } + + @Theory + public void initialProduction_containsInitialResource(ResourceType type) { + Board board = new Board(TestUtils.createWonder(type), 0, new Settings(5)); + Resources resources = TestUtils.createResources(type); + assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); + } + + @Theory + public void removeGold_successfulWhenNotTooMuch(@FromDataPoints("gold") int initialGold, + @FromDataPoints("gold") int goldRemoved) { + assumeTrue(goldRemoved >= 0); + assumeTrue(initialGold >= goldRemoved); + Board board = new Board(TestUtils.createWonder(), 0, new Settings(5)); + board.setGold(initialGold); + board.removeGold(goldRemoved); + assertEquals(initialGold - goldRemoved, board.getGold()); + } + + @Theory + public void removeGold_failsWhenTooMuch(@FromDataPoints("gold") int initialGold, + @FromDataPoints("gold") int goldRemoved) { + assumeTrue(goldRemoved >= 0); + assumeTrue(initialGold < goldRemoved); + thrown.expect(InsufficientFundsException.class); + Board board = new Board(TestUtils.createWonder(), 0, new Settings(5)); + board.setGold(initialGold); + board.removeGold(goldRemoved); + } + + @Theory + public void getNbCardsOfColor_properCount_singleColor(ResourceType type, @FromDataPoints("nbCards") int nbCards, + @FromDataPoints("nbCards") int nbOtherCards, Color color) { + Board board = TestUtils.createBoard(type); + TestUtils.addCards(board, nbCards, nbOtherCards, color); + assertEquals(nbCards, board.getNbCardsOfColor(Collections.singletonList(color))); + } + + @Theory + public void getNbCardsOfColor_properCount_multiColors(ResourceType type, @FromDataPoints("nbCards") int nbCards1, + @FromDataPoints("nbCards") int nbCards2, + @FromDataPoints("nbCards") int nbOtherCards, Color color1, + Color color2) { + Board board = TestUtils.createBoard(type); + TestUtils.addCards(board, nbCards1, color1); + TestUtils.addCards(board, nbCards2, color2); + TestUtils.addCards(board, nbOtherCards, TestUtils.getDifferentColorFrom(color1, color2)); + assertEquals(nbCards1 + nbCards2, board.getNbCardsOfColor(Arrays.asList(color1, color2))); + } + + @Test + public void setCopiedGuild_succeedsOnPurpleCard() { + Board board = TestUtils.createBoard(ResourceType.CLAY); + Card card = TestUtils.createCard(Color.PURPLE); + + board.setCopiedGuild(card); + assertSame(card, board.getCopiedGuild()); + } + + @Theory + public void setCopiedGuild_failsOnNonPurpleCard(Color color) { + assumeTrue(color != Color.PURPLE); + Board board = TestUtils.createBoard(ResourceType.CLAY); + Card card = TestUtils.createCard(color); + + thrown.expect(IllegalArgumentException.class); + board.setCopiedGuild(card); + } + + @Theory + public void hasSpecial(SpecialAbility applied, SpecialAbility tested) { + Board board = TestUtils.createBoard(ResourceType.CLAY); + Table table = new Table(Collections.singletonList(board)); + SpecialAbilityActivation special = new SpecialAbilityActivation(applied); + + special.apply(table, 0); + + assertEquals(applied == tested, board.hasSpecial(tested)); + } + + @Test + public void canPlayFreeCard() { + Board board = TestUtils.createBoard(ResourceType.CLAY); + Table table = new Table(Collections.singletonList(board)); + SpecialAbilityActivation special = new SpecialAbilityActivation(SpecialAbility.ONE_FREE_PER_AGE); + + special.apply(table, 0); + + assertTrue(board.canPlayFreeCard(0)); + assertTrue(board.canPlayFreeCard(1)); + assertTrue(board.canPlayFreeCard(2)); + + board.consumeFreeCard(0); + + assertFalse(board.canPlayFreeCard(0)); + assertTrue(board.canPlayFreeCard(1)); + assertTrue(board.canPlayFreeCard(2)); + + board.consumeFreeCard(1); + + assertFalse(board.canPlayFreeCard(0)); + assertFalse(board.canPlayFreeCard(1)); + assertTrue(board.canPlayFreeCard(2)); + + board.consumeFreeCard(2); + + assertFalse(board.canPlayFreeCard(0)); + assertFalse(board.canPlayFreeCard(1)); + assertFalse(board.canPlayFreeCard(2)); + } + + @Theory + public void computePoints_gold(@FromDataPoints("gold") int gold) { + assumeTrue(gold >= 0); + Board board = TestUtils.createBoard(ResourceType.WOOD); + Table table = new Table(Collections.singletonList(board)); + board.setGold(gold); + + PlayerScore score = board.computePoints(table); + assertEquals(gold / 3, (int) score.getPoints(ScoreCategory.GOLD)); + assertEquals(gold / 3, score.getTotalPoints()); + } + + @Theory + public void computePoints_(@FromDataPoints("gold") int gold) { + assumeTrue(gold >= 0); + Board board = TestUtils.createBoard(ResourceType.WOOD); + Table table = new Table(Collections.singletonList(board)); + board.setGold(gold); + + Effect effect = new RawPointsIncrease(5); + TestUtils.playCardWithEffect(table, 0, Color.BLUE, effect); + + PlayerScore score = board.computePoints(table); + assertEquals(gold / 3, (int) score.getPoints(ScoreCategory.GOLD)); + assertEquals(5, (int) score.getPoints(ScoreCategory.CIVIL)); + assertEquals(5 + gold / 3, score.getTotalPoints()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java new file mode 100644 index 00000000..b391c6b0 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java @@ -0,0 +1,64 @@ +package org.luxons.sevenwonders.game.boards; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Rule; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.boards.Military.UnknownAgeException; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class MilitaryTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @DataPoints("points") + public static int[] points() { + return new int[] {0, 1, 3, 5}; + } + + @DataPoints("ages") + public static int[] ages() { + return new int[] {1, 2, 3}; + } + + private static Military createMilitary(int age, int nbPointsPerVictory, int nbPointsPerDefeat) { + Map<Integer, Integer> wonPointsPerAge = new HashMap<>(); + wonPointsPerAge.put(age, nbPointsPerVictory); + return new Military(nbPointsPerDefeat, wonPointsPerAge); + } + + @Theory + public void victory_addsCorrectPoints(@FromDataPoints("ages") int age, + @FromDataPoints("points") int nbPointsPerVictory) { + Military military = createMilitary(age, nbPointsPerVictory, 0); + int initialPoints = military.getTotalPoints(); + + military.victory(age); + assertEquals(initialPoints + nbPointsPerVictory, military.getTotalPoints()); + } + + @Theory + public void victory_failsIfUnknownAge(@FromDataPoints("points") int nbPointsPerVictory) { + Military military = createMilitary(0, nbPointsPerVictory, 0); + thrown.expect(UnknownAgeException.class); + military.victory(1); + } + + @Theory + public void defeat_removesCorrectPoints(@FromDataPoints("points") int nbPointsLostPerDefeat) { + Military military = createMilitary(0, 0, nbPointsLostPerDefeat); + int initialPoints = military.getTotalPoints(); + + military.defeat(); + assertEquals(initialPoints - nbPointsLostPerDefeat, military.getTotalPoints()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.java new file mode 100644 index 00000000..9f60e572 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.java @@ -0,0 +1,44 @@ +package org.luxons.sevenwonders.game.boards; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class RelativeBoardPositionTest { + + @DataPoints + public static int[] nbPlayers() { + return new int[] {1, 2, 3, 5, 7, 9}; + } + + @Theory + public void getIndexFrom_wrapLeft(int nbPlayers) { + assumeTrue(nbPlayers >= 2); + int last = nbPlayers - 1; + assertEquals(last, RelativeBoardPosition.LEFT.getIndexFrom(0, nbPlayers)); + assertEquals(0, RelativeBoardPosition.SELF.getIndexFrom(0, nbPlayers)); + assertEquals(1, RelativeBoardPosition.RIGHT.getIndexFrom(0, nbPlayers)); + } + + @Theory + public void getIndexFrom_wrapRight(int nbPlayers) { + assumeTrue(nbPlayers >= 2); + int last = nbPlayers - 1; + assertEquals(last - 1, RelativeBoardPosition.LEFT.getIndexFrom(last, nbPlayers)); + assertEquals(last, RelativeBoardPosition.SELF.getIndexFrom(last, nbPlayers)); + assertEquals(0, RelativeBoardPosition.RIGHT.getIndexFrom(last, nbPlayers)); + } + + @Theory + public void getIndexFrom_noWrap(int nbPlayers) { + assumeTrue(nbPlayers >= 3); + assertEquals(0, RelativeBoardPosition.LEFT.getIndexFrom(1, nbPlayers)); + assertEquals(1, RelativeBoardPosition.SELF.getIndexFrom(1, nbPlayers)); + assertEquals(2, RelativeBoardPosition.RIGHT.getIndexFrom(1, nbPlayers)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java new file mode 100644 index 00000000..24c63b31 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/boards/ScienceTest.java @@ -0,0 +1,101 @@ +package org.luxons.sevenwonders.game.boards; + +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class ScienceTest { + + @DataPoints + public static int[][] quantitiesWithExpectedPoints() { + // compasses, wheels, tablets, jokers, expected points + return new int[][] {{0, 0, 0, 1, 1}, {0, 0, 1, 0, 1}, {0, 0, 0, 2, 4}, {0, 0, 1, 1, 4}, {0, 0, 2, 0, 4}, + {0, 0, 0, 3, 10}, {0, 0, 1, 2, 10}, {0, 1, 1, 1, 10}, {1, 1, 1, 0, 10}, {0, 0, 0, 4, 16}, + {0, 0, 1, 3, 16}, {0, 0, 2, 2, 16}, {0, 0, 3, 1, 16}, {0, 0, 4, 0, 16}}; + } + + @DataPoints + public static int[] quantitiesDataPoints() { + return new int[] {0, 1, 3, 5, 8}; + } + + @Test + public void addAll_empty() { + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science empty = new Science(); + initial.addAll(empty); + assertEquals(3, initial.getQuantity(ScienceType.COMPASS)); + assertEquals(4, initial.getQuantity(ScienceType.WHEEL)); + assertEquals(5, initial.getQuantity(ScienceType.TABLET)); + assertEquals(1, initial.getJokers()); + } + + @Test + public void addAll_noJoker() { + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(1, 2, 3, 0); + initial.addAll(other); + assertEquals(4, initial.getQuantity(ScienceType.COMPASS)); + assertEquals(6, initial.getQuantity(ScienceType.WHEEL)); + assertEquals(8, initial.getQuantity(ScienceType.TABLET)); + assertEquals(1, initial.getJokers()); + } + + @Test + public void addAll_withJokers() { + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(0, 0, 0, 3); + initial.addAll(other); + assertEquals(3, initial.getQuantity(ScienceType.COMPASS)); + assertEquals(4, initial.getQuantity(ScienceType.WHEEL)); + assertEquals(5, initial.getQuantity(ScienceType.TABLET)); + assertEquals(4, initial.getJokers()); + } + + @Test + public void addAll_mixed() { + Science initial = TestUtils.createScience(3, 4, 5, 1); + Science other = TestUtils.createScience(1, 2, 3, 4); + initial.addAll(other); + assertEquals(4, initial.getQuantity(ScienceType.COMPASS)); + assertEquals(6, initial.getQuantity(ScienceType.WHEEL)); + assertEquals(8, initial.getQuantity(ScienceType.TABLET)); + assertEquals(5, initial.getJokers()); + } + + @Theory + public void computePoints_compassesOnly_noJoker(int compasses) { + Science science = TestUtils.createScience(compasses, 0, 0, 0); + assertEquals(compasses * compasses, science.computePoints()); + } + + @Theory + public void computePoints_wheelsOnly_noJoker(int wheels) { + Science science = TestUtils.createScience(0, wheels, 0, 0); + assertEquals(wheels * wheels, science.computePoints()); + } + + @Theory + public void computePoints_tabletsOnly_noJoker(int tablets) { + Science science = TestUtils.createScience(0, 0, tablets, 0); + assertEquals(tablets * tablets, science.computePoints()); + } + + @Theory + public void computePoints_allSameNoJoker(int eachSymbol) { + Science science = TestUtils.createScience(eachSymbol, eachSymbol, eachSymbol, 0); + assertEquals(3 * eachSymbol * eachSymbol + 7 * eachSymbol, science.computePoints()); + } + + @Theory + public void computePoints_expectation(int[] expectation) { + Science science = TestUtils.createScience(expectation[0], expectation[1], expectation[2], expectation[3]); + assertEquals(expectation[4], science.computePoints()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardBackTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardBackTest.java new file mode 100644 index 00000000..6f637f87 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardBackTest.java @@ -0,0 +1,15 @@ +package org.luxons.sevenwonders.game.cards; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CardBackTest { + + @Test + public void initializedWithImage() { + String imagePath = "whateverimage.png"; + CardBack back = new CardBack(imagePath); + assertEquals(imagePath, back.getImage()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java new file mode 100644 index 00000000..437c5f21 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -0,0 +1,109 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.effects.Effect; +import org.luxons.sevenwonders.game.effects.ProductionIncrease; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.wonders.Wonder; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.luxons.sevenwonders.game.test.TestUtils.createCard; + +public class CardTest { + + private Table table; + + private Card treeFarmCard; + + @Before + public void initBoard() { + Settings settings = new Settings(3); + + List<Board> boards = new ArrayList<>(3); + boards.add(new Board(new Wonder("TestWonder", ResourceType.WOOD), 0, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.STONE), 1, settings)); + boards.add(new Board(new Wonder("TestWonder", ResourceType.PAPYRUS), 2, settings)); + table = new Table(boards); + + Requirements treeFarmRequirements = new Requirements(); + treeFarmRequirements.setGold(1); + + ProductionIncrease treeFarmEffect = new ProductionIncrease(); + treeFarmEffect.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); + + List<Effect> effects = Collections.singletonList(treeFarmEffect); + + treeFarmCard = new Card("Tree Farm", Color.BROWN, treeFarmRequirements, effects, "", null, null); + } + + @Test + public void playCardCostingMoney() { + table.getBoard(0).setGold(3); + table.getBoard(1).setGold(3); + table.getBoard(2).setGold(3); + treeFarmCard.applyTo(table, 0, new ArrayList<>()); + assertEquals(2, table.getBoard(0).getGold()); + assertEquals(3, table.getBoard(1).getGold()); + assertEquals(3, table.getBoard(2).getGold()); + } + + @Test + public void equals_falseWhenNull() { + Card card = createCard("TestCard"); + //noinspection ObjectEqualsNull + assertFalse(card.equals(null)); + } + + @Test + public void equals_falseWhenDifferentClass() { + Card card = createCard("TestCard"); + Object object = new Object(); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(card.equals(object)); + } + + @Test + public void equals_trueWhenSame() { + Card card = createCard("TestCard"); + assertEquals(card, card); + } + + @Test + public void equals_trueWhenSameContent() { + Card card1 = createCard("TestCard"); + Card card2 = createCard("TestCard"); + assertTrue(card1.equals(card2)); + } + + @Test + public void equals_falseWhenDifferentName() { + Card card1 = createCard("TestCard1"); + Card card2 = createCard("TestCard2"); + assertFalse(card1.equals(card2)); + } + + @Test + public void hashCode_sameWhenSameContent() { + Card card1 = createCard("TestCard"); + Card card2 = createCard("TestCard"); + assertEquals(card1.hashCode(), card2.hashCode()); + } + + @Test + public void hashCode_differentWhenDifferentName() { + Card card1 = createCard("TestCard1"); + Card card2 = createCard("TestCard2"); + assertNotEquals(card1.hashCode(), card2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java new file mode 100644 index 00000000..8adeb44d --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java @@ -0,0 +1,112 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.cards.Decks.CardNotFoundException; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class DecksTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @DataPoints + public static int[] dataPoints() { + return new int[] {1, 2, 3, 5, 10}; + } + + private static Decks createDecks(int nbAges, int nbCardsPerAge) { + Map<Integer, List<Card>> cardsPerAge = new HashMap<>(); + for (int age = 1; age <= nbAges; age++) { + int firstCardNumber = (age - 1) * nbCardsPerAge; + cardsPerAge.put(age, TestUtils.createSampleCards(firstCardNumber, nbCardsPerAge)); + } + return new Decks(cardsPerAge); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsOnNullNameWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard(null); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsOnEmptyNameWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard(""); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard("Any name"); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsWhenCardIsNotFound() { + Decks decks = createDecks(3, 20); + decks.getCard("Unknown name"); + } + + @Test + public void getCard_succeedsWhenCardIsFound() { + Decks decks = createDecks(3, 20); + Card card = decks.getCard("Test Card 3"); + assertEquals("Test Card 3", card.getName()); + } + + @Test(expected = IllegalArgumentException.class) + public void deal_failsOnZeroPlayers() { + Decks decks = createDecks(3, 20); + decks.deal(1, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void deal_failsOnMissingAge() { + Decks decks = createDecks(2, 0); + decks.deal(4, 10); + } + + @Theory + public void deal_failsWhenTooFewPlayers(int nbPlayers, int nbCards) { + assumeTrue(nbCards % nbPlayers != 0); + thrown.expect(IllegalArgumentException.class); + Decks decks = createDecks(1, nbCards); + decks.deal(1, nbPlayers); + } + + @Theory + public void deal_succeedsOnZeroCards(int nbPlayers) { + Decks decks = createDecks(1, 0); + Hands hands = decks.deal(1, nbPlayers); + for (int i = 0; i < nbPlayers; i++) { + assertNotNull(hands.get(i)); + assertTrue(hands.get(i).isEmpty()); + } + } + + @Theory + public void deal_evenDistribution(int nbPlayers, int nbCardsPerPlayer) { + int nbCardsPerAge = nbPlayers * nbCardsPerPlayer; + Decks decks = createDecks(1, nbCardsPerAge); + Hands hands = decks.deal(1, nbPlayers); + for (int i = 0; i < nbPlayers; i++) { + assertEquals(nbCardsPerPlayer, hands.get(i).size()); + } + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.java new file mode 100644 index 00000000..ddd69b70 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.java @@ -0,0 +1,15 @@ +package org.luxons.sevenwonders.game.cards; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HandRotationDirectionTest { + + @Test + public void testAgesDirections() { + assertEquals(HandRotationDirection.LEFT, HandRotationDirection.forAge(1)); + assertEquals(HandRotationDirection.RIGHT, HandRotationDirection.forAge(2)); + assertEquals(HandRotationDirection.LEFT, HandRotationDirection.forAge(3)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java new file mode 100644 index 00000000..c20508e6 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java @@ -0,0 +1,143 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.HandCard; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.Hands.PlayerIndexOutOfBoundsException; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class HandsTest { + + @DataPoints("nbCardsPerPlayer") + public static int[] nbCardsPerPlayer() { + return new int[] {0, 1, 2, 3, 4, 5, 6, 7}; + } + + @DataPoints("nbPlayers") + public static int[] nbPlayers() { + return new int[] {3, 4, 5, 6, 7}; + } + + private static Hands createHands(int nbPlayers, int nbCardsPerPlayer) { + Map<Integer, List<Card>> hands = new HashMap<>(); + for (int p = 0; p < nbPlayers; p++) { + int firstCardNumber = (p - 1) * nbCardsPerPlayer; + hands.put(p, TestUtils.createSampleCards(firstCardNumber, nbCardsPerPlayer)); + } + return new Hands(hands, nbPlayers); + } + + @Test(expected = PlayerIndexOutOfBoundsException.class) + public void get_failsOnMissingPlayer() { + Hands hands = createHands(4, 7); + hands.get(5); + } + + @Test + public void get_retrievesCorrectCards() { + List<Card> hand0 = TestUtils.createSampleCards(0, 5); + List<Card> hand1 = TestUtils.createSampleCards(5, 10); + Map<Integer, List<Card>> handsMap = new HashMap<>(); + handsMap.put(0, hand0); + handsMap.put(1, hand1); + Hands hands = new Hands(handsMap, 2); + assertEquals(hand0, hands.get(0)); + assertEquals(hand1, hands.get(1)); + } + + @Theory + public void isEmpty_falseWhenAtLeast1_allSame(@FromDataPoints("nbPlayers") int nbPlayers, + @FromDataPoints("nbCardsPerPlayer") int nbCardsPerPlayer) { + assumeTrue(nbCardsPerPlayer >= 1); + Hands hands = createHands(nbPlayers, nbCardsPerPlayer); + assertFalse(hands.isEmpty()); + } + + @Theory + public void isEmpty_trueWhenAllEmpty(@FromDataPoints("nbPlayers") int nbPlayers) { + Hands hands = createHands(nbPlayers, 0); + assertTrue(hands.isEmpty()); + } + + @Theory + public void maxOneCardRemains_falseWhenAtLeast2_allSame(@FromDataPoints("nbPlayers") int nbPlayers, + @FromDataPoints("nbCardsPerPlayer") int nbCardsPerPlayer) { + assumeTrue(nbCardsPerPlayer >= 2); + Hands hands = createHands(nbPlayers, nbCardsPerPlayer); + assertFalse(hands.maxOneCardRemains()); + } + + @Theory + public void maxOneCardRemains_trueWhenAtMost1_allSame(@FromDataPoints("nbPlayers") int nbPlayers, + @FromDataPoints("nbCardsPerPlayer") int nbCardsPerPlayer) { + assumeTrue(nbCardsPerPlayer <= 1); + Hands hands = createHands(nbPlayers, nbCardsPerPlayer); + assertTrue(hands.maxOneCardRemains()); + } + + @Theory + public void maxOneCardRemains_trueWhenAtMost1_someZero(@FromDataPoints("nbPlayers") int nbPlayers) { + Hands hands = createHands(nbPlayers, 1); + hands.get(0).remove(0); + assertTrue(hands.maxOneCardRemains()); + } + + @Theory + public void gatherAndClear(@FromDataPoints("nbPlayers") int nbPlayers, + @FromDataPoints("nbCardsPerPlayer") int nbCardsPerPlayer) { + Hands hands = createHands(nbPlayers, nbCardsPerPlayer); + List<Card> remainingCards = hands.gatherAndClear(); + assertEquals(nbPlayers * nbCardsPerPlayer, remainingCards.size()); + assertTrue(hands.isEmpty()); + } + + @Test + public void rotate_movesOfCorrectOffset_right() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(HandRotationDirection.RIGHT); + assertEquals(rotated.get(1), hands.get(0)); + assertEquals(rotated.get(2), hands.get(1)); + assertEquals(rotated.get(0), hands.get(2)); + } + + @Test + public void rotate_movesOfCorrectOffset_left() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(HandRotationDirection.LEFT); + assertEquals(rotated.get(2), hands.get(0)); + assertEquals(rotated.get(0), hands.get(1)); + assertEquals(rotated.get(1), hands.get(2)); + } + + @Test + public void createHand_containsAllCards() { + List<Card> hand0 = TestUtils.createSampleCards(0, 5); + List<Card> hand1 = TestUtils.createSampleCards(5, 10); + Map<Integer, List<Card>> handsMap = new HashMap<>(); + handsMap.put(0, hand0); + handsMap.put(1, hand1); + Hands hands = new Hands(handsMap, 2); + + Table table = TestUtils.createTable(2); + List<HandCard> hand = hands.createHand(table, 0); + + for (HandCard handCard : hand) { + assertTrue(hand0.contains(handCard.getCard())); + } + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java new file mode 100644 index 00000000..b01f9002 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java @@ -0,0 +1,149 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.BoughtResources; +import org.luxons.sevenwonders.game.resources.Provider; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class RequirementsTest { + + @DataPoints + public static int[] goldAmounts() { + return new int[]{0, 1, 2, 5}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Test + public void getResources_emptyAfterInit() throws Exception { + Requirements requirements = new Requirements(); + assertTrue(requirements.getResources().isEmpty()); + } + + @Test + public void setResources_success() throws Exception { + Requirements requirements = new Requirements(); + Resources resources = new Resources(); + requirements.setResources(resources); + assertSame(resources, requirements.getResources()); + } + + @Theory + public void goldRequirement(int boardGold, int requiredGold) { + Requirements requirements = new Requirements(); + requirements.setGold(requiredGold); + + Board board = TestUtils.createBoard(ResourceType.CLAY, boardGold); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, Collections.emptyList())); + assertEquals(boardGold >= requiredGold, requirements.couldBeMetBy(table, 0)); + } + + @Theory + public void resourceRequirement_initialResource(ResourceType initialResource, ResourceType requiredResource) { + Requirements requirements = TestUtils.createRequirements(requiredResource); + + Board board = TestUtils.createBoard(initialResource, 0); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(initialResource == requiredResource, + requirements.areMetWithHelpBy(board, Collections.emptyList())); + + if (initialResource == requiredResource) { + assertTrue(requirements.couldBeMetBy(table, 0)); + } + } + + @Theory + public void resourceRequirement_ownProduction(ResourceType initialResource, ResourceType producedResource, + ResourceType requiredResource) { + assumeTrue(initialResource != requiredResource); + + Requirements requirements = TestUtils.createRequirements(requiredResource); + + Board board = TestUtils.createBoard(initialResource, 0); + board.getProduction().addFixedResource(producedResource, 1); + Table table = new Table(Collections.singletonList(board)); + + assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); + assertEquals(producedResource == requiredResource, + requirements.areMetWithHelpBy(board, Collections.emptyList())); + + if (producedResource == requiredResource) { + assertTrue(requirements.couldBeMetBy(table, 0)); + } + } + + @Theory + public void resourceRequirement_boughtResource(ResourceType initialResource, ResourceType boughtResource, + ResourceType requiredResource) { + assumeTrue(initialResource != requiredResource); + + Requirements requirements = TestUtils.createRequirements(requiredResource); + + Board board = TestUtils.createBoard(initialResource, 2); + Board neighbourBoard = TestUtils.createBoard(initialResource, 0); + neighbourBoard.getPublicProduction().addFixedResource(boughtResource, 1); + Table table = new Table(Arrays.asList(board, neighbourBoard)); + + BoughtResources resources = new BoughtResources(); + resources.setProvider(Provider.RIGHT_PLAYER); + resources.setResources(TestUtils.createResources(boughtResource)); + + assertFalse(requirements.areMetWithoutNeighboursBy(board)); + assertEquals(boughtResource == requiredResource, + requirements.areMetWithHelpBy(board, Collections.singletonList(resources))); + + if (boughtResource == requiredResource) { + assertTrue(requirements.couldBeMetBy(table, 0)); + } + } + + @Theory + public void pay_boughtResource(ResourceType initialResource, ResourceType requiredResource) { + assumeTrue(initialResource != requiredResource); + + Requirements requirements = TestUtils.createRequirements(requiredResource); + + Board board = TestUtils.createBoard(initialResource, 2); + Board neighbourBoard = TestUtils.createBoard(requiredResource, 0); + Table table = new Table(Arrays.asList(board, neighbourBoard)); + + BoughtResources boughtResources = new BoughtResources(); + boughtResources.setProvider(Provider.RIGHT_PLAYER); + boughtResources.setResources(TestUtils.createResources(requiredResource)); + + assertFalse(requirements.areMetWithoutNeighboursBy(board)); + assertTrue(requirements.areMetWithHelpBy(board, Collections.singletonList(boughtResources))); + assertTrue(requirements.couldBeMetBy(table, 0)); + + requirements.pay(table, 0, Collections.singletonList(boughtResources)); + + assertEquals(0, board.getGold()); + assertEquals(2, neighbourBoard.getGold()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java new file mode 100644 index 00000000..e678e6a5 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java @@ -0,0 +1,16 @@ +package org.luxons.sevenwonders.game.data; + +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; + +public class GameDefinitionLoaderTest { + + @Test + public void successfulLoad() { + GameDefinitionLoader loader = new GameDefinitionLoader(); + GameDefinition gameDefinition = loader.getGameDefinition(); + assertNotNull(gameDefinition); + } + +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java new file mode 100644 index 00000000..c693a2a2 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java @@ -0,0 +1,19 @@ +package org.luxons.sevenwonders.game.data; + +import org.junit.Test; +import org.luxons.sevenwonders.game.Game; +import org.luxons.sevenwonders.game.api.CustomizableSettings; + +import static org.junit.Assert.assertNotNull; + +public class GameDefinitionTest { + + @Test + public void successfulGameInit() { + GameDefinition gameDefinition = new GameDefinitionLoader().getGameDefinition(); + assertNotNull(gameDefinition); + + Game game = gameDefinition.initGame(0, new CustomizableSettings(), 7); + assertNotNull(game); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java new file mode 100644 index 00000000..0b7de3d6 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java @@ -0,0 +1,96 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import java.util.Random; + +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class WonderSidePickMethodTest { + + private Random random; + + private Random random2; + + @DataPoints + public static WonderSide[] sides() { + return WonderSide.values(); + } + + @Before + public void setUp() { + random = new Random(123); // starts with TRUE + random2 = new Random(123456); // starts with FALSE + } + + @Test + public void pick_allA() { + WonderSide side = null; + for (int i = 0; i < 10; i++) { + side = WonderSidePickMethod.ALL_A.pickSide(random, side); + assertEquals(WonderSide.A, side); + } + } + + @Test + public void pick_allB() { + WonderSide side = null; + for (int i = 0; i < 10; i++) { + side = WonderSidePickMethod.ALL_B.pickSide(random, side); + assertEquals(WonderSide.B, side); + } + } + + @Test + public void pick_eachRandom() { + WonderSide side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, null); + assertEquals(WonderSide.A, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side); + assertEquals(WonderSide.A, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random, side); + assertEquals(WonderSide.A, side); + } + + @Test + public void pick_eachRandom2() { + WonderSide side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, null); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side); + assertEquals(WonderSide.A, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side); + assertEquals(WonderSide.A, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side); + assertEquals(WonderSide.B, side); + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2, side); + assertEquals(WonderSide.B, side); + } + + @Theory + public void pick_allSameRandom_sameAsFirst(WonderSide firstSide) { + WonderSide side = firstSide; + for (int i = 0; i < 10; i++) { + side = WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random, side); + assertEquals(firstSide, side); + } + } + + @Test + public void pick_allSameRandom_firstIsRandom() { + assertEquals(WonderSide.A, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random, null)); + assertEquals(WonderSide.B, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random2, null)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java new file mode 100644 index 00000000..861d5a09 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java @@ -0,0 +1,128 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.effects.GoldIncrease; +import org.luxons.sevenwonders.game.effects.MilitaryReinforcements; +import org.luxons.sevenwonders.game.effects.ProductionIncrease; +import org.luxons.sevenwonders.game.effects.RawPointsIncrease; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class NumericEffectSerializerTest { + + private Gson gson; + + @DataPoints + public static int[] dataPoints() { + return new int[] {-2, -1, 0, 1, 2, 5}; + } + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(MilitaryReinforcements.class, new NumericEffectSerializer()) + .registerTypeAdapter(RawPointsIncrease.class, new NumericEffectSerializer()) + .registerTypeAdapter(GoldIncrease.class, new NumericEffectSerializer()) + // ProductionIncrease is not a numeric effect, it is here for negative testing purpose + .registerTypeAdapter(ProductionIncrease.class, new NumericEffectSerializer()) + .create(); + } + + @Test + public void serialize_militaryReinforcements_null() { + assertEquals("null", gson.toJson(null, MilitaryReinforcements.class)); + } + + @Test + public void serialize_rawPointsIncrease_null() { + assertEquals("null", gson.toJson(null, RawPointsIncrease.class)); + } + + @Test + public void serialize_goldIncrease_null() { + assertEquals("null", gson.toJson(null, GoldIncrease.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failOnUnknownType() { + gson.toJson(new ProductionIncrease()); + } + + @Theory + public void serialize_militaryReinforcements(int count) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(count); + assertEquals(String.valueOf(count), gson.toJson(reinforcements)); + } + + @Theory + public void serialize_rawPointsIncrease(int count) { + RawPointsIncrease points = new RawPointsIncrease(count); + assertEquals(String.valueOf(count), gson.toJson(points)); + } + + @Theory + public void serialize_goldIncrease(int count) { + GoldIncrease goldIncrease = new GoldIncrease(count); + assertEquals(String.valueOf(count), gson.toJson(goldIncrease)); + } + + @Theory + public void deserialize_militaryReinforcements(int count) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(count); + assertEquals(reinforcements, gson.fromJson(String.valueOf(count), MilitaryReinforcements.class)); + } + + @Theory + public void deserialize_rawPointsIncrease(int count) { + RawPointsIncrease points = new RawPointsIncrease(count); + assertEquals(points, gson.fromJson(String.valueOf(count), RawPointsIncrease.class)); + } + + @Theory + public void deserialize_goldIncrease(int count) { + GoldIncrease goldIncrease = new GoldIncrease(count); + assertEquals(goldIncrease, gson.fromJson(String.valueOf(count), GoldIncrease.class)); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_militaryReinforcements_failOnEmptyString() { + gson.fromJson("\"\"", MilitaryReinforcements.class); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_rawPointsIncrease_failOnEmptyString() { + gson.fromJson("\"\"", RawPointsIncrease.class); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_goldIncrease_failOnEmptyString() { + gson.fromJson("\"\"", GoldIncrease.class); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_militaryReinforcements_failOnNonNumericString() { + gson.fromJson("\"abc\"", MilitaryReinforcements.class); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_rawPointsIncrease_failOnNonNumericString() { + gson.fromJson("\"abc\"", RawPointsIncrease.class); + } + + @Test(expected = NumberFormatException.class) + public void deserialize_goldIncrease_failOnNonNumericString() { + gson.fromJson("\"abc\"", GoldIncrease.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnUnknownType() { + gson.fromJson("\"2\"", ProductionIncrease.class); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java new file mode 100644 index 00000000..8c5108ba --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java @@ -0,0 +1,189 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.effects.ProductionIncrease; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ProductionIncreaseSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + Type resourceTypeList = new TypeToken<List<ResourceType>>() { + }.getType(); + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) + .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) + .create(); + } + + private static ProductionIncrease create(boolean sellable, int wood, int stone, int clay) { + Production production = new Production(); + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood); + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone); + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay); + } + ProductionIncrease prodIncrease = new ProductionIncrease(); + prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); + return prodIncrease; + } + + private static ProductionIncrease createChoice(boolean sellable, ResourceType... types) { + Production production = new Production(); + production.addChoice(types); + ProductionIncrease prodIncrease = new ProductionIncrease(); + prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); + return prodIncrease; + } + + @Test + public void serialize_nullAsNull() { + assertEquals("null", gson.toJson(null, ProductionIncrease.class)); + } + + @Test + public void serialize_emptyProdIncreaseAsNull() { + ProductionIncrease prodIncrease = new ProductionIncrease(); + assertEquals("null", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_singleType() { + ProductionIncrease prodIncrease = create(true, 1, 0, 0); + assertEquals("\"W\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_mixedTypes() { + ProductionIncrease prodIncrease = create(true, 1, 1, 1); + assertEquals("\"WSC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_mixedTypes_notSellable() { + ProductionIncrease prodIncrease = create(false, 1, 1, 1); + assertEquals("\"(WSC)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_choice2() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_choice3() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"(W/O/C)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_choice2_unordered() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test + public void serialize_choice3_unordered() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMultipleChoices() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); + prodIncrease.getProduction().addChoice(ResourceType.ORE, ResourceType.GLASS); + gson.toJson(prodIncrease, ProductionIncrease.class); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMixedFixedAndChoices() { + ProductionIncrease prodIncrease = create(true, 1, 0, 0); + prodIncrease.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); + gson.toJson(prodIncrease, ProductionIncrease.class); + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", ProductionIncrease.class)); + } + + @Test + public void deserialize_emptyList() { + ProductionIncrease prodIncrease = new ProductionIncrease(); + assertEquals(prodIncrease, gson.fromJson("\"\"", ProductionIncrease.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageString() { + gson.fromJson("\"this is garbage\"", ProductionIncrease.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageStringWithSlashes() { + gson.fromJson("\"this/is/garbage\"", ProductionIncrease.class); + } + + @Test + public void deserialize_singleType() { + ProductionIncrease prodIncrease = create(true, 1, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"W\"", ProductionIncrease.class)); + } + + @Test + public void deserialize_multipleTimesSameType_notSellable() { + ProductionIncrease prodIncrease = create(false, 3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"(WWW)\"", ProductionIncrease.class)); + } + + @Test + public void deserialize_mixedTypes() { + ProductionIncrease prodIncrease = create(true, 1, 1, 1); + assertEquals(prodIncrease, gson.fromJson("\"WCS\"", ProductionIncrease.class)); + } + + @Test + public void deserialize_choice2() { + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); + } + + @Test + public void deserialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"(W/O/C)\"", ProductionIncrease.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnMultipleResourcesInChoice() { + gson.fromJson("\"W/SS/C\"", ProductionIncrease.class); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java new file mode 100644 index 00000000..86ee16e9 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java @@ -0,0 +1,199 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ProductionSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + Type resourceTypeList = new TypeToken<List<ResourceType>>() { + }.getType(); + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) + .create(); + } + + private static Production create(int wood, int stone, int clay) { + Production production = new Production(); + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood); + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone); + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay); + } + return production; + } + + private static Production createChoice(ResourceType... types) { + Production production = new Production(); + production.addChoice(types); + return production; + } + + @Test + public void serialize_nullAsNull() { + assertEquals("null", gson.toJson(null, Production.class)); + } + + @Test + public void serialize_emptyProdIncreaseAsNull() { + Production prodIncrease = new Production(); + assertEquals("null", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals("\"W\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals("\"WWW\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals("\"WSC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypesMultiple() { + Production prodIncrease = create(2, 1, 2); + assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMultipleChoices() { + Production production = createChoice(ResourceType.WOOD, ResourceType.CLAY); + production.addChoice(ResourceType.ORE, ResourceType.GLASS); + gson.toJson(production, Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMixedFixedAndChoices() { + Production production = create(1, 0, 0); + production.addChoice(ResourceType.WOOD, ResourceType.CLAY); + gson.toJson(production, Production.class); + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", Production.class)); + } + + @Test + public void deserialize_emptyList() { + Production prodIncrease = new Production(); + assertEquals(prodIncrease, gson.fromJson("\"\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageString() { + gson.fromJson("\"this is garbage\"", Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageStringWithSlashes() { + gson.fromJson("\"this/is/garbage\"", Production.class); + } + + @Test + public void deserialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"W\"", Production.class)); + } + + @Test + public void deserialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"WWW\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals(prodIncrease, gson.fromJson("\"WCS\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes_unordered() { + Production prodIncrease = create(1, 3, 2); + assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", Production.class)); + } + + @Test + public void deserialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test + public void deserialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnMultipleResourcesInChoice() { + gson.fromJson("\"W/SS/C\"", Production.class); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java new file mode 100644 index 00000000..3a688f4d --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java @@ -0,0 +1,51 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ResourceTypeSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()).create(); + } + + @Test + public void serialize_useSymbolForEachType() { + for (ResourceType type : ResourceType.values()) { + String expectedJson = "\"" + type.getSymbol() + "\""; + assertEquals(expectedJson, gson.toJson(type)); + } + } + + @Test + public void deserialize_useSymbolForEachType() { + for (ResourceType type : ResourceType.values()) { + String typeInJson = "\"" + type.getSymbol() + "\""; + assertEquals(type, gson.fromJson(typeInJson, ResourceType.class)); + } + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", ResourceType.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failsOnEmptyString() { + gson.fromJson("\"\"", ResourceType.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failsOnGarbageString() { + gson.fromJson("\"thisisgarbage\"", ResourceType.class); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java new file mode 100644 index 00000000..b5011fa5 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java @@ -0,0 +1,102 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ResourceTypesSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(createListTypeToken(), new ResourceTypesSerializer()).create(); + } + + private static Type createListTypeToken() { + return new TypeToken<List<ResourceType>>() { + }.getType(); + } + + @Test + public void serialize_null() { + assertEquals("null", gson.toJson(null, createListTypeToken())); + } + + @Test + public void serialize_emptyList() { + List<ResourceType> types = new ArrayList<>(); + assertEquals("\"\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_singleType() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + assertEquals("\"W\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_multipleTimesSameType() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + assertEquals("\"WWW\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void serialize_mixedTypes() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.CLAY); + types.add(ResourceType.STONE); + assertEquals("\"WCS\"", gson.toJson(types, createListTypeToken())); + } + + @Test + public void deserialize_null() { + assertNull(gson.fromJson("null", createListTypeToken())); + } + + @Test + public void deserialize_emptyList() { + List<ResourceType> types = new ArrayList<>(); + assertEquals(types, gson.fromJson("\"\"", createListTypeToken())); + } + + @Test + public void deserialize_singleType() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + assertEquals(types, gson.fromJson("\"W\"", createListTypeToken())); + } + + @Test + public void deserialize_multipleTimesSameType() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + types.add(ResourceType.WOOD); + assertEquals(types, gson.fromJson("\"WWW\"", createListTypeToken())); + } + + @Test + public void deserialize_mixedTypes() { + List<ResourceType> types = new ArrayList<>(); + types.add(ResourceType.WOOD); + types.add(ResourceType.CLAY); + types.add(ResourceType.STONE); + assertEquals(types, gson.fromJson("\"WCS\"", createListTypeToken())); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java new file mode 100644 index 00000000..e1dc4e2b --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java @@ -0,0 +1,108 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ResourcesSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()).create(); + } + + @Test + public void serialize_null() { + assertEquals("null", gson.toJson(null, Resources.class)); + } + + @Test + public void serialize_emptyResourcesToNull() { + Resources resources = new Resources(); + assertEquals("null", gson.toJson(resources)); + } + + @Test + public void serialize_singleType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals("\"W\"", gson.toJson(resources)); + } + + @Test + public void serialize_multipleTimesSameType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + assertEquals("\"WWW\"", gson.toJson(resources)); + } + + @Test + public void serialize_mixedTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 1); + assertEquals("\"WSC\"", gson.toJson(resources)); + } + + @Test + public void serialize_mixedTypes_unordered() { + Resources resources = new Resources(); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.WOOD, 2); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.STONE, 1); + assertEquals("\"WWSCC\"", gson.toJson(resources)); + } + + @Test + public void deserialize_null() { + assertNull(gson.fromJson("null", Resources.class)); + } + + @Test + public void deserialize_emptyList() { + Resources resources = new Resources(); + assertEquals(resources, gson.fromJson("\"\"", Resources.class)); + } + + @Test + public void deserialize_singleType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals(resources, gson.fromJson("\"W\"", Resources.class)); + } + + @Test + public void deserialize_multipleTimesSameType() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + assertEquals(resources, gson.fromJson("\"WWW\"", Resources.class)); + } + + @Test + public void deserialize_mixedTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.CLAY, 1); + resources.add(ResourceType.STONE, 1); + assertEquals(resources, gson.fromJson("\"WCS\"", Resources.class)); + } + + @Test + public void deserialize_mixedTypes_unordered() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + resources.add(ResourceType.CLAY, 2); + resources.add(ResourceType.STONE, 3); + assertEquals(resources, gson.fromJson("\"SCWCSS\"", Resources.class)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java new file mode 100644 index 00000000..0387e198 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java @@ -0,0 +1,145 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.effects.ScienceProgress; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class ScienceProgressSerializerTest { + + private static final String COMPASS_STR = "\"COMPASS\""; + + private static final String WHEEL_STR = "\"WHEEL\""; + + private static final String TABLET_STR = "\"TABLET\""; + + private static final String JOKER_STR = "\"any\""; + + private Gson gson; + + @Before + public void setUp() { + gson = new GsonBuilder().registerTypeAdapter(ScienceProgress.class, new ScienceProgressSerializer()).create(); + } + + @Test + public void serialize_emptyToNull() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 0); + String json = gson.toJson(progress); + assertEquals("null", json); + } + + @Test + public void serialize_oneCompass() { + ScienceProgress progress = TestUtils.createScienceProgress(1, 0, 0, 0); + String json = gson.toJson(progress); + assertEquals(COMPASS_STR, json); + } + + @Test + public void serialize_oneWheel() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 1, 0, 0); + String json = gson.toJson(progress); + assertEquals(WHEEL_STR, json); + } + + @Test + public void serialize_oneTablet() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 1, 0); + String json = gson.toJson(progress); + assertEquals(TABLET_STR, json); + } + + @Test + public void serialize_oneJoker() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 1); + String json = gson.toJson(progress); + assertEquals(JOKER_STR, json); + } + + @Test(expected = UnsupportedOperationException.class) + public void serialize_failOnMultipleCompasses() { + ScienceProgress progress = TestUtils.createScienceProgress(2, 0, 0, 0); + gson.toJson(progress); + } + + @Test(expected = UnsupportedOperationException.class) + public void serialize_failOnMultipleWheels() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 2, 0, 0); + gson.toJson(progress); + } + + @Test(expected = UnsupportedOperationException.class) + public void serialize_failOnMultipleTablets() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 2, 0); + gson.toJson(progress); + } + + @Test(expected = UnsupportedOperationException.class) + public void serialize_failOnMultipleJokers() { + ScienceProgress progress = TestUtils.createScienceProgress(0, 0, 0, 2); + gson.toJson(progress); + } + + @Test(expected = UnsupportedOperationException.class) + public void serialize_failOnMixedElements() { + ScienceProgress progress = TestUtils.createScienceProgress(1, 1, 0, 0); + gson.toJson(progress); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnEmptyString() { + gson.fromJson("\"\"", ScienceProgress.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageString() { + gson.fromJson("thisisgarbage", ScienceProgress.class); + } + + @Test + public void deserialize_compass() { + ScienceProgress progress = gson.fromJson(COMPASS_STR, ScienceProgress.class); + assertNotNull(progress.getScience()); + assertEquals(1, progress.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(0, progress.getScience().getJokers()); + } + + @Test + public void deserialize_wheel() { + ScienceProgress progress = gson.fromJson(WHEEL_STR, ScienceProgress.class); + assertNotNull(progress.getScience()); + assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(1, progress.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(0, progress.getScience().getJokers()); + } + + @Test + public void deserialize_tablet() { + ScienceProgress progress = gson.fromJson(TABLET_STR, ScienceProgress.class); + assertNotNull(progress.getScience()); + assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(1, progress.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(0, progress.getScience().getJokers()); + } + + @Test + public void deserialize_joker() { + ScienceProgress progress = gson.fromJson(JOKER_STR, ScienceProgress.class); + assertNotNull(progress.getScience()); + assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(1, progress.getScience().getJokers()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.java new file mode 100644 index 00000000..bacea896 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.java @@ -0,0 +1,142 @@ +package org.luxons.sevenwonders.game.effects; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Before; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.BoardElementType; +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; +import org.luxons.sevenwonders.game.cards.CardBack; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class BonusPerBoardElementTest { + + private Table table; + + @DataPoints + public static int[] values() { + return new int[]{0, 1, 2, 3}; + } + + @DataPoints + public static Color[] colors() { + return Color.values(); + } + + @DataPoints + public static RelativeBoardPosition[] positions() { + return RelativeBoardPosition.values(); + } + + @Before + public void setUp() { + table = TestUtils.createTable(4); + } + + private static BonusPerBoardElement createBonus(BoardElementType type, int gold, int points, Color... colors) { + BonusPerBoardElement bonus = new BonusPerBoardElement(); + bonus.setType(type); + bonus.setGold(gold); + bonus.setPoints(points); + bonus.setColors(Arrays.asList(colors)); + return bonus; + } + + @Theory + public void computePoints_countsCards(RelativeBoardPosition boardPosition, int nbCards, int nbOtherCards, + int points, int gold, Color color) { + Board board = table.getBoard(0, boardPosition); + TestUtils.addCards(board, nbCards, nbOtherCards, color); + + BonusPerBoardElement bonus = createBonus(BoardElementType.CARD, gold, points, color); + bonus.setBoards(Collections.singletonList(boardPosition)); + + assertEquals(nbCards * points, bonus.computePoints(table, 0)); + } + + @Theory + public void computePoints_countsDefeatTokens(RelativeBoardPosition boardPosition, int nbDefeatTokens, int points, + int gold) { + Board board = table.getBoard(0, boardPosition); + for (int i = 0; i < nbDefeatTokens; i++) { + board.getMilitary().defeat(); + } + + BonusPerBoardElement bonus = createBonus(BoardElementType.DEFEAT_TOKEN, gold, points); + bonus.setBoards(Collections.singletonList(boardPosition)); + + assertEquals(nbDefeatTokens * points, bonus.computePoints(table, 0)); + } + + @Theory + public void computePoints_countsWonderStages(RelativeBoardPosition boardPosition, int nbStages, int points, + int gold) { + Board board = table.getBoard(0, boardPosition); + for (int i = 0; i < nbStages; i++) { + board.getWonder().buildLevel(new CardBack("")); + } + + BonusPerBoardElement bonus = createBonus(BoardElementType.BUILT_WONDER_STAGES, gold, points); + bonus.setBoards(Collections.singletonList(boardPosition)); + + assertEquals(nbStages * points, bonus.computePoints(table, 0)); + } + + @Theory + public void apply_countsCards(RelativeBoardPosition boardPosition, int nbCards, int nbOtherCards, int points, + int gold, Color color) { + Board board = table.getBoard(0, boardPosition); + TestUtils.addCards(board, nbCards, nbOtherCards, color); + + BonusPerBoardElement bonus = createBonus(BoardElementType.CARD, gold, points, color); + bonus.setBoards(Collections.singletonList(boardPosition)); + + Board selfBoard = table.getBoard(0); + int initialGold = selfBoard.getGold(); + bonus.apply(table, 0); + assertEquals(initialGold + nbCards * gold, selfBoard.getGold()); + } + + @Theory + public void apply_countsDefeatTokens(RelativeBoardPosition boardPosition, int nbDefeatTokens, int points, + int gold) { + Board board = table.getBoard(0, boardPosition); + for (int i = 0; i < nbDefeatTokens; i++) { + board.getMilitary().defeat(); + } + + BonusPerBoardElement bonus = createBonus(BoardElementType.DEFEAT_TOKEN, gold, points); + bonus.setBoards(Collections.singletonList(boardPosition)); + + Board selfBoard = table.getBoard(0); + int initialGold = selfBoard.getGold(); + bonus.apply(table, 0); + assertEquals(initialGold + nbDefeatTokens * gold, selfBoard.getGold()); + } + + @Theory + public void apply_countsWonderStages(RelativeBoardPosition boardPosition, int nbStages, int points, int gold) { + Board board = table.getBoard(0, boardPosition); + for (int i = 0; i < nbStages; i++) { + board.getWonder().buildLevel(new CardBack("")); + } + + BonusPerBoardElement bonus = createBonus(BoardElementType.BUILT_WONDER_STAGES, gold, points); + bonus.setBoards(Collections.singletonList(boardPosition)); + + Board selfBoard = table.getBoard(0); + int initialGold = selfBoard.getGold(); + bonus.apply(table, 0); + assertEquals(initialGold + nbStages * gold, selfBoard.getGold()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java new file mode 100644 index 00000000..c6819d3a --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java @@ -0,0 +1,72 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.Assume; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.BoughtResources; +import org.luxons.sevenwonders.game.resources.Provider; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class DiscountTest { + + @DataPoints + public static int[] discountedPrices() { + return new int[] {0, 1, 2}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @DataPoints + public static Provider[] providers() { + return Provider.values(); + } + + @Theory + public void apply_givesDiscountedPrice(int discountedPrice, ResourceType discountedType, Provider provider) { + Board board = TestUtils.createBoard(ResourceType.CLAY, 3); + Discount discount = new Discount(); + discount.setDiscountedPrice(discountedPrice); + discount.getProviders().add(provider); + discount.getResourceTypes().add(discountedType); + discount.apply(board); + + BoughtResources boughtResources = TestUtils.createBoughtResources(provider, discountedType); + assertEquals(discountedPrice, board.getTradingRules().computeCost(boughtResources)); + } + + @Theory + public void apply_doesNotAffectOtherResources(int discountedPrice, ResourceType discountedType, Provider provider, + ResourceType otherType, Provider otherProvider) { + Assume.assumeTrue(otherProvider != provider); + Assume.assumeTrue(otherType != discountedType); + + Board board = TestUtils.createBoard(ResourceType.CLAY, 3); + Discount discount = new Discount(); + discount.setDiscountedPrice(discountedPrice); + discount.getProviders().add(provider); + discount.getResourceTypes().add(discountedType); + discount.apply(board); + + // this is the default in the settings used by TestUtils.createBoard() + int normalPrice = 2; + + BoughtResources fromOtherType = TestUtils.createBoughtResources(provider, otherType); + assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherType)); + + BoughtResources fromOtherProvider = TestUtils.createBoughtResources(otherProvider, discountedType); + assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProvider)); + + BoughtResources fromOtherProviderAndType = TestUtils.createBoughtResources(otherProvider, otherType); + assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProviderAndType)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java new file mode 100644 index 00000000..be124251 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.java @@ -0,0 +1,80 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Theories.class) +public class GoldIncreaseTest { + + @DataPoints + public static int[] goldAmounts() { + return new int[] {-5, -1, 0, 1, 2, 5, 10}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Theory + public void apply_increaseGoldWithRightAmount(int initialAmount, int goldIncreaseAmount, ResourceType type) { + Board board = TestUtils.createBoard(type, initialAmount); + GoldIncrease goldIncrease = new GoldIncrease(goldIncreaseAmount); + + goldIncrease.apply(board); + + assertEquals(initialAmount + goldIncreaseAmount, board.getGold()); + } + + @Theory + public void computePoints_isAlwaysZero(int gold) { + GoldIncrease goldIncrease = new GoldIncrease(gold); + Table table = TestUtils.createTable(5); + assertEquals(0, goldIncrease.computePoints(table, 0)); + } + + @Theory + public void equals_falseWhenNull(int gold) { + GoldIncrease goldIncrease = new GoldIncrease(gold); + //noinspection ObjectEqualsNull + assertFalse(goldIncrease.equals(null)); + } + + @Theory + public void equals_falseWhenDifferentClass(int gold) { + GoldIncrease goldIncrease = new GoldIncrease(gold); + MilitaryReinforcements reinforcements = new MilitaryReinforcements(gold); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(goldIncrease.equals(reinforcements)); + } + + @Theory + public void equals_trueWhenSame(int gold) { + GoldIncrease goldIncrease = new GoldIncrease(gold); + assertEquals(goldIncrease, goldIncrease); + } + + @Theory + public void equals_trueWhenSameContent(int gold) { + GoldIncrease goldIncrease1 = new GoldIncrease(gold); + GoldIncrease goldIncrease2 = new GoldIncrease(gold); + assertTrue(goldIncrease1.equals(goldIncrease2)); + } + + @Theory + public void hashCode_sameWhenSameContent(int gold) { + GoldIncrease goldIncrease1 = new GoldIncrease(gold); + GoldIncrease goldIncrease2 = new GoldIncrease(gold); + assertEquals(goldIncrease1.hashCode(), goldIncrease2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java new file mode 100644 index 00000000..478af746 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.java @@ -0,0 +1,81 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Theories.class) +public class MilitaryReinforcementsTest { + + @DataPoints + public static int[] shieldCounts() { + return new int[] {0, 1, 2, 3, 5}; + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Theory + public void apply_increaseGoldWithRightAmount(int initialShields, int additionalShields, ResourceType type) { + Board board = TestUtils.createBoard(type); + board.getMilitary().addShields(initialShields); + + MilitaryReinforcements reinforcements = new MilitaryReinforcements(additionalShields); + reinforcements.apply(board); + + assertEquals(initialShields + additionalShields, board.getMilitary().getNbShields()); + } + + @Theory + public void computePoints_isAlwaysZero(int shields) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(shields); + Table table = TestUtils.createTable(5); + assertEquals(0, reinforcements.computePoints(table, 0)); + } + + @Theory + public void equals_falseWhenNull(int shields) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(shields); + //noinspection ObjectEqualsNull + assertFalse(reinforcements.equals(null)); + } + + @Theory + public void equals_falseWhenDifferentClass(int shields) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(shields); + GoldIncrease goldIncrease = new GoldIncrease(shields); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(reinforcements.equals(goldIncrease)); + } + + @Theory + public void equals_trueWhenSame(int shields) { + MilitaryReinforcements reinforcements = new MilitaryReinforcements(shields); + assertEquals(reinforcements, reinforcements); + } + + @Theory + public void equals_trueWhenSameContent(int shields) { + MilitaryReinforcements reinforcements1 = new MilitaryReinforcements(shields); + MilitaryReinforcements reinforcements2 = new MilitaryReinforcements(shields); + assertTrue(reinforcements1.equals(reinforcements2)); + } + + @Theory + public void hashCode_sameWhenSameContent(int shields) { + MilitaryReinforcements reinforcements1 = new MilitaryReinforcements(shields); + MilitaryReinforcements reinforcements2 = new MilitaryReinforcements(shields); + assertEquals(reinforcements1.hashCode(), reinforcements2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java new file mode 100644 index 00000000..b6a47292 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java @@ -0,0 +1,109 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Theories.class) +public class ProductionIncreaseTest { + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + private static ProductionIncrease createProductionIncrease(ResourceType... types) { + ProductionIncrease effect = new ProductionIncrease(); + effect.getProduction().addAll(TestUtils.createFixedProduction(types)); + return effect; + } + + @Theory + public void apply_boardContainsAddedResourceType(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { + Board board = TestUtils.createBoard(initialType); + ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(false); + + effect.apply(board); + + Resources resources = TestUtils.createResources(initialType, addedType); + assertTrue(board.getProduction().contains(resources)); + assertFalse(board.getPublicProduction().contains(resources)); + + Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); + assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); + } + + @Theory + public void apply_boardContainsAddedResourceType_sellable(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { + Board board = TestUtils.createBoard(initialType); + ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(true); + + effect.apply(board); + + Resources resources = TestUtils.createResources(initialType, addedType); + assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); + + Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); + assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); + } + + @Theory + public void computePoints_isAlwaysZero(ResourceType addedType) { + ProductionIncrease effect = createProductionIncrease(addedType); + Table table = TestUtils.createTable(5); + assertEquals(0, effect.computePoints(table, 0)); + } + + @Theory + public void equals_falseWhenNull(ResourceType addedType) { + ProductionIncrease effect = createProductionIncrease(addedType); + //noinspection ObjectEqualsNull + assertFalse(effect.equals(null)); + } + + @Theory + public void equals_falseWhenDifferentClass(ResourceType addedType) { + ProductionIncrease effect = createProductionIncrease(addedType); + Production production = TestUtils.createFixedProduction(addedType); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(effect.equals(production)); + } + + @Theory + public void equals_trueWhenSame(ResourceType addedType) { + ProductionIncrease effect = createProductionIncrease(addedType); + assertEquals(effect, effect); + } + + @Theory + public void equals_trueWhenSameContent(ResourceType addedType) { + ProductionIncrease effect1 = createProductionIncrease(addedType); + ProductionIncrease effect2 = createProductionIncrease(addedType); + assertTrue(effect1.equals(effect2)); + } + + @Theory + public void hashCode_sameWhenSameContent(ResourceType addedType) { + ProductionIncrease effect1 = createProductionIncrease(addedType); + ProductionIncrease effect2 = createProductionIncrease(addedType); + assertEquals(effect1.hashCode(), effect2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.java new file mode 100644 index 00000000..020eda73 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.java @@ -0,0 +1,63 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(Theories.class) +public class RawPointsIncreaseTest { + + @DataPoints + public static int[] points() { + return new int[] {0, 1, 2, 3, 5}; + } + + @Theory + public void computePoints_equalsNbOfPoints(int points) { + RawPointsIncrease rawPointsIncrease = new RawPointsIncrease(points); + Table table = TestUtils.createTable(5); + assertEquals(points, rawPointsIncrease.computePoints(table, 0)); + } + + @Theory + public void equals_falseWhenNull(int points) { + RawPointsIncrease rawPointsIncrease = new RawPointsIncrease(points); + //noinspection ObjectEqualsNull + assertFalse(rawPointsIncrease.equals(null)); + } + + @Theory + public void equals_falseWhenDifferentClass(int points) { + RawPointsIncrease rawPointsIncrease = new RawPointsIncrease(points); + GoldIncrease goldIncrease = new GoldIncrease(points); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(rawPointsIncrease.equals(goldIncrease)); + } + + @Theory + public void equals_trueWhenSame(int points) { + RawPointsIncrease rawPointsIncrease = new RawPointsIncrease(points); + assertEquals(rawPointsIncrease, rawPointsIncrease); + } + + @Theory + public void equals_trueWhenSameContent(int points) { + RawPointsIncrease rawPointsIncrease1 = new RawPointsIncrease(points); + RawPointsIncrease rawPointsIncrease2 = new RawPointsIncrease(points); + assertTrue(rawPointsIncrease1.equals(rawPointsIncrease2)); + } + + @Theory + public void hashCode_sameWhenSameContent(int points) { + RawPointsIncrease rawPointsIncrease1 = new RawPointsIncrease(points); + RawPointsIncrease rawPointsIncrease2 = new RawPointsIncrease(points); + assertEquals(rawPointsIncrease1.hashCode(), rawPointsIncrease2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java new file mode 100644 index 00000000..b5402a7e --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/ScienceProgressTest.java @@ -0,0 +1,38 @@ +package org.luxons.sevenwonders.game.effects; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +@RunWith(Theories.class) +public class ScienceProgressTest { + + @DataPoints + public static int[] elementsCount() { + return new int[] {0, 1, 2}; + } + + @Theory + public void apply_initContainsAddedScience(int initCompasses, int initWheels, int initTablets, int initJokers, + int compasses, int wheels, int tablets, int jokers) { + Board board = TestUtils.createBoard(ResourceType.ORE); + Science initialScience = TestUtils.createScience(initCompasses, initWheels, initTablets, initJokers); + board.getScience().addAll(initialScience); + + ScienceProgress effect = TestUtils.createScienceProgress(compasses, wheels, tablets, jokers); + effect.apply(board); + + assertEquals(initCompasses + compasses, board.getScience().getQuantity(ScienceType.COMPASS)); + assertEquals(initWheels + wheels, board.getScience().getQuantity(ScienceType.WHEEL)); + assertEquals(initTablets + tablets, board.getScience().getQuantity(ScienceType.TABLET)); + assertEquals(initJokers + jokers, board.getScience().getJokers()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.java new file mode 100644 index 00000000..0f30a3a5 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.java @@ -0,0 +1,94 @@ +package org.luxons.sevenwonders.game.effects; + +import java.util.Arrays; + +import org.junit.Assume; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.BoardElementType; +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(Theories.class) +public class SpecialAbilityActivationTest { + + @DataPoints + public static SpecialAbility[] abilities() { + return SpecialAbility.values(); + } + + @DataPoints + public static RelativeBoardPosition[] neighbours() { + return new RelativeBoardPosition[] {RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT}; + } + + @DataPoints + public static Card[] guilds() { + BonusPerBoardElement bonus = new BonusPerBoardElement(); + bonus.setType(BoardElementType.CARD); + bonus.setColors(Arrays.asList(Color.GREY, Color.BROWN)); + bonus.setBoards(Arrays.asList(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT)); + bonus.setPoints(1); + + BonusPerBoardElement bonus2 = new BonusPerBoardElement(); + bonus2.setType(BoardElementType.BUILT_WONDER_STAGES); + bonus2.setBoards( + Arrays.asList(RelativeBoardPosition.LEFT, RelativeBoardPosition.SELF, RelativeBoardPosition.RIGHT)); + bonus2.setPoints(1); + + return new Card[] {TestUtils.createGuildCard(1, bonus), TestUtils.createGuildCard(2, bonus2)}; + } + + @Theory + public void apply_addsAbility(SpecialAbility ability) { + SpecialAbilityActivation effect = new SpecialAbilityActivation(ability); + Table table = TestUtils.createTable(5); + + effect.apply(table, 0); + + Board board = table.getBoard(0); + assertTrue(board.hasSpecial(ability)); + } + + @Theory + public void computePoints_zeroExceptForCopyGuild(SpecialAbility ability) { + Assume.assumeTrue(ability != SpecialAbility.COPY_GUILD); + + SpecialAbilityActivation effect = new SpecialAbilityActivation(ability); + Table table = TestUtils.createTable(5); + + assertEquals(0, effect.computePoints(table, 0)); + } + + @Theory + public void computePoints_copiedGuild(Card guildCard, RelativeBoardPosition neighbour) { + SpecialAbilityActivation effect = new SpecialAbilityActivation(SpecialAbility.COPY_GUILD); + Table table = TestUtils.createTable(5); + + Board neighbourBoard = table.getBoard(0, neighbour); + neighbourBoard.addCard(guildCard); + + Board board = table.getBoard(0); + board.setCopiedGuild(guildCard); + + int directPointsFromGuildCard = guildCard.getEffects().stream().mapToInt(e -> e.computePoints(table, 0)).sum(); + assertEquals(directPointsFromGuildCard, effect.computePoints(table, 0)); + } + + @Test(expected = IllegalStateException.class) + public void computePoints_copyGuild_failWhenNoChosenGuild() { + SpecialAbilityActivation effect = new SpecialAbilityActivation(SpecialAbility.COPY_GUILD); + Table table = TestUtils.createTable(5); + effect.computePoints(table, 0); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java new file mode 100644 index 00000000..e6438789 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java @@ -0,0 +1,96 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.Arrays; + +import org.junit.Test; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +public class BestPriceCalculatorTest { + + @Test + public void bestPrice_0forEmptyResources() { + Table table = TestUtils.createTable(3); + Resources resources = new Resources(); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0)); + } + + @Test + public void bestPrice_fixedResources_defaultCost() { + Board left = TestUtils.createBoard(ResourceType.STONE); + Board main = TestUtils.createBoard(ResourceType.STONE); + Board right = TestUtils.createBoard(ResourceType.WOOD); + Table table = new Table(Arrays.asList(main, right, left)); + + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0)); + assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 1)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2)); + } + + @Test + public void bestPrice_fixedResources_overridenCost() { + Board main = TestUtils.createBoard(ResourceType.STONE); + main.getTradingRules().setCost(ResourceType.WOOD, Provider.RIGHT_PLAYER, 1); + + Board left = TestUtils.createBoard(ResourceType.WOOD); + Board right = TestUtils.createBoard(ResourceType.WOOD); + Board opposite = TestUtils.createBoard(ResourceType.GLASS); + Table table = new Table(Arrays.asList(main, right, opposite, left)); + + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); + assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 3)); + } + + @Test + public void bestPrice_mixedResources_overridenCost() { + Board left = TestUtils.createBoard(ResourceType.WOOD); + + Board main = TestUtils.createBoard(ResourceType.STONE); + main.getTradingRules().setCost(ResourceType.WOOD, Provider.RIGHT_PLAYER, 1); + + Board right = TestUtils.createBoard(ResourceType.ORE); + right.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); + right.getPublicProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); + + Table table = new Table(Arrays.asList(main, right, left)); + + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 1); + assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2)); + } + + @Test + public void bestPrice_chooseCheapest() { + Board left = TestUtils.createBoard(ResourceType.WOOD); + + Board main = TestUtils.createBoard(ResourceType.WOOD); + main.getProduction().addChoice(ResourceType.ORE, ResourceType.CLAY); + main.getTradingRules().setCost(ResourceType.CLAY, Provider.RIGHT_PLAYER, 1); + + Board right = TestUtils.createBoard(ResourceType.WOOD); + right.getProduction().addFixedResource(ResourceType.ORE, 1); + right.getProduction().addFixedResource(ResourceType.CLAY, 1); + right.getPublicProduction().addFixedResource(ResourceType.ORE, 1); + right.getPublicProduction().addFixedResource(ResourceType.CLAY, 1); + + Table table = new Table(Arrays.asList(main, right, left)); + + Resources resources = new Resources(); + resources.add(ResourceType.ORE, 1); + resources.add(ResourceType.CLAY, 1); + assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); + assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); + assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 2)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java new file mode 100644 index 00000000..2247147c --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java @@ -0,0 +1,324 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ProductionTest { + + private Resources emptyResources; + + private Resources resources1Wood; + + private Resources resources1Stone; + + private Resources resources1Stone1Wood; + + private Resources resources2Stones; + + private Resources resources2Stones3Clay; + + @Before + public void init() { + emptyResources = new Resources(); + + resources1Wood = new Resources(); + resources1Wood.add(ResourceType.WOOD, 1); + + resources1Stone = new Resources(); + resources1Stone.add(ResourceType.STONE, 1); + + resources1Stone1Wood = new Resources(); + resources1Stone1Wood.add(ResourceType.STONE, 1); + resources1Stone1Wood.add(ResourceType.WOOD, 1); + + resources2Stones = new Resources(); + resources2Stones.add(ResourceType.STONE, 2); + + resources2Stones3Clay = new Resources(); + resources2Stones3Clay.add(ResourceType.STONE, 2); + resources2Stones3Clay.add(ResourceType.CLAY, 3); + } + + @Test + public void contains_newProductionContainsEmpty() { + Production production = new Production(); + assertTrue(production.contains(emptyResources)); + } + + @Test + public void contains_singleFixedResource_noneAtAll() { + Production production = new Production(); + assertFalse(production.contains(resources2Stones)); + } + + @Test + public void contains_singleFixedResource_notEnough() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 1); + assertFalse(production.contains(resources2Stones)); + } + + @Test + public void contains_singleFixedResource_justEnough() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 2); + assertTrue(production.contains(resources2Stones)); + } + + @Test + public void contains_singleFixedResource_moreThanEnough() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 3); + assertTrue(production.contains(resources2Stones)); + } + + @Test + public void contains_singleFixedResource_moreThanEnough_amongOthers() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 3); + production.addFixedResource(ResourceType.CLAY, 2); + assertTrue(production.contains(resources2Stones)); + } + + @Test + public void contains_multipleFixedResources_notEnoughOfOne() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 3); + production.addFixedResource(ResourceType.CLAY, 1); + assertFalse(production.contains(resources2Stones3Clay)); + } + + @Test + public void contains_multipleFixedResources_notEnoughOfBoth() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 1); + production.addFixedResource(ResourceType.CLAY, 1); + assertFalse(production.contains(resources2Stones3Clay)); + } + + @Test + public void contains_multipleFixedResources_moreThanEnough() { + Production production = new Production(); + production.addFixedResource(ResourceType.STONE, 3); + production.addFixedResource(ResourceType.CLAY, 5); + assertTrue(production.contains(resources2Stones3Clay)); + } + + @Test + public void contains_singleChoice_containsEmpty() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.CLAY); + assertTrue(production.contains(emptyResources)); + } + + @Test + public void contains_singleChoice_enough() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + assertTrue(production.contains(resources1Wood)); + assertTrue(production.contains(resources1Stone)); + } + + @Test + public void contains_multipleChoices_notBoth() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.CLAY); + production.addChoice(ResourceType.STONE, ResourceType.CLAY); + production.addChoice(ResourceType.STONE, ResourceType.CLAY); + assertFalse(production.contains(resources2Stones3Clay)); + } + + @Test + public void contains_multipleChoices_enough() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void contains_multipleChoices_enoughReverseOrder() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void contains_multipleChoices_moreThanEnough() { + Production production = new Production(); + production.addChoice(ResourceType.LOOM, ResourceType.GLASS, ResourceType.PAPYRUS); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void contains_mixedFixedAndChoice_enough() { + Production production = new Production(); + production.addFixedResource(ResourceType.WOOD, 1); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void addAll_empty() { + Production production = new Production(); + production.addAll(emptyResources); + assertTrue(production.contains(emptyResources)); + } + + @Test + public void addAll_singleResource() { + Production production = new Production(); + production.addAll(resources1Stone); + assertTrue(production.contains(resources1Stone)); + } + + @Test + public void addAll_multipleResources() { + Production production = new Production(); + production.addAll(resources2Stones3Clay); + assertTrue(production.contains(resources2Stones3Clay)); + } + + @Test + public void addAll_production_multipleFixedResources() { + Production production = new Production(); + production.addAll(resources2Stones3Clay); + + Production production2 = new Production(); + production2.addAll(production); + + assertTrue(production2.contains(resources2Stones3Clay)); + } + + @Test + public void addAll_production_multipleChoices() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + + Production production2 = new Production(); + production2.addAll(production); + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void addAll_production_mixedFixedResourcesAndChoices() { + Production production = new Production(); + production.addFixedResource(ResourceType.WOOD, 1); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + + Production production2 = new Production(); + production2.addAll(production); + + assertTrue(production.contains(resources1Stone1Wood)); + } + + @Test + public void asChoices_empty() { + Production production = new Production(); + assertTrue(production.asChoices().isEmpty()); + } + + @Test + public void asChoices_onlyChoices() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.WOOD); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + production.addChoice(ResourceType.CLAY, ResourceType.LOOM, ResourceType.GLASS); + assertEquals(production.getAlternativeResources(), production.asChoices()); + } + + @Test + public void asChoices_onlyFixed() { + Production production = new Production(); + production.addFixedResource(ResourceType.WOOD, 1); + production.addFixedResource(ResourceType.CLAY, 2); + + Set<Set<ResourceType>> expected = new HashSet<>(); + expected.add(EnumSet.of(ResourceType.WOOD)); + expected.add(EnumSet.of(ResourceType.CLAY)); + expected.add(EnumSet.of(ResourceType.CLAY)); + + assertEquals(expected, production.asChoices()); + } + + @Test + public void asChoices_mixed() { + Production production = new Production(); + production.addChoice(ResourceType.STONE, ResourceType.ORE); + production.addChoice(ResourceType.CLAY, ResourceType.LOOM, ResourceType.GLASS); + production.addFixedResource(ResourceType.WOOD, 1); + production.addFixedResource(ResourceType.CLAY, 2); + + Set<Set<ResourceType>> expected = new HashSet<>(); + expected.add(EnumSet.of(ResourceType.STONE, ResourceType.ORE)); + expected.add(EnumSet.of(ResourceType.CLAY, ResourceType.LOOM, ResourceType.GLASS)); + expected.add(EnumSet.of(ResourceType.WOOD)); + expected.add(EnumSet.of(ResourceType.CLAY)); + expected.add(EnumSet.of(ResourceType.CLAY)); + + assertEquals(expected, production.asChoices()); + } + + @Test + public void equals_falseWhenNull() { + Production production = new Production(); + production.addFixedResource(ResourceType.GLASS, 1); + production.addChoice(ResourceType.ORE, ResourceType.WOOD); + //noinspection ObjectEqualsNull + assertFalse(production.equals(null)); + } + + @Test + public void equals_falseWhenDifferentClass() { + Production production = new Production(); + production.addFixedResource(ResourceType.GLASS, 1); + Resources resources = new Resources(); + resources.add(ResourceType.GLASS, 1); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(production.equals(resources)); + } + + @Test + public void equals_trueWhenSame() { + Production production = new Production(); + assertEquals(production, production); + } + + @Test + public void equals_trueWhenSameContent() { + Production production1 = new Production(); + Production production2 = new Production(); + assertTrue(production1.equals(production2)); + production1.addFixedResource(ResourceType.GLASS, 1); + production2.addFixedResource(ResourceType.GLASS, 1); + assertTrue(production1.equals(production2)); + production1.addChoice(ResourceType.ORE, ResourceType.WOOD); + production2.addChoice(ResourceType.ORE, ResourceType.WOOD); + assertTrue(production1.equals(production2)); + } + + @Test + public void hashCode_sameWhenSameContent() { + Production production1 = new Production(); + Production production2 = new Production(); + assertEquals(production1.hashCode(), production2.hashCode()); + production1.addFixedResource(ResourceType.GLASS, 1); + production2.addFixedResource(ResourceType.GLASS, 1); + assertEquals(production1.hashCode(), production2.hashCode()); + production1.addChoice(ResourceType.ORE, ResourceType.WOOD); + production2.addChoice(ResourceType.ORE, ResourceType.WOOD); + assertEquals(production1.hashCode(), production2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java new file mode 100644 index 00000000..1f260a11 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java @@ -0,0 +1,485 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.NoSuchElementException; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ResourcesTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void init_shouldBeEmpty() { + Resources resources = new Resources(); + for (ResourceType resourceType : ResourceType.values()) { + assertEquals(0, resources.getQuantity(resourceType)); + } + assertEquals(0, resources.size()); + assertTrue(resources.isEmpty()); + } + + @Test + public void add_zero() { + Resources resources = new Resources(); + resources.add(ResourceType.CLAY, 0); + assertEquals(0, resources.getQuantity(ResourceType.CLAY)); + assertEquals(0, resources.size()); + assertTrue(resources.isEmpty()); + } + + @Test + public void add_simple() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + assertEquals(3, resources.getQuantity(ResourceType.WOOD)); + assertEquals(3, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void add_multipleCallsStacked() { + Resources resources = new Resources(); + resources.add(ResourceType.ORE, 3); + resources.add(ResourceType.ORE, 2); + assertEquals(5, resources.getQuantity(ResourceType.ORE)); + assertEquals(5, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void add_interlaced() { + Resources resources = new Resources(); + resources.add(ResourceType.GLASS, 3); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.WOOD, 4); + resources.add(ResourceType.GLASS, 2); + assertEquals(5, resources.getQuantity(ResourceType.GLASS)); + assertEquals(10, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void remove_some() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + resources.remove(ResourceType.WOOD, 2); + assertEquals(1, resources.getQuantity(ResourceType.WOOD)); + assertEquals(1, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void remove_all() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 3); + resources.remove(ResourceType.WOOD, 3); + assertEquals(0, resources.getQuantity(ResourceType.WOOD)); + assertEquals(0, resources.size()); + assertTrue(resources.isEmpty()); + } + + @Test + public void remove_tooMany() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 2); + + thrown.expect(NoSuchElementException.class); + resources.remove(ResourceType.WOOD, 3); + } + + @Test + public void addAll_empty() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources emptyResources = new Resources(); + + resources.addAll(emptyResources); + assertEquals(1, resources.getQuantity(ResourceType.STONE)); + assertEquals(3, resources.getQuantity(ResourceType.CLAY)); + assertEquals(0, resources.getQuantity(ResourceType.ORE)); + assertEquals(0, resources.getQuantity(ResourceType.GLASS)); + assertEquals(0, resources.getQuantity(ResourceType.LOOM)); + assertEquals(4, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void addAll_zeros() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources emptyResources = new Resources(); + emptyResources.add(ResourceType.STONE, 0); + emptyResources.add(ResourceType.CLAY, 0); + + resources.addAll(emptyResources); + assertEquals(1, resources.getQuantity(ResourceType.STONE)); + assertEquals(3, resources.getQuantity(ResourceType.CLAY)); + assertEquals(0, resources.getQuantity(ResourceType.ORE)); + assertEquals(0, resources.getQuantity(ResourceType.GLASS)); + assertEquals(0, resources.getQuantity(ResourceType.LOOM)); + assertEquals(4, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void addAll_same() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources resources2 = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 6); + + resources.addAll(resources2); + assertEquals(3, resources.getQuantity(ResourceType.STONE)); + assertEquals(9, resources.getQuantity(ResourceType.CLAY)); + assertEquals(0, resources.getQuantity(ResourceType.ORE)); + assertEquals(0, resources.getQuantity(ResourceType.GLASS)); + assertEquals(0, resources.getQuantity(ResourceType.LOOM)); + assertEquals(12, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void addAll_overlap() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources resources2 = new Resources(); + resources.add(ResourceType.CLAY, 6); + resources.add(ResourceType.ORE, 4); + + resources.addAll(resources2); + assertEquals(1, resources.getQuantity(ResourceType.STONE)); + assertEquals(9, resources.getQuantity(ResourceType.CLAY)); + assertEquals(4, resources.getQuantity(ResourceType.ORE)); + assertEquals(0, resources.getQuantity(ResourceType.GLASS)); + assertEquals(0, resources.getQuantity(ResourceType.LOOM)); + assertEquals(14, resources.size()); + assertFalse(resources.isEmpty()); + } + + @Test + public void contains_emptyContainsEmpty() { + Resources emptyResources = new Resources(); + Resources emptyResources2 = new Resources(); + assertTrue(emptyResources.contains(emptyResources2)); + } + + @Test + public void contains_singleTypeContainsEmpty() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + + Resources emptyResources = new Resources(); + + assertTrue(resources.contains(emptyResources)); + } + + @Test + public void contains_multipleTypesContainsEmpty() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources emptyResources = new Resources(); + + assertTrue(resources.contains(emptyResources)); + } + + @Test + public void contains_self() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + assertTrue(resources.contains(resources)); + } + + @Test + public void contains_allOfEachType() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.STONE, 1); + resources2.add(ResourceType.CLAY, 3); + + assertTrue(resources.contains(resources2)); + } + + @Test + public void contains_someOfEachType() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.STONE, 1); + resources2.add(ResourceType.CLAY, 3); + + assertTrue(resources.contains(resources2)); + } + + @Test + public void contains_someOfSomeTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.CLAY, 3); + + assertTrue(resources.contains(resources2)); + } + + @Test + public void contains_allOfSomeTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.CLAY, 4); + + assertTrue(resources.contains(resources2)); + } + + @Test + public void minus_empty() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources emptyResources = new Resources(); + + Resources diff = resources.minus(emptyResources); + assertEquals(1, diff.getQuantity(ResourceType.STONE)); + assertEquals(3, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_self() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources diff = resources.minus(resources); + assertEquals(0, diff.getQuantity(ResourceType.STONE)); + assertEquals(0, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_allOfEachType() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 1); + resources.add(ResourceType.CLAY, 3); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.STONE, 1); + resources2.add(ResourceType.CLAY, 3); + + Resources diff = resources.minus(resources2); + assertEquals(0, diff.getQuantity(ResourceType.STONE)); + assertEquals(0, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_someOfEachType() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.STONE, 1); + resources2.add(ResourceType.CLAY, 3); + + Resources diff = resources.minus(resources2); + assertEquals(1, diff.getQuantity(ResourceType.STONE)); + assertEquals(1, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_someOfSomeTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.CLAY, 3); + + Resources diff = resources.minus(resources2); + assertEquals(2, diff.getQuantity(ResourceType.STONE)); + assertEquals(1, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_allOfSomeTypes() { + Resources resources = new Resources(); + resources.add(ResourceType.STONE, 2); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.CLAY, 4); + + Resources diff = resources.minus(resources2); + assertEquals(2, diff.getQuantity(ResourceType.STONE)); + assertEquals(0, diff.getQuantity(ResourceType.CLAY)); + assertEquals(0, diff.getQuantity(ResourceType.ORE)); + assertEquals(0, diff.getQuantity(ResourceType.GLASS)); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_tooMuchOfExistingType() { + Resources resources = new Resources(); + resources.add(ResourceType.CLAY, 4); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.CLAY, 5); + + Resources diff = resources.minus(resources2); + assertEquals(0, diff.getQuantity(ResourceType.CLAY)); + } + + @Test + public void minus_someOfAnAbsentType() { + Resources resources = new Resources(); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.LOOM, 5); + + Resources diff = resources.minus(resources2); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void minus_someOfATypeWithZero() { + Resources resources = new Resources(); + resources.add(ResourceType.LOOM, 0); + + Resources resources2 = new Resources(); + resources2.add(ResourceType.LOOM, 5); + + Resources diff = resources.minus(resources2); + assertEquals(0, diff.getQuantity(ResourceType.LOOM)); + } + + @Test + public void isEmpty_noElement() { + Resources resources = new Resources(); + assertTrue(resources.isEmpty()); + } + + @Test + public void isEmpty_singleZeroElement() { + Resources resources = new Resources(); + resources.add(ResourceType.LOOM, 0); + assertTrue(resources.isEmpty()); + } + + @Test + public void isEmpty_multipleZeroElements() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 0); + resources.add(ResourceType.ORE, 0); + resources.add(ResourceType.LOOM, 0); + assertTrue(resources.isEmpty()); + } + + @Test + public void isEmpty_singleElementMoreThanZero() { + Resources resources = new Resources(); + resources.add(ResourceType.LOOM, 3); + assertFalse(resources.isEmpty()); + } + + @Test + public void isEmpty_mixedZeroAndNonZeroElements() { + Resources resources = new Resources(); + resources.add(ResourceType.WOOD, 0); + resources.add(ResourceType.LOOM, 3); + assertFalse(resources.isEmpty()); + } + + @Test + public void isEmpty_mixedZeroAndNonZeroElements_reverseOrder() { + Resources resources = new Resources(); + resources.add(ResourceType.ORE, 3); + resources.add(ResourceType.PAPYRUS, 0); + assertFalse(resources.isEmpty()); + } + + @Test + public void equals_falseWhenNull() { + Resources resources = new Resources(); + resources.add(ResourceType.GLASS, 1); + //noinspection ObjectEqualsNull + assertFalse(resources.equals(null)); + } + + @Test + public void equals_falseWhenDifferentClass() { + Resources resources = new Resources(); + resources.add(ResourceType.GLASS, 1); + Production production = new Production(); + production.addFixedResource(ResourceType.GLASS, 1); + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(resources.equals(production)); + } + + @Test + public void equals_trueWhenSame() { + Resources resources = new Resources(); + assertEquals(resources, resources); + } + + @Test + public void equals_trueWhenSameContent() { + Resources resources1 = new Resources(); + Resources resources2 = new Resources(); + assertTrue(resources1.equals(resources2)); + resources1.add(ResourceType.GLASS, 1); + resources2.add(ResourceType.GLASS, 1); + assertTrue(resources1.equals(resources2)); + } + + @Test + public void hashCode_sameWhenSameContent() { + Resources resources1 = new Resources(); + Resources resources2 = new Resources(); + assertEquals(resources1.hashCode(), resources2.hashCode()); + resources1.add(ResourceType.GLASS, 1); + resources2.add(ResourceType.GLASS, 1); + assertEquals(resources1.hashCode(), resources2.hashCode()); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java new file mode 100644 index 00000000..0fd1b52b --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java @@ -0,0 +1,112 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeTrue; + +@RunWith(Theories.class) +public class TradingRulesTest { + + @DataPoints + public static int[] costs() { + return new int[]{0, 1, 2}; + } + + @DataPoints + public static Provider[] providers() { + return Provider.values(); + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Theory + public void setCost_overridesCost(int defaultCost, int overriddenCost, Provider overriddenProvider, + Provider provider, ResourceType type) { + assumeTrue(defaultCost != overriddenCost); + assumeTrue(overriddenProvider != provider); + + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(type, overriddenProvider, overriddenCost); + + assertEquals(overriddenCost, rules.getCost(type, overriddenProvider)); + assertEquals(defaultCost, rules.getCost(type, provider)); + } + + @Theory + public void computeCost_zeroForNoResources(int defaultCost) { + TradingRules rules = new TradingRules(defaultCost); + assertEquals(0, rules.computeCost(new ArrayList<>())); + } + + @Theory + public void computeCost_defaultCostWhenNoOverride(int defaultCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_twiceDefaultFor2Resources(int defaultCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type, type); + assertEquals(2 * defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_overriddenCost(int defaultCost, int overriddenCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(type, provider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(overriddenCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_defaultCostWhenOverrideOnOtherProviderOrType(int defaultCost, int overriddenCost, + Provider overriddenProvider, + ResourceType overriddenType, Provider provider, + ResourceType type) { + assumeTrue(overriddenProvider != provider || overriddenType != type); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(overriddenType, overriddenProvider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_oneDefaultAndOneOverriddenType(int defaultCost, int overriddenCost, + ResourceType overriddenType, Provider provider, + ResourceType type) { + assumeTrue(overriddenType != type); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(overriddenType, provider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, overriddenType, type); + assertEquals(defaultCost + overriddenCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_oneDefaultAndOneOverriddenProvider(int defaultCost, int overriddenCost, + Provider overriddenProvider, Provider provider, + ResourceType type) { + assumeTrue(overriddenProvider != provider); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(type, overriddenProvider, overriddenCost); + + List<BoughtResources> boughtResources = new ArrayList<>(2); + boughtResources.add(TestUtils.createBoughtResources(provider, type)); + boughtResources.add(TestUtils.createBoughtResources(overriddenProvider, type)); + + assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources)); + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java new file mode 100644 index 00000000..4b727944 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -0,0 +1,199 @@ +package org.luxons.sevenwonders.game.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.CustomizableSettings; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.effects.Effect; +import org.luxons.sevenwonders.game.effects.ScienceProgress; +import org.luxons.sevenwonders.game.resources.BoughtResources; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.Provider; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; +import org.luxons.sevenwonders.game.wonders.Wonder; +import org.luxons.sevenwonders.game.wonders.WonderStage; + +public class TestUtils { + + private static final long SEED = 42; + + public static CustomizableSettings createCustomizableSettings() { + CustomizableSettings customizableSettings = new CustomizableSettings(); + customizableSettings.setRandomSeedForTests(SEED); + return customizableSettings; + } + + private static Settings createSettings(int nbPlayers) { + return new Settings(nbPlayers, createCustomizableSettings()); + } + + public static Table createTable(int nbPlayers) { + return new Table(createBoards(nbPlayers)); + } + + private static List<Board> createBoards(int count) { + Settings settings = createSettings(count); + List<Board> boards = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + boards.add(createBoard(settings, ResourceType.WOOD)); + } + return boards; + } + + private static Board createBoard(Settings settings, ResourceType initialResource) { + Wonder wonder = createWonder(initialResource); + return new Board(wonder, 0, settings); + } + + public static Board createBoard(ResourceType initialResource) { + return createBoard(createSettings(5), initialResource); + } + + private static Board createBoard(ResourceType initialResource, ResourceType... production) { + Board board = createBoard(initialResource); + board.getProduction().addAll(createFixedProduction(production)); + return board; + } + + public static Board createBoard(ResourceType initialResource, int gold, ResourceType... production) { + Board board = createBoard(initialResource, production); + board.setGold(gold); + return board; + } + + public static Wonder createWonder() { + return createWonder(ResourceType.WOOD); + } + + public static Wonder createWonder(ResourceType initialResource) { + WonderStage stage1 = new WonderStage(); + stage1.setRequirements(new Requirements()); + WonderStage stage2 = new WonderStage(); + stage1.setRequirements(new Requirements()); + WonderStage stage3 = new WonderStage(); + stage1.setRequirements(new Requirements()); + return new Wonder("Test Wonder " + initialResource.getSymbol(), initialResource, stage1, stage2, stage3); + } + + public static Production createFixedProduction(ResourceType... producedTypes) { + Production production = new Production(); + Resources fixedProducedResources = production.getFixedResources(); + fixedProducedResources.addAll(createResources(producedTypes)); + return production; + } + + public static Resources createResources(ResourceType... types) { + Resources resources = new Resources(); + for (ResourceType producedType : types) { + resources.add(producedType, 1); + } + return resources; + } + + public static BoughtResources createBoughtResources(Provider provider, ResourceType... resources) { + BoughtResources boughtResources = new BoughtResources(); + boughtResources.setProvider(provider); + boughtResources.setResources(TestUtils.createResources(resources)); + return boughtResources; + } + + public static Requirements createRequirements(ResourceType... types) { + Resources resources = createResources(types); + Requirements requirements = new Requirements(); + requirements.setResources(resources); + return requirements; + } + + public static List<Card> createSampleCards(int fromIndex, int nbCards) { + List<Card> sampleCards = new ArrayList<>(); + for (int i = fromIndex; i < fromIndex + nbCards; i++) { + sampleCards.add(createCard(i, Color.BLUE)); + } + return sampleCards; + } + + public static Card createCard(String name) { + return new Card(name, Color.BLUE, new Requirements(), null, null, null, null); + } + + public static Card createCard(Color color) { + return new Card("Test Card", color, new Requirements(), null, null, null, null); + } + + public static Card createCard(Color color, Effect effect) { + List<Effect> effects = Collections.singletonList(effect); + return new Card("Test Card", color, new Requirements(), effects, null, null, null); + } + + private static Card createCard(int num, Color color) { + return new Card("Test Card " + num, color, new Requirements(), null, null, null, null); + } + + public static Card createGuildCard(int num, Effect effect) { + List<Effect> effects = Collections.singletonList(effect); + return new Card("Test Guild " + num, Color.PURPLE, new Requirements(), effects, null, null, null); + } + + public static void addCards(Board board, int nbCardsOfColor, int nbOtherCards, Color color) { + addCards(board, nbCardsOfColor, color); + Color otherColor = getDifferentColorFrom(color); + addCards(board, nbOtherCards, otherColor); + } + + public static void addCards(Board board, int nbCards, Color color) { + for (int i = 0; i < nbCards; i++) { + board.addCard(createCard(i, color)); + } + } + + public static Color getDifferentColorFrom(Color... colors) { + List<Color> forbiddenColors = Arrays.asList(colors); + for (Color color : Color.values()) { + if (!forbiddenColors.contains(color)) { + return color; + } + } + throw new IllegalArgumentException("All colors are forbidden!"); + } + + public static ScienceProgress createScienceProgress(int compasses, int wheels, int tablets, int jokers) { + ScienceProgress progress = new ScienceProgress(); + progress.setScience(TestUtils.createScience(compasses, wheels, tablets, jokers)); + return progress; + } + + public static Science createScience(int compasses, int wheels, int tablets, int jokers) { + Science science = new Science(); + if (compasses > 0) { + science.add(ScienceType.COMPASS, compasses); + } + if (wheels > 0) { + science.add(ScienceType.WHEEL, wheels); + } + if (tablets > 0) { + science.add(ScienceType.TABLET, tablets); + } + if (jokers > 0) { + science.addJoker(jokers); + } + return science; + } + + public static void playCardWithEffect(Table table, int playerIndex, Color color, Effect effect) { + Card card = createCard(color, effect); + Board board = table.getBoard(playerIndex); + board.addCard(card); + card.applyTo(table, playerIndex, Collections.emptyList()); + } +} |