diff options
-rw-r--r-- | src/main/java/org/luxons/sevenwonders/game/boards/Military.java | 11 | ||||
-rw-r--r-- | src/test/java/org/luxons/sevenwonders/game/boards/MilitaryTest.java | 70 |
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 |