diff options
author | Joffrey Bion <joffrey.bion@amadeus.com> | 2018-07-09 15:22:53 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@amadeus.com> | 2018-07-09 15:22:53 +0200 |
commit | f92facc7ccfc9564f7481940c5b23b66a473982f (patch) | |
tree | 00ba42ee9cad92aa7d82336a85b891e02f784869 /game-engine/src/test/java | |
parent | Kotlin mig: Boards package (diff) | |
download | seven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.tar.gz seven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.tar.bz2 seven-wonders-f92facc7ccfc9564f7481940c5b23b66a473982f.zip |
Kotlin mig: moves package
Diffstat (limited to 'game-engine/src/test/java')
6 files changed, 0 insertions, 1163 deletions
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.java deleted file mode 100644 index 87e1d704..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/moves/BuildWonderMoveTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.luxons.sevenwonders.game.moves; - -import java.util.Collections; -import java.util.List; - -import org.junit.Test; -import org.luxons.sevenwonders.game.Settings; -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.cards.Card; -import org.luxons.sevenwonders.game.test.TestUtilsKt; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class BuildWonderMoveTest { - - @Test(expected = InvalidMoveException.class) - public void validate_failsWhenCardNotInHand() { - Table table = TestUtilsKt.testTable(3); - List<Card> hand = TestUtilsKt.sampleCards(0, 7); - Card anotherCard = TestUtilsKt.testCard("Card that is not in the hand"); - Move move = TestUtilsKt.createMove(0, anotherCard, MoveType.UPGRADE_WONDER); - - move.validate(table, hand); - } - - @Test(expected = InvalidMoveException.class) - public void validate_failsWhenWonderIsCompletelyBuilt() { - Settings settings = TestUtilsKt.testSettings(3); - Table table = TestUtilsKt.testTable(settings); - List<Card> hand = TestUtilsKt.sampleCards(0, 7); - - fillPlayerWonderLevels(settings, table, hand); - - // should fail because the wonder is already full - buildOneWonderLevel(settings, table, hand, 4); - } - - private static void fillPlayerWonderLevels(Settings settings, Table table, List<Card> hand) { - try { - int nbLevels = table.getBoard(0).getWonder().getStages().size(); - for (int i = 0; i < nbLevels; i++) { - buildOneWonderLevel(settings, table, hand, i); - } - } catch (InvalidMoveException e) { - fail("Building wonder levels should not fail before being full"); - } - } - - private static void buildOneWonderLevel(Settings settings, Table table, List<Card> hand, int cardIndex) { - Card card = hand.get(cardIndex); - Move move = TestUtilsKt.createMove(0, card, MoveType.UPGRADE_WONDER); - move.validate(table, hand); - move.place(table, Collections.emptyList(), settings); - move.activate(table, Collections.emptyList(), settings); - } - - @Test - public void place_increasesWonderLevel() { - Settings settings = TestUtilsKt.testSettings(3); - Table table = TestUtilsKt.testTable(settings); - List<Card> hand = TestUtilsKt.sampleCards(0, 7); - Card cardToUse = hand.get(0); - Move move = TestUtilsKt.createMove(0, cardToUse, MoveType.UPGRADE_WONDER); - move.validate(table, hand); // should not fail - - int initialStage = table.getBoard(0).getWonder().getNbBuiltStages(); - - move.place(table, Collections.emptyList(), settings); - - int newStage = table.getBoard(0).getWonder().getNbBuiltStages(); - - // we need to see the level increase before activation so that other players - assertEquals(initialStage + 1, newStage); - } - -} 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 deleted file mode 100644 index bb0e757b..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java +++ /dev/null @@ -1,129 +0,0 @@ -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.TestUtilsKt; - -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 { - - @Test - public void bestPrice_0forEmptyResources() { - Table table = TestUtilsKt.testTable(3); - Resources resources = new Resources(); - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0)); - assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 0)); - } - - @Test - public void bestPrice_fixedResources_defaultCost() { - Board left = TestUtilsKt.testBoard(STONE); - Board main = TestUtilsKt.testBoard(STONE); - Board right = TestUtilsKt.testBoard(WOOD); - Table table = new Table(Arrays.asList(main, right, left)); - - Resources resources = TestUtilsKt.createResources(STONE, STONE); - assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 0)); - assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 1)); - assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2)); - - ResourceTransaction stoneLeftSingle = TestUtilsKt.createTransaction(LEFT_PLAYER, STONE); - ResourceTransaction stoneRightSingle = TestUtilsKt.createTransaction(RIGHT_PLAYER, STONE); - - ResourceTransactions stoneLeft = TestUtilsKt.createTransactions(stoneLeftSingle); - ResourceTransactions stoneRight = TestUtilsKt.createTransactions(stoneRightSingle); - ResourceTransactions stoneLeftAndRight = TestUtilsKt.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 = TestUtilsKt.testBoard(STONE); - main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1); - - Board left = TestUtilsKt.testBoard(WOOD); - Board right = TestUtilsKt.testBoard(WOOD); - Board opposite = TestUtilsKt.testBoard(GLASS); - Table table = new Table(Arrays.asList(main, right, opposite, left)); - - Resources resources = TestUtilsKt.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)); - - ResourceTransactions woodLeft = TestUtilsKt.createTransactions(LEFT_PLAYER, WOOD); - ResourceTransactions woodRight = TestUtilsKt.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 = TestUtilsKt.testBoard(WOOD); - - Board main = TestUtilsKt.testBoard(STONE); - main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1); - - Board right = TestUtilsKt.testBoard(ORE); - right.getProduction().addChoice(WOOD, CLAY); - right.getPublicProduction().addChoice(WOOD, CLAY); - - Table table = new Table(Arrays.asList(main, right, left)); - - Resources resources = TestUtilsKt.createResources(WOOD); - assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2)); - - ResourceTransactions woodRight = TestUtilsKt.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 = TestUtilsKt.testBoard(WOOD); - - Board main = TestUtilsKt.testBoard(WOOD); - main.getProduction().addChoice(CLAY, ORE); - main.getTradingRules().setCost(CLAY, RIGHT_PLAYER, 1); - - Board right = TestUtilsKt.testBoard(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 = TestUtilsKt.createResources(ORE, CLAY); - assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0)); - assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1)); - assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 2)); - - ResourceTransactions oreAndClayLeft = TestUtilsKt.createTransactions(LEFT_PLAYER, ORE, CLAY); - ResourceTransactions clayRight = TestUtilsKt.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/ProductionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java deleted file mode 100644 index 2247147c..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java +++ /dev/null @@ -1,324 +0,0 @@ -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/ResourceTransactionsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java deleted file mode 100644 index 775973b5..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -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.TestUtilsKt; - -import static org.junit.Assert.assertEquals; - -public class ResourceTransactionsTest { - - @Test - public void toTransactions() { - List<ResourceTransaction> transactionList = new ArrayList<>(); - transactionList.add(TestUtilsKt.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD)); - transactionList.add(TestUtilsKt.createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY)); - transactionList.add(TestUtilsKt.createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD)); - - ResourceTransactions transactions = new ResourceTransactions(transactionList); - - Set<ResourceTransaction> expectedNormalized = new HashSet<>(); - expectedNormalized.add( - TestUtilsKt.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY)); - expectedNormalized.add(TestUtilsKt.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/ResourcesTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java deleted file mode 100644 index 1f260a11..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java +++ /dev/null @@ -1,485 +0,0 @@ -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 deleted file mode 100644 index 3c447753..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.ArrayList; -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.TestUtilsKt; - -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 ResourceTransactions())); - } - - @Theory - public void computeCost_defaultCostWhenNoOverride(int defaultCost, Provider provider, ResourceType type) { - TradingRules rules = new TradingRules(defaultCost); - ResourceTransactions transactions = TestUtilsKt.createTransactions(provider, type); - assertEquals(defaultCost, rules.computeCost(transactions)); - } - - @Theory - public void computeCost_twiceDefaultFor2Resources(int defaultCost, Provider provider, ResourceType type) { - TradingRules rules = new TradingRules(defaultCost); - ResourceTransactions transactions = TestUtilsKt.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); - ResourceTransactions transactions = TestUtilsKt.createTransactions(provider, type); - assertEquals(overriddenCost, rules.computeCost(transactions)); - } - - @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); - ResourceTransactions transactions = TestUtilsKt.createTransactions(provider, type); - assertEquals(defaultCost, rules.computeCost(transactions)); - } - - @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); - ResourceTransactions transactions = TestUtilsKt.createTransactions(provider, overriddenType, type); - assertEquals(defaultCost + overriddenCost, rules.computeCost(transactions)); - } - - @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<ResourceTransaction> boughtResources = new ArrayList<>(2); - boughtResources.add(TestUtilsKt.createTransaction(provider, type)); - boughtResources.add(TestUtilsKt.createTransaction(overriddenProvider, type)); - - assertEquals(defaultCost + overriddenCost, rules.computeCost(new ResourceTransactions(boughtResources))); - } -} |