diff options
author | Joffrey Bion <joffrey.bion@amadeus.com> | 2018-04-27 18:44:31 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-04-27 21:52:33 +0200 |
commit | 5794738944e822760c0648e1bc41cbab95497add (patch) | |
tree | 22052285a4683effa2a30eb7b8a3de87e9b1cfe2 /game-engine/src/test/java | |
parent | Use independent WS clients for each test to prevent race conditions on CI (diff) | |
download | seven-wonders-5794738944e822760c0648e1bc41cbab95497add.tar.gz seven-wonders-5794738944e822760c0648e1bc41cbab95497add.tar.bz2 seven-wonders-5794738944e822760c0648e1bc41cbab95497add.zip |
Bought resources refactoring
Diffstat (limited to 'game-engine/src/test/java')
8 files changed, 171 insertions, 119 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 index 73315dd2..ff34dc27 100644 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Test; import org.luxons.sevenwonders.game.api.CustomizableSettings; @@ -17,7 +18,8 @@ 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.resources.BestPriceCalculator; -import org.luxons.sevenwonders.game.resources.BoughtResources; +import org.luxons.sevenwonders.game.resources.ResourceTransaction; +import org.luxons.sevenwonders.game.resources.ResourceTransactions; import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.test.TestUtils; @@ -95,7 +97,7 @@ public class GameTest { private static PlayerMove createPlayCardMove(PlayerTurnInfo turnInfo) { for (HandCard handCard : turnInfo.getHand()) { if (handCard.isPlayable()) { - List<BoughtResources> resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo); + Set<ResourceTransaction> resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo); return TestUtils.createPlayerMove(handCard.getCard().getName(), MoveType.PLAY, resourcesToBuy); } } @@ -103,12 +105,15 @@ public class GameTest { return TestUtils.createPlayerMove(firstCardInHand.getCard().getName(), MoveType.DISCARD); } - private static List<BoughtResources> findResourcesToBuyFor(HandCard handCard, PlayerTurnInfo turnInfo) { + private static Set<ResourceTransaction> findResourcesToBuyFor(HandCard handCard, PlayerTurnInfo turnInfo) { if (handCard.isFree()) { - return Collections.emptyList(); + return Collections.emptySet(); } Resources requiredResources = handCard.getCard().getRequirements().getResources(); - return BestPriceCalculator.bestSolution(requiredResources, turnInfo.getTable(), turnInfo.getPlayerIndex()); + Table table = turnInfo.getTable(); + int playerIndex = turnInfo.getPlayerIndex(); + ResourceTransactions transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex); + return transactions.toTransactions(); } private static PlayerMove createPickGuildMove(PlayerTurnInfo turnInfo) { 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 index 437c5f21..2a3b6806 100644 --- 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 @@ -11,6 +11,7 @@ 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.ResourceTransactions; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -52,7 +53,7 @@ public class CardTest { table.getBoard(0).setGold(3); table.getBoard(1).setGold(3); table.getBoard(2).setGold(3); - treeFarmCard.applyTo(table, 0, new ArrayList<>()); + treeFarmCard.applyTo(table, 0, new ResourceTransactions()); assertEquals(2, table.getBoard(0).getGold()); assertEquals(3, table.getBoard(1).getGold()); assertEquals(3, table.getBoard(2).getGold()); 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 index b01f9002..d2f505a4 100644 --- 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 @@ -10,8 +10,8 @@ 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.ResourceTransactions; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.test.TestUtils; @@ -27,7 +27,7 @@ public class RequirementsTest { @DataPoints public static int[] goldAmounts() { - return new int[]{0, 1, 2, 5}; + return new int[] {0, 1, 2, 5}; } @DataPoints @@ -36,13 +36,13 @@ public class RequirementsTest { } @Test - public void getResources_emptyAfterInit() throws Exception { + public void getResources_emptyAfterInit() { Requirements requirements = new Requirements(); assertTrue(requirements.getResources().isEmpty()); } @Test - public void setResources_success() throws Exception { + public void setResources_success() { Requirements requirements = new Requirements(); Resources resources = new Resources(); requirements.setResources(resources); @@ -58,8 +58,8 @@ public class RequirementsTest { 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)); + assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, new ResourceTransactions())); + assertEquals(boardGold >= requiredGold, requirements.areMetBy(table, 0)); } @Theory @@ -71,16 +71,16 @@ public class RequirementsTest { assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); assertEquals(initialResource == requiredResource, - requirements.areMetWithHelpBy(board, Collections.emptyList())); + requirements.areMetWithHelpBy(board, new ResourceTransactions())); if (initialResource == requiredResource) { - assertTrue(requirements.couldBeMetBy(table, 0)); + assertTrue(requirements.areMetBy(table, 0)); } } @Theory public void resourceRequirement_ownProduction(ResourceType initialResource, ResourceType producedResource, - ResourceType requiredResource) { + ResourceType requiredResource) { assumeTrue(initialResource != requiredResource); Requirements requirements = TestUtils.createRequirements(requiredResource); @@ -91,16 +91,16 @@ public class RequirementsTest { assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)); assertEquals(producedResource == requiredResource, - requirements.areMetWithHelpBy(board, Collections.emptyList())); + requirements.areMetWithHelpBy(board, new ResourceTransactions())); if (producedResource == requiredResource) { - assertTrue(requirements.couldBeMetBy(table, 0)); + assertTrue(requirements.areMetBy(table, 0)); } } @Theory public void resourceRequirement_boughtResource(ResourceType initialResource, ResourceType boughtResource, - ResourceType requiredResource) { + ResourceType requiredResource) { assumeTrue(initialResource != requiredResource); Requirements requirements = TestUtils.createRequirements(requiredResource); @@ -110,16 +110,13 @@ public class RequirementsTest { 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)); + ResourceTransactions resources = TestUtils.createTransactions(Provider.RIGHT_PLAYER, boughtResource); assertFalse(requirements.areMetWithoutNeighboursBy(board)); - assertEquals(boughtResource == requiredResource, - requirements.areMetWithHelpBy(board, Collections.singletonList(resources))); + assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources)); if (boughtResource == requiredResource) { - assertTrue(requirements.couldBeMetBy(table, 0)); + assertTrue(requirements.areMetBy(table, 0)); } } @@ -133,15 +130,14 @@ public class RequirementsTest { 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)); + ResourceTransactions transactions = TestUtils.createTransactions(Provider.RIGHT_PLAYER, + requiredResource); assertFalse(requirements.areMetWithoutNeighboursBy(board)); - assertTrue(requirements.areMetWithHelpBy(board, Collections.singletonList(boughtResources))); - assertTrue(requirements.couldBeMetBy(table, 0)); + assertTrue(requirements.areMetWithHelpBy(board, transactions)); + assertTrue(requirements.areMetBy(table, 0)); - requirements.pay(table, 0, Collections.singletonList(boughtResources)); + requirements.pay(table, 0, transactions); assertEquals(0, board.getGold()); assertEquals(2, neighbourBoard.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 index c6819d3a..1de3a502 100644 --- 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 @@ -6,8 +6,8 @@ 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.ResourceTransactions; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.test.TestUtils; @@ -40,8 +40,8 @@ public class DiscountTest { discount.getResourceTypes().add(discountedType); discount.apply(board); - BoughtResources boughtResources = TestUtils.createBoughtResources(provider, discountedType); - assertEquals(discountedPrice, board.getTradingRules().computeCost(boughtResources)); + ResourceTransactions transactions = TestUtils.createTransactions(provider, discountedType); + assertEquals(discountedPrice, board.getTradingRules().computeCost(transactions)); } @Theory @@ -60,13 +60,13 @@ public class DiscountTest { // this is the default in the settings used by TestUtils.createBoard() int normalPrice = 2; - BoughtResources fromOtherType = TestUtils.createBoughtResources(provider, otherType); + ResourceTransactions fromOtherType = TestUtils.createTransactions(provider, otherType); assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherType)); - BoughtResources fromOtherProvider = TestUtils.createBoughtResources(otherProvider, discountedType); + ResourceTransactions fromOtherProvider = TestUtils.createTransactions(otherProvider, discountedType); assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProvider)); - BoughtResources fromOtherProviderAndType = TestUtils.createBoughtResources(otherProvider, otherType); + ResourceTransactions fromOtherProviderAndType = TestUtils.createTransactions(otherProvider, otherType); assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProviderAndType)); } } 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 index 9f43ec6b..a5017097 100644 --- 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 @@ -1,7 +1,6 @@ package org.luxons.sevenwonders.game.resources; import java.util.Arrays; -import java.util.Collections; import org.junit.Test; import org.luxons.sevenwonders.game.api.Table; @@ -9,6 +8,13 @@ import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.test.TestUtils; import static org.junit.Assert.assertEquals; +import static org.luxons.sevenwonders.game.resources.Provider.LEFT_PLAYER; +import static org.luxons.sevenwonders.game.resources.Provider.RIGHT_PLAYER; +import static org.luxons.sevenwonders.game.resources.ResourceType.CLAY; +import static org.luxons.sevenwonders.game.resources.ResourceType.GLASS; +import static org.luxons.sevenwonders.game.resources.ResourceType.ORE; +import static org.luxons.sevenwonders.game.resources.ResourceType.STONE; +import static org.luxons.sevenwonders.game.resources.ResourceType.WOOD; public class BestPriceCalculatorTest { @@ -17,107 +23,107 @@ public class BestPriceCalculatorTest { Table table = TestUtils.createTable(3); Resources resources = new Resources(); assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 0)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(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); + Board left = TestUtils.createBoard(STONE); + Board main = TestUtils.createBoard(STONE); + Board right = TestUtils.createBoard(WOOD); Table table = new Table(Arrays.asList(main, right, left)); - Resources resources = new Resources(); - resources.add(ResourceType.STONE, 2); + Resources resources = TestUtils.createResources(STONE, STONE); assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 0)); assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 1)); assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2)); - BoughtResources stoneLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.STONE)); - BoughtResources stoneRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.STONE)); - assertEquals(Collections.singletonList(stoneLeft), BestPriceCalculator.bestSolution(resources, table, 0)); - assertEquals(Arrays.asList(stoneLeft, stoneRight), BestPriceCalculator.bestSolution(resources, table, 1)); - assertEquals(Collections.singletonList(stoneRight), BestPriceCalculator.bestSolution(resources, table, 2)); + ResourceTransaction stoneLeftSingle = TestUtils.createTransaction(LEFT_PLAYER, STONE); + ResourceTransaction stoneRightSingle = TestUtils.createTransaction(RIGHT_PLAYER, STONE); + + ResourceTransactions stoneLeft = TestUtils.createTransactions(stoneLeftSingle); + ResourceTransactions stoneRight = TestUtils.createTransactions(stoneRightSingle); + ResourceTransactions stoneLeftAndRight = TestUtils.createTransactions(stoneLeftSingle, stoneRightSingle); + + assertEquals(stoneLeft, BestPriceCalculator.bestSolution(resources, table, 0)); + assertEquals(stoneLeftAndRight, BestPriceCalculator.bestSolution(resources, table, 1)); + assertEquals(stoneRight, BestPriceCalculator.bestSolution(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 main = TestUtils.createBoard(STONE); + main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1); - Board left = TestUtils.createBoard(ResourceType.WOOD); - Board right = TestUtils.createBoard(ResourceType.WOOD); - Board opposite = TestUtils.createBoard(ResourceType.GLASS); + Board left = TestUtils.createBoard(WOOD); + Board right = TestUtils.createBoard(WOOD); + Board opposite = TestUtils.createBoard(GLASS); Table table = new Table(Arrays.asList(main, right, opposite, left)); - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); + Resources resources = TestUtils.createResources(WOOD); 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)); - BoughtResources woodLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.WOOD)); - BoughtResources woodRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.WOOD)); - assertEquals(Collections.singletonList(woodRight), BestPriceCalculator.bestSolution(resources, table, 0)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1)); - assertEquals(Collections.singletonList(woodLeft), BestPriceCalculator.bestSolution(resources, table, 2)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 3)); + ResourceTransactions woodLeft = TestUtils.createTransactions(LEFT_PLAYER, WOOD); + ResourceTransactions woodRight = TestUtils.createTransactions(RIGHT_PLAYER, WOOD); + assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)); + assertEquals(woodLeft, BestPriceCalculator.bestSolution(resources, table, 2)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 3)); } @Test public void bestPrice_mixedResources_overridenCost() { - Board left = TestUtils.createBoard(ResourceType.WOOD); + Board left = TestUtils.createBoard(WOOD); - Board main = TestUtils.createBoard(ResourceType.STONE); - main.getTradingRules().setCost(ResourceType.WOOD, Provider.RIGHT_PLAYER, 1); + Board main = TestUtils.createBoard(STONE); + main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1); - Board right = TestUtils.createBoard(ResourceType.ORE); - right.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); - right.getPublicProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); + Board right = TestUtils.createBoard(ORE); + right.getProduction().addChoice(WOOD, CLAY); + right.getPublicProduction().addChoice(WOOD, CLAY); Table table = new Table(Arrays.asList(main, right, left)); - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); + Resources resources = TestUtils.createResources(WOOD); assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2)); - BoughtResources woodRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.WOOD)); - assertEquals(Collections.singletonList(woodRight), BestPriceCalculator.bestSolution(resources, table, 0)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 2)); + ResourceTransactions woodRight = TestUtils.createTransactions(RIGHT_PLAYER, WOOD); + + assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 2)); } @Test public void bestPrice_chooseCheapest() { - Board left = TestUtils.createBoard(ResourceType.WOOD); + Board left = TestUtils.createBoard(WOOD); - Board main = TestUtils.createBoard(ResourceType.WOOD); - main.getProduction().addChoice(ResourceType.CLAY, ResourceType.ORE); - main.getTradingRules().setCost(ResourceType.CLAY, Provider.RIGHT_PLAYER, 1); + Board main = TestUtils.createBoard(WOOD); + main.getProduction().addChoice(CLAY, ORE); + main.getTradingRules().setCost(CLAY, 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); + Board right = TestUtils.createBoard(WOOD); + right.getProduction().addFixedResource(ORE, 1); + right.getProduction().addFixedResource(CLAY, 1); + right.getPublicProduction().addFixedResource(ORE, 1); + right.getPublicProduction().addFixedResource(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); + Resources resources = TestUtils.createResources(ORE, CLAY); assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 2)); - BoughtResources oreLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.ORE)); - BoughtResources clayLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.CLAY)); - BoughtResources clayRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.CLAY)); - assertEquals(Collections.singletonList(clayRight), BestPriceCalculator.bestSolution(resources, table, 0)); - assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1)); - assertEquals(Arrays.asList(oreLeft, clayLeft), BestPriceCalculator.bestSolution(resources, table, 2)); + ResourceTransactions oreAndClayLeft = TestUtils.createTransactions(LEFT_PLAYER, ORE, CLAY); + ResourceTransactions clayRight = TestUtils.createTransactions(RIGHT_PLAYER, CLAY); + assertEquals(clayRight, BestPriceCalculator.bestSolution(resources, table, 0)); + assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1)); + assertEquals(oreAndClayLeft, BestPriceCalculator.bestSolution(resources, table, 2)); } } diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java new file mode 100644 index 00000000..68795053 --- /dev/null +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java @@ -0,0 +1,36 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.assertEquals; + +public class ResourceTransactionsTest { + + @Test + public void toTransactions() { + List<ResourceTransaction> transactionList = new ArrayList<>(); + transactionList.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD)); + transactionList.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY)); + transactionList.add(TestUtils.createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD)); + + ResourceTransactions transactions = new ResourceTransactions(transactionList); + + Set<ResourceTransaction> expectedNormalized = new HashSet<>(); + expectedNormalized.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY)); + expectedNormalized.add(TestUtils.createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD)); + + assertEquals(expectedNormalized, new HashSet<>(transactions.toTransactions())); + } + + @Test(expected = IllegalStateException.class) + public void remove_failsIfNoResourceForThatProvider() { + ResourceTransactions transactions = new ResourceTransactions(); + transactions.remove(Provider.LEFT_PLAYER, Resources.of(ResourceType.WOOD)); + } +} 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 index 0fd1b52b..d9f32c78 100644 --- 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 @@ -1,7 +1,6 @@ package org.luxons.sevenwonders.game.resources; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.junit.experimental.theories.DataPoints; @@ -47,29 +46,29 @@ public class TradingRulesTest { @Theory public void computeCost_zeroForNoResources(int defaultCost) { TradingRules rules = new TradingRules(defaultCost); - assertEquals(0, rules.computeCost(new ArrayList<>())); + assertEquals(0, rules.computeCost(new ResourceTransactions())); } @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))); + ResourceTransactions transactions = TestUtils.createTransactions(provider, type); + assertEquals(defaultCost, rules.computeCost(transactions)); } @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))); + ResourceTransactions transactions = TestUtils.createTransactions(provider, type, type); + assertEquals(2 * defaultCost, rules.computeCost(transactions)); } @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))); + ResourceTransactions transactions = TestUtils.createTransactions(provider, type); + assertEquals(overriddenCost, rules.computeCost(transactions)); } @Theory @@ -80,8 +79,8 @@ public class TradingRulesTest { 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))); + ResourceTransactions transactions = TestUtils.createTransactions(provider, type); + assertEquals(defaultCost, rules.computeCost(transactions)); } @Theory @@ -91,8 +90,8 @@ public class TradingRulesTest { 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))); + ResourceTransactions transactions = TestUtils.createTransactions(provider, overriddenType, type); + assertEquals(defaultCost + overriddenCost, rules.computeCost(transactions)); } @Theory @@ -103,10 +102,10 @@ public class TradingRulesTest { 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)); + List<ResourceTransaction> boughtResources = new ArrayList<>(2); + boughtResources.add(TestUtils.createTransaction(provider, type)); + boughtResources.add(TestUtils.createTransaction(overriddenProvider, type)); - assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources)); + assertEquals(defaultCost + overriddenCost, rules.computeCost(new ResourceTransactions(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 index 5adaa098..73a08a32 100644 --- 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 @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.test; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -22,9 +23,10 @@ import org.luxons.sevenwonders.game.effects.Effect; import org.luxons.sevenwonders.game.effects.ScienceProgress; import org.luxons.sevenwonders.game.moves.Move; import org.luxons.sevenwonders.game.moves.MoveType; -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.ResourceTransaction; +import org.luxons.sevenwonders.game.resources.ResourceTransactions; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -111,11 +113,17 @@ public class TestUtils { 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 ResourceTransactions createTransactions(Provider provider, ResourceType... resources) { + ResourceTransaction transaction = createTransaction(provider, resources); + return new ResourceTransactions(Collections.singletonList(transaction)); + } + + public static ResourceTransactions createTransactions(ResourceTransaction... transactions) { + return new ResourceTransactions(Arrays.asList(transactions)); + } + + public static ResourceTransaction createTransaction(Provider provider, ResourceType... resources) { + return new ResourceTransaction(provider, TestUtils.createResources(resources)); } public static Requirements createRequirements(ResourceType... types) { @@ -216,23 +224,24 @@ public class TestUtils { Card card = createCard(color, effect); Board board = table.getBoard(playerIndex); board.addCard(card); - card.applyTo(table, playerIndex, Collections.emptyList()); + card.applyTo(table, playerIndex, new ResourceTransactions()); } - public static Move createMove(int playerIndex, Card card, MoveType type, BoughtResources... boughtResources) { - PlayerMove playerMove = createPlayerMove(card.getName(), type, Arrays.asList(boughtResources)); + public static Move createMove(int playerIndex, Card card, MoveType type, ResourceTransaction... transactions) { + PlayerMove playerMove = createPlayerMove(card.getName(), type, Arrays.asList(transactions)); return type.resolve(playerIndex, card, playerMove); } public static PlayerMove createPlayerMove(String cardName, MoveType type) { - return createPlayerMove(cardName, type, Collections.emptyList()); + return createPlayerMove(cardName, type, Collections.emptySet()); } - public static PlayerMove createPlayerMove(String cardName, MoveType type, List<BoughtResources> boughtResources) { + public static PlayerMove createPlayerMove(String cardName, MoveType type, + Collection<ResourceTransaction> transactions) { PlayerMove playerMove = new PlayerMove(); playerMove.setCardName(cardName); playerMove.setType(type); - playerMove.setBoughtResources(boughtResources); + playerMove.setTransactions(transactions); return playerMove; } } |