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 | |
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
20 files changed, 1045 insertions, 1054 deletions
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java deleted file mode 100644 index e678e6a5..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import org.junit.Test; - -import static org.junit.Assert.assertNotNull; - -public class GameDefinitionLoaderTest { - - @Test - public void successfulLoad() { - GameDefinitionLoader loader = new GameDefinitionLoader(); - GameDefinition gameDefinition = loader.getGameDefinition(); - assertNotNull(gameDefinition); - } - -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java deleted file mode 100644 index 08e2783b..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import org.junit.Test; -import org.luxons.sevenwonders.game.Game; -import org.luxons.sevenwonders.game.api.CustomizableSettings; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class GameDefinitionTest { - - @Test - public void successfulGameInit() { - GameDefinition gameDefinition = new GameDefinitionLoader().getGameDefinition(); - assertNotNull(gameDefinition); - assertEquals(3, gameDefinition.getMinPlayers()); - assertEquals(7, gameDefinition.getMaxPlayers()); - - Game game = gameDefinition.initGame(0, new CustomizableSettings(), 7); - assertNotNull(game); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java deleted file mode 100644 index f45ad4ea..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.luxons.sevenwonders.game.data.definitions; - -import java.util.Random; - -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 static org.junit.Assert.assertEquals; - -@RunWith(Theories.class) -public class WonderSidePickMethodTest { - - private Random random; - - private Random random2; - - @DataPoints - public static WonderSide[] sides() { - return WonderSide.values(); - } - - @Before - public void setUp() { - random = new Random(123); // starts with TRUE - random2 = new Random(123456); // starts with FALSE - } - - @Test - public void pick_allA() { - WonderSide side = null; - for (int i = 0; i < 10; i++) { - side = WonderSidePickMethod.ALL_A.pickSide(random, side); - assertEquals(WonderSide.A, side); - } - } - - @Test - public void pick_allB() { - WonderSide side = null; - for (int i = 0; i < 10; i++) { - side = WonderSidePickMethod.ALL_B.pickSide(random, side); - assertEquals(WonderSide.B, side); - } - } - - @Test - public void pick_eachRandom() { - WonderSide 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 - public void pick_eachRandom2() { - WonderSide 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 - public void pick_allSameRandom_sameAsFirst(WonderSide firstSide) { - WonderSide side = firstSide; - for (int i = 0; i < 10; i++) { - side = WonderSidePickMethod.SAME_RANDOM_FOR_ALL.pickSide(random, side); - assertEquals(firstSide, side); - } - } - - @Test - public void 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)); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java deleted file mode 100644 index d7aeeeb2..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializerTest.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -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.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; - -import static org.junit.Assert.assertEquals; - -@RunWith(Theories.class) -public class NumericEffectSerializerTest { - - private Gson gson; - - @DataPoints - public static int[] dataPoints() { - return new int[] {-2, -1, 0, 1, 2, 5}; - } - - @Before - public void setUp() { - gson = new GsonBuilder().registerTypeAdapter(MilitaryReinforcements.class, new NumericEffectSerializer()) - .registerTypeAdapter(RawPointsIncrease.class, new NumericEffectSerializer()) - .registerTypeAdapter(GoldIncrease.class, new NumericEffectSerializer()) - // ProductionIncrease is not a numeric effect, it is here for negative testing purpose - .registerTypeAdapter(ProductionIncrease.class, new NumericEffectSerializer()) - .create(); - } - - @Test - public void serialize_militaryReinforcements_null() { - assertEquals("null", gson.toJson(null, MilitaryReinforcements.class)); - } - - @Test - public void serialize_rawPointsIncrease_null() { - assertEquals("null", gson.toJson(null, RawPointsIncrease.class)); - } - - @Test - public void serialize_goldIncrease_null() { - assertEquals("null", gson.toJson(null, GoldIncrease.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void serialize_failOnUnknownType() { - gson.toJson(new ProductionIncrease(new Production(), false)); - } - - @Theory - public void serialize_militaryReinforcements(int count) { - MilitaryReinforcements reinforcements = new MilitaryReinforcements(count); - assertEquals(String.valueOf(count), gson.toJson(reinforcements)); - } - - @Theory - public void serialize_rawPointsIncrease(int count) { - RawPointsIncrease points = new RawPointsIncrease(count); - assertEquals(String.valueOf(count), gson.toJson(points)); - } - - @Theory - public void serialize_goldIncrease(int count) { - GoldIncrease goldIncrease = new GoldIncrease(count); - assertEquals(String.valueOf(count), gson.toJson(goldIncrease)); - } - - @Theory - public void deserialize_militaryReinforcements(int count) { - MilitaryReinforcements reinforcements = new MilitaryReinforcements(count); - assertEquals(reinforcements, gson.fromJson(String.valueOf(count), MilitaryReinforcements.class)); - } - - @Theory - public void deserialize_rawPointsIncrease(int count) { - RawPointsIncrease points = new RawPointsIncrease(count); - assertEquals(points, gson.fromJson(String.valueOf(count), RawPointsIncrease.class)); - } - - @Theory - public void deserialize_goldIncrease(int count) { - GoldIncrease goldIncrease = new GoldIncrease(count); - assertEquals(goldIncrease, gson.fromJson(String.valueOf(count), GoldIncrease.class)); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_militaryReinforcements_failOnEmptyString() { - gson.fromJson("\"\"", MilitaryReinforcements.class); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_rawPointsIncrease_failOnEmptyString() { - gson.fromJson("\"\"", RawPointsIncrease.class); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_goldIncrease_failOnEmptyString() { - gson.fromJson("\"\"", GoldIncrease.class); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_militaryReinforcements_failOnNonNumericString() { - gson.fromJson("\"abc\"", MilitaryReinforcements.class); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_rawPointsIncrease_failOnNonNumericString() { - gson.fromJson("\"abc\"", RawPointsIncrease.class); - } - - @Test(expected = NumberFormatException.class) - public void deserialize_goldIncrease_failOnNonNumericString() { - gson.fromJson("\"abc\"", GoldIncrease.class); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnUnknownType() { - gson.fromJson("\"2\"", ProductionIncrease.class); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java deleted file mode 100644 index fd2e593b..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java +++ /dev/null @@ -1,183 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -import java.lang.reflect.Type; -import java.util.List; - -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.game.effects.ProductionIncrease; -import org.luxons.sevenwonders.game.resources.Production; -import org.luxons.sevenwonders.game.resources.ResourceType; -import org.luxons.sevenwonders.game.resources.Resources; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -public class ProductionIncreaseSerializerTest { - - private Gson gson; - - @Before - public void setUp() { - Type resourceTypeList = new TypeToken<List<ResourceType>>() { - }.getType(); - gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) - .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) - .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) - .registerTypeAdapter(Production.class, new ProductionSerializer()) - .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) - .create(); - } - - private static ProductionIncrease create(boolean sellable, int wood, int stone, int clay) { - Production production = new 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 new ProductionIncrease(production, sellable); - } - - private static ProductionIncrease createChoice(boolean sellable, ResourceType... types) { - Production production = new Production(); - production.addChoice(types); - return new ProductionIncrease(production, sellable); - } - - @Test - public void serialize_nullAsNull() { - assertEquals("null", gson.toJson(null, ProductionIncrease.class)); - } - - @Test - public void serialize_emptyProdIncreaseAsNull() { - ProductionIncrease prodIncrease = new ProductionIncrease(new Production(), false); - assertEquals("null", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_singleType() { - ProductionIncrease prodIncrease = create(true, 1, 0, 0); - assertEquals("\"W\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_mixedTypes() { - ProductionIncrease prodIncrease = create(true, 1, 1, 1); - assertEquals("\"WSC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_mixedTypes_notSellable() { - ProductionIncrease prodIncrease = create(false, 1, 1, 1); - assertEquals("\"(WSC)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_choice2() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); - assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_choice3() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_choice3_notSellable() { - ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals("\"(W/O/C)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_choice2_unordered() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD); - assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test - public void serialize_choice3_unordered() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); - assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void serialize_failIfMultipleChoices() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); - prodIncrease.getProduction().addChoice(ResourceType.ORE, ResourceType.GLASS); - gson.toJson(prodIncrease, ProductionIncrease.class); - } - - @Test(expected = IllegalArgumentException.class) - public void serialize_failIfMixedFixedAndChoices() { - ProductionIncrease prodIncrease = create(true, 1, 0, 0); - prodIncrease.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); - gson.toJson(prodIncrease, ProductionIncrease.class); - } - - @Test - public void deserialize_nullFromNull() { - assertNull(gson.fromJson("null", ProductionIncrease.class)); - } - - @Test - public void deserialize_emptyList() { - ProductionIncrease prodIncrease = new ProductionIncrease(new Production(), true); - assertEquals(prodIncrease, gson.fromJson("\"\"", ProductionIncrease.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnGarbageString() { - gson.fromJson("\"this is garbage\"", ProductionIncrease.class); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnGarbageStringWithSlashes() { - gson.fromJson("\"this/is/garbage\"", ProductionIncrease.class); - } - - @Test - public void deserialize_singleType() { - ProductionIncrease prodIncrease = create(true, 1, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"W\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_multipleTimesSameType_notSellable() { - ProductionIncrease prodIncrease = create(false, 3, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"(WWW)\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_mixedTypes() { - ProductionIncrease prodIncrease = create(true, 1, 1, 1); - assertEquals(prodIncrease, gson.fromJson("\"WCS\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice2() { - ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice3_notSellable() { - ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"(W/O/C)\"", ProductionIncrease.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnMultipleResourcesInChoice() { - gson.fromJson("\"W/SS/C\"", ProductionIncrease.class); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java deleted file mode 100644 index 86ee16e9..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java +++ /dev/null @@ -1,199 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -import java.lang.reflect.Type; -import java.util.List; - -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.game.resources.Production; -import org.luxons.sevenwonders.game.resources.ResourceType; -import org.luxons.sevenwonders.game.resources.Resources; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -public class ProductionSerializerTest { - - private Gson gson; - - @Before - public void setUp() { - Type resourceTypeList = new TypeToken<List<ResourceType>>() { - }.getType(); - gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) - .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) - .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) - .registerTypeAdapter(Production.class, new ProductionSerializer()) - .create(); - } - - private static Production create(int wood, int stone, int clay) { - Production production = new 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 static Production createChoice(ResourceType... types) { - Production production = new Production(); - production.addChoice(types); - return production; - } - - @Test - public void serialize_nullAsNull() { - assertEquals("null", gson.toJson(null, Production.class)); - } - - @Test - public void serialize_emptyProdIncreaseAsNull() { - Production prodIncrease = new Production(); - assertEquals("null", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_singleType() { - Production prodIncrease = create(1, 0, 0); - assertEquals("\"W\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_multipleTimesSameType() { - Production prodIncrease = create(3, 0, 0); - assertEquals("\"WWW\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_mixedTypes() { - Production prodIncrease = create(1, 1, 1); - assertEquals("\"WSC\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_mixedTypesMultiple() { - Production prodIncrease = create(2, 1, 2); - assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_choice2() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); - assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_choice3() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_choice2_unordered() { - Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); - assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test - public void serialize_choice3_unordered() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); - assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void serialize_failIfMultipleChoices() { - Production production = createChoice(ResourceType.WOOD, ResourceType.CLAY); - production.addChoice(ResourceType.ORE, ResourceType.GLASS); - gson.toJson(production, Production.class); - } - - @Test(expected = IllegalArgumentException.class) - public void serialize_failIfMixedFixedAndChoices() { - Production production = create(1, 0, 0); - production.addChoice(ResourceType.WOOD, ResourceType.CLAY); - gson.toJson(production, Production.class); - } - - @Test - public void deserialize_nullFromNull() { - assertNull(gson.fromJson("null", Production.class)); - } - - @Test - public void deserialize_emptyList() { - Production prodIncrease = new Production(); - assertEquals(prodIncrease, gson.fromJson("\"\"", Production.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnGarbageString() { - gson.fromJson("\"this is garbage\"", Production.class); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnGarbageStringWithSlashes() { - gson.fromJson("\"this/is/garbage\"", Production.class); - } - - @Test - public void deserialize_singleType() { - Production prodIncrease = create(1, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"W\"", Production.class)); - } - - @Test - public void deserialize_multipleTimesSameType() { - Production prodIncrease = create(3, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"WWW\"", Production.class)); - } - - @Test - public void deserialize_mixedTypes() { - Production prodIncrease = create(1, 1, 1); - assertEquals(prodIncrease, gson.fromJson("\"WCS\"", Production.class)); - } - - @Test - public void deserialize_mixedTypes_unordered() { - Production prodIncrease = create(1, 3, 2); - assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", Production.class)); - } - - @Test - public void deserialize_choice2() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); - } - - @Test - public void deserialize_choice3() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); - } - - @Test - public void deserialize_choice2_unordered() { - Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); - assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); - } - - @Test - public void deserialize_choice3_unordered() { - Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnMultipleResourcesInChoice() { - gson.fromJson("\"W/SS/C\"", Production.class); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java deleted file mode 100644 index 3a688f4d..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializerTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import org.junit.Before; -import org.junit.Test; -import org.luxons.sevenwonders.game.resources.ResourceType; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -public class ResourceTypeSerializerTest { - - private Gson gson; - - @Before - public void setUp() { - gson = new GsonBuilder().registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()).create(); - } - - @Test - public void serialize_useSymbolForEachType() { - for (ResourceType type : ResourceType.values()) { - String expectedJson = "\"" + type.getSymbol() + "\""; - assertEquals(expectedJson, gson.toJson(type)); - } - } - - @Test - public void deserialize_useSymbolForEachType() { - for (ResourceType type : ResourceType.values()) { - String typeInJson = "\"" + type.getSymbol() + "\""; - assertEquals(type, gson.fromJson(typeInJson, ResourceType.class)); - } - } - - @Test - public void deserialize_nullFromNull() { - assertNull(gson.fromJson("null", ResourceType.class)); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failsOnEmptyString() { - gson.fromJson("\"\"", ResourceType.class); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failsOnGarbageString() { - gson.fromJson("\"thisisgarbage\"", ResourceType.class); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java deleted file mode 100644 index b5011fa5..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializerTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - -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.game.resources.ResourceType; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -public class ResourceTypesSerializerTest { - - private Gson gson; - - @Before - public void setUp() { - gson = new GsonBuilder().registerTypeAdapter(createListTypeToken(), new ResourceTypesSerializer()).create(); - } - - private static Type createListTypeToken() { - return new TypeToken<List<ResourceType>>() { - }.getType(); - } - - @Test - public void serialize_null() { - assertEquals("null", gson.toJson(null, createListTypeToken())); - } - - @Test - public void serialize_emptyList() { - List<ResourceType> types = new ArrayList<>(); - assertEquals("\"\"", gson.toJson(types, createListTypeToken())); - } - - @Test - public void serialize_singleType() { - List<ResourceType> types = new ArrayList<>(); - types.add(ResourceType.WOOD); - assertEquals("\"W\"", gson.toJson(types, createListTypeToken())); - } - - @Test - public void serialize_multipleTimesSameType() { - List<ResourceType> types = new ArrayList<>(); - types.add(ResourceType.WOOD); - types.add(ResourceType.WOOD); - types.add(ResourceType.WOOD); - assertEquals("\"WWW\"", gson.toJson(types, createListTypeToken())); - } - - @Test - public void serialize_mixedTypes() { - List<ResourceType> types = new ArrayList<>(); - types.add(ResourceType.WOOD); - types.add(ResourceType.CLAY); - types.add(ResourceType.STONE); - assertEquals("\"WCS\"", gson.toJson(types, createListTypeToken())); - } - - @Test - public void deserialize_null() { - assertNull(gson.fromJson("null", createListTypeToken())); - } - - @Test - public void deserialize_emptyList() { - List<ResourceType> types = new ArrayList<>(); - assertEquals(types, gson.fromJson("\"\"", createListTypeToken())); - } - - @Test - public void deserialize_singleType() { - List<ResourceType> types = new ArrayList<>(); - types.add(ResourceType.WOOD); - assertEquals(types, gson.fromJson("\"W\"", createListTypeToken())); - } - - @Test - public void deserialize_multipleTimesSameType() { - List<ResourceType> types = new ArrayList<>(); - types.add(ResourceType.WOOD); - types.add(ResourceType.WOOD); - types.add(ResourceType.WOOD); - assertEquals(types, gson.fromJson("\"WWW\"", createListTypeToken())); - } - - @Test - public void deserialize_mixedTypes() { - List<ResourceType> types = new ArrayList<>(); - 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/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java deleted file mode 100644 index e1dc4e2b..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializerTest.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import org.junit.Before; -import org.junit.Test; -import org.luxons.sevenwonders.game.resources.ResourceType; -import org.luxons.sevenwonders.game.resources.Resources; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -public class ResourcesSerializerTest { - - private Gson gson; - - @Before - public void setUp() { - gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()).create(); - } - - @Test - public void serialize_null() { - assertEquals("null", gson.toJson(null, Resources.class)); - } - - @Test - public void serialize_emptyResourcesToNull() { - Resources resources = new Resources(); - assertEquals("null", gson.toJson(resources)); - } - - @Test - public void serialize_singleType() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); - assertEquals("\"W\"", gson.toJson(resources)); - } - - @Test - public void serialize_multipleTimesSameType() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 3); - assertEquals("\"WWW\"", gson.toJson(resources)); - } - - @Test - public void serialize_mixedTypes() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); - resources.add(ResourceType.STONE, 1); - resources.add(ResourceType.CLAY, 1); - assertEquals("\"WSC\"", gson.toJson(resources)); - } - - @Test - public void serialize_mixedTypes_unordered() { - Resources resources = new 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 - public void deserialize_null() { - assertNull(gson.fromJson("null", Resources.class)); - } - - @Test - public void deserialize_emptyList() { - Resources resources = new Resources(); - assertEquals(resources, gson.fromJson("\"\"", Resources.class)); - } - - @Test - public void deserialize_singleType() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); - assertEquals(resources, gson.fromJson("\"W\"", Resources.class)); - } - - @Test - public void deserialize_multipleTimesSameType() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 3); - assertEquals(resources, gson.fromJson("\"WWW\"", Resources.class)); - } - - @Test - public void deserialize_mixedTypes() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); - resources.add(ResourceType.CLAY, 1); - resources.add(ResourceType.STONE, 1); - assertEquals(resources, gson.fromJson("\"WCS\"", Resources.class)); - } - - @Test - public void deserialize_mixedTypes_unordered() { - Resources resources = new Resources(); - resources.add(ResourceType.WOOD, 1); - resources.add(ResourceType.CLAY, 2); - resources.add(ResourceType.STONE, 3); - assertEquals(resources, gson.fromJson("\"SCWCSS\"", Resources.class)); - } -} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java deleted file mode 100644 index 517a4b1f..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java +++ /dev/null @@ -1,146 +0,0 @@ -package org.luxons.sevenwonders.game.data.serializers; - -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.TestUtilsKt; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class ScienceProgressSerializerTest { - - private static final String COMPASS_STR = "\"COMPASS\""; - - private static final String WHEEL_STR = "\"WHEEL\""; - - private static final String TABLET_STR = "\"TABLET\""; - - private static final String JOKER_STR = "\"any\""; - - private Gson gson; - - @Before - public void setUp() { - gson = new GsonBuilder().registerTypeAdapter(ScienceProgress.class, new ScienceProgressSerializer()).create(); - } - - @Test - public void serialize_emptyToNull() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 0, 0, 0); - String json = gson.toJson(progress); - assertEquals("null", json); - } - - @Test - public void serialize_oneCompass() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(1, 0, 0, 0); - String json = gson.toJson(progress); - assertEquals(COMPASS_STR, json); - } - - @Test - public void serialize_oneWheel() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 1, 0, 0); - String json = gson.toJson(progress); - assertEquals(WHEEL_STR, json); - } - - @Test - public void serialize_oneTablet() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 0, 1, 0); - String json = gson.toJson(progress); - assertEquals(TABLET_STR, json); - } - - @Test - public void serialize_oneJoker() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 0, 0, 1); - String json = gson.toJson(progress); - assertEquals(JOKER_STR, json); - } - - @Test(expected = UnsupportedOperationException.class) - public void serialize_failOnMultipleCompasses() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(2, 0, 0, 0); - gson.toJson(progress); - } - - @Test(expected = UnsupportedOperationException.class) - public void serialize_failOnMultipleWheels() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 2, 0, 0); - gson.toJson(progress); - } - - @Test(expected = UnsupportedOperationException.class) - public void serialize_failOnMultipleTablets() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 0, 2, 0); - gson.toJson(progress); - } - - @Test(expected = UnsupportedOperationException.class) - public void serialize_failOnMultipleJokers() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(0, 0, 0, 2); - gson.toJson(progress); - } - - @Test(expected = UnsupportedOperationException.class) - public void serialize_failOnMixedElements() { - ScienceProgress progress = TestUtilsKt.createScienceProgress(1, 1, 0, 0); - gson.toJson(progress); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnEmptyString() { - gson.fromJson("\"\"", ScienceProgress.class); - } - - @Test(expected = IllegalArgumentException.class) - public void deserialize_failOnGarbageString() { - gson.fromJson("thisisgarbage", ScienceProgress.class); - } - - @Test - public void deserialize_compass() { - ScienceProgress progress = gson.fromJson(COMPASS_STR, ScienceProgress.class); - assertNotNull(progress.getScience()); - assertEquals(1, progress.getScience().getQuantity(ScienceType.COMPASS)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); - assertEquals(0, progress.getScience().getJokers()); - } - - @Test - public void deserialize_wheel() { - ScienceProgress progress = gson.fromJson(WHEEL_STR, ScienceProgress.class); - assertNotNull(progress.getScience()); - assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); - assertEquals(1, progress.getScience().getQuantity(ScienceType.WHEEL)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); - assertEquals(0, progress.getScience().getJokers()); - } - - @Test - public void deserialize_tablet() { - ScienceProgress progress = gson.fromJson(TABLET_STR, ScienceProgress.class); - assertNotNull(progress.getScience()); - assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); - assertEquals(1, progress.getScience().getQuantity(ScienceType.TABLET)); - assertEquals(0, progress.getScience().getJokers()); - } - - @Test - public void deserialize_joker() { - ScienceProgress progress = gson.fromJson(JOKER_STR, ScienceProgress.class); - assertNotNull(progress.getScience()); - assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL)); - assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET)); - assertEquals(1, progress.getScience().getJokers()); - } -} 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\"" + } +} |