diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-20 22:47:25 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-21 01:29:28 +0100 |
commit | b21b0b854de124776d547c9cb2d42443396833f9 (patch) | |
tree | 1b3caaecf062d5a95d3d1e492faf965b942c30a9 /src/main/java/org | |
parent | Remove experimental TestController (diff) | |
download | seven-wonders-b21b0b854de124776d547c9cb2d42443396833f9.tar.gz seven-wonders-b21b0b854de124776d547c9cb2d42443396833f9.tar.bz2 seven-wonders-b21b0b854de124776d547c9cb2d42443396833f9.zip |
Add test for ScienceProgressSerializer
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/game/boards/Science.java | 2 | ||||
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java | 17 |
2 files changed, 14 insertions, 5 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java index f3d6a681..71687657 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java @@ -32,7 +32,7 @@ public class Science { } public int size() { - return quantities.size() + jokers; + return quantities.values().stream().mapToInt(q -> q).sum() + jokers; } public int computePoints() { diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java index 0535b90a..b6e38540 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java @@ -2,6 +2,10 @@ package org.luxons.sevenwonders.game.data.serializers; import java.lang.reflect.Type; +import org.luxons.sevenwonders.game.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.effects.ScienceProgress; + import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -10,9 +14,6 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; -import org.luxons.sevenwonders.game.boards.Science; -import org.luxons.sevenwonders.game.boards.ScienceType; -import org.luxons.sevenwonders.game.effects.ScienceProgress; public class ScienceProgressSerializer implements JsonSerializer<ScienceProgress>, JsonDeserializer<ScienceProgress> { @@ -47,9 +48,17 @@ public class ScienceProgressSerializer implements JsonSerializer<ScienceProgress if ("any".equals(s)) { science.addJoker(1); } else { - science.add(context.deserialize(json, ScienceType.class), 1); + science.add(deserializeScienceType(json, context), 1); } scienceProgress.setScience(science); return scienceProgress; } + + private ScienceType deserializeScienceType(JsonElement json, JsonDeserializationContext context) { + ScienceType type = context.deserialize(json, ScienceType.class); + if (type == null) { + throw new IllegalArgumentException("Invalid science type " + json.getAsString()); + } + return type; + } } |