summaryrefslogtreecommitdiff
path: root/game-engine/src/test/kotlin
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2018-07-09 11:37:41 +0200
committerjbion <joffrey.bion@amadeus.com>2018-07-09 11:37:41 +0200
commita829a1769bb14c1557b98378bd7e985c8dc54013 (patch)
tree735300c1eaa9543aa6b002cfe1aa1b648272c579 /game-engine/src/test/kotlin
parentKotlin mig: wonders package (diff)
downloadseven-wonders-a829a1769bb14c1557b98378bd7e985c8dc54013.tar.gz
seven-wonders-a829a1769bb14c1557b98378bd7e985c8dc54013.tar.bz2
seven-wonders-a829a1769bb14c1557b98378bd7e985c8dc54013.zip
Kotlin mig: Boards package
Diffstat (limited to 'game-engine/src/test/kotlin')
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/TableTest.kt8
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt226
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/MilitaryTest.kt71
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.kt48
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/ScienceTest.kt119
5 files changed, 468 insertions, 4 deletions
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/TableTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/TableTest.kt
index ab4655ce..3ae873a2 100644
--- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/TableTest.kt
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/TableTest.kt
@@ -47,10 +47,10 @@ class TableTest {
assumeTrue(nbPlayers >= 4)
val table = testTable(nbPlayers)
val guildCards = createGuildCards(4)
- table.getBoard(0).playedCards.add(guildCards[0])
- table.getBoard(0).playedCards.add(guildCards[1])
- table.getBoard(1).playedCards.add(guildCards[2])
- table.getBoard(2).playedCards.add(guildCards[3])
+ table.getBoard(0).addCard(guildCards[0])
+ table.getBoard(0).addCard(guildCards[1])
+ table.getBoard(1).addCard(guildCards[2])
+ table.getBoard(2).addCard(guildCards[3])
val neightbourCards0 = table.getNeighbourGuildCards(0)
assertEquals(1, neightbourCards0.size.toLong())
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
new file mode 100644
index 00000000..4142206b
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt
@@ -0,0 +1,226 @@
+package org.luxons.sevenwonders.game.boards
+
+import junit.framework.TestCase.assertEquals
+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.FromDataPoints
+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.Settings
+import org.luxons.sevenwonders.game.api.Table
+import org.luxons.sevenwonders.game.boards.Board.InsufficientFundsException
+import org.luxons.sevenwonders.game.cards.Color
+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.scoring.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.testBoard
+import org.luxons.sevenwonders.game.test.testCard
+import org.luxons.sevenwonders.game.test.testCustomizableSettings
+import org.luxons.sevenwonders.game.test.testWonder
+
+@RunWith(Theories::class)
+class BoardTest {
+
+ @JvmField
+ @Rule
+ var thrown: ExpectedException = ExpectedException.none()
+
+ @Theory
+ fun initialGold_respectsSettings(@FromDataPoints("gold") goldAmountInSettings: Int) {
+ val customSettings = testCustomizableSettings(goldAmountInSettings)
+ val settings = Settings(5, customSettings)
+ val board = Board(testWonder(), 0, settings)
+ assertEquals(goldAmountInSettings, board.gold)
+ }
+
+ @Theory
+ fun initialProduction_containsInitialResource(type: ResourceType) {
+ val board = Board(testWonder(type), 0, Settings(5))
+ val resources = createResources(type)
+ assertTrue(board.production.contains(resources))
+ assertTrue(board.publicProduction.contains(resources))
+ }
+
+ @Theory
+ fun removeGold_successfulWhenNotTooMuch(
+ @FromDataPoints("gold") initialGold: Int,
+ @FromDataPoints("gold") goldRemoved: Int
+ ) {
+ assumeTrue(goldRemoved >= 0)
+ assumeTrue(initialGold >= goldRemoved)
+ val board = Board(testWonder(), 0, Settings(5))
+ board.gold = initialGold
+ board.removeGold(goldRemoved)
+ assertEquals(initialGold - goldRemoved, board.gold)
+ }
+
+ @Theory
+ fun removeGold_failsWhenTooMuch(
+ @FromDataPoints("gold") initialGold: Int,
+ @FromDataPoints("gold") goldRemoved: Int
+ ) {
+ assumeTrue(goldRemoved >= 0)
+ assumeTrue(initialGold < goldRemoved)
+ thrown.expect(InsufficientFundsException::class.java)
+ val board = Board(testWonder(), 0, Settings(5))
+ board.gold = initialGold
+ board.removeGold(goldRemoved)
+ }
+
+ @Theory
+ fun getNbCardsOfColor_properCount_singleColor(
+ type: ResourceType, @FromDataPoints("nbCards") nbCards: Int,
+ @FromDataPoints("nbCards") nbOtherCards: Int, color: Color
+ ) {
+ val board = testBoard(type)
+ addCards(board, nbCards, nbOtherCards, color)
+ assertEquals(nbCards, board.getNbCardsOfColor(listOf(color)))
+ }
+
+ @Theory
+ fun getNbCardsOfColor_properCount_multiColors(
+ type: ResourceType, @FromDataPoints("nbCards") nbCards1: Int,
+ @FromDataPoints("nbCards") nbCards2: Int,
+ @FromDataPoints("nbCards") nbOtherCards: Int, color1: Color,
+ color2: Color
+ ) {
+ val board = testBoard(type)
+ addCards(board, nbCards1, color1)
+ addCards(board, nbCards2, color2)
+ addCards(board, nbOtherCards, getDifferentColorFrom(color1, color2))
+ assertEquals(nbCards1 + nbCards2, board.getNbCardsOfColor(listOf(color1, color2)))
+ }
+
+ @Test
+ fun setCopiedGuild_succeedsOnPurpleCard() {
+ val board = testBoard(ResourceType.CLAY)
+ val card = testCard(Color.PURPLE)
+
+ board.copiedGuild = card
+ assertSame(card, board.copiedGuild)
+ }
+
+ @Theory
+ fun setCopiedGuild_failsOnNonPurpleCard(color: Color) {
+ assumeTrue(color !== Color.PURPLE)
+ val board = testBoard(ResourceType.CLAY)
+ val card = testCard(color)
+
+ thrown.expect(IllegalArgumentException::class.java)
+ board.copiedGuild = card
+ }
+
+ @Theory
+ fun hasSpecial(applied: SpecialAbility, tested: SpecialAbility) {
+ val board = testBoard(ResourceType.CLAY)
+ val table = Table(listOf(board))
+ val special = SpecialAbilityActivation(applied)
+
+ special.apply(table, 0)
+
+ assertEquals(applied === tested, board.hasSpecial(tested))
+ }
+
+ @Test
+ fun canPlayFreeCard() {
+ val board = testBoard(ResourceType.CLAY)
+ val table = Table(listOf(board))
+ val special = SpecialAbilityActivation(SpecialAbility.ONE_FREE_PER_AGE)
+
+ special.apply(table, 0)
+
+ assertTrue(board.canPlayFreeCard(0))
+ assertTrue(board.canPlayFreeCard(1))
+ assertTrue(board.canPlayFreeCard(2))
+
+ board.consumeFreeCard(0)
+
+ assertFalse(board.canPlayFreeCard(0))
+ assertTrue(board.canPlayFreeCard(1))
+ assertTrue(board.canPlayFreeCard(2))
+
+ board.consumeFreeCard(1)
+
+ assertFalse(board.canPlayFreeCard(0))
+ assertFalse(board.canPlayFreeCard(1))
+ assertTrue(board.canPlayFreeCard(2))
+
+ board.consumeFreeCard(2)
+
+ assertFalse(board.canPlayFreeCard(0))
+ assertFalse(board.canPlayFreeCard(1))
+ assertFalse(board.canPlayFreeCard(2))
+ }
+
+ @Theory
+ fun computePoints_gold(@FromDataPoints("gold") gold: Int) {
+ assumeTrue(gold >= 0)
+ val board = testBoard(ResourceType.WOOD)
+ val table = Table(listOf(board))
+ board.gold = gold
+
+ val score = board.computePoints(table)
+ assertEquals(gold / 3, score.getPoints(ScoreCategory.GOLD))
+ assertEquals(gold / 3, score.totalPoints)
+ }
+
+ @Theory
+ fun computePoints_(@FromDataPoints("gold") gold: Int) {
+ assumeTrue(gold >= 0)
+ val board = testBoard(ResourceType.WOOD)
+ val table = Table(listOf(board))
+ board.gold = gold
+
+ val effect = RawPointsIncrease(5)
+ playCardWithEffect(table, 0, Color.BLUE, effect)
+
+ val score = board.computePoints(table)
+ assertEquals(gold / 3, score.getPoints(ScoreCategory.GOLD))
+ assertEquals(5, score.getPoints(ScoreCategory.CIVIL))
+ assertEquals(5 + gold / 3, score.totalPoints)
+ }
+
+ companion object {
+
+ @JvmStatic
+ @DataPoints("gold")
+ fun goldAmounts(): IntArray {
+ return intArrayOf(-3, -1, 0, 1, 2, 3)
+ }
+
+ @JvmStatic
+ @DataPoints("nbCards")
+ fun nbCards(): IntArray {
+ return intArrayOf(0, 1, 2)
+ }
+
+ @JvmStatic
+ @DataPoints
+ fun resourceTypes(): Array<ResourceType> {
+ return ResourceType.values()
+ }
+
+ @JvmStatic
+ @DataPoints
+ fun colors(): Array<Color> {
+ return Color.values()
+ }
+
+ @JvmStatic
+ @DataPoints
+ fun specialAbilities(): Array<SpecialAbility> {
+ return SpecialAbility.values()
+ }
+ }
+}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/MilitaryTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/MilitaryTest.kt
new file mode 100644
index 00000000..34a599c9
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/MilitaryTest.kt
@@ -0,0 +1,71 @@
+package org.luxons.sevenwonders.game.boards
+
+import java.util.HashMap
+
+import org.junit.Rule
+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.rules.ExpectedException
+import org.junit.runner.RunWith
+import org.luxons.sevenwonders.game.boards.Military.UnknownAgeException
+
+import org.junit.Assert.assertEquals
+
+@RunWith(Theories::class)
+class MilitaryTest {
+
+ @JvmField
+ @Rule
+ var thrown = ExpectedException.none()
+
+ @Theory
+ fun victory_addsCorrectPoints(
+ @FromDataPoints("ages") age: Int,
+ @FromDataPoints("points") nbPointsPerVictory: Int
+ ) {
+ val military = createMilitary(age, nbPointsPerVictory, 0)
+ val initialPoints = military.totalPoints
+
+ military.victory(age)
+ assertEquals((initialPoints + nbPointsPerVictory).toLong(), military.totalPoints.toLong())
+ }
+
+ @Theory
+ fun victory_failsIfUnknownAge(@FromDataPoints("points") nbPointsPerVictory: Int) {
+ val military = createMilitary(0, nbPointsPerVictory, 0)
+ thrown.expect(UnknownAgeException::class.java)
+ military.victory(1)
+ }
+
+ @Theory
+ fun defeat_removesCorrectPoints(@FromDataPoints("points") nbPointsLostPerDefeat: Int) {
+ val military = createMilitary(0, 0, nbPointsLostPerDefeat)
+ val initialPoints = military.totalPoints
+
+ military.defeat()
+ assertEquals((initialPoints - nbPointsLostPerDefeat).toLong(), military.totalPoints.toLong())
+ }
+
+ companion object {
+
+ @JvmStatic
+ @DataPoints("points")
+ fun points(): IntArray {
+ return intArrayOf(0, 1, 3, 5)
+ }
+
+ @JvmStatic
+ @DataPoints("ages")
+ fun ages(): IntArray {
+ return intArrayOf(1, 2, 3)
+ }
+
+ private fun createMilitary(age: Int, nbPointsPerVictory: Int, nbPointsPerDefeat: Int): Military {
+ val wonPointsPerAge = HashMap<Int, Int>()
+ wonPointsPerAge[age] = nbPointsPerVictory
+ return Military(nbPointsPerDefeat, wonPointsPerAge)
+ }
+ }
+}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.kt
new file mode 100644
index 00000000..d9c79ac5
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/RelativeBoardPositionTest.kt
@@ -0,0 +1,48 @@
+package org.luxons.sevenwonders.game.boards
+
+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.junit.Assert.assertEquals
+import org.junit.Assume.assumeTrue
+
+@RunWith(Theories::class)
+class RelativeBoardPositionTest {
+
+ @Theory
+ fun getIndexFrom_wrapLeft(nbPlayers: Int) {
+ assumeTrue(nbPlayers >= 2)
+ val last = nbPlayers - 1
+ assertEquals(last.toLong(), RelativeBoardPosition.LEFT.getIndexFrom(0, nbPlayers).toLong())
+ assertEquals(0, RelativeBoardPosition.SELF.getIndexFrom(0, nbPlayers).toLong())
+ assertEquals(1, RelativeBoardPosition.RIGHT.getIndexFrom(0, nbPlayers).toLong())
+ }
+
+ @Theory
+ fun getIndexFrom_wrapRight(nbPlayers: Int) {
+ assumeTrue(nbPlayers >= 2)
+ val last = nbPlayers - 1
+ assertEquals((last - 1).toLong(), RelativeBoardPosition.LEFT.getIndexFrom(last, nbPlayers).toLong())
+ assertEquals(last.toLong(), RelativeBoardPosition.SELF.getIndexFrom(last, nbPlayers).toLong())
+ assertEquals(0, RelativeBoardPosition.RIGHT.getIndexFrom(last, nbPlayers).toLong())
+ }
+
+ @Theory
+ fun getIndexFrom_noWrap(nbPlayers: Int) {
+ assumeTrue(nbPlayers >= 3)
+ assertEquals(0, RelativeBoardPosition.LEFT.getIndexFrom(1, nbPlayers).toLong())
+ assertEquals(1, RelativeBoardPosition.SELF.getIndexFrom(1, nbPlayers).toLong())
+ assertEquals(2, RelativeBoardPosition.RIGHT.getIndexFrom(1, nbPlayers).toLong())
+ }
+
+ companion object {
+
+ @JvmStatic
+ @DataPoints
+ fun nbPlayers(): IntArray {
+ return intArrayOf(1, 2, 3, 5, 7, 9)
+ }
+ }
+}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/ScienceTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/ScienceTest.kt
new file mode 100644
index 00000000..8c2041e4
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/ScienceTest.kt
@@ -0,0 +1,119 @@
+package org.luxons.sevenwonders.game.boards
+
+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.test.*
+
+import org.junit.Assert.assertEquals
+
+@RunWith(Theories::class)
+class ScienceTest {
+
+ @Test
+ fun addAll_empty() {
+ val initial = createScience(3, 4, 5, 1)
+ val empty = Science()
+ initial.addAll(empty)
+ assertEquals(3, initial.getQuantity(ScienceType.COMPASS).toLong())
+ assertEquals(4, initial.getQuantity(ScienceType.WHEEL).toLong())
+ assertEquals(5, initial.getQuantity(ScienceType.TABLET).toLong())
+ assertEquals(1, initial.jokers.toLong())
+ }
+
+ @Test
+ fun addAll_noJoker() {
+ val initial = createScience(3, 4, 5, 1)
+ val other = createScience(1, 2, 3, 0)
+ initial.addAll(other)
+ assertEquals(4, initial.getQuantity(ScienceType.COMPASS).toLong())
+ assertEquals(6, initial.getQuantity(ScienceType.WHEEL).toLong())
+ assertEquals(8, initial.getQuantity(ScienceType.TABLET).toLong())
+ assertEquals(1, initial.jokers.toLong())
+ }
+
+ @Test
+ fun addAll_withJokers() {
+ val initial = createScience(3, 4, 5, 1)
+ val other = createScience(0, 0, 0, 3)
+ initial.addAll(other)
+ assertEquals(3, initial.getQuantity(ScienceType.COMPASS).toLong())
+ assertEquals(4, initial.getQuantity(ScienceType.WHEEL).toLong())
+ assertEquals(5, initial.getQuantity(ScienceType.TABLET).toLong())
+ assertEquals(4, initial.jokers.toLong())
+ }
+
+ @Test
+ fun addAll_mixed() {
+ val initial = createScience(3, 4, 5, 1)
+ val other = createScience(1, 2, 3, 4)
+ initial.addAll(other)
+ assertEquals(4, initial.getQuantity(ScienceType.COMPASS).toLong())
+ assertEquals(6, initial.getQuantity(ScienceType.WHEEL).toLong())
+ assertEquals(8, initial.getQuantity(ScienceType.TABLET).toLong())
+ assertEquals(5, initial.jokers.toLong())
+ }
+
+ @Theory
+ fun computePoints_compassesOnly_noJoker(compasses: Int) {
+ val science = createScience(compasses, 0, 0, 0)
+ assertEquals((compasses * compasses).toLong(), science.computePoints().toLong())
+ }
+
+ @Theory
+ fun computePoints_wheelsOnly_noJoker(wheels: Int) {
+ val science = createScience(0, wheels, 0, 0)
+ assertEquals((wheels * wheels).toLong(), science.computePoints().toLong())
+ }
+
+ @Theory
+ fun computePoints_tabletsOnly_noJoker(tablets: Int) {
+ val science = createScience(0, 0, tablets, 0)
+ assertEquals((tablets * tablets).toLong(), science.computePoints().toLong())
+ }
+
+ @Theory
+ fun computePoints_allSameNoJoker(eachSymbol: Int) {
+ val science = createScience(eachSymbol, eachSymbol, eachSymbol, 0)
+ assertEquals((3 * eachSymbol * eachSymbol + 7 * eachSymbol).toLong(), science.computePoints().toLong())
+ }
+
+ @Theory
+ fun computePoints_expectation(expectation: IntArray) {
+ val science = createScience(expectation[0], expectation[1], expectation[2], expectation[3])
+ assertEquals(expectation[4].toLong(), science.computePoints().toLong())
+ }
+
+ companion object {
+
+ @JvmStatic
+ @DataPoints
+ fun quantitiesWithExpectedPoints(): Array<IntArray> {
+ // compasses, wheels, tablets, jokers, expected points
+ return arrayOf(
+ intArrayOf(0, 0, 0, 1, 1),
+ intArrayOf(0, 0, 1, 0, 1),
+ intArrayOf(0, 0, 0, 2, 4),
+ intArrayOf(0, 0, 1, 1, 4),
+ intArrayOf(0, 0, 2, 0, 4),
+ intArrayOf(0, 0, 0, 3, 10),
+ intArrayOf(0, 0, 1, 2, 10),
+ intArrayOf(0, 1, 1, 1, 10),
+ intArrayOf(1, 1, 1, 0, 10),
+ intArrayOf(0, 0, 0, 4, 16),
+ intArrayOf(0, 0, 1, 3, 16),
+ intArrayOf(0, 0, 2, 2, 16),
+ intArrayOf(0, 0, 3, 1, 16),
+ intArrayOf(0, 0, 4, 0, 16)
+ )
+ }
+
+ @JvmStatic
+ @DataPoints
+ fun quantitiesDataPoints(): IntArray {
+ return intArrayOf(0, 1, 3, 5, 8)
+ }
+ }
+}
bgstack15