diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-06 00:43:50 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@amadeus.com> | 2018-07-06 16:06:23 +0200 |
commit | 9049dd004df71619e08a350c0a6e49455041d31b (patch) | |
tree | 3abc77d660f646bb64e9ab13549bdd16442de7cd /game-engine/src/test/kotlin/org/luxons | |
parent | Kotlin mig: game definitions (diff) | |
download | seven-wonders-9049dd004df71619e08a350c0a6e49455041d31b.tar.gz seven-wonders-9049dd004df71619e08a350c0a6e49455041d31b.tar.bz2 seven-wonders-9049dd004df71619e08a350c0a6e49455041d31b.zip |
Kotlin mig: cards package
Diffstat (limited to 'game-engine/src/test/kotlin/org/luxons')
7 files changed, 475 insertions, 24 deletions
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardBackTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardBackTest.kt new file mode 100644 index 00000000..68dcd5e7 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardBackTest.kt @@ -0,0 +1,15 @@ +package org.luxons.sevenwonders.game.cards + +import org.junit.Test + +import org.junit.Assert.assertEquals + +class CardBackTest { + + @Test + fun initializedWithImage() { + val imagePath = "whateverimage.png" + val (image) = CardBack(imagePath) + assertEquals(imagePath, image) + } +} 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 new file mode 100644 index 00000000..170ae1e4 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.cards + +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.luxons.sevenwonders.game.Settings +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.boards.Board +import org.luxons.sevenwonders.game.effects.ProductionIncrease +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.Resources +import org.luxons.sevenwonders.game.test.testCard +import org.luxons.sevenwonders.game.wonders.Wonder + +class CardTest { + + private var table: Table? = null + + private var treeFarmCard: Card? = null + + @Before + fun initBoard() { + val settings = Settings(3) + + val boards = listOf( + Board(Wonder("TestWonder", ResourceType.WOOD), 0, settings), + Board(Wonder("TestWonder", ResourceType.STONE), 1, settings), + Board(Wonder("TestWonder", ResourceType.PAPYRUS), 2, settings) + ) + table = Table(boards) + + val treeFarmRequirements = Requirements(1) + val treeFarmEffect = ProductionIncrease() + treeFarmEffect.production.addChoice(ResourceType.WOOD, ResourceType.CLAY) + + treeFarmCard = testCard("Tree Farm", Color.BROWN, treeFarmEffect, treeFarmRequirements) + } + + @Test + fun playCardCostingMoney() { + table!!.getBoard(0).gold = 3 + table!!.getBoard(1).gold = 3 + table!!.getBoard(2).gold = 3 + treeFarmCard!!.applyTo(table!!, 0, ResourceTransactions()) + 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/DecksTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/DecksTest.kt new file mode 100644 index 00000000..673f8946 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/DecksTest.kt @@ -0,0 +1,99 @@ +package org.luxons.sevenwonders.game.cards + +import org.junit.Assert.* +import org.junit.Assume.assumeTrue +import org.junit.Rule +import org.junit.Test +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.rules.ExpectedException +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.cards.Decks.CardNotFoundException +import org.luxons.sevenwonders.game.test.sampleCards + +@RunWith(Theories::class) +class DecksTest { + + @JvmField + @Rule + val thrown: ExpectedException = ExpectedException.none() + + @Test(expected = IllegalArgumentException::class) + fun getCard_failsOnEmptyNameWhenDeckIsEmpty() { + val decks = createDecks(0, 0) + decks.getCard(0, "") + } + + @Test(expected = IllegalArgumentException::class) + fun getCard_failsWhenDeckIsEmpty() { + val decks = createDecks(0, 0) + decks.getCard(0, "Any name") + } + + @Test(expected = CardNotFoundException::class) + fun getCard_failsWhenCardIsNotFound() { + val decks = createDecks(3, 20) + decks.getCard(1, "Unknown name") + } + + @Test + fun getCard_succeedsWhenCardIsFound() { + val decks = createDecks(3, 20) + val (name) = decks.getCard(1, "Test Card 3") + assertEquals("Test Card 3", name) + } + + @Test(expected = IllegalArgumentException::class) + fun deal_failsOnZeroPlayers() { + val decks = createDecks(3, 20) + decks.deal(1, 0) + } + + @Test(expected = IllegalArgumentException::class) + fun deal_failsOnMissingAge() { + val decks = createDecks(2, 0) + decks.deal(4, 10) + } + + @Theory + fun deal_failsWhenTooFewPlayers(nbPlayers: Int, nbCards: Int) { + assumeTrue(nbCards % nbPlayers != 0) + thrown.expect(IllegalArgumentException::class.java) + val decks = createDecks(1, nbCards) + decks.deal(1, nbPlayers) + } + + @Theory + fun deal_succeedsOnZeroCards(nbPlayers: Int) { + val decks = createDecks(1, 0) + val hands = decks.deal(1, nbPlayers) + for (i in 0 until nbPlayers) { + assertNotNull(hands[i]) + assertTrue(hands[i].isEmpty()) + } + } + + @Theory + fun deal_evenDistribution(nbPlayers: Int, nbCardsPerPlayer: Int) { + val nbCardsPerAge = nbPlayers * nbCardsPerPlayer + val decks = createDecks(1, nbCardsPerAge) + val hands = decks.deal(1, nbPlayers) + for (i in 0 until nbPlayers) { + assertEquals(nbCardsPerPlayer.toLong(), hands[i].size.toLong()) + } + } + + companion object { + + @JvmStatic + @DataPoints + fun dataPoints(): IntArray { + return intArrayOf(1, 2, 3, 5, 10) + } + + private fun createDecks(nbAges: Int, nbCardsPerAge: Int): Decks { + return Decks(IntRange(1, nbAges).map { it to sampleCards(0, nbCardsPerAge) }.toMap()) + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.kt new file mode 100644 index 00000000..d3a533ea --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandRotationDirectionTest.kt @@ -0,0 +1,15 @@ +package org.luxons.sevenwonders.game.cards + +import org.junit.Test + +import org.junit.Assert.assertEquals + +class HandRotationDirectionTest { + + @Test + fun testAgesDirections() { + assertEquals(HandRotationDirection.LEFT, HandRotationDirection.forAge(1)) + assertEquals(HandRotationDirection.RIGHT, HandRotationDirection.forAge(2)) + assertEquals(HandRotationDirection.LEFT, HandRotationDirection.forAge(3)) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt new file mode 100644 index 00000000..7ba21089 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/HandsTest.kt @@ -0,0 +1,125 @@ +package org.luxons.sevenwonders.game.cards + +import org.junit.Assert.* +import org.junit.Assume.assumeTrue +import org.junit.Test +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.FromDataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.test.sampleCards +import org.luxons.sevenwonders.game.test.testTable + +@RunWith(Theories::class) +class HandsTest { + + @Test(expected = IndexOutOfBoundsException::class) + fun get_failsOnMissingPlayer() { + val hands = createHands(4, 7) + hands[5] + } + + @Test + fun get_retrievesCorrectCards() { + val hand0 = sampleCards(0, 5) + val hand1 = sampleCards(5, 10) + val hands = Hands(listOf(hand0, hand1)) + assertEquals(hand0, hands[0]) + assertEquals(hand1, hands[1]) + } + + @Theory + fun isEmpty_falseWhenAtLeast1_allSame( + @FromDataPoints("nbPlayers") nbPlayers: Int, + @FromDataPoints("nbCardsPerPlayer") nbCardsPerPlayer: Int + ) { + assumeTrue(nbCardsPerPlayer >= 1) + val hands = createHands(nbPlayers, nbCardsPerPlayer) + assertFalse(hands.isEmpty) + } + + @Theory + fun isEmpty_trueWhenAllEmpty(@FromDataPoints("nbPlayers") nbPlayers: Int) { + val hands = createHands(nbPlayers, 0) + assertTrue(hands.isEmpty) + } + + @Theory + fun maxOneCardRemains_falseWhenAtLeast2_allSame( + @FromDataPoints("nbPlayers") nbPlayers: Int, + @FromDataPoints("nbCardsPerPlayer") nbCardsPerPlayer: Int + ) { + assumeTrue(nbCardsPerPlayer >= 2) + val hands = createHands(nbPlayers, nbCardsPerPlayer) + assertFalse(hands.maxOneCardRemains()) + } + + @Theory + fun maxOneCardRemains_trueWhenAtMost1_allSame( + @FromDataPoints("nbPlayers") nbPlayers: Int, + @FromDataPoints("nbCardsPerPlayer") nbCardsPerPlayer: Int + ) { + assumeTrue(nbCardsPerPlayer <= 1) + val hands = createHands(nbPlayers, nbCardsPerPlayer) + assertTrue(hands.maxOneCardRemains()) + } + + @Theory + fun maxOneCardRemains_trueWhenAtMost1_someZero(@FromDataPoints("nbPlayers") nbPlayers: Int) { + val hands = createHands(nbPlayers, 1) + hands[0].drop(1) + assertTrue(hands.maxOneCardRemains()) + } + + @Test + fun rotate_movesOfCorrectOffset_right() { + val hands = createHands(3, 7) + val rotated = hands.rotate(HandRotationDirection.RIGHT) + assertEquals(rotated[1], hands[0]) + assertEquals(rotated[2], hands[1]) + assertEquals(rotated[0], hands[2]) + } + + @Test + fun rotate_movesOfCorrectOffset_left() { + val hands = createHands(3, 7) + val rotated = hands.rotate(HandRotationDirection.LEFT) + assertEquals(rotated[2], hands[0]) + assertEquals(rotated[0], hands[1]) + assertEquals(rotated[1], hands[2]) + } + + @Test + fun createHand_containsAllCards() { + val hand0 = sampleCards(0, 5) + val hand1 = sampleCards(5, 10) + val hands = Hands(listOf(hand0, hand1)) + + val table = testTable(2) + val hand = hands.createHand(table, 0) + + for (handCard in hand) { + assertTrue(hand0.contains(handCard.card)) + } + } + + companion object { + + @JvmStatic + @DataPoints("nbCardsPerPlayer") + fun nbCardsPerPlayer(): IntArray { + return intArrayOf(0, 1, 2, 3, 4, 5, 6, 7) + } + + @JvmStatic + @DataPoints("nbPlayers") + fun nbPlayers(): IntArray { + return intArrayOf(3, 4, 5, 6, 7) + } + + private fun createHands(nbPlayers: Int, nbCardsPerPlayer: Int): Hands { + return sampleCards(0, nbCardsPerPlayer * nbPlayers).deal(nbPlayers) + } + } +} 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 new file mode 100644 index 00000000..196091a6 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/RequirementsTest.kt @@ -0,0 +1,158 @@ +package org.luxons.sevenwonders.game.cards + +import java.util.Arrays +import java.util.Collections + +import org.junit.Test +import org.junit.experimental.theories.DataPoints +import org.junit.experimental.theories.Theories +import org.junit.experimental.theories.Theory +import org.junit.runner.RunWith +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.boards.Board +import org.luxons.sevenwonders.game.resources.Provider +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.Resources +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue + +@RunWith(Theories::class) +class RequirementsTest { + + @Test + fun getResources_emptyAfterInit() { + val (_, resources) = Requirements() + assertTrue(resources.isEmpty) + } + + @Test + fun setResources_success() { + val resources = Resources() + val (_, resources1) = Requirements(0, resources) + assertSame(resources, resources1) + } + + @Theory + fun goldRequirement(boardGold: Int, requiredGold: Int) { + val requirements = Requirements(requiredGold) + + val board = testBoard(ResourceType.CLAY, boardGold) + val table = Table(listOf(board)) + + assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board)) + assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, ResourceTransactions())) + assertEquals(boardGold >= requiredGold, requirements.areMetBy(table, 0)) + } + + @Theory + fun resourceRequirement_initialResource(initialResource: ResourceType, requiredResource: ResourceType) { + val requirements = createRequirements(requiredResource) + + val board = testBoard(initialResource, 0) + val table = Table(listOf(board)) + + assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) + assertEquals( + initialResource == requiredResource, + requirements.areMetWithHelpBy(board, ResourceTransactions()) + ) + + if (initialResource == requiredResource) { + assertTrue(requirements.areMetBy(table, 0)) + } + } + + @Theory + fun resourceRequirement_ownProduction( + initialResource: ResourceType, producedResource: ResourceType, + requiredResource: ResourceType + ) { + assumeTrue(initialResource != requiredResource) + + val requirements = createRequirements(requiredResource) + + val board = testBoard(initialResource, 0) + board.production.addFixedResource(producedResource, 1) + val table = Table(listOf(board)) + + assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board)) + assertEquals( + producedResource == requiredResource, + requirements.areMetWithHelpBy(board, ResourceTransactions()) + ) + + if (producedResource == requiredResource) { + assertTrue(requirements.areMetBy(table, 0)) + } + } + + @Theory + fun resourceRequirement_boughtResource( + initialResource: ResourceType, boughtResource: ResourceType, + requiredResource: ResourceType + ) { + assumeTrue(initialResource != requiredResource) + + val requirements = createRequirements(requiredResource) + + val board = testBoard(initialResource, 2) + val neighbourBoard = testBoard(initialResource, 0) + neighbourBoard.publicProduction.addFixedResource(boughtResource, 1) + val table = Table(Arrays.asList(board, neighbourBoard)) + + val resources = createTransactions(Provider.RIGHT_PLAYER, boughtResource) + + assertFalse(requirements.areMetWithoutNeighboursBy(board)) + assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources)) + + if (boughtResource == requiredResource) { + assertTrue(requirements.areMetBy(table, 0)) + } + } + + @Theory + fun pay_boughtResource(initialResource: ResourceType, requiredResource: ResourceType) { + assumeTrue(initialResource != requiredResource) + + val requirements = createRequirements(requiredResource) + + val board = testBoard(initialResource, 2) + val neighbourBoard = testBoard(requiredResource, 0) + val table = Table(Arrays.asList(board, neighbourBoard)) + + val transactions = createTransactions( + Provider.RIGHT_PLAYER, + requiredResource + ) + + assertFalse(requirements.areMetWithoutNeighboursBy(board)) + assertTrue(requirements.areMetWithHelpBy(board, transactions)) + assertTrue(requirements.areMetBy(table, 0)) + + requirements.pay(table, 0, transactions) + + assertEquals(0, board.gold.toLong()) + assertEquals(2, neighbourBoard.gold.toLong()) + } + + companion object { + + @JvmStatic + @DataPoints + fun goldAmounts(): IntArray { + return intArrayOf(0, 1, 2, 5) + } + + @JvmStatic + @DataPoints + fun resourceTypes(): Array<ResourceType> { + return ResourceType.values() + } + } +} 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 65cf8d5b..85caa35f 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 @@ -115,17 +115,11 @@ fun createTransaction(provider: Provider, vararg resources: ResourceType): Resou fun createRequirements(vararg types: ResourceType): Requirements { val resources = createResources(*types) - val requirements = Requirements() - requirements.resources = resources - return requirements + return Requirements(resources = resources) } -fun createSampleCards(fromIndex: Int, nbCards: Int): List<Card> { - val sampleCards = ArrayList<Card>() - for (i in fromIndex until fromIndex + nbCards) { - sampleCards.add(testCard(i, Color.BLUE)) - } - return sampleCards +fun sampleCards(fromIndex: Int, nbCards: Int): List<Card> { + return List(nbCards) { i -> testCard(i + fromIndex, Color.BLUE) } } fun testCard(name: String): Card { @@ -148,14 +142,18 @@ fun createGuildCards(count: Int): List<Card> { return IntRange(0, count).map { createGuildCard(it) } } -fun createGuildCard(num: Int, vararg effects: Effect): Card { - return testCard("Test Guild $num", Color.PURPLE, *effects) +fun createGuildCard(num: Int, effect: Effect? = null): Card { + return testCard("Test Guild $num", Color.PURPLE, effect) } -private fun testCard(name: String, color: Color, vararg effects: Effect): Card { - val card = Card(name, color, Requirements(), Arrays.asList(*effects), null, null, "path/to/card/image") - card.back = createCardBack() - return card +fun testCard( + name: String = "Test Card", + color: Color = Color.BLUE, + effect: Effect? = null, + requirements: Requirements = Requirements() +): Card { + val effects = if (effect == null) emptyList() else listOf(effect) + return Card(name, color, requirements, effects, null, emptyList(), "path/to/card/image", createCardBack()) } private fun createCardBack(): CardBack { @@ -218,12 +216,3 @@ fun createMove(playerIndex: Int, card: Card, type: MoveType, vararg transactions val playerMove = PlayerMove(type, card.name, Arrays.asList(*transactions)) return type.resolve(playerIndex, card, playerMove) } - -@JvmOverloads -fun createPlayerMove( - type: MoveType, - cardName: String?, - transactions: Collection<ResourceTransaction> = emptySet() -): PlayerMove { - return PlayerMove(type, cardName, transactions) -} |