diff options
Diffstat (limited to 'game-engine/src/test/kotlin/org')
14 files changed, 384 insertions, 499 deletions
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt index 5f76b42e..fb232d9e 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt @@ -11,7 +11,9 @@ import org.luxons.sevenwonders.game.data.GameDefinitionLoader import org.luxons.sevenwonders.game.data.LAST_AGE import org.luxons.sevenwonders.game.moves.MoveType import org.luxons.sevenwonders.game.resources.ResourceTransaction -import org.luxons.sevenwonders.game.resources.bestTransaction +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.Resources +import org.luxons.sevenwonders.game.resources.bestSolution import org.luxons.sevenwonders.game.test.testCustomizableSettings import java.util.HashMap @@ -78,16 +80,19 @@ class GameTest { return PlayerMove(MoveType.DISCARD, firstCardInHand.card.name) } - private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): List<ResourceTransaction> { + private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): Collection<ResourceTransaction> { if (handCard.isFree) { return emptyList() } val requiredResources = handCard.card.requirements.resources val table = turnInfo.table val playerIndex = turnInfo.playerIndex - val transactions = bestTransaction(requiredResources, PlayerContext(playerIndex, table, listOf())) // we're supposed to have a best transaction plan because the card is playable - return transactions!!.asList() + return bestTransaction(requiredResources, PlayerContext(playerIndex, table, listOf())) + } + + private fun bestTransaction(resources: Resources, player: Player): ResourceTransactions { + return bestSolution(resources, player).possibleTransactions.first() } private fun createPickGuildMove(turnInfo: PlayerTurnInfo): PlayerMove { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt index 68799145..0f2a3e0c 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt @@ -18,9 +18,9 @@ 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.resourcesOf import org.luxons.sevenwonders.game.score.ScoreCategory import org.luxons.sevenwonders.game.test.addCards -import org.luxons.sevenwonders.game.test.createResources import org.luxons.sevenwonders.game.test.getDifferentColorFrom import org.luxons.sevenwonders.game.test.playCardWithEffect import org.luxons.sevenwonders.game.test.singleBoardPlayer @@ -47,7 +47,7 @@ class BoardTest { @Theory fun initialProduction_containsInitialResource(type: ResourceType) { val board = Board(testWonder(type), 0, Settings(5)) - val resources = createResources(type) + val resources = resourcesOf(type) assertTrue(board.production.contains(resources)) assertTrue(board.publicProduction.contains(resources)) } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt index e9f95b55..b8be570f 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt @@ -8,8 +8,8 @@ import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.effects.ProductionIncrease import org.luxons.sevenwonders.game.resources.Production -import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.noTransactions import org.luxons.sevenwonders.game.test.testCard import org.luxons.sevenwonders.game.wonders.Wonder @@ -36,7 +36,7 @@ class CardTest { table.getBoard(0).gold = 3 table.getBoard(1).gold = 3 table.getBoard(2).gold = 3 - treeFarmCard.applyTo(SimplePlayer(0, table), ResourceTransactions()) + treeFarmCard.applyTo(SimplePlayer(0, table), noTransactions()) assertEquals(2, table.getBoard(0).gold.toLong()) assertEquals(3, table.getBoard(1).gold.toLong()) assertEquals(3, table.getBoard(2).gold.toLong()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt index 95a5e174..a26034f2 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt @@ -10,9 +10,9 @@ import org.junit.runner.RunWith import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.api.Table 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.resources.emptyResources +import org.luxons.sevenwonders.game.resources.noTransactions import org.luxons.sevenwonders.game.test.createRequirements import org.luxons.sevenwonders.game.test.createTransactions import org.luxons.sevenwonders.game.test.singleBoardPlayer @@ -25,14 +25,14 @@ class RequirementsTest { @Test fun getResources_emptyAfterInit() { val (_, resources) = Requirements() - assertTrue(resources.isEmpty) + assertTrue(resources.isEmpty()) } @Test fun setResources_success() { - val resources = Resources() - val (_, resources1) = Requirements(0, resources) - assertSame(resources, resources1) + val resources = emptyResources() + val requirements = Requirements(0, resources) + assertSame(resources, requirements.resources) } @Theory @@ -43,7 +43,7 @@ class RequirementsTest { val player = singleBoardPlayer(board) assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board)) - assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, ResourceTransactions())) + assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, noTransactions())) assertEquals(boardGold >= requiredGold, requirements.areMetBy(player)) } @@ -55,10 +55,7 @@ class RequirementsTest { val player = singleBoardPlayer(board) assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) - assertEquals( - initialResource == requiredResource, - requirements.areMetWithHelpBy(board, ResourceTransactions()) - ) + assertEquals(initialResource == requiredResource, requirements.areMetWithHelpBy(board, noTransactions())) if (initialResource == requiredResource) { assertTrue(requirements.areMetBy(player)) @@ -67,7 +64,8 @@ class RequirementsTest { @Theory fun resourceRequirement_ownProduction( - initialResource: ResourceType, producedResource: ResourceType, + initialResource: ResourceType, + producedResource: ResourceType, requiredResource: ResourceType ) { assumeTrue(initialResource != requiredResource) @@ -79,10 +77,7 @@ class RequirementsTest { val player = singleBoardPlayer(board) assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) - assertEquals( - producedResource == requiredResource, - requirements.areMetWithHelpBy(board, ResourceTransactions()) - ) + assertEquals(producedResource == requiredResource, requirements.areMetWithHelpBy(board, noTransactions())) if (producedResource == requiredResource) { assertTrue(requirements.areMetBy(player)) @@ -91,7 +86,8 @@ class RequirementsTest { @Theory fun resourceRequirement_boughtResource( - initialResource: ResourceType, boughtResource: ResourceType, + initialResource: ResourceType, + boughtResource: ResourceType, requiredResource: ResourceType ) { assumeTrue(initialResource != requiredResource) @@ -108,10 +104,7 @@ class RequirementsTest { assertFalse(requirements.areMetWithoutNeighboursBy(board)) assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources)) - - if (boughtResource == requiredResource) { - assertTrue(requirements.areMetBy(player)) - } + assertEquals(boughtResource == requiredResource, requirements.areMetBy(player)) } @Theory @@ -125,10 +118,7 @@ class RequirementsTest { val table = Table(Arrays.asList(board, neighbourBoard)) val player = SimplePlayer(0, table) - val transactions = createTransactions( - Provider.RIGHT_PLAYER, - requiredResource - ) + val transactions = createTransactions(Provider.RIGHT_PLAYER, requiredResource) assertFalse(requirements.areMetWithoutNeighboursBy(board)) assertTrue(requirements.areMetWithHelpBy(board, transactions)) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.kt index 5b0a47ab..1b87e593 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.kt @@ -8,6 +8,7 @@ import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.game.effects.ProductionIncrease +import org.luxons.sevenwonders.game.resources.MutableResources import org.luxons.sevenwonders.game.resources.Production import org.luxons.sevenwonders.game.resources.ResourceType import org.luxons.sevenwonders.game.resources.Resources @@ -22,6 +23,7 @@ class ProductionIncreaseSerializerTest { }.type gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()) + .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer()) .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer()) .registerTypeAdapter(Production::class.java, ProductionSerializer()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.kt index f74e21c5..66d4013a 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.kt @@ -7,6 +7,7 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test +import org.luxons.sevenwonders.game.resources.MutableResources import org.luxons.sevenwonders.game.resources.Production import org.luxons.sevenwonders.game.resources.ResourceType import org.luxons.sevenwonders.game.resources.Resources @@ -17,10 +18,9 @@ class ProductionSerializerTest { @Before fun setUp() { - val resourceTypeList = object : TypeToken<List<ResourceType>>() { - - }.type + val resourceTypeList = object : TypeToken<List<ResourceType>>() {}.type gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()) + .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer()) .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer()) .registerTypeAdapter(Production::class.java, ProductionSerializer()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt index 500c2666..426e0f0e 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt @@ -6,8 +6,11 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Before import org.junit.Test -import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.MutableResources +import org.luxons.sevenwonders.game.resources.ResourceType.* import org.luxons.sevenwonders.game.resources.Resources +import org.luxons.sevenwonders.game.resources.emptyResources +import org.luxons.sevenwonders.game.resources.resourcesOf class ResourcesSerializerTest { @@ -15,7 +18,10 @@ class ResourcesSerializerTest { @Before fun setUp() { - gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()).create() + gson = GsonBuilder() + .registerTypeAdapter(Resources::class.java, ResourcesSerializer()) + .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer()) + .create() } @Test @@ -25,40 +31,31 @@ class ResourcesSerializerTest { @Test fun serialize_emptyResourcesToNull() { - val resources = Resources() + val resources = emptyResources() assertEquals("null", gson!!.toJson(resources)) } @Test fun serialize_singleType() { - val resources = Resources() - resources.add(ResourceType.WOOD, 1) + val resources = resourcesOf(WOOD) assertEquals("\"W\"", gson!!.toJson(resources)) } @Test fun serialize_multipleTimesSameType() { - val resources = Resources() - resources.add(ResourceType.WOOD, 3) + val resources = resourcesOf(WOOD to 3) assertEquals("\"WWW\"", gson!!.toJson(resources)) } @Test fun serialize_mixedTypes() { - val resources = Resources() - resources.add(ResourceType.WOOD, 1) - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 1) + val resources = resourcesOf(WOOD, STONE, CLAY) assertEquals("\"WSC\"", gson!!.toJson(resources)) } @Test fun serialize_mixedTypes_unordered() { - val resources = Resources() - resources.add(ResourceType.CLAY, 1) - resources.add(ResourceType.WOOD, 2) - resources.add(ResourceType.CLAY, 1) - resources.add(ResourceType.STONE, 1) + val resources = resourcesOf(CLAY to 1, WOOD to 2, CLAY to 1, STONE to 1) assertEquals("\"CCWWS\"", gson!!.toJson(resources)) } @@ -69,39 +66,31 @@ class ResourcesSerializerTest { @Test fun deserialize_emptyList() { - val resources = Resources() + val resources = emptyResources() assertEquals(resources, gson!!.fromJson("\"\"", Resources::class.java)) } @Test fun deserialize_singleType() { - val resources = Resources() - resources.add(ResourceType.WOOD, 1) + val resources = resourcesOf(WOOD) assertEquals(resources, gson!!.fromJson("\"W\"", Resources::class.java)) } @Test fun deserialize_multipleTimesSameType() { - val resources = Resources() - resources.add(ResourceType.WOOD, 3) + val resources = resourcesOf(WOOD to 3) assertEquals(resources, gson!!.fromJson("\"WWW\"", Resources::class.java)) } @Test fun deserialize_mixedTypes() { - val resources = Resources() - resources.add(ResourceType.WOOD, 1) - resources.add(ResourceType.CLAY, 1) - resources.add(ResourceType.STONE, 1) + val resources = resourcesOf(WOOD, CLAY, STONE) assertEquals(resources, gson!!.fromJson("\"WCS\"", Resources::class.java)) } @Test fun deserialize_mixedTypes_unordered() { - val resources = Resources() - resources.add(ResourceType.WOOD, 1) - resources.add(ResourceType.CLAY, 2) - resources.add(ResourceType.STONE, 3) + val resources = resourcesOf(WOOD to 1, CLAY to 2, STONE to 3) assertEquals(resources, gson!!.fromJson("\"SCWCSS\"", Resources::class.java)) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt index 9d5be34f..7973131c 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt @@ -7,7 +7,7 @@ import org.junit.experimental.theories.Theory import org.junit.runner.RunWith import org.luxons.sevenwonders.game.SimplePlayer import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.test.createResources +import org.luxons.sevenwonders.game.resources.resourcesOf import org.luxons.sevenwonders.game.test.fixedProduction import org.luxons.sevenwonders.game.test.testBoard import org.luxons.sevenwonders.game.test.testTable @@ -26,11 +26,11 @@ class ProductionIncreaseTest { effect.applyTo(board) - val resources = createResources(initialType, addedType) + val resources = resourcesOf(initialType, addedType) assertTrue(board.production.contains(resources)) assertFalse(board.publicProduction.contains(resources)) - val moreResources = createResources(initialType, addedType, extraType) + val moreResources = resourcesOf(initialType, addedType, extraType) assertFalse(board.production.contains(moreResources)) assertFalse(board.publicProduction.contains(moreResources)) } @@ -46,11 +46,11 @@ class ProductionIncreaseTest { effect.applyTo(board) - val resources = createResources(initialType, addedType) + val resources = resourcesOf(initialType, addedType) assertTrue(board.production.contains(resources)) assertTrue(board.publicProduction.contains(resources)) - val moreResources = createResources(initialType, addedType, extraType) + val moreResources = resourcesOf(initialType, addedType, extraType) assertFalse(board.production.contains(moreResources)) assertFalse(board.publicProduction.contains(moreResources)) } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt index ca279d97..67537644 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.kt @@ -7,7 +7,6 @@ import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.resources.Provider.LEFT_PLAYER import org.luxons.sevenwonders.game.resources.Provider.RIGHT_PLAYER import org.luxons.sevenwonders.game.resources.ResourceType.* -import org.luxons.sevenwonders.game.test.createResources import org.luxons.sevenwonders.game.test.createTransaction import org.luxons.sevenwonders.game.test.createTransactions import org.luxons.sevenwonders.game.test.testBoard @@ -16,13 +15,16 @@ import java.util.Arrays class BestPriceCalculatorTest { + private fun solutions(price: Int, vararg resourceTransactions: ResourceTransactions) = + TransactionPlan(price, setOf(*resourceTransactions)) + @Test fun bestPrice_0forEmptyResources() { val table = testTable(3) val player0 = SimplePlayer(0, table) - val emptyResources = Resources() - val emptyTransactions = ResourceTransactions() - assertEquals(TransactionPlan(0, emptyTransactions), bestSolution(emptyResources, player0)) + val emptyResources = emptyResources() + val emptyTransactions = noTransactions() + assertEquals(solutions(0, emptyTransactions), bestSolution(emptyResources, player0)) } @Test @@ -31,12 +33,12 @@ class BestPriceCalculatorTest { val main = testBoard(STONE) val right = testBoard(WOOD) val table = Table(Arrays.asList(main, right, left)) - + val player0 = SimplePlayer(0, table) val player1 = SimplePlayer(1, table) val player2 = SimplePlayer(2, table) - val resources = createResources(STONE, STONE) + val resources = resourcesOf(STONE, STONE) val stoneLeftSingle = createTransaction(LEFT_PLAYER, STONE) val stoneRightSingle = createTransaction(RIGHT_PLAYER, STONE) @@ -45,9 +47,9 @@ class BestPriceCalculatorTest { val stoneRight = createTransactions(stoneRightSingle) val stoneLeftAndRight = createTransactions(stoneLeftSingle, stoneRightSingle) - assertEquals(TransactionPlan(2, stoneLeft), bestSolution(resources, player0)) - assertEquals(TransactionPlan(4, stoneLeftAndRight), bestSolution(resources, player1)) - assertEquals(TransactionPlan(2, stoneRight), bestSolution(resources, player2)) + assertEquals(solutions(2, stoneLeft), bestSolution(resources, player0)) + assertEquals(solutions(4, stoneLeftAndRight), bestSolution(resources, player1)) + assertEquals(solutions(2, stoneRight), bestSolution(resources, player2)) } @Test @@ -65,15 +67,15 @@ class BestPriceCalculatorTest { val player2 = SimplePlayer(2, table) val player3 = SimplePlayer(3, table) - val resources = createResources(WOOD) + val resources = resourcesOf(WOOD) val woodLeft = createTransactions(LEFT_PLAYER, WOOD) val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, player0)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) - assertEquals(TransactionPlan(2, woodLeft), bestSolution(resources, player2)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player3)) + assertEquals(solutions(1, woodRight), bestSolution(resources, player0)) + assertEquals(solutions(0, noTransactions()), bestSolution(resources, player1)) + assertEquals(solutions(2, woodLeft, woodRight), bestSolution(resources, player2)) + assertEquals(solutions(0, noTransactions()), bestSolution(resources, player3)) } @Test @@ -93,12 +95,12 @@ class BestPriceCalculatorTest { val player1 = SimplePlayer(1, table) val player2 = SimplePlayer(2, table) - val resources = createResources(WOOD) + val resources = resourcesOf(WOOD) val woodRight = createTransactions(RIGHT_PLAYER, WOOD) - assertEquals(TransactionPlan(1, woodRight), bestSolution(resources, player0)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player2)) + assertEquals(solutions(1, woodRight), bestSolution(resources, player0)) + assertEquals(solutions(0, noTransactions()), bestSolution(resources, player1)) + assertEquals(solutions(0, noTransactions()), bestSolution(resources, player2)) } @Test @@ -121,12 +123,12 @@ class BestPriceCalculatorTest { val player1 = SimplePlayer(1, table) val player2 = SimplePlayer(2, table) - val resources = createResources(ORE, CLAY) + val resources = resourcesOf(ORE, CLAY) val oreAndClayLeft = createTransactions(LEFT_PLAYER, ORE, CLAY) val clayRight = createTransactions(RIGHT_PLAYER, CLAY) - assertEquals(TransactionPlan(1, clayRight), bestSolution(resources, player0)) - assertEquals(TransactionPlan(0, ResourceTransactions()), bestSolution(resources, player1)) - assertEquals(TransactionPlan(4, oreAndClayLeft), bestSolution(resources, player2)) + assertEquals(solutions(1, clayRight), bestSolution(resources, player0)) + assertEquals(solutions(0, noTransactions()), bestSolution(resources, player1)) + assertEquals(solutions(4, oreAndClayLeft), bestSolution(resources, player2)) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt index 92623a2b..79be288f 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ProductionTest.kt @@ -3,38 +3,27 @@ package org.luxons.sevenwonders.game.resources import org.junit.Assert.* import org.junit.Before import org.junit.Test +import org.luxons.sevenwonders.game.resources.ResourceType.* import java.util.EnumSet import java.util.HashSet class ProductionTest { - private var emptyResources: Resources = Resources() - private var resources1Wood: Resources = Resources() - private var resources1Stone: Resources = Resources() - private var resources1Stone1Wood: Resources = Resources() - private var resources2Stones: Resources = Resources() - private var resources2Stones3Clay: Resources = Resources() + private lateinit var emptyResources: Resources + private lateinit var resources1Wood: Resources + private lateinit var resources1Stone: Resources + private lateinit var resources1Stone1Wood: Resources + private lateinit var resources2Stones: Resources + private lateinit var resources2Stones3Clay: Resources @Before fun init() { - emptyResources = Resources() - - resources1Wood = Resources() - resources1Wood.add(ResourceType.WOOD, 1) - - resources1Stone = Resources() - resources1Stone.add(ResourceType.STONE, 1) - - resources1Stone1Wood = Resources() - resources1Stone1Wood.add(ResourceType.STONE, 1) - resources1Stone1Wood.add(ResourceType.WOOD, 1) - - resources2Stones = Resources() - resources2Stones.add(ResourceType.STONE, 2) - - resources2Stones3Clay = Resources() - resources2Stones3Clay.add(ResourceType.STONE, 2) - resources2Stones3Clay.add(ResourceType.CLAY, 3) + emptyResources = emptyResources() + resources1Wood = resourcesOf(WOOD) + resources1Stone = resourcesOf(STONE) + resources1Stone1Wood = resourcesOf(STONE to 1, WOOD to 1) + resources2Stones = resourcesOf(STONE to 2) + resources2Stones3Clay = resourcesOf(STONE to 2, CLAY to 3) } @Test @@ -52,67 +41,67 @@ class ProductionTest { @Test fun contains_singleFixedResource_notEnough() { val production = Production() - production.addFixedResource(ResourceType.STONE, 1) + production.addFixedResource(STONE, 1) assertFalse(production.contains(resources2Stones)) } @Test fun contains_singleFixedResource_justEnough() { val production = Production() - production.addFixedResource(ResourceType.STONE, 2) + production.addFixedResource(STONE, 2) assertTrue(production.contains(resources2Stones)) } @Test fun contains_singleFixedResource_moreThanEnough() { val production = Production() - production.addFixedResource(ResourceType.STONE, 3) + production.addFixedResource(STONE, 3) assertTrue(production.contains(resources2Stones)) } @Test fun contains_singleFixedResource_moreThanEnough_amongOthers() { val production = Production() - production.addFixedResource(ResourceType.STONE, 3) - production.addFixedResource(ResourceType.CLAY, 2) + production.addFixedResource(STONE, 3) + production.addFixedResource(CLAY, 2) assertTrue(production.contains(resources2Stones)) } @Test fun contains_multipleFixedResources_notEnoughOfOne() { val production = Production() - production.addFixedResource(ResourceType.STONE, 3) - production.addFixedResource(ResourceType.CLAY, 1) + production.addFixedResource(STONE, 3) + production.addFixedResource(CLAY, 1) assertFalse(production.contains(resources2Stones3Clay)) } @Test fun contains_multipleFixedResources_notEnoughOfBoth() { val production = Production() - production.addFixedResource(ResourceType.STONE, 1) - production.addFixedResource(ResourceType.CLAY, 1) + production.addFixedResource(STONE, 1) + production.addFixedResource(CLAY, 1) assertFalse(production.contains(resources2Stones3Clay)) } @Test fun contains_multipleFixedResources_moreThanEnough() { val production = Production() - production.addFixedResource(ResourceType.STONE, 3) - production.addFixedResource(ResourceType.CLAY, 5) + production.addFixedResource(STONE, 3) + production.addFixedResource(CLAY, 5) assertTrue(production.contains(resources2Stones3Clay)) } @Test fun contains_singleChoice_containsEmpty() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.CLAY) + production.addChoice(STONE, CLAY) assertTrue(production.contains(emptyResources)) } @Test fun contains_singleChoice_enough() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.WOOD) + production.addChoice(STONE, WOOD) assertTrue(production.contains(resources1Wood)) assertTrue(production.contains(resources1Stone)) } @@ -120,42 +109,42 @@ class ProductionTest { @Test fun contains_multipleChoices_notBoth() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.CLAY) - production.addChoice(ResourceType.STONE, ResourceType.CLAY) - production.addChoice(ResourceType.STONE, ResourceType.CLAY) + production.addChoice(STONE, CLAY) + production.addChoice(STONE, CLAY) + production.addChoice(STONE, CLAY) assertFalse(production.contains(resources2Stones3Clay)) } @Test fun contains_multipleChoices_enough() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.ORE) - production.addChoice(ResourceType.STONE, ResourceType.WOOD) + production.addChoice(STONE, ORE) + production.addChoice(STONE, WOOD) assertTrue(production.contains(resources1Stone1Wood)) } @Test fun contains_multipleChoices_enoughReverseOrder() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.WOOD) - production.addChoice(ResourceType.STONE, ResourceType.ORE) + production.addChoice(STONE, WOOD) + production.addChoice(STONE, ORE) assertTrue(production.contains(resources1Stone1Wood)) } @Test fun contains_multipleChoices_moreThanEnough() { val production = Production() - production.addChoice(ResourceType.LOOM, ResourceType.GLASS, ResourceType.PAPYRUS) - production.addChoice(ResourceType.STONE, ResourceType.ORE) - production.addChoice(ResourceType.STONE, ResourceType.WOOD) + production.addChoice(LOOM, GLASS, PAPYRUS) + production.addChoice(STONE, ORE) + production.addChoice(STONE, WOOD) assertTrue(production.contains(resources1Stone1Wood)) } @Test fun contains_mixedFixedAndChoice_enough() { val production = Production() - production.addFixedResource(ResourceType.WOOD, 1) - production.addChoice(ResourceType.STONE, ResourceType.WOOD) + production.addFixedResource(WOOD, 1) + production.addChoice(STONE, WOOD) assertTrue(production.contains(resources1Stone1Wood)) } @@ -194,8 +183,8 @@ class ProductionTest { @Test fun addAll_production_multipleChoices() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.WOOD) - production.addChoice(ResourceType.STONE, ResourceType.ORE) + production.addChoice(STONE, WOOD) + production.addChoice(STONE, ORE) val production2 = Production() production2.addAll(production) @@ -205,8 +194,8 @@ class ProductionTest { @Test fun addAll_production_mixedFixedResourcesAndChoices() { val production = Production() - production.addFixedResource(ResourceType.WOOD, 1) - production.addChoice(ResourceType.STONE, ResourceType.WOOD) + production.addFixedResource(WOOD, 1) + production.addChoice(STONE, WOOD) val production2 = Production() production2.addAll(production) @@ -223,22 +212,22 @@ class ProductionTest { @Test fun asChoices_onlyChoices() { val production = Production() - production.addChoice(ResourceType.STONE, ResourceType.WOOD) - production.addChoice(ResourceType.STONE, ResourceType.ORE) - production.addChoice(ResourceType.CLAY, ResourceType.LOOM, ResourceType.GLASS) + production.addChoice(STONE, WOOD) + production.addChoice(STONE, ORE) + production.addChoice(CLAY, LOOM, GLASS) assertEquals(production.getAlternativeResources(), production.asChoices()) } @Test fun asChoices_onlyFixed() { val production = Production() - production.addFixedResource(ResourceType.WOOD, 1) - production.addFixedResource(ResourceType.CLAY, 2) + production.addFixedResource(WOOD, 1) + production.addFixedResource(CLAY, 2) val expected = HashSet<Set<ResourceType>>() - expected.add(EnumSet.of(ResourceType.WOOD)) - expected.add(EnumSet.of(ResourceType.CLAY)) - expected.add(EnumSet.of(ResourceType.CLAY)) + expected.add(EnumSet.of(WOOD)) + expected.add(EnumSet.of(CLAY)) + expected.add(EnumSet.of(CLAY)) assertEquals(expected, production.asChoices()) } @@ -246,17 +235,17 @@ class ProductionTest { @Test fun asChoices_mixed() { val production = 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) + production.addChoice(STONE, ORE) + production.addChoice(CLAY, LOOM, GLASS) + production.addFixedResource(WOOD, 1) + production.addFixedResource(CLAY, 2) val expected = HashSet<Set<ResourceType>>() - 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)) + expected.add(EnumSet.of(STONE, ORE)) + expected.add(EnumSet.of(CLAY, LOOM, GLASS)) + expected.add(EnumSet.of(WOOD)) + expected.add(EnumSet.of(CLAY)) + expected.add(EnumSet.of(CLAY)) assertEquals(expected, production.asChoices()) } @@ -272,11 +261,11 @@ class ProductionTest { val production1 = Production() val production2 = Production() assertTrue(production1 == production2) - production1.addFixedResource(ResourceType.GLASS, 1) - production2.addFixedResource(ResourceType.GLASS, 1) + production1.addFixedResource(GLASS, 1) + production2.addFixedResource(GLASS, 1) assertTrue(production1 == production2) - production1.addChoice(ResourceType.ORE, ResourceType.WOOD) - production2.addChoice(ResourceType.ORE, ResourceType.WOOD) + production1.addChoice(ORE, WOOD) + production2.addChoice(ORE, WOOD) assertTrue(production1 == production2) } @@ -285,11 +274,11 @@ class ProductionTest { val production1 = Production() val production2 = Production() assertEquals(production1.hashCode().toLong(), production2.hashCode().toLong()) - production1.addFixedResource(ResourceType.GLASS, 1) - production2.addFixedResource(ResourceType.GLASS, 1) + production1.addFixedResource(GLASS, 1) + production2.addFixedResource(GLASS, 1) assertEquals(production1.hashCode().toLong(), production2.hashCode().toLong()) - production1.addChoice(ResourceType.ORE, ResourceType.WOOD) - production2.addChoice(ResourceType.ORE, ResourceType.WOOD) + production1.addChoice(ORE, WOOD) + production2.addChoice(ORE, WOOD) assertEquals(production1.hashCode().toLong(), production2.hashCode().toLong()) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt index 26ab16fa..172df278 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.kt @@ -2,31 +2,25 @@ package org.luxons.sevenwonders.game.resources import org.junit.Assert.assertEquals import org.junit.Test +import org.luxons.sevenwonders.game.resources.ResourceType.CLAY +import org.luxons.sevenwonders.game.resources.ResourceType.WOOD import org.luxons.sevenwonders.game.test.createTransaction +import org.luxons.sevenwonders.game.test.of class ResourceTransactionsTest { @Test fun toTransactions() { - val transactionList = listOf( - createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD), - createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY), - createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD) + val transactionMap = mapOf( + Provider.LEFT_PLAYER to (1 of WOOD) + (1 of CLAY), + Provider.RIGHT_PLAYER to (1 of WOOD) ) - val transactions = ResourceTransactions(transactionList) - val expectedNormalized = setOf( - createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY), - createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD) + createTransaction(Provider.LEFT_PLAYER, WOOD, CLAY), + createTransaction(Provider.RIGHT_PLAYER, WOOD) ) - assertEquals(expectedNormalized, transactions.asList().toSet()) - } - - @Test(expected = IllegalStateException::class) - fun remove_failsIfNoResourceForThatProvider() { - val transactions = ResourceTransactions() - transactions.remove(Provider.LEFT_PLAYER, Resources(ResourceType.WOOD)) + assertEquals(expectedNormalized, transactionMap.toTransactions().toSet()) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt index a6ffb6c6..9fa0bc55 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/ResourcesTest.kt @@ -4,6 +4,7 @@ import org.junit.Assert.* import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException +import org.luxons.sevenwonders.game.resources.ResourceType.* import java.util.NoSuchElementException class ResourcesTest { @@ -14,503 +15,417 @@ class ResourcesTest { @Test fun init_shouldBeEmpty() { - val resources = Resources() - for (resourceType in ResourceType.values()) { - assertEquals(0, resources.getQuantity(resourceType).toLong()) + val resources = emptyResources() + for (resourceType in values()) { + assertEquals(0, resources[resourceType]) } - assertEquals(0, resources.size().toLong()) - assertTrue(resources.isEmpty) + assertEquals(0, resources.size) + assertTrue(resources.isEmpty()) } @Test fun add_zero() { - val resources = Resources() - resources.add(ResourceType.CLAY, 0) - assertEquals(0, resources.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, resources.size().toLong()) - assertTrue(resources.isEmpty) + val resources = mutableResourcesOf() + resources.add(CLAY, 0) + assertEquals(0, resources[CLAY]) + assertEquals(0, resources.size) + assertTrue(resources.isEmpty()) } @Test fun add_simple() { - val resources = Resources() - resources.add(ResourceType.WOOD, 3) - assertEquals(3, resources.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(3, resources.size().toLong()) - assertFalse(resources.isEmpty) + val resources = mutableResourcesOf() + resources.add(WOOD, 3) + assertEquals(3, resources[WOOD]) + assertEquals(3, resources.size) + assertFalse(resources.isEmpty()) } @Test fun add_multipleCallsStacked() { - val resources = Resources() - resources.add(ResourceType.ORE, 3) - resources.add(ResourceType.ORE, 2) - assertEquals(5, resources.getQuantity(ResourceType.ORE).toLong()) - assertEquals(5, resources.size().toLong()) - assertFalse(resources.isEmpty) + val resources = mutableResourcesOf() + resources.add(ORE, 3) + resources.add(ORE, 2) + assertEquals(5, resources[ORE]) + assertEquals(5, resources.size) + assertFalse(resources.isEmpty()) } @Test fun add_interlaced() { - val resources = 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).toLong()) - assertEquals(10, resources.size().toLong()) - assertFalse(resources.isEmpty) + val resources = mutableResourcesOf() + resources.add(GLASS, 3) + resources.add(STONE, 1) + resources.add(WOOD, 4) + resources.add(GLASS, 2) + assertEquals(5, resources[GLASS]) + assertEquals(10, resources.size) + assertFalse(resources.isEmpty()) } @Test fun plus_zero() { - val resources = Resources(mapOf(ResourceType.CLAY to 2)) - val resourcesPlusZero = resources + Resources() - val zeroPlusResources = Resources() + resources + val resources = resourcesOf(CLAY to 2) + val resourcesPlusZero = resources + emptyResources() + val zeroPlusResources = emptyResources() + resources - assertEquals(2, resourcesPlusZero.getQuantity(ResourceType.CLAY)) - assertEquals(2, resourcesPlusZero.size()) - assertEquals(2, zeroPlusResources.getQuantity(ResourceType.CLAY)) - assertEquals(2, zeroPlusResources.size()) + assertEquals(2, resourcesPlusZero[CLAY]) + assertEquals(2, resourcesPlusZero.size) + assertEquals(2, zeroPlusResources[CLAY]) + assertEquals(2, zeroPlusResources.size) } @Test fun plus_sameResource() { - val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) - val resources2 = Resources(mapOf(ResourceType.WOOD to 3)) + val resources1 = resourcesOf(WOOD to 1) + val resources2 = resourcesOf(WOOD to 3) val sum = resources1 + resources2 - assertEquals(4, sum.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(4, sum.size()) + assertEquals(1, resources1.size) + assertEquals(3, resources2.size) + assertEquals(4, sum[WOOD]) + assertEquals(4, sum.size) } @Test - fun plus_differentResources() { - val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) - val resources2 = Resources(mapOf(ResourceType.ORE to 3)) + fun plus_differentemptyResources() { + val resources1 = resourcesOf(WOOD to 1) + val resources2 = resourcesOf(ORE to 3) val sum = resources1 + resources2 - assertEquals(1, sum.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(3, sum.getQuantity(ResourceType.ORE).toLong()) - assertEquals(4, sum.size()) + assertEquals(1, resources1.size) + assertEquals(3, resources2.size) + assertEquals(1, sum[WOOD]) + assertEquals(3, sum[ORE]) + assertEquals(4, sum.size) } @Test - fun plus_overlappingResources() { - val resources1 = Resources(mapOf(ResourceType.WOOD to 1)) - val resources2 = Resources(mapOf(ResourceType.WOOD to 2, ResourceType.ORE to 4)) + fun plus_overlappingemptyResources() { + val resources1 = resourcesOf(WOOD to 1) + val resources2 = resourcesOf(WOOD to 2, ORE to 4) val sum = resources1 + resources2 - assertEquals(3, sum.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(4, sum.getQuantity(ResourceType.ORE).toLong()) - assertEquals(7, sum.size()) + assertEquals(1, resources1.size) + assertEquals(6, resources2.size) + assertEquals(3, sum[WOOD]) + assertEquals(4, sum[ORE]) + assertEquals(7, sum.size) } @Test fun remove_some() { - val resources = Resources() - resources.add(ResourceType.WOOD, 3) - resources.remove(ResourceType.WOOD, 2) - assertEquals(1, resources.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(1, resources.size().toLong()) - assertFalse(resources.isEmpty) + val resources = mutableResourcesOf(WOOD to 3) + resources.remove(WOOD, 2) + assertEquals(1, resources[WOOD]) + assertEquals(1, resources.size) + assertFalse(resources.isEmpty()) } @Test fun remove_all() { - val resources = Resources() - resources.add(ResourceType.WOOD, 3) - resources.remove(ResourceType.WOOD, 3) - assertEquals(0, resources.getQuantity(ResourceType.WOOD).toLong()) - assertEquals(0, resources.size().toLong()) - assertTrue(resources.isEmpty) + val resources = mutableResourcesOf(WOOD to 3) + resources.remove(WOOD, 3) + assertEquals(0, resources[WOOD]) + assertEquals(0, resources.size) + assertTrue(resources.isEmpty()) } @Test fun remove_tooMany() { - val resources = Resources() - resources.add(ResourceType.WOOD, 2) + val resources = mutableResourcesOf(WOOD to 2) thrown.expect(NoSuchElementException::class.java) - resources.remove(ResourceType.WOOD, 3) + resources.remove(WOOD, 3) } @Test fun addAll_empty() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) + val resources = mutableResourcesOf(STONE to 1, CLAY to 3) - val emptyResources = Resources() + val emptyResources = emptyResources() - resources.addAll(emptyResources) - assertEquals(1, resources.getQuantity(ResourceType.STONE).toLong()) - assertEquals(3, resources.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.LOOM).toLong()) - assertEquals(4, resources.size().toLong()) - assertFalse(resources.isEmpty) + resources.add(emptyResources) + assertEquals(1, resources[STONE]) + assertEquals(3, resources[CLAY]) + assertEquals(0, resources[ORE]) + assertEquals(0, resources[GLASS]) + assertEquals(0, resources[LOOM]) + assertEquals(4, resources.size) + assertFalse(resources.isEmpty()) } @Test fun addAll_zeros() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) + val resources = mutableResourcesOf(STONE to 1, CLAY to 3) - val emptyResources = Resources() - emptyResources.add(ResourceType.STONE, 0) - emptyResources.add(ResourceType.CLAY, 0) + val emptyResources = resourcesOf(STONE to 0, CLAY to 0) - resources.addAll(emptyResources) - assertEquals(1, resources.getQuantity(ResourceType.STONE).toLong()) - assertEquals(3, resources.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.LOOM).toLong()) - assertEquals(4, resources.size().toLong()) - assertFalse(resources.isEmpty) + resources.add(emptyResources) + assertEquals(1, resources[STONE]) + assertEquals(3, resources[CLAY]) + assertEquals(0, resources[ORE]) + assertEquals(0, resources[GLASS]) + assertEquals(0, resources[LOOM]) + assertEquals(4, resources.size) + assertFalse(resources.isEmpty()) } @Test fun addAll_same() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) + val resources = mutableResourcesOf(STONE to 1, CLAY to 3) + val resources2 = resourcesOf(STONE to 2, CLAY to 6) - val resources2 = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 6) - - resources.addAll(resources2) - assertEquals(3, resources.getQuantity(ResourceType.STONE).toLong()) - assertEquals(9, resources.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.LOOM).toLong()) - assertEquals(12, resources.size().toLong()) - assertFalse(resources.isEmpty) + resources.add(resources2) + assertEquals(3, resources[STONE]) + assertEquals(9, resources[CLAY]) + assertEquals(0, resources[ORE]) + assertEquals(0, resources[GLASS]) + assertEquals(0, resources[LOOM]) + assertEquals(12, resources.size) + assertFalse(resources.isEmpty()) } @Test fun addAll_overlap() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) - - val resources2 = Resources() - resources.add(ResourceType.CLAY, 6) - resources.add(ResourceType.ORE, 4) + val resources = mutableResourcesOf(STONE to 1, CLAY to 3) + val resources2 = resourcesOf(CLAY to 6, ORE to 4) - resources.addAll(resources2) - assertEquals(1, resources.getQuantity(ResourceType.STONE).toLong()) - assertEquals(9, resources.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(4, resources.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, resources.getQuantity(ResourceType.LOOM).toLong()) - assertEquals(14, resources.size().toLong()) - assertFalse(resources.isEmpty) + resources.add(resources2) + assertEquals(1, resources[STONE]) + assertEquals(9, resources[CLAY]) + assertEquals(4, resources[ORE]) + assertEquals(0, resources[GLASS]) + assertEquals(0, resources[LOOM]) + assertEquals(14, resources.size) + assertFalse(resources.isEmpty()) } @Test fun contains_emptyContainsEmpty() { - val emptyResources = Resources() - val emptyResources2 = Resources() + val emptyResources = emptyResources() + val emptyResources2 = emptyResources() assertTrue(emptyResources.containsAll(emptyResources2)) } @Test fun contains_singleTypeContainsEmpty() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - - val emptyResources = Resources() + val resources = resourcesOf(STONE to 1) + val emptyResources = emptyResources() assertTrue(resources.containsAll(emptyResources)) } @Test fun contains_multipleTypesContainsEmpty() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) - - val emptyResources = Resources() + val resources = resourcesOf(STONE to 1, CLAY to 3) + val emptyResources = emptyResources() assertTrue(resources.containsAll(emptyResources)) } @Test fun contains_self() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 1, CLAY to 3) assertTrue(resources.containsAll(resources)) } @Test fun contains_allOfEachType() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) - - val resources2 = Resources() - resources2.add(ResourceType.STONE, 1) - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 1, CLAY to 3) + val resources2 = resourcesOf(STONE to 1, CLAY to 3) assertTrue(resources.containsAll(resources2)) } @Test fun contains_someOfEachType() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.STONE, 1) - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(STONE to 1, CLAY to 3) assertTrue(resources.containsAll(resources2)) } @Test fun contains_someOfSomeTypes() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(CLAY to 3) assertTrue(resources.containsAll(resources2)) } @Test fun contains_allOfSomeTypes() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.CLAY, 4) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(CLAY to 4) assertTrue(resources.containsAll(resources2)) } @Test fun minus_empty() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) - - val emptyResources = Resources() + val resources = resourcesOf(STONE to 1, CLAY to 3) + val emptyResources = emptyResources() val diff = resources.minus(emptyResources) - assertEquals(1, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(3, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(1, diff[STONE]) + assertEquals(3, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_self() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 1, CLAY to 3) val diff = resources.minus(resources) - assertEquals(0, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(0, diff[STONE]) + assertEquals(0, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_allOfEachType() { - val resources = Resources() - resources.add(ResourceType.STONE, 1) - resources.add(ResourceType.CLAY, 3) - - val resources2 = Resources() - resources2.add(ResourceType.STONE, 1) - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 1, CLAY to 3) + val resources2 = resourcesOf(STONE to 1, CLAY to 3) val diff = resources.minus(resources2) - assertEquals(0, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(0, diff[STONE]) + assertEquals(0, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_someOfEachType() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.STONE, 1) - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(STONE to 1, CLAY to 3) val diff = resources.minus(resources2) - assertEquals(1, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(1, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(1, diff[STONE]) + assertEquals(1, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_someOfSomeTypes() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.CLAY, 3) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(CLAY to 3) val diff = resources.minus(resources2) - assertEquals(2, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(1, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(2, diff[STONE]) + assertEquals(1, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_allOfSomeTypes() { - val resources = Resources() - resources.add(ResourceType.STONE, 2) - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.CLAY, 4) + val resources = resourcesOf(STONE to 2, CLAY to 4) + val resources2 = resourcesOf(CLAY to 4) val diff = resources.minus(resources2) - assertEquals(2, diff.getQuantity(ResourceType.STONE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.CLAY).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.ORE).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.GLASS).toLong()) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(2, diff[STONE]) + assertEquals(0, diff[CLAY]) + assertEquals(0, diff[ORE]) + assertEquals(0, diff[GLASS]) + assertEquals(0, diff[LOOM]) } @Test fun minus_tooMuchOfExistingType() { - val resources = Resources() - resources.add(ResourceType.CLAY, 4) - - val resources2 = Resources() - resources2.add(ResourceType.CLAY, 5) + val resources = resourcesOf(CLAY to 4) + val resources2 = resourcesOf(CLAY to 5) val diff = resources.minus(resources2) - assertEquals(0, diff.getQuantity(ResourceType.CLAY).toLong()) + assertEquals(0, diff[CLAY]) } @Test fun minus_someOfAnAbsentType() { - val resources = Resources() - - val resources2 = Resources() - resources2.add(ResourceType.LOOM, 5) + val resources = emptyResources() + val resources2 = resourcesOf(LOOM to 5) val diff = resources.minus(resources2) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(0, diff[LOOM]) } @Test fun minus_someOfATypeWithZero() { - val resources = Resources() - resources.add(ResourceType.LOOM, 0) - - val resources2 = Resources() - resources2.add(ResourceType.LOOM, 5) + val resources = resourcesOf(LOOM to 0) + val resources2 = resourcesOf(LOOM to 5) val diff = resources.minus(resources2) - assertEquals(0, diff.getQuantity(ResourceType.LOOM).toLong()) + assertEquals(0, diff[LOOM]) } @Test fun isEmpty_noElement() { - val resources = Resources() - assertTrue(resources.isEmpty) + val resources = emptyResources() + assertTrue(resources.isEmpty()) } @Test fun isEmpty_singleZeroElement() { - val resources = Resources() - resources.add(ResourceType.LOOM, 0) - assertTrue(resources.isEmpty) + val resources = resourcesOf(LOOM to 0) + assertTrue(resources.isEmpty()) } @Test fun isEmpty_multipleZeroElements() { - val resources = Resources() - resources.add(ResourceType.WOOD, 0) - resources.add(ResourceType.ORE, 0) - resources.add(ResourceType.LOOM, 0) - assertTrue(resources.isEmpty) + val resources = resourcesOf(WOOD to 0, ORE to 0, LOOM to 0) + assertTrue(resources.isEmpty()) } @Test fun isEmpty_singleElementMoreThanZero() { - val resources = Resources() - resources.add(ResourceType.LOOM, 3) - assertFalse(resources.isEmpty) + val resources = resourcesOf(LOOM to 3) + assertFalse(resources.isEmpty()) } @Test fun isEmpty_mixedZeroAndNonZeroElements() { - val resources = Resources() - resources.add(ResourceType.WOOD, 0) - resources.add(ResourceType.LOOM, 3) - assertFalse(resources.isEmpty) + val resources = resourcesOf(WOOD to 0, LOOM to 3) + assertFalse(resources.isEmpty()) } @Test fun isEmpty_mixedZeroAndNonZeroElements_reverseOrder() { - val resources = Resources() - resources.add(ResourceType.ORE, 3) - resources.add(ResourceType.PAPYRUS, 0) - assertFalse(resources.isEmpty) - } - - @Test - fun equals_falseWhenNull() { - val resources = Resources() - resources.add(ResourceType.GLASS, 1) - - assertFalse(resources == null) + val resources = resourcesOf(ORE to 3, PAPYRUS to 0) + assertFalse(resources.isEmpty()) } @Test fun equals_trueWhenSame() { - val resources = Resources() + val resources = emptyResources() assertEquals(resources, resources) } @Test fun equals_trueWhenSameContent() { - val resources1 = Resources() - val resources2 = Resources() + val resources1 = mutableResourcesOf() + val resources2 = mutableResourcesOf() assertTrue(resources1 == resources2) - resources1.add(ResourceType.GLASS, 1) - resources2.add(ResourceType.GLASS, 1) + resources1.add(GLASS, 1) + resources2.add(GLASS, 1) assertTrue(resources1 == resources2) } @Test fun hashCode_sameWhenSameContent() { - val resources1 = Resources() - val resources2 = Resources() - assertEquals(resources1.hashCode().toLong(), resources2.hashCode().toLong()) - resources1.add(ResourceType.GLASS, 1) - resources2.add(ResourceType.GLASS, 1) - assertEquals(resources1.hashCode().toLong(), resources2.hashCode().toLong()) + val resources1 = mutableResourcesOf() + val resources2 = mutableResourcesOf() + assertEquals(resources1.hashCode(), resources2.hashCode()) + resources1.add(GLASS, 1) + resources2.add(GLASS, 1) + assertEquals(resources1.hashCode(), resources2.hashCode()) } } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt index 859c997a..ac3f72de 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt @@ -1,15 +1,14 @@ package org.luxons.sevenwonders.game.resources -import java.util.ArrayList - +import org.junit.Assert.assertEquals +import org.junit.Assume.assumeTrue 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.* - -import org.junit.Assert.assertEquals -import org.junit.Assume.assumeTrue +import org.luxons.sevenwonders.game.test.createTransaction +import org.luxons.sevenwonders.game.test.createTransactions +import java.util.ArrayList @RunWith(Theories::class) class TradingRulesTest { @@ -32,7 +31,7 @@ class TradingRulesTest { @Theory fun computeCost_zeroForNoResources(defaultCost: Int) { val rules = TradingRules(defaultCost) - assertEquals(0, rules.computeCost(ResourceTransactions()).toLong()) + assertEquals(0, rules.computeCost(noTransactions()).toLong()) } @Theory @@ -59,9 +58,11 @@ class TradingRulesTest { @Theory fun computeCost_defaultCostWhenOverrideOnOtherProviderOrType( - defaultCost: Int, overriddenCost: Int, + defaultCost: Int, + overriddenCost: Int, overriddenProvider: Provider, - overriddenType: ResourceType, provider: Provider, + overriddenType: ResourceType, + provider: Provider, type: ResourceType ) { assumeTrue(overriddenProvider != provider || overriddenType != type) @@ -73,8 +74,10 @@ class TradingRulesTest { @Theory fun computeCost_oneDefaultAndOneOverriddenType( - defaultCost: Int, overriddenCost: Int, - overriddenType: ResourceType, provider: Provider, + defaultCost: Int, + overriddenCost: Int, + overriddenType: ResourceType, + provider: Provider, type: ResourceType ) { assumeTrue(overriddenType != type) @@ -86,8 +89,10 @@ class TradingRulesTest { @Theory fun computeCost_oneDefaultAndOneOverriddenProvider( - defaultCost: Int, overriddenCost: Int, - overriddenProvider: Provider, provider: Provider, + defaultCost: Int, + overriddenCost: Int, + overriddenProvider: Provider, + provider: Provider, type: ResourceType ) { assumeTrue(overriddenProvider != provider) @@ -98,10 +103,7 @@ class TradingRulesTest { boughtResources.add(createTransaction(provider, type)) boughtResources.add(createTransaction(overriddenProvider, type)) - assertEquals( - (defaultCost + overriddenCost).toLong(), - rules.computeCost(ResourceTransactions(boughtResources)).toLong() - ) + assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources)) } companion object { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt index bf718d32..1a360a77 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt @@ -24,6 +24,8 @@ 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.resources.noTransactions +import org.luxons.sevenwonders.game.resources.resourcesOf import org.luxons.sevenwonders.game.wonders.Wonder import org.luxons.sevenwonders.game.wonders.WonderStage import java.util.Arrays @@ -90,34 +92,29 @@ private fun createWonderStage(vararg effects: Effect): WonderStage { fun fixedProduction(vararg producedTypes: ResourceType): Production { val production = Production() - val fixedProducedResources = production.fixedResources - fixedProducedResources.addAll(createResources(*producedTypes)) + production.addAll(resourcesOf(*producedTypes)) return production } -fun createResources(vararg types: ResourceType): Resources { - val resources = Resources() - for (producedType in types) { - resources.add(producedType, 1) - } - return resources +infix fun Int.of(type: ResourceType): Resources { + return resourcesOf(type to this) } internal fun createTransactions(provider: Provider, vararg resources: ResourceType): ResourceTransactions { val transaction = createTransaction(provider, *resources) - return ResourceTransactions(listOf(transaction)) + return listOf(transaction) } internal fun createTransactions(vararg transactions: ResourceTransaction): ResourceTransactions { - return ResourceTransactions(Arrays.asList(*transactions)) + return transactions.toList() } fun createTransaction(provider: Provider, vararg resources: ResourceType): ResourceTransaction { - return ResourceTransaction(provider, createResources(*resources)) + return ResourceTransaction(provider, resourcesOf(*resources)) } fun createRequirements(vararg types: ResourceType): Requirements { - val resources = createResources(*types) + val resources = resourcesOf(*types) return Requirements(resources = resources) } @@ -209,7 +206,7 @@ fun createScience(compasses: Int, wheels: Int, tablets: Int, jokers: Int): Scien internal fun playCardWithEffect(player: Player, color: Color, effect: Effect) { val card = testCard(color, effect) player.board.addCard(card) - card.applyTo(player, ResourceTransactions()) + card.applyTo(player, noTransactions()) } internal fun createMove(context: PlayerContext, card: Card, type: MoveType, vararg transactions: ResourceTransaction): Move { |