summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-25 15:54:58 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-25 15:55:13 +0100
commit9cc964926891b9655571d553fbf0bbf2bc4174f2 (patch)
tree9d6747b329f5b0578b06b7e9ee1873b681d6a5dd
parentAdd military conflict resolution at end of age (diff)
downloadseven-wonders-9cc964926891b9655571d553fbf0bbf2bc4174f2.tar.gz
seven-wonders-9cc964926891b9655571d553fbf0bbf2bc4174f2.tar.bz2
seven-wonders-9cc964926891b9655571d553fbf0bbf2bc4174f2.zip
Add proper test for military victory/defeat points
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Military.java11
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java70
2 files changed, 80 insertions, 1 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Military.java b/src/main/java/org/luxons/sevenwonders/game/boards/Military.java
index 08559336..fb93fa96 100644
--- a/src/main/java/org/luxons/sevenwonders/game/boards/Military.java
+++ b/src/main/java/org/luxons/sevenwonders/game/boards/Military.java
@@ -33,7 +33,10 @@ public class Military {
}
public void victory(int age) {
- int wonPoints = settings.getWonPointsPerVictoryPerAge().get(age);
+ Integer wonPoints = settings.getWonPointsPerVictoryPerAge().get(age);
+ if (wonPoints == null) {
+ throw new UnknownAgeException(age);
+ }
totalPoints += wonPoints;
}
@@ -42,4 +45,10 @@ public class Military {
totalPoints -= lostPoints;
nbDefeatTokens++;
}
+
+ static final class UnknownAgeException extends IllegalArgumentException {
+ UnknownAgeException(int unknownAge) {
+ super(String.valueOf(unknownAge));
+ }
+ }
}
diff --git a/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java b/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java
new file mode 100644
index 00000000..9a1fcb65
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java
@@ -0,0 +1,70 @@
+package org.luxons.sevenwonders.game.boards;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Rule;
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.FromDataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.luxons.sevenwonders.game.Settings;
+import org.luxons.sevenwonders.game.boards.Military.UnknownAgeException;
+
+import static org.junit.Assert.*;
+
+@RunWith(Theories.class)
+public class MilitaryTest {
+
+ @DataPoints("points")
+ public static int[] points() {
+ return new int[] {0, 1, 3, 5};
+ }
+
+ @DataPoints("ages")
+ public static int[] ages() {
+ return new int[] {1, 2, 3};
+ }
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private static Military createMilitary(int age, int nbPointsPerVictory, int nbPointsPerDefeat) {
+ Map<Integer, Integer> wonPointsPerAge = new HashMap<>();
+ wonPointsPerAge.put(age, nbPointsPerVictory);
+
+ Settings settings = new Settings();
+ settings.setWonPointsPerVictoryPerAge(wonPointsPerAge);
+ settings.setLostPointsPerDefeat(nbPointsPerDefeat);
+
+ return new Military(settings);
+ }
+
+ @Theory
+ public void victory_addsCorrectPoints(@FromDataPoints("ages") int age, @FromDataPoints("points") int
+ nbPointsPerVictory) {
+ Military military = createMilitary(age, nbPointsPerVictory, 0);
+ int initialPoints = military.getTotalPoints();
+
+ military.victory(age);
+ assertEquals(initialPoints + nbPointsPerVictory, military.getTotalPoints());
+ }
+
+ @Theory
+ public void victory_failsIfUnknownAge(@FromDataPoints("points") int nbPointsPerVictory) {
+ Military military = createMilitary(0, nbPointsPerVictory, 0);
+ thrown.expect(UnknownAgeException.class);
+ military.victory(1);
+ }
+
+ @Theory
+ public void defeat_removesCorrectPoints(@FromDataPoints("points") int nbPointsLostPerDefeat) {
+ Military military = createMilitary(0, 0, nbPointsLostPerDefeat);
+ int initialPoints = military.getTotalPoints();
+
+ military.defeat();
+ assertEquals(initialPoints - nbPointsLostPerDefeat, military.getTotalPoints());
+ }
+} \ No newline at end of file
bgstack15