diff options
author | jbion <joffrey.bion@amadeus.com> | 2018-07-08 12:22:38 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2018-07-08 12:25:38 +0200 |
commit | f9167e3203a80cd50f50e43cda23464cba5cf78a (patch) | |
tree | c85ffebe6aae6cb729dbb60470465a86bc2740d4 /game-engine/src/test/kotlin/org | |
parent | Kotlin mig: effects package (diff) | |
download | seven-wonders-f9167e3203a80cd50f50e43cda23464cba5cf78a.tar.gz seven-wonders-f9167e3203a80cd50f50e43cda23464cba5cf78a.tar.bz2 seven-wonders-f9167e3203a80cd50f50e43cda23464cba5cf78a.zip |
Kotlin mig: serializers tests
Diffstat (limited to 'game-engine/src/test/kotlin/org')
10 files changed, 1045 insertions, 0 deletions
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt new file mode 100644 index 00000000..468ee5de --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt @@ -0,0 +1,14 @@ +package org.luxons.sevenwonders.game.data + +import org.junit.Assert.assertNotNull +import org.junit.Test + +class GameDefinitionLoaderTest { + + @Test + fun successfulLoad() { + val loader = GameDefinitionLoader() + val gameDefinition = loader.gameDefinition + assertNotNull(gameDefinition) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt new file mode 100644 index 00000000..3f010123 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt @@ -0,0 +1,20 @@ +package org.luxons.sevenwonders.game.data + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.luxons.sevenwonders.game.api.CustomizableSettings + +class GameDefinitionTest { + + @Test + fun successfulGameInit() { + val gameDefinition = GameDefinitionLoader().gameDefinition + assertNotNull(gameDefinition) + assertEquals(3, gameDefinition.minPlayers.toLong()) + assertEquals(7, gameDefinition.maxPlayers.toLong()) + + val game = gameDefinition.initGame(0, CustomizableSettings(), 7) + assertNotNull(game) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt new file mode 100644 index 00000000..5b84d466 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt @@ -0,0 +1,99 @@ +package org.luxons.sevenwonders.game.data.definitions + +import org.junit.Assert.assertEquals +import org.junit.Before +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.data.WonderSide +import org.luxons.sevenwonders.game.data.WonderSidePickMethod +import java.util.Random + +@RunWith(Theories::class) +class WonderSidePickMethodTest { + + private var random: Random? = null + + private var random2: Random? = null + + @Before + fun setUp() { + random = Random(123) // starts with TRUE + random2 = Random(123456) // starts with FALSE + } + + @Test + fun pick_allA() { + var side: WonderSide? = null + for (i in 0..9) { + side = WonderSidePickMethod.ALL_A.pickSide(random!!, side) + assertEquals(WonderSide.A, side) + } + } + + @Test + fun pick_allB() { + var side: WonderSide? = null + for (i in 0..9) { + side = WonderSidePickMethod.ALL_B.pickSide(random!!, side) + assertEquals(WonderSide.B, side) + } + } + + @Test + fun pick_eachRandom() { + var side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, null) + assertEquals(WonderSide.A, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, side) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, side) + assertEquals(WonderSide.A, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, side) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, side) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random!!, side) + assertEquals(WonderSide.A, side) + } + + @Test + fun pick_eachRandom2() { + var side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, null) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, side) + assertEquals(WonderSide.A, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, side) + assertEquals(WonderSide.A, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, side) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, side) + assertEquals(WonderSide.B, side) + side = WonderSidePickMethod.EACH_RANDOM.pickSide(random2!!, side) + assertEquals(WonderSide.B, side) + } + + @Theory + fun pick_allSameRandom_sameAsFirst(firstSide: WonderSide) { + var side = firstSide + for (i in 0..9) { + side = WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random!!, side) + assertEquals(firstSide, side) + } + } + + @Test + fun pick_allSameRandom_firstIsRandom() { + assertEquals(WonderSide.A, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random!!, null)) + assertEquals(WonderSide.B, WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random2!!, null)) + } + + companion object { + + @DataPoints + fun sides(): Array<WonderSide> { + return WonderSide.values() + } + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.kt new file mode 100644 index 00000000..22898c8a --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.kt @@ -0,0 +1,132 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import org.junit.Assert.assertEquals +import org.junit.Before +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.effects.GoldIncrease +import org.luxons.sevenwonders.game.effects.MilitaryReinforcements +import org.luxons.sevenwonders.game.effects.ProductionIncrease +import org.luxons.sevenwonders.game.effects.RawPointsIncrease +import org.luxons.sevenwonders.game.resources.Production + +@RunWith(Theories::class) +class NumericEffectSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + gson = GsonBuilder().registerTypeAdapter(MilitaryReinforcements::class.java, NumericEffectSerializer()) + .registerTypeAdapter(RawPointsIncrease::class.java, NumericEffectSerializer()) + .registerTypeAdapter(GoldIncrease::class.java, NumericEffectSerializer()) + // ProductionIncrease is not a numeric effect, it is here for negative testing purpose + .registerTypeAdapter(ProductionIncrease::class.java, NumericEffectSerializer()) + .create() + } + + @Test + fun serialize_militaryReinforcements_null() { + assertEquals("null", gson!!.toJson(null, MilitaryReinforcements::class.java)) + } + + @Test + fun serialize_rawPointsIncrease_null() { + assertEquals("null", gson!!.toJson(null, RawPointsIncrease::class.java)) + } + + @Test + fun serialize_goldIncrease_null() { + assertEquals("null", gson!!.toJson(null, GoldIncrease::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun serialize_failOnUnknownType() { + gson!!.toJson(ProductionIncrease(Production(), false)) + } + + @Theory + fun serialize_militaryReinforcements(count: Int) { + val reinforcements = MilitaryReinforcements(count) + assertEquals(count.toString(), gson!!.toJson(reinforcements)) + } + + @Theory + fun serialize_rawPointsIncrease(count: Int) { + val points = RawPointsIncrease(count) + assertEquals(count.toString(), gson!!.toJson(points)) + } + + @Theory + fun serialize_goldIncrease(count: Int) { + val goldIncrease = GoldIncrease(count) + assertEquals(count.toString(), gson!!.toJson(goldIncrease)) + } + + @Theory + fun deserialize_militaryReinforcements(count: Int) { + val reinforcements = MilitaryReinforcements(count) + assertEquals(reinforcements, gson!!.fromJson(count.toString(), MilitaryReinforcements::class.java)) + } + + @Theory + fun deserialize_rawPointsIncrease(count: Int) { + val points = RawPointsIncrease(count) + assertEquals(points, gson!!.fromJson(count.toString(), RawPointsIncrease::class.java)) + } + + @Theory + fun deserialize_goldIncrease(count: Int) { + val goldIncrease = GoldIncrease(count) + assertEquals(goldIncrease, gson!!.fromJson(count.toString(), GoldIncrease::class.java)) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_militaryReinforcements_failOnEmptyString() { + gson!!.fromJson("\"\"", MilitaryReinforcements::class.java) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_rawPointsIncrease_failOnEmptyString() { + gson!!.fromJson("\"\"", RawPointsIncrease::class.java) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_goldIncrease_failOnEmptyString() { + gson!!.fromJson("\"\"", GoldIncrease::class.java) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_militaryReinforcements_failOnNonNumericString() { + gson!!.fromJson("\"abc\"", MilitaryReinforcements::class.java) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_rawPointsIncrease_failOnNonNumericString() { + gson!!.fromJson("\"abc\"", RawPointsIncrease::class.java) + } + + @Test(expected = NumberFormatException::class) + fun deserialize_goldIncrease_failOnNonNumericString() { + gson!!.fromJson("\"abc\"", GoldIncrease::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnUnknownType() { + gson!!.fromJson("\"2\"", ProductionIncrease::class.java) + } + + companion object { + + @JvmStatic + @DataPoints + fun dataPoints(): IntArray { + return intArrayOf(-2, -1, 0, 1, 2, 5) + } + } +} 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 new file mode 100644 index 00000000..5b0a47ab --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.kt @@ -0,0 +1,180 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken +import org.junit.Assert.assertEquals +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.Production +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.Resources + +class ProductionIncreaseSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + val resourceTypeList = object : TypeToken<List<ResourceType>>() { + + }.type + gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()) + .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer()) + .registerTypeAdapter(Production::class.java, ProductionSerializer()) + .registerTypeAdapter(ProductionIncrease::class.java, ProductionIncreaseSerializer()) + .create() + } + + private fun create(sellable: Boolean, wood: Int, stone: Int, clay: Int): ProductionIncrease { + val production = Production() + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood) + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone) + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay) + } + return ProductionIncrease(production, sellable) + } + + private fun createChoice(sellable: Boolean, vararg types: ResourceType): ProductionIncrease { + val production = Production() + production.addChoice(*types) + return ProductionIncrease(production, sellable) + } + + @Test + fun serialize_nullAsNull() { + assertEquals("null", gson!!.toJson(null, ProductionIncrease::class.java)) + } + + @Test + fun serialize_emptyProdIncreaseAsNull() { + val prodIncrease = ProductionIncrease(Production(), false) + assertEquals("null", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_singleType() { + val prodIncrease = create(true, 1, 0, 0) + assertEquals("\"W\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_mixedTypes() { + val prodIncrease = create(true, 1, 1, 1) + assertEquals("\"WSC\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_mixedTypes_notSellable() { + val prodIncrease = create(false, 1, 1, 1) + assertEquals("\"(WSC)\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_choice2() { + val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY) + assertEquals("\"W/C\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_choice3() { + val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY) + assertEquals("\"W/O/C\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_choice3_notSellable() { + val prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY) + assertEquals("\"(W/O/C)\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_choice2_unordered() { + val prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD) + assertEquals("\"W/C\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test + fun serialize_choice3_unordered() { + val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE) + assertEquals("\"W/O/C\"", gson!!.toJson(prodIncrease, ProductionIncrease::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun serialize_failIfMultipleChoices() { + val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY) + prodIncrease.production.addChoice(ResourceType.ORE, ResourceType.GLASS) + gson!!.toJson(prodIncrease, ProductionIncrease::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun serialize_failIfMixedFixedAndChoices() { + val prodIncrease = create(true, 1, 0, 0) + prodIncrease.production.addChoice(ResourceType.WOOD, ResourceType.CLAY) + gson!!.toJson(prodIncrease, ProductionIncrease::class.java) + } + + @Test + fun deserialize_nullFromNull() { + assertNull(gson!!.fromJson("null", ProductionIncrease::class.java)) + } + + @Test + fun deserialize_emptyList() { + val prodIncrease = ProductionIncrease(Production(), true) + assertEquals(prodIncrease, gson!!.fromJson("\"\"", ProductionIncrease::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnGarbageString() { + gson!!.fromJson("\"this is garbage\"", ProductionIncrease::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnGarbageStringWithSlashes() { + gson!!.fromJson("\"this/is/garbage\"", ProductionIncrease::class.java) + } + + @Test + fun deserialize_singleType() { + val prodIncrease = create(true, 1, 0, 0) + assertEquals(prodIncrease, gson!!.fromJson("\"W\"", ProductionIncrease::class.java)) + } + + @Test + fun deserialize_multipleTimesSameType_notSellable() { + val prodIncrease = create(false, 3, 0, 0) + assertEquals(prodIncrease, gson!!.fromJson("\"(WWW)\"", ProductionIncrease::class.java)) + } + + @Test + fun deserialize_mixedTypes() { + val prodIncrease = create(true, 1, 1, 1) + assertEquals(prodIncrease, gson!!.fromJson("\"WCS\"", ProductionIncrease::class.java)) + } + + @Test + fun deserialize_choice2() { + val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY) + assertEquals(prodIncrease, gson!!.fromJson("\"W/C\"", ProductionIncrease::class.java)) + } + + @Test + fun deserialize_choice3_notSellable() { + val prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY) + assertEquals(prodIncrease, gson!!.fromJson("\"(W/O/C)\"", ProductionIncrease::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnMultipleResourcesInChoice() { + gson!!.fromJson("\"W/SS/C\"", ProductionIncrease::class.java) + } +} 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 new file mode 100644 index 00000000..f74e21c5 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.kt @@ -0,0 +1,196 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Before +import org.junit.Test +import org.luxons.sevenwonders.game.resources.Production +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.Resources + +class ProductionSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + val resourceTypeList = object : TypeToken<List<ResourceType>>() { + + }.type + gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()) + .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer()) + .registerTypeAdapter(Production::class.java, ProductionSerializer()) + .create() + } + + private fun create(wood: Int, stone: Int, clay: Int): Production { + val production = Production() + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood) + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone) + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay) + } + return production + } + + private fun createChoice(vararg types: ResourceType): Production { + val production = Production() + production.addChoice(*types) + return production + } + + @Test + fun serialize_nullAsNull() { + assertEquals("null", gson!!.toJson(null, Production::class.java)) + } + + @Test + fun serialize_emptyProdIncreaseAsNull() { + val prodIncrease = Production() + assertEquals("null", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_singleType() { + val prodIncrease = create(1, 0, 0) + assertEquals("\"W\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_multipleTimesSameType() { + val prodIncrease = create(3, 0, 0) + assertEquals("\"WWW\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_mixedTypes() { + val prodIncrease = create(1, 1, 1) + assertEquals("\"WSC\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_mixedTypesMultiple() { + val prodIncrease = create(2, 1, 2) + assertEquals("\"WWSCC\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_choice2() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY) + assertEquals("\"W/C\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_choice3() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY) + assertEquals("\"W/O/C\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_choice2_unordered() { + val prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD) + assertEquals("\"W/C\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test + fun serialize_choice3_unordered() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE) + assertEquals("\"W/O/C\"", gson!!.toJson(prodIncrease, Production::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun serialize_failIfMultipleChoices() { + val production = createChoice(ResourceType.WOOD, ResourceType.CLAY) + production.addChoice(ResourceType.ORE, ResourceType.GLASS) + gson!!.toJson(production, Production::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun serialize_failIfMixedFixedAndChoices() { + val production = create(1, 0, 0) + production.addChoice(ResourceType.WOOD, ResourceType.CLAY) + gson!!.toJson(production, Production::class.java) + } + + @Test + fun deserialize_nullFromNull() { + assertNull(gson!!.fromJson("null", Production::class.java)) + } + + @Test + fun deserialize_emptyList() { + val prodIncrease = Production() + assertEquals(prodIncrease, gson!!.fromJson("\"\"", Production::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnGarbageString() { + gson!!.fromJson("\"this is garbage\"", Production::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnGarbageStringWithSlashes() { + gson!!.fromJson("\"this/is/garbage\"", Production::class.java) + } + + @Test + fun deserialize_singleType() { + val prodIncrease = create(1, 0, 0) + assertEquals(prodIncrease, gson!!.fromJson("\"W\"", Production::class.java)) + } + + @Test + fun deserialize_multipleTimesSameType() { + val prodIncrease = create(3, 0, 0) + assertEquals(prodIncrease, gson!!.fromJson("\"WWW\"", Production::class.java)) + } + + @Test + fun deserialize_mixedTypes() { + val prodIncrease = create(1, 1, 1) + assertEquals(prodIncrease, gson!!.fromJson("\"WCS\"", Production::class.java)) + } + + @Test + fun deserialize_mixedTypes_unordered() { + val prodIncrease = create(1, 3, 2) + assertEquals(prodIncrease, gson!!.fromJson("\"SCWCSS\"", Production::class.java)) + } + + @Test + fun deserialize_choice2() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY) + assertEquals(prodIncrease, gson!!.fromJson("\"W/C\"", Production::class.java)) + } + + @Test + fun deserialize_choice3() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY) + assertEquals(prodIncrease, gson!!.fromJson("\"W/O/C\"", Production::class.java)) + } + + @Test + fun deserialize_choice2_unordered() { + val prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD) + assertEquals(prodIncrease, gson!!.fromJson("\"W/C\"", Production::class.java)) + } + + @Test + fun deserialize_choice3_unordered() { + val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE) + assertEquals(prodIncrease, gson!!.fromJson("\"W/O/C\"", Production::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnMultipleResourcesInChoice() { + gson!!.fromJson("\"W/SS/C\"", Production::class.java) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.kt new file mode 100644 index 00000000..79e51bd0 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.kt @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +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 + +class ResourceTypeSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + gson = GsonBuilder().registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()).create() + } + + @Test + fun serialize_useSymbolForEachType() { + for (type in ResourceType.values()) { + val expectedJson = "\"" + type.symbol + "\"" + assertEquals(expectedJson, gson!!.toJson(type)) + } + } + + @Test + fun deserialize_useSymbolForEachType() { + for (type in ResourceType.values()) { + val typeInJson = "\"" + type.symbol + "\"" + assertEquals(type, gson!!.fromJson(typeInJson, ResourceType::class.java)) + } + } + + @Test + fun deserialize_nullFromNull() { + assertNull(gson!!.fromJson("null", ResourceType::class.java)) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failsOnEmptyString() { + gson!!.fromJson("\"\"", ResourceType::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failsOnGarbageString() { + gson!!.fromJson("\"thisisgarbage\"", ResourceType::class.java) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.kt new file mode 100644 index 00000000..24aed03d --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.kt @@ -0,0 +1,100 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken +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 java.lang.reflect.Type +import java.util.ArrayList + +class ResourceTypesSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + gson = GsonBuilder().registerTypeAdapter(createListTypeToken(), ResourceTypesSerializer()).create() + } + + private fun createListTypeToken(): Type { + return object : TypeToken<List<ResourceType>>() { + + }.type + } + + @Test + fun serialize_null() { + assertEquals("null", gson!!.toJson(null, createListTypeToken())) + } + + @Test + fun serialize_emptyList() { + val types = ArrayList<ResourceType>() + assertEquals("\"\"", gson!!.toJson(types, createListTypeToken())) + } + + @Test + fun serialize_singleType() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + assertEquals("\"W\"", gson!!.toJson(types, createListTypeToken())) + } + + @Test + fun serialize_multipleTimesSameType() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + types.add(ResourceType.WOOD) + types.add(ResourceType.WOOD) + assertEquals("\"WWW\"", gson!!.toJson(types, createListTypeToken())) + } + + @Test + fun serialize_mixedTypes() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + types.add(ResourceType.CLAY) + types.add(ResourceType.STONE) + assertEquals("\"WCS\"", gson!!.toJson(types, createListTypeToken())) + } + + @Test + fun deserialize_null() { + assertNull(gson!!.fromJson("null", createListTypeToken())) + } + + @Test + fun deserialize_emptyList() { + val types = ArrayList<ResourceType>() + assertEquals(types, gson!!.fromJson("\"\"", createListTypeToken())) + } + + @Test + fun deserialize_singleType() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + assertEquals(types, gson!!.fromJson("\"W\"", createListTypeToken())) + } + + @Test + fun deserialize_multipleTimesSameType() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + types.add(ResourceType.WOOD) + types.add(ResourceType.WOOD) + assertEquals(types, gson!!.fromJson("\"WWW\"", createListTypeToken())) + } + + @Test + fun deserialize_mixedTypes() { + val types = ArrayList<ResourceType>() + types.add(ResourceType.WOOD) + types.add(ResourceType.CLAY) + types.add(ResourceType.STONE) + assertEquals(types, gson!!.fromJson("\"WCS\"", createListTypeToken())) + } +} 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 new file mode 100644 index 00000000..ffb4114c --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.kt @@ -0,0 +1,107 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +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.Resources + +class ResourcesSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer()).create() + } + + @Test + fun serialize_null() { + assertEquals("null", gson!!.toJson(null, Resources::class.java)) + } + + @Test + fun serialize_emptyResourcesToNull() { + val resources = Resources() + assertEquals("null", gson!!.toJson(resources)) + } + + @Test + fun serialize_singleType() { + val resources = Resources() + resources.add(ResourceType.WOOD, 1) + assertEquals("\"W\"", gson!!.toJson(resources)) + } + + @Test + fun serialize_multipleTimesSameType() { + val resources = Resources() + resources.add(ResourceType.WOOD, 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) + 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) + assertEquals("\"WWSCC\"", gson!!.toJson(resources)) + } + + @Test + fun deserialize_null() { + assertNull(gson!!.fromJson("null", Resources::class.java)) + } + + @Test + fun deserialize_emptyList() { + val resources = Resources() + assertEquals(resources, gson!!.fromJson("\"\"", Resources::class.java)) + } + + @Test + fun deserialize_singleType() { + val resources = Resources() + resources.add(ResourceType.WOOD, 1) + assertEquals(resources, gson!!.fromJson("\"W\"", Resources::class.java)) + } + + @Test + fun deserialize_multipleTimesSameType() { + val resources = Resources() + resources.add(ResourceType.WOOD, 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) + 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) + assertEquals(resources, gson!!.fromJson("\"SCWCSS\"", Resources::class.java)) + } +} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.kt new file mode 100644 index 00000000..adafced2 --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.kt @@ -0,0 +1,147 @@ +package org.luxons.sevenwonders.game.data.serializers + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Before +import org.junit.Test +import org.luxons.sevenwonders.game.boards.ScienceType +import org.luxons.sevenwonders.game.effects.ScienceProgress +import org.luxons.sevenwonders.game.test.createScienceProgress + +class ScienceProgressSerializerTest { + + private var gson: Gson? = null + + @Before + fun setUp() { + gson = GsonBuilder().registerTypeAdapter(ScienceProgress::class.java, ScienceProgressSerializer()).create() + } + + @Test + fun serialize_emptyToNull() { + val progress = createScienceProgress(0, 0, 0, 0) + val json = gson!!.toJson(progress) + assertEquals("null", json) + } + + @Test + fun serialize_oneCompass() { + val progress = createScienceProgress(1, 0, 0, 0) + val json = gson!!.toJson(progress) + assertEquals(COMPASS_STR, json) + } + + @Test + fun serialize_oneWheel() { + val progress = createScienceProgress(0, 1, 0, 0) + val json = gson!!.toJson(progress) + assertEquals(WHEEL_STR, json) + } + + @Test + fun serialize_oneTablet() { + val progress = createScienceProgress(0, 0, 1, 0) + val json = gson!!.toJson(progress) + assertEquals(TABLET_STR, json) + } + + @Test + fun serialize_oneJoker() { + val progress = createScienceProgress(0, 0, 0, 1) + val json = gson!!.toJson(progress) + assertEquals(JOKER_STR, json) + } + + @Test(expected = UnsupportedOperationException::class) + fun serialize_failOnMultipleCompasses() { + val progress = createScienceProgress(2, 0, 0, 0) + gson!!.toJson(progress) + } + + @Test(expected = UnsupportedOperationException::class) + fun serialize_failOnMultipleWheels() { + val progress = createScienceProgress(0, 2, 0, 0) + gson!!.toJson(progress) + } + + @Test(expected = UnsupportedOperationException::class) + fun serialize_failOnMultipleTablets() { + val progress = createScienceProgress(0, 0, 2, 0) + gson!!.toJson(progress) + } + + @Test(expected = UnsupportedOperationException::class) + fun serialize_failOnMultipleJokers() { + val progress = createScienceProgress(0, 0, 0, 2) + gson!!.toJson(progress) + } + + @Test(expected = UnsupportedOperationException::class) + fun serialize_failOnMixedElements() { + val progress = createScienceProgress(1, 1, 0, 0) + gson!!.toJson(progress) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnEmptyString() { + gson!!.fromJson("\"\"", ScienceProgress::class.java) + } + + @Test(expected = IllegalArgumentException::class) + fun deserialize_failOnGarbageString() { + gson!!.fromJson("thisisgarbage", ScienceProgress::class.java) + } + + @Test + fun deserialize_compass() { + val progress = gson!!.fromJson(COMPASS_STR, ScienceProgress::class.java) + assertNotNull(progress.science) + assertEquals(1, progress.science.getQuantity(ScienceType.COMPASS).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.TABLET).toLong()) + assertEquals(0, progress.science.jokers.toLong()) + } + + @Test + fun deserialize_wheel() { + val progress = gson!!.fromJson(WHEEL_STR, ScienceProgress::class.java) + assertNotNull(progress.science) + assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS).toLong()) + assertEquals(1, progress.science.getQuantity(ScienceType.WHEEL).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.TABLET).toLong()) + assertEquals(0, progress.science.jokers.toLong()) + } + + @Test + fun deserialize_tablet() { + val progress = gson!!.fromJson(TABLET_STR, ScienceProgress::class.java) + assertNotNull(progress.science) + assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL).toLong()) + assertEquals(1, progress.science.getQuantity(ScienceType.TABLET).toLong()) + assertEquals(0, progress.science.jokers.toLong()) + } + + @Test + fun deserialize_joker() { + val progress = gson!!.fromJson(JOKER_STR, ScienceProgress::class.java) + assertNotNull(progress.science) + assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL).toLong()) + assertEquals(0, progress.science.getQuantity(ScienceType.TABLET).toLong()) + assertEquals(1, progress.science.jokers.toLong()) + } + + companion object { + + private val COMPASS_STR = "\"COMPASS\"" + + private val WHEEL_STR = "\"WHEEL\"" + + private val TABLET_STR = "\"TABLET\"" + + private val JOKER_STR = "\"any\"" + } +} |