diff options
author | jbion <joffrey.bion@amadeus.com> | 2018-07-07 16:27:47 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2018-07-07 23:40:44 +0200 |
commit | b30630782ee35cfed275217324f28285ad39d715 (patch) | |
tree | bda9944ccf95b25c602408580b8c536cbeb009ab /game-engine/src/test/kotlin | |
parent | Kotlin mig: Game class (diff) | |
download | seven-wonders-b30630782ee35cfed275217324f28285ad39d715.tar.gz seven-wonders-b30630782ee35cfed275217324f28285ad39d715.tar.bz2 seven-wonders-b30630782ee35cfed275217324f28285ad39d715.zip |
Kotlin mig: effects package
Diffstat (limited to 'game-engine/src/test/kotlin')
10 files changed, 549 insertions, 5 deletions
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 170ae1e4..3253136c 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 @@ -7,6 +7,7 @@ import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.effects.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.Resources @@ -31,8 +32,9 @@ class CardTest { table = Table(boards) val treeFarmRequirements = Requirements(1) - val treeFarmEffect = ProductionIncrease() - treeFarmEffect.production.addChoice(ResourceType.WOOD, ResourceType.CLAY) + val treeFarmProduction = Production() + treeFarmProduction.addChoice(ResourceType.WOOD, ResourceType.CLAY) + val treeFarmEffect = ProductionIncrease(treeFarmProduction, false) treeFarmCard = testCard("Tree Farm", Color.BROWN, treeFarmEffect, treeFarmRequirements) } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt new file mode 100644 index 00000000..ccc1c142 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/BonusPerBoardElementTest.kt @@ -0,0 +1,141 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.Before +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.boards.BoardElementType +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.cards.Color +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals + +@RunWith(Theories::class) +class BonusPerBoardElementTest { + + private var table: Table? = null + + @Before + fun setUp() { + table = testTable(4) + } + + @Theory + fun computePoints_countsCards( + boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, + points: Int, gold: Int, color: Color + ) { + val board = table!!.getBoard(0, boardPosition) + addCards(board, nbCards, nbOtherCards, color) + + val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.CARD, gold, points, listOf(color)) + + assertEquals((nbCards * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + } + + @Theory + fun computePoints_countsDefeatTokens( + boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, + gold: Int + ) { + val board = table!!.getBoard(0, boardPosition) + for (i in 0 until nbDefeatTokens) { + board.military.defeat() + } + + val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.DEFEAT_TOKEN, gold, points, listOf()) + + assertEquals((nbDefeatTokens * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + } + + @Theory + fun computePoints_countsWonderStages( + boardPosition: RelativeBoardPosition, nbStages: Int, points: Int, + gold: Int + ) { + val board = table!!.getBoard(0, boardPosition) + for (i in 0 until nbStages) { + board.wonder.buildLevel(CardBack("")) + } + + val bonus = + BonusPerBoardElement(listOf(boardPosition), BoardElementType.BUILT_WONDER_STAGES, gold, points, listOf()) + + assertEquals((nbStages * points).toLong(), bonus.computePoints(table!!, 0).toLong()) + } + + @Theory + fun apply_countsCards( + boardPosition: RelativeBoardPosition, nbCards: Int, nbOtherCards: Int, points: Int, + gold: Int, color: Color + ) { + val board = table!!.getBoard(0, boardPosition) + addCards(board, nbCards, nbOtherCards, color) + + val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.CARD, gold, points, listOf(color)) + + val selfBoard = table!!.getBoard(0) + val initialGold = selfBoard.gold + bonus.apply(table!!, 0) + assertEquals((initialGold + nbCards * gold).toLong(), selfBoard.gold.toLong()) + } + + @Theory + fun apply_countsDefeatTokens( + boardPosition: RelativeBoardPosition, nbDefeatTokens: Int, points: Int, + gold: Int + ) { + val board = table!!.getBoard(0, boardPosition) + for (i in 0 until nbDefeatTokens) { + board.military.defeat() + } + + val bonus = BonusPerBoardElement(listOf(boardPosition), BoardElementType.DEFEAT_TOKEN, gold, points, listOf()) + + val selfBoard = table!!.getBoard(0) + val initialGold = selfBoard.gold + bonus.apply(table!!, 0) + assertEquals((initialGold + nbDefeatTokens * gold).toLong(), selfBoard.gold.toLong()) + } + + @Theory + fun apply_countsWonderStages(boardPosition: RelativeBoardPosition, nbStages: Int, points: Int, gold: Int) { + val board = table!!.getBoard(0, boardPosition) + for (i in 0 until nbStages) { + board.wonder.buildLevel(CardBack("")) + } + + val bonus = + BonusPerBoardElement(listOf(boardPosition), BoardElementType.BUILT_WONDER_STAGES, gold, points, listOf()) + + val selfBoard = table!!.getBoard(0) + val initialGold = selfBoard.gold + bonus.apply(table!!, 0) + assertEquals((initialGold + nbStages * gold).toLong(), selfBoard.gold.toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun values(): IntArray { + return intArrayOf(0, 1, 2, 3) + } + + @JvmStatic + @DataPoints + fun colors(): Array<Color> { + return Color.values() + } + + @JvmStatic + @DataPoints + fun positions(): Array<RelativeBoardPosition> { + return RelativeBoardPosition.values() + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt new file mode 100644 index 00000000..e228d585 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/DiscountTest.kt @@ -0,0 +1,72 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.Assert.assertEquals +import org.junit.Assume +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.resources.Provider +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.test.createTransactions +import org.luxons.sevenwonders.game.test.testBoard + +@RunWith(Theories::class) +class DiscountTest { + + @Theory + fun apply_givesDiscountedPrice(discountedPrice: Int, discountedType: ResourceType, provider: Provider) { + val board = testBoard(ResourceType.CLAY, 3) + val discount = Discount(listOf(discountedType), listOf(provider), discountedPrice) + discount.apply(board) + + val transactions = createTransactions(provider, discountedType) + assertEquals(discountedPrice.toLong(), board.tradingRules.computeCost(transactions).toLong()) + } + + @Theory + fun apply_doesNotAffectOtherResources( + discountedPrice: Int, discountedType: ResourceType, provider: Provider, + otherType: ResourceType, otherProvider: Provider + ) { + Assume.assumeTrue(otherProvider != provider) + Assume.assumeTrue(otherType != discountedType) + + val board = testBoard(ResourceType.CLAY, 3) + val discount = Discount(listOf(discountedType), listOf(provider), discountedPrice) + discount.apply(board) + + // this is the default in the settings used by TestUtilsKt.testBoard() + val normalPrice = 2 + + val fromOtherType = createTransactions(provider, otherType) + assertEquals(normalPrice.toLong(), board.tradingRules.computeCost(fromOtherType).toLong()) + + val fromOtherProvider = createTransactions(otherProvider, discountedType) + assertEquals(normalPrice.toLong(), board.tradingRules.computeCost(fromOtherProvider).toLong()) + + val fromOtherProviderAndType = createTransactions(otherProvider, otherType) + assertEquals(normalPrice.toLong(), board.tradingRules.computeCost(fromOtherProviderAndType).toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun discountedPrices(): IntArray { + return intArrayOf(0, 1, 2) + } + + @JvmStatic + @DataPoints + fun resourceTypes(): Array<ResourceType> { + return ResourceType.values() + } + + @JvmStatic + @DataPoints + fun providers(): Array<Provider> { + return Provider.values() + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt new file mode 100644 index 00000000..175f15ea --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/GoldIncreaseTest.kt @@ -0,0 +1,46 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals + +@RunWith(Theories::class) +class GoldIncreaseTest { + + @Theory + fun apply_increaseGoldWithRightAmount(initialAmount: Int, goldIncreaseAmount: Int, type: ResourceType) { + val board = testBoard(type, initialAmount) + val goldIncrease = GoldIncrease(goldIncreaseAmount) + + goldIncrease.apply(board) + + assertEquals((initialAmount + goldIncreaseAmount).toLong(), board.gold.toLong()) + } + + @Theory + fun computePoints_isAlwaysZero(gold: Int) { + val goldIncrease = GoldIncrease(gold) + val table = testTable(5) + assertEquals(0, goldIncrease.computePoints(table, 0).toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun goldAmounts(): IntArray { + return intArrayOf(-5, -1, 0, 1, 2, 5, 10) + } + + @JvmStatic + @DataPoints + fun resourceTypes(): Array<ResourceType> { + return ResourceType.values() + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt new file mode 100644 index 00000000..a8bd01d6 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/MilitaryReinforcementsTest.kt @@ -0,0 +1,47 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals + +@RunWith(Theories::class) +class MilitaryReinforcementsTest { + + @Theory + fun apply_increaseGoldWithRightAmount(initialShields: Int, additionalShields: Int, type: ResourceType) { + val board = testBoard(type) + board.military.addShields(initialShields) + + val reinforcements = MilitaryReinforcements(additionalShields) + reinforcements.apply(board) + + assertEquals((initialShields + additionalShields).toLong(), board.military.nbShields.toLong()) + } + + @Theory + fun computePoints_isAlwaysZero(shields: Int) { + val reinforcements = MilitaryReinforcements(shields) + val table = testTable(5) + assertEquals(0, reinforcements.computePoints(table, 0).toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun shieldCounts(): IntArray { + return intArrayOf(0, 1, 2, 3, 5) + } + + @JvmStatic + @DataPoints + fun resourceTypes(): Array<ResourceType> { + return ResourceType.values() + } + } +} 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 new file mode 100644 index 00000000..6f8da55d --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.kt @@ -0,0 +1,72 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue + +@RunWith(Theories::class) +class ProductionIncreaseTest { + + @Theory + fun apply_boardContainsAddedResourceType( + initialType: ResourceType, + addedType: ResourceType, + extraType: ResourceType + ) { + val board = testBoard(initialType) + val effect = ProductionIncrease(fixedProduction(addedType), false) + + effect.apply(board) + + val resources = createResources(initialType, addedType) + assertTrue(board.production.contains(resources)) + assertFalse(board.publicProduction.contains(resources)) + + val moreResources = createResources(initialType, addedType, extraType) + assertFalse(board.production.contains(moreResources)) + assertFalse(board.publicProduction.contains(moreResources)) + } + + @Theory + fun apply_boardContainsAddedResourceType_sellable( + initialType: ResourceType, + addedType: ResourceType, + extraType: ResourceType + ) { + val board = testBoard(initialType) + val effect = ProductionIncrease(fixedProduction(addedType), true) + + effect.apply(board) + + val resources = createResources(initialType, addedType) + assertTrue(board.production.contains(resources)) + assertTrue(board.publicProduction.contains(resources)) + + val moreResources = createResources(initialType, addedType, extraType) + assertFalse(board.production.contains(moreResources)) + assertFalse(board.publicProduction.contains(moreResources)) + } + + @Theory + fun computePoints_isAlwaysZero(addedType: ResourceType) { + val effect = ProductionIncrease(fixedProduction(addedType), false) + val table = testTable(5) + assertEquals(0, effect.computePoints(table, 0).toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun resourceTypes(): Array<ResourceType> { + return ResourceType.values() + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt new file mode 100644 index 00000000..844c28eb --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/RawPointsIncreaseTest.kt @@ -0,0 +1,28 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.Assert.assertEquals +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.testTable + +@RunWith(Theories::class) +class RawPointsIncreaseTest { + + @Theory + fun computePoints_equalsNbOfPoints(points: Int) { + val rawPointsIncrease = RawPointsIncrease(points) + val table = testTable(5) + assertEquals(points.toLong(), rawPointsIncrease.computePoints(table, 0).toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun points(): IntArray { + return intArrayOf(0, 1, 2, 3, 5) + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt new file mode 100644 index 00000000..cbea1581 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/ScienceProgressTest.kt @@ -0,0 +1,43 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.Assert.assertEquals +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.boards.ScienceType +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.test.createScience +import org.luxons.sevenwonders.game.test.createScienceProgress +import org.luxons.sevenwonders.game.test.testBoard + +@RunWith(Theories::class) +class ScienceProgressTest { + + @Theory + fun apply_initContainsAddedScience( + initCompasses: Int, initWheels: Int, initTablets: Int, initJokers: Int, + compasses: Int, wheels: Int, tablets: Int, jokers: Int + ) { + val board = testBoard(ResourceType.ORE) + val initialScience = createScience(initCompasses, initWheels, initTablets, initJokers) + board.science.addAll(initialScience) + + val effect = createScienceProgress(compasses, wheels, tablets, jokers) + effect.apply(board) + + assertEquals((initCompasses + compasses).toLong(), board.science.getQuantity(ScienceType.COMPASS).toLong()) + assertEquals((initWheels + wheels).toLong(), board.science.getQuantity(ScienceType.WHEEL).toLong()) + assertEquals((initTablets + tablets).toLong(), board.science.getQuantity(ScienceType.TABLET).toLong()) + assertEquals((initJokers + jokers).toLong(), board.science.jokers.toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun elementsCount(): IntArray { + return intArrayOf(0, 1, 2) + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt new file mode 100644 index 00000000..7282d60d --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/effects/SpecialAbilityActivationTest.kt @@ -0,0 +1,95 @@ +package org.luxons.sevenwonders.game.effects + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Assume +import org.junit.Test +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.boards.BoardElementType +import org.luxons.sevenwonders.game.boards.RelativeBoardPosition +import org.luxons.sevenwonders.game.cards.Card +import org.luxons.sevenwonders.game.cards.Color +import org.luxons.sevenwonders.game.test.createGuildCard +import org.luxons.sevenwonders.game.test.testTable + +@RunWith(Theories::class) +class SpecialAbilityActivationTest { + + @Theory + fun apply_addsAbility(ability: SpecialAbility) { + val effect = SpecialAbilityActivation(ability) + val table = testTable(5) + + effect.apply(table, 0) + + val board = table.getBoard(0) + assertTrue(board.hasSpecial(ability)) + } + + @Theory + fun computePoints_zeroExceptForCopyGuild(ability: SpecialAbility) { + Assume.assumeTrue(ability !== SpecialAbility.COPY_GUILD) + + val effect = SpecialAbilityActivation(ability) + val table = testTable(5) + + assertEquals(0, effect.computePoints(table, 0).toLong()) + } + + @Theory + fun computePoints_copiedGuild(guildCard: Card, neighbour: RelativeBoardPosition) { + val effect = SpecialAbilityActivation(SpecialAbility.COPY_GUILD) + val table = testTable(5) + + val neighbourBoard = table.getBoard(0, neighbour) + neighbourBoard.addCard(guildCard) + + val board = table.getBoard(0) + board.copiedGuild = guildCard + + val directPointsFromGuildCard = guildCard.effects.stream().mapToInt { e -> e.computePoints(table, 0) }.sum() + assertEquals(directPointsFromGuildCard.toLong(), effect.computePoints(table, 0).toLong()) + } + + @Test(expected = IllegalStateException::class) + fun computePoints_copyGuild_failWhenNoChosenGuild() { + val effect = SpecialAbilityActivation(SpecialAbility.COPY_GUILD) + val table = testTable(5) + effect.computePoints(table, 0) + } + + companion object { + + @JvmStatic + @DataPoints + fun abilities(): Array<SpecialAbility> { + return SpecialAbility.values() + } + + @JvmStatic + @DataPoints + fun neighbours(): Array<RelativeBoardPosition> { + return arrayOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT) + } + + @JvmStatic + @DataPoints + fun guilds(): Array<Card> { + val bonus = BonusPerBoardElement( + listOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.RIGHT), + BoardElementType.CARD, + points = 1, + colors = listOf(Color.GREY, Color.BROWN) + ) + val bonus2 = BonusPerBoardElement( + listOf(RelativeBoardPosition.LEFT, RelativeBoardPosition.SELF, RelativeBoardPosition.RIGHT), + BoardElementType.BUILT_WONDER_STAGES, + points = 1 + ) + return arrayOf(createGuildCard(1, bonus), createGuildCard(2, bonus2)) + } + } +} 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 85caa35f..28ab714d 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 @@ -183,9 +183,7 @@ fun getDifferentColorFrom(vararg colors: Color): Color { } fun createScienceProgress(compasses: Int, wheels: Int, tablets: Int, jokers: Int): ScienceProgress { - val progress = ScienceProgress() - progress.science = createScience(compasses, wheels, tablets, jokers) - return progress + return ScienceProgress(createScience(compasses, wheels, tablets, jokers)) } fun createScience(compasses: Int, wheels: Int, tablets: Int, jokers: Int): Science { |