summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-17 08:46:31 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-17 08:46:31 +0200
commita0bab4f87a323b881f40a45741e96eb2d2dc15c4 (patch)
tree47b54c00454aa8eda92f51474180e05354b250f2
parentAdd test for Requirements coverage (diff)
downloadseven-wonders-a0bab4f87a323b881f40a45741e96eb2d2dc15c4.tar.gz
seven-wonders-a0bab4f87a323b881f40a45741e96eb2d2dc15c4.tar.bz2
seven-wonders-a0bab4f87a323b881f40a45741e96eb2d2dc15c4.zip
Add tests for board score computation
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java16
-rw-r--r--backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java47
-rw-r--r--backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java12
3 files changed, 65 insertions, 10 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java b/backend/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
index f67924e1..f4a0d832 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
@@ -1,21 +1,31 @@
package org.luxons.sevenwonders.game.scoring;
import java.util.HashMap;
+import java.util.Map;
-public class PlayerScore extends HashMap<ScoreCategory, Integer> {
+public class PlayerScore {
private final int boardGold;
+ private final Map<ScoreCategory, Integer> scoresByCategory = new HashMap<>();
+
private int totalPoints = 0;
public PlayerScore(int boardGold) {
this.boardGold = boardGold;
}
- @Override
public Integer put(ScoreCategory category, Integer points) {
totalPoints += points;
- return super.put(category, points);
+ return scoresByCategory.put(category, points);
+ }
+
+ public int getPoints(ScoreCategory category) {
+ return scoresByCategory.get(category);
+ }
+
+ public Map<ScoreCategory, Integer> getPointsPerCategory() {
+ return scoresByCategory;
}
public int getTotalPoints() {
diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java
index a4070c3c..90a5e5d2 100644
--- a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java
+++ b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java
@@ -17,10 +17,14 @@ import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.boards.Board.InsufficientFundsException;
import org.luxons.sevenwonders.game.cards.Card;
import org.luxons.sevenwonders.game.cards.Color;
+import org.luxons.sevenwonders.game.effects.Effect;
+import org.luxons.sevenwonders.game.effects.RawPointsIncrease;
import org.luxons.sevenwonders.game.effects.SpecialAbility;
import org.luxons.sevenwonders.game.effects.SpecialAbilityActivation;
import org.luxons.sevenwonders.game.resources.ResourceType;
import org.luxons.sevenwonders.game.resources.Resources;
+import org.luxons.sevenwonders.game.scoring.PlayerScore;
+import org.luxons.sevenwonders.game.scoring.ScoreCategory;
import org.luxons.sevenwonders.game.test.TestUtils;
import static junit.framework.TestCase.assertEquals;
@@ -34,12 +38,12 @@ public class BoardTest {
@DataPoints("gold")
public static int[] goldAmounts() {
- return new int[] {-3, -1, 0, 1, 2, 3};
+ return new int[]{-3, -1, 0, 1, 2, 3};
}
@DataPoints("nbCards")
public static int[] nbCards() {
- return new int[] {0, 1, 2};
+ return new int[]{0, 1, 2};
}
@DataPoints
@@ -79,7 +83,7 @@ public class BoardTest {
@Theory
public void removeGold_successfulWhenNotTooMuch(@FromDataPoints("gold") int initialGold,
- @FromDataPoints("gold") int goldRemoved) {
+ @FromDataPoints("gold") int goldRemoved) {
assumeTrue(goldRemoved >= 0);
assumeTrue(initialGold >= goldRemoved);
Board board = new Board(TestUtils.createWonder(), 0, new Settings(5));
@@ -90,7 +94,7 @@ public class BoardTest {
@Theory
public void removeGold_failsWhenTooMuch(@FromDataPoints("gold") int initialGold,
- @FromDataPoints("gold") int goldRemoved) {
+ @FromDataPoints("gold") int goldRemoved) {
assumeTrue(goldRemoved >= 0);
assumeTrue(initialGold < goldRemoved);
thrown.expect(InsufficientFundsException.class);
@@ -101,7 +105,7 @@ public class BoardTest {
@Theory
public void getNbCardsOfColor_properCount_singleColor(ResourceType type, @FromDataPoints("nbCards") int nbCards,
- @FromDataPoints("nbCards") int nbOtherCards, Color color) {
+ @FromDataPoints("nbCards") int nbOtherCards, Color color) {
Board board = TestUtils.createBoard(type);
TestUtils.addCards(board, nbCards, nbOtherCards, color);
assertEquals(nbCards, board.getNbCardsOfColor(Collections.singletonList(color)));
@@ -109,8 +113,9 @@ public class BoardTest {
@Theory
public void getNbCardsOfColor_properCount_multiColors(ResourceType type, @FromDataPoints("nbCards") int nbCards1,
- @FromDataPoints("nbCards") int nbCards2, @FromDataPoints("nbCards") int nbOtherCards, Color color1,
- Color color2) {
+ @FromDataPoints("nbCards") int nbCards2,
+ @FromDataPoints("nbCards") int nbOtherCards, Color color1,
+ Color color2) {
Board board = TestUtils.createBoard(type);
TestUtils.addCards(board, nbCards1, color1);
TestUtils.addCards(board, nbCards2, color2);
@@ -178,4 +183,32 @@ public class BoardTest {
assertFalse(board.canPlayFreeCard(1));
assertFalse(board.canPlayFreeCard(2));
}
+
+ @Theory
+ public void computePoints_gold(@FromDataPoints("gold") int gold) {
+ assumeTrue(gold >= 0);
+ Board board = TestUtils.createBoard(ResourceType.WOOD);
+ Table table = new Table(Collections.singletonList(board));
+ board.setGold(gold);
+
+ PlayerScore score = board.computePoints(table);
+ assertEquals(gold / 3, (int) score.getPoints(ScoreCategory.GOLD));
+ assertEquals(gold / 3, score.getTotalPoints());
+ }
+
+ @Theory
+ public void computePoints_(@FromDataPoints("gold") int gold) {
+ assumeTrue(gold >= 0);
+ Board board = TestUtils.createBoard(ResourceType.WOOD);
+ Table table = new Table(Collections.singletonList(board));
+ board.setGold(gold);
+
+ Effect effect = new RawPointsIncrease(5);
+ TestUtils.playCardWithEffect(table, 0, Color.BLUE, effect);
+
+ PlayerScore score = board.computePoints(table);
+ assertEquals(gold / 3, (int) score.getPoints(ScoreCategory.GOLD));
+ assertEquals(5, (int) score.getPoints(ScoreCategory.CIVIL));
+ assertEquals(5 + gold / 3, score.getTotalPoints());
+ }
}
diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
index 65ba4369..4b727944 100644
--- a/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
+++ b/backend/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
@@ -131,6 +131,11 @@ public class TestUtils {
return new Card("Test Card", color, new Requirements(), null, null, null, null);
}
+ public static Card createCard(Color color, Effect effect) {
+ List<Effect> effects = Collections.singletonList(effect);
+ return new Card("Test Card", color, new Requirements(), effects, null, null, null);
+ }
+
private static Card createCard(int num, Color color) {
return new Card("Test Card " + num, color, new Requirements(), null, null, null, null);
}
@@ -184,4 +189,11 @@ public class TestUtils {
}
return science;
}
+
+ public static void playCardWithEffect(Table table, int playerIndex, Color color, Effect effect) {
+ Card card = createCard(color, effect);
+ Board board = table.getBoard(playerIndex);
+ board.addCard(card);
+ card.applyTo(table, playerIndex, Collections.emptyList());
+ }
}
bgstack15