summaryrefslogtreecommitdiff
path: root/sw-engine/src/test/kotlin
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2021-01-29 03:02:48 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2021-01-29 03:02:48 +0100
commit459ee67652d01fd37b3dd696e4fa96f123ae44ee (patch)
treedb38c61cd5c0816eebb28561fc1bce5ec1b6f305 /sw-engine/src/test/kotlin
parentClarify docker local run (diff)
downloadseven-wonders-459ee67652d01fd37b3dd696e4fa96f123ae44ee.tar.gz
seven-wonders-459ee67652d01fd37b3dd696e4fa96f123ae44ee.tar.bz2
seven-wonders-459ee67652d01fd37b3dd696e4fa96f123ae44ee.zip
Move from Gson to Kotlin Serialization for definitions
Diffstat (limited to 'sw-engine/src/test/kotlin')
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/NumericEffectSerializerTest.kt148
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionIncreaseSerializerTest.kt192
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializerTest.kt75
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypeSerializerTest.kt27
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypesSerializerTest.kt35
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourcesSerializerTest.kt50
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ScienceProgressSerializerTest.kt46
7 files changed, 91 insertions, 482 deletions
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/NumericEffectSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/NumericEffectSerializerTest.kt
deleted file mode 100644
index 520db340..00000000
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/NumericEffectSerializerTest.kt
+++ /dev/null
@@ -1,148 +0,0 @@
-package org.luxons.sevenwonders.engine.data.serializers
-
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-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.engine.effects.GoldIncrease
-import org.luxons.sevenwonders.engine.effects.MilitaryReinforcements
-import org.luxons.sevenwonders.engine.effects.ProductionIncrease
-import org.luxons.sevenwonders.engine.effects.RawPointsIncrease
-import org.luxons.sevenwonders.engine.resources.Production
-import kotlin.test.assertEquals
-import kotlin.test.assertFailsWith
-
-@RunWith(Theories::class)
-class NumericEffectSerializerTest {
-
- private lateinit var gson: Gson
-
- @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
- fun serialize_failOnUnknownType() {
- assertFailsWith<IllegalArgumentException> {
- 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<MilitaryReinforcements>(count.toString()))
- }
-
- @Theory
- fun deserialize_rawPointsIncrease(count: Int) {
- val points = RawPointsIncrease(count)
- assertEquals(points, gson.fromJson<RawPointsIncrease>(count.toString()))
- }
-
- @Theory
- fun deserialize_goldIncrease(count: Int) {
- val goldIncrease = GoldIncrease(count)
- assertEquals(goldIncrease, gson.fromJson<GoldIncrease>(count.toString()))
- }
-
- @Test
- fun deserialize_militaryReinforcements_failOnEmptyString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<MilitaryReinforcements>("\"\"")
- }
- }
-
- @Test
- fun deserialize_rawPointsIncrease_failOnEmptyString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<RawPointsIncrease>("\"\"")
- }
- }
-
- @Test
- fun deserialize_goldIncrease_failOnEmptyString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<GoldIncrease>("\"\"")
- }
- }
-
- @Test
- fun deserialize_militaryReinforcements_failOnNonNumericString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<MilitaryReinforcements>("\"abc\"")
- }
- }
-
- @Test
- fun deserialize_rawPointsIncrease_failOnNonNumericString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<RawPointsIncrease>("\"abc\"")
- }
- }
-
- @Test
- fun deserialize_goldIncrease_failOnNonNumericString() {
- assertFailsWith<NumberFormatException> {
- gson.fromJson<GoldIncrease>("\"abc\"")
- }
- }
-
- @Test
- fun deserialize_failOnUnknownType() {
- assertFailsWith<IllegalArgumentException> {
- gson.fromJson<ProductionIncrease>("\"2\"")
- }
- }
-
- companion object {
-
- @JvmStatic
- @DataPoints
- fun dataPoints(): IntArray = intArrayOf(-2, -1, 0, 1, 2, 5)
- }
-}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionIncreaseSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionIncreaseSerializerTest.kt
deleted file mode 100644
index 664ccedc..00000000
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionIncreaseSerializerTest.kt
+++ /dev/null
@@ -1,192 +0,0 @@
-package org.luxons.sevenwonders.engine.data.serializers
-
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import com.google.gson.reflect.TypeToken
-import org.junit.Before
-import org.junit.Test
-import org.luxons.sevenwonders.engine.effects.ProductionIncrease
-import org.luxons.sevenwonders.engine.resources.MutableResources
-import org.luxons.sevenwonders.engine.resources.Production
-import org.luxons.sevenwonders.engine.resources.Resources
-import org.luxons.sevenwonders.model.resources.ResourceType
-import kotlin.test.assertEquals
-import kotlin.test.assertFailsWith
-import kotlin.test.assertNull
-
-class ProductionIncreaseSerializerTest {
-
- private lateinit var gson: Gson
-
- @Before
- fun setUp() {
- val resourceTypeList = object : TypeToken<List<ResourceType>>() {}.type
- gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer())
- .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer())
- .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer())
- .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer())
- .registerTypeAdapter(Production::class.java, ProductionSerializer())
- .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))
- }
-
- @Test
- fun serialize_singleType() {
- val prodIncrease = create(true, 1, 0, 0)
- assertEquals("\"W\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_mixedTypes() {
- val prodIncrease = create(true, 1, 1, 1)
- assertEquals("\"WSC\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_mixedTypes_notSellable() {
- val prodIncrease = create(false, 1, 1, 1)
- assertEquals("\"(WSC)\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_choice2() {
- val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY)
- assertEquals("\"W/C\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_choice3() {
- val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY)
- assertEquals("\"W/O/C\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_choice3_notSellable() {
- val prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY)
- assertEquals("\"(W/O/C)\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_choice2_unordered() {
- val prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD)
- assertEquals("\"W/C\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_choice3_unordered() {
- val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE)
- assertEquals("\"W/O/C\"", gson.toJson(prodIncrease))
- }
-
- @Test
- fun serialize_failIfMultipleChoices() {
- val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY)
- prodIncrease.production.addChoice(ResourceType.ORE, ResourceType.GLASS)
- assertFailsWith<IllegalArgumentException> {
- gson.toJson(prodIncrease)
- }
- }
-
- @Test
- fun serialize_failIfMixedFixedAndChoices() {
- val prodIncrease = create(true, 1, 0, 0)
- prodIncrease.production.addChoice(ResourceType.WOOD, ResourceType.CLAY)
- assertFailsWith<IllegalArgumentException> {
- gson.toJson(prodIncrease)
- }
- }
-
- @Test
- fun deserialize_nullFromNull() {
- assertNull(gson.fromJson("null", ProductionIncrease::class.java))
- }
-
- @Test
- fun deserialize_emptyList() {
- val prodIncrease = ProductionIncrease(Production(), true)
- assertEquals(prodIncrease, gson.fromJson("\"\""))
- }
-
- @Test
- fun deserialize_failOnGarbageString() {
- assertFailsWith(IllegalArgumentException::class) {
- gson.fromJson<ProductionIncrease>("\"this is garbage\"")
- }
- }
-
- @Test
- fun deserialize_failOnGarbageStringWithSlashes() {
- assertFailsWith(IllegalArgumentException::class) {
- gson.fromJson<ProductionIncrease>("\"this/is/garbage\"")
- }
- }
-
- @Test
- fun deserialize_singleType() {
- val prodIncrease = create(true, 1, 0, 0)
- assertEquals(prodIncrease, gson.fromJson("\"W\""))
- }
-
- @Test
- fun deserialize_multipleTimesSameType_notSellable() {
- val prodIncrease = create(false, 3, 0, 0)
- assertEquals(prodIncrease, gson.fromJson("\"(WWW)\""))
- }
-
- @Test
- fun deserialize_mixedTypes() {
- val prodIncrease = create(true, 1, 1, 1)
- assertEquals(prodIncrease, gson.fromJson("\"WCS\""))
- }
-
- @Test
- fun deserialize_choice2() {
- val prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY)
- assertEquals(prodIncrease, gson.fromJson("\"W/C\""))
- }
-
- @Test
- fun deserialize_choice3_notSellable() {
- val prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY)
- assertEquals(prodIncrease, gson.fromJson("\"(W/O/C)\""))
- }
-
- @Test
- fun deserialize_failOnMultipleResourcesInChoice() {
- assertFailsWith(IllegalArgumentException::class) {
- gson.fromJson<ProductionIncrease>("\"W/SS/C\"")
- }
- }
-}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializerTest.kt
index 314bc43c..25e9d68c 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializerTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializerTest.kt
@@ -1,14 +1,11 @@
package org.luxons.sevenwonders.engine.data.serializers
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import com.google.gson.reflect.TypeToken
-import org.junit.Before
+import kotlinx.serialization.builtins.nullable
+import kotlinx.serialization.decodeFromString
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
import org.junit.Test
-import org.luxons.sevenwonders.engine.resources.MutableResources
import org.luxons.sevenwonders.engine.resources.Production
-import org.luxons.sevenwonders.engine.resources.Resources
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -16,19 +13,6 @@ import kotlin.test.assertNull
class ProductionSerializerTest {
- private lateinit var gson: Gson
-
- @Before
- fun setUp() {
- val resourceTypeList = object : TypeToken<List<ResourceType>>() {}.type
- gson = GsonBuilder().registerTypeAdapter(Resources::class.java, ResourcesSerializer())
- .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer())
- .registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer())
- .registerTypeAdapter(resourceTypeList, ResourceTypesSerializer())
- .registerTypeAdapter(Production::class.java, ProductionSerializer())
- .create()
- }
-
private fun create(wood: Int, stone: Int, clay: Int): Production {
val production = Production()
if (wood > 0) {
@@ -51,61 +35,61 @@ class ProductionSerializerTest {
@Test
fun serialize_nullAsNull() {
- assertEquals("null", gson.toJson(null, Production::class.java))
+ assertEquals("null", Json.encodeToString(Production.serializer().nullable, null))
}
@Test
fun serialize_emptyProdIncreaseAsNull() {
val prodIncrease = Production()
- assertEquals("null", gson.toJson(prodIncrease))
+ assertEquals("null", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_singleType() {
val prodIncrease = create(1, 0, 0)
- assertEquals("\"W\"", gson.toJson(prodIncrease))
+ assertEquals("\"W\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_multipleTimesSameType() {
val prodIncrease = create(3, 0, 0)
- assertEquals("\"WWW\"", gson.toJson(prodIncrease))
+ assertEquals("\"WWW\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_mixedTypes() {
val prodIncrease = create(1, 1, 1)
- assertEquals("\"WSC\"", gson.toJson(prodIncrease))
+ assertEquals("\"WSC\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_mixedTypesMultiple() {
val prodIncrease = create(2, 1, 2)
- assertEquals("\"WWSCC\"", gson.toJson(prodIncrease))
+ assertEquals("\"WWSCC\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_choice2() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY)
- assertEquals("\"W/C\"", gson.toJson(prodIncrease))
+ assertEquals("\"W/C\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_choice3() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY)
- assertEquals("\"W/O/C\"", gson.toJson(prodIncrease))
+ assertEquals("\"W/O/C\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_choice2_unordered() {
val prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD)
- assertEquals("\"W/C\"", gson.toJson(prodIncrease))
+ assertEquals("\"W/C\"", Json.encodeToString(prodIncrease))
}
@Test
fun serialize_choice3_unordered() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE)
- assertEquals("\"W/O/C\"", gson.toJson(prodIncrease))
+ assertEquals("\"W/O/C\"", Json.encodeToString(prodIncrease))
}
@Test
@@ -113,7 +97,7 @@ class ProductionSerializerTest {
val production = createChoice(ResourceType.WOOD, ResourceType.CLAY)
production.addChoice(ResourceType.ORE, ResourceType.GLASS)
assertFailsWith<IllegalArgumentException> {
- gson.toJson(production)
+ Json.encodeToString(production)
}
}
@@ -122,87 +106,86 @@ class ProductionSerializerTest {
val production = create(1, 0, 0)
production.addChoice(ResourceType.WOOD, ResourceType.CLAY)
assertFailsWith<IllegalArgumentException> {
- gson.toJson(production)
+ Json.encodeToString(production)
}
}
@Test
fun deserialize_nullFromNull() {
- assertNull(gson.fromJson("null", Production::class.java))
+ assertNull(Json.decodeFromString<Production?>("null"))
}
@Test
fun deserialize_emptyList() {
- val prodIncrease = Production()
- assertEquals(prodIncrease, gson.fromJson("\"\""))
+ assertEquals(Production(), Json.decodeFromString("\"\""))
}
@Test
fun deserialize_failOnGarbageString() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<Production>("\"this is garbage\"")
+ Json.decodeFromString<Production>("\"this is garbage\"")
}
}
@Test
fun deserialize_failOnGarbageStringWithSlashes() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<Production>("\"this/is/garbage\"")
+ Json.decodeFromString<Production>("\"this/is/garbage\"")
}
}
@Test
fun deserialize_singleType() {
val prodIncrease = create(1, 0, 0)
- assertEquals(prodIncrease, gson.fromJson("\"W\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"W\""))
}
@Test
fun deserialize_multipleTimesSameType() {
val prodIncrease = create(3, 0, 0)
- assertEquals(prodIncrease, gson.fromJson("\"WWW\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"WWW\""))
}
@Test
fun deserialize_mixedTypes() {
val prodIncrease = create(1, 1, 1)
- assertEquals(prodIncrease, gson.fromJson("\"WCS\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"WCS\""))
}
@Test
fun deserialize_mixedTypes_unordered() {
val prodIncrease = create(1, 3, 2)
- assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"SCWCSS\""))
}
@Test
fun deserialize_choice2() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY)
- assertEquals(prodIncrease, gson.fromJson("\"W/C\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"W/C\""))
}
@Test
fun deserialize_choice3() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY)
- assertEquals(prodIncrease, gson.fromJson("\"W/O/C\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"W/O/C\""))
}
@Test
fun deserialize_choice2_unordered() {
val prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD)
- assertEquals(prodIncrease, gson.fromJson("\"W/C\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"W/C\""))
}
@Test
fun deserialize_choice3_unordered() {
val prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE)
- assertEquals(prodIncrease, gson.fromJson("\"W/O/C\""))
+ assertEquals(prodIncrease, Json.decodeFromString("\"W/O/C\""))
}
@Test
fun deserialize_failOnMultipleResourcesInChoice() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<Production>("\"W/SS/C\"")
+ Json.decodeFromString<Production>("\"W/SS/C\"")
}
}
}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypeSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypeSerializerTest.kt
index e256efc3..3956ff0c 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypeSerializerTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypeSerializerTest.kt
@@ -1,9 +1,7 @@
package org.luxons.sevenwonders.engine.data.serializers
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import org.junit.Before
+import kotlinx.serialization.builtins.nullable
+import kotlinx.serialization.json.Json
import org.junit.Test
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.test.assertEquals
@@ -12,45 +10,38 @@ import kotlin.test.assertNull
class ResourceTypeSerializerTest {
- private lateinit var gson: Gson
-
- @Before
- fun setUp() {
- gson = GsonBuilder().registerTypeAdapter(ResourceType::class.java, ResourceTypeSerializer()).create()
- }
-
@Test
fun serialize_useSymbolForEachType() {
ResourceType.values().forEach { type ->
- val expectedJson = "\"" + type.symbol + "\""
- assertEquals(expectedJson, gson.toJson(type))
+ val expectedjson = "\"" + type.symbol + "\""
+ assertEquals(expectedjson, Json.encodeToString(ResourceTypeSerializer, type))
}
}
@Test
fun deserialize_useSymbolForEachType() {
ResourceType.values().forEach { type ->
- val typeInJson = "\"" + type.symbol + "\""
- assertEquals(type, gson.fromJson(typeInJson))
+ val typeInjson = "\"" + type.symbol + "\""
+ assertEquals(type, Json.decodeFromString(ResourceTypeSerializer, typeInjson))
}
}
@Test
fun deserialize_nullFromNull() {
- assertNull(gson.fromJson("null", ResourceType::class.java))
+ assertNull(Json.decodeFromString(ResourceTypeSerializer.nullable, "null"))
}
@Test
fun deserialize_failsOnEmptyString() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<ResourceType>("\"\"")
+ Json.decodeFromString(ResourceTypeSerializer, "\"\"")
}
}
@Test
fun deserialize_failsOnGarbageString() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<ResourceType>("\"thisisgarbage\"")
+ Json.decodeFromString(ResourceTypeSerializer, "\"thisisgarbage\"")
}
}
}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypesSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypesSerializerTest.kt
index 8c2ce2fc..b7ea6c9e 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypesSerializerTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourceTypesSerializerTest.kt
@@ -1,11 +1,7 @@
package org.luxons.sevenwonders.engine.data.serializers
-import com.github.salomonbrys.kotson.fromJson
-import com.github.salomonbrys.kotson.typeToken
-import com.github.salomonbrys.kotson.typedToJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import org.junit.Before
+import kotlinx.serialization.builtins.nullable
+import kotlinx.serialization.json.Json
import org.junit.Test
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.test.assertEquals
@@ -13,68 +9,61 @@ import kotlin.test.assertNull
class ResourceTypesSerializerTest {
- private lateinit var gson: Gson
-
- @Before
- fun setUp() {
- gson = GsonBuilder().registerTypeAdapter(typeToken<List<ResourceType>>(), ResourceTypesSerializer()).create()
- }
-
@Test
fun serialize_null() {
- assertEquals("null", gson.toJson(null, typeToken<List<ResourceType>>()))
+ assertEquals("null", Json.encodeToString(ResourceTypesSerializer.nullable, null))
}
@Test
fun serialize_emptyList() {
val types = emptyList<ResourceType>()
- assertEquals("\"\"", gson.typedToJson(types))
+ assertEquals("\"\"", Json.encodeToString(ResourceTypesSerializer, types))
}
@Test
fun serialize_singleType() {
val types = listOf(ResourceType.WOOD)
- assertEquals("\"W\"", gson.typedToJson(types))
+ assertEquals("\"W\"", Json.encodeToString(ResourceTypesSerializer, types))
}
@Test
fun serialize_multipleTimesSameType() {
val types = List(3) { ResourceType.WOOD }
- assertEquals("\"WWW\"", gson.typedToJson(types))
+ assertEquals("\"WWW\"", Json.encodeToString(ResourceTypesSerializer, types))
}
@Test
fun serialize_mixedTypes() {
val types = listOf(ResourceType.WOOD, ResourceType.CLAY, ResourceType.STONE)
- assertEquals("\"WCS\"", gson.typedToJson(types))
+ assertEquals("\"WCS\"", Json.encodeToString(ResourceTypesSerializer, types))
}
@Test
fun deserialize_null() {
- assertNull(gson.fromJson("null", typeToken<List<ResourceType>>()))
+ assertNull(Json.decodeFromString(ResourceTypesSerializer.nullable, "null"))
}
@Test
fun deserialize_emptyList() {
val types = emptyList<ResourceType>()
- assertEquals(types, gson.fromJson("\"\""))
+ assertEquals(types, Json.decodeFromString(ResourceTypesSerializer, "\"\""))
}
@Test
fun deserialize_singleType() {
val types = listOf(ResourceType.WOOD)
- assertEquals(types, gson.fromJson("\"W\""))
+ assertEquals(types, Json.decodeFromString(ResourceTypesSerializer, "\"W\""))
}
@Test
fun deserialize_multipleTimesSameType() {
val types = List(3) { ResourceType.WOOD }
- assertEquals(types, gson.fromJson("\"WWW\""))
+ assertEquals(types, Json.decodeFromString(ResourceTypesSerializer, "\"WWW\""))
}
@Test
fun deserialize_mixedTypes() {
val types = listOf(ResourceType.WOOD, ResourceType.CLAY, ResourceType.STONE)
- assertEquals(types, gson.fromJson("\"WCS\""))
+ assertEquals(types, Json.decodeFromString(ResourceTypesSerializer, "\"WCS\""))
}
}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourcesSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourcesSerializerTest.kt
index bc5808a0..614babe4 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourcesSerializerTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ResourcesSerializerTest.kt
@@ -1,99 +1,93 @@
package org.luxons.sevenwonders.engine.data.serializers
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import org.junit.Before
+import kotlinx.serialization.builtins.nullable
+import kotlinx.serialization.decodeFromString
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
+import kotlinx.serialization.modules.SerializersModule
import org.junit.Test
-import org.luxons.sevenwonders.engine.resources.MutableResources
import org.luxons.sevenwonders.engine.resources.Resources
import org.luxons.sevenwonders.engine.resources.emptyResources
import org.luxons.sevenwonders.engine.resources.resourcesOf
-import org.luxons.sevenwonders.model.resources.ResourceType.CLAY
-import org.luxons.sevenwonders.model.resources.ResourceType.STONE
-import org.luxons.sevenwonders.model.resources.ResourceType.WOOD
+import org.luxons.sevenwonders.model.resources.ResourceType.*
import kotlin.test.assertEquals
import kotlin.test.assertNull
class ResourcesSerializerTest {
- private lateinit var gson: Gson
-
- @Before
- fun setUp() {
- gson = GsonBuilder() //
- .registerTypeAdapter(Resources::class.java, ResourcesSerializer())
- .registerTypeAdapter(MutableResources::class.java, ResourcesSerializer())
- .create()
+ private val json: Json = Json {
+ serializersModule = SerializersModule {
+ contextual(Resources::class, ResourcesSerializer)
+ }
}
@Test
fun serialize_null() {
- assertEquals("null", gson.toJson(null, Resources::class.java))
+ assertEquals("null", json.encodeToString(ResourcesSerializer.nullable, null))
}
@Test
fun serialize_emptyResourcesToNull() {
val resources = emptyResources()
- assertEquals("null", gson.toJson(resources))
+ assertEquals("null", json.encodeToString(resources))
}
@Test
fun serialize_singleType() {
val resources = resourcesOf(WOOD)
- assertEquals("\"W\"", gson.toJson(resources))
+ assertEquals("\"W\"", json.encodeToString(resources))
}
@Test
fun serialize_multipleTimesSameType() {
val resources = resourcesOf(WOOD to 3)
- assertEquals("\"WWW\"", gson.toJson(resources))
+ assertEquals("\"WWW\"", json.encodeToString(resources))
}
@Test
fun serialize_mixedTypes() {
val resources = resourcesOf(WOOD, STONE, CLAY)
- assertEquals("\"WSC\"", gson.toJson(resources))
+ assertEquals("\"WSC\"", json.encodeToString(resources))
}
@Test
fun serialize_mixedTypes_unordered() {
val resources = resourcesOf(CLAY to 1, WOOD to 2, CLAY to 1, STONE to 1)
- assertEquals("\"CCWWS\"", gson.toJson(resources))
+ assertEquals("\"CCWWS\"", json.encodeToString(resources))
}
@Test
fun deserialize_null() {
- assertNull(gson.fromJson("null", Resources::class.java))
+ assertNull(json.decodeFromString<Resources?>("null"))
}
@Test
fun deserialize_emptyList() {
val resources = emptyResources()
- assertEquals(resources, gson.fromJson("\"\""))
+ assertEquals(resources, json.decodeFromString("\"\""))
}
@Test
fun deserialize_singleType() {
val resources = resourcesOf(WOOD)
- assertEquals(resources, gson.fromJson("\"W\""))
+ assertEquals(resources, json.decodeFromString("\"W\""))
}
@Test
fun deserialize_multipleTimesSameType() {
val resources = resourcesOf(WOOD to 3)
- assertEquals(resources, gson.fromJson("\"WWW\""))
+ assertEquals(resources, json.decodeFromString("\"WWW\""))
}
@Test
fun deserialize_mixedTypes() {
val resources = resourcesOf(WOOD, CLAY, STONE)
- assertEquals(resources, gson.fromJson("\"WCS\""))
+ assertEquals(resources, json.decodeFromString("\"WCS\""))
}
@Test
fun deserialize_mixedTypes_unordered() {
val resources = resourcesOf(WOOD to 1, CLAY to 2, STONE to 3)
- assertEquals(resources, gson.fromJson("\"SCWCSS\""))
+ assertEquals(resources, json.decodeFromString("\"SCWCSS\""))
}
}
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ScienceProgressSerializerTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ScienceProgressSerializerTest.kt
index 19b1e572..1519ec7e 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ScienceProgressSerializerTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/serializers/ScienceProgressSerializerTest.kt
@@ -1,9 +1,8 @@
package org.luxons.sevenwonders.engine.data.serializers
-import com.github.salomonbrys.kotson.fromJson
-import com.google.gson.Gson
-import com.google.gson.GsonBuilder
-import org.junit.Before
+import kotlinx.serialization.decodeFromString
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.Json
import org.junit.Test
import org.luxons.sevenwonders.engine.boards.ScienceType
import org.luxons.sevenwonders.engine.effects.ScienceProgress
@@ -19,45 +18,38 @@ private const val JOKER_STR = "\"any\""
class ScienceProgressSerializerTest {
- private lateinit var gson: Gson
-
- @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)
+ val json = Json.encodeToString(progress)
assertEquals("null", json)
}
@Test
fun serialize_oneCompass() {
val progress = createScienceProgress(1, 0, 0, 0)
- val json = gson.toJson(progress)
+ val json = Json.encodeToString(progress)
assertEquals(COMPASS_STR, json)
}
@Test
fun serialize_oneWheel() {
val progress = createScienceProgress(0, 1, 0, 0)
- val json = gson.toJson(progress)
+ val json = Json.encodeToString(progress)
assertEquals(WHEEL_STR, json)
}
@Test
fun serialize_oneTablet() {
val progress = createScienceProgress(0, 0, 1, 0)
- val json = gson.toJson(progress)
+ val json = Json.encodeToString(progress)
assertEquals(TABLET_STR, json)
}
@Test
fun serialize_oneJoker() {
val progress = createScienceProgress(0, 0, 0, 1)
- val json = gson.toJson(progress)
+ val json = Json.encodeToString(progress)
assertEquals(JOKER_STR, json)
}
@@ -65,7 +57,7 @@ class ScienceProgressSerializerTest {
fun serialize_failOnMultipleCompasses() {
assertFailsWith<UnsupportedOperationException> {
val progress = createScienceProgress(2, 0, 0, 0)
- gson.toJson(progress)
+ Json.encodeToString(progress)
}
}
@@ -73,7 +65,7 @@ class ScienceProgressSerializerTest {
fun serialize_failOnMultipleWheels() {
assertFailsWith<UnsupportedOperationException> {
val progress = createScienceProgress(0, 2, 0, 0)
- gson.toJson(progress)
+ Json.encodeToString(progress)
}
}
@@ -81,7 +73,7 @@ class ScienceProgressSerializerTest {
fun serialize_failOnMultipleTablets() {
assertFailsWith<UnsupportedOperationException> {
val progress = createScienceProgress(0, 0, 2, 0)
- gson.toJson(progress)
+ Json.encodeToString(progress)
}
}
@@ -89,7 +81,7 @@ class ScienceProgressSerializerTest {
fun serialize_failOnMultipleJokers() {
assertFailsWith<UnsupportedOperationException> {
val progress = createScienceProgress(0, 0, 0, 2)
- gson.toJson(progress)
+ Json.encodeToString(progress)
}
}
@@ -97,27 +89,27 @@ class ScienceProgressSerializerTest {
fun serialize_failOnMixedElements() {
assertFailsWith<UnsupportedOperationException> {
val progress = createScienceProgress(1, 1, 0, 0)
- gson.toJson(progress)
+ Json.encodeToString(progress)
}
}
@Test
fun deserialize_failOnEmptyString() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<ScienceProgress>("\"\"")
+ Json.decodeFromString<ScienceProgress>("\"\"")
}
}
@Test
fun deserialize_failOnGarbageString() {
assertFailsWith<IllegalArgumentException> {
- gson.fromJson<ScienceProgress>("thisisgarbage")
+ Json.decodeFromString<ScienceProgress>("thisisgarbage")
}
}
@Test
fun deserialize_compass() {
- val progress = gson.fromJson<ScienceProgress>(COMPASS_STR)
+ val progress = Json.decodeFromString<ScienceProgress>(COMPASS_STR)
assertNotNull(progress.science)
assertEquals(1, progress.science.getQuantity(ScienceType.COMPASS))
assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL))
@@ -127,7 +119,7 @@ class ScienceProgressSerializerTest {
@Test
fun deserialize_wheel() {
- val progress = gson.fromJson<ScienceProgress>(WHEEL_STR)
+ val progress = Json.decodeFromString<ScienceProgress>(WHEEL_STR)
assertNotNull(progress.science)
assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS))
assertEquals(1, progress.science.getQuantity(ScienceType.WHEEL))
@@ -137,7 +129,7 @@ class ScienceProgressSerializerTest {
@Test
fun deserialize_tablet() {
- val progress = gson.fromJson<ScienceProgress>(TABLET_STR)
+ val progress = Json.decodeFromString<ScienceProgress>(TABLET_STR)
assertNotNull(progress.science)
assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS))
assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL))
@@ -147,7 +139,7 @@ class ScienceProgressSerializerTest {
@Test
fun deserialize_joker() {
- val progress = gson.fromJson<ScienceProgress>(JOKER_STR)
+ val progress = Json.decodeFromString<ScienceProgress>(JOKER_STR)
assertNotNull(progress.science)
assertEquals(0, progress.science.getQuantity(ScienceType.COMPASS))
assertEquals(0, progress.science.getQuantity(ScienceType.WHEEL))
bgstack15