diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-21 12:36:30 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-21 12:36:30 +0100 |
commit | 6ea510e94ee51b221bac4ffe494c0307fc6e1064 (patch) | |
tree | a957b49b9514f1c252699f6a929cc85417377fe0 | |
parent | Add test for NumericEffectSerializer (diff) | |
download | seven-wonders-6ea510e94ee51b221bac4ffe494c0307fc6e1064.tar.gz seven-wonders-6ea510e94ee51b221bac4ffe494c0307fc6e1064.tar.bz2 seven-wonders-6ea510e94ee51b221bac4ffe494c0307fc6e1064.zip |
Add test for Decks
5 files changed, 116 insertions, 8 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Decks.java b/src/main/java/org/luxons/sevenwonders/game/Decks.java index abc8e817..5ecc3000 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Decks.java +++ b/src/main/java/org/luxons/sevenwonders/game/Decks.java @@ -39,6 +39,9 @@ public class Decks { } private void validateNbCards(List<Card> deck, int nbPlayers) { + if (nbPlayers == 0) { + throw new IllegalArgumentException("Cannot deal cards between 0 players"); + } if (deck.size() % nbPlayers != 0) { throw new IllegalArgumentException( String.format("Cannot deal %d cards evenly between %d players", deck.size(), nbPlayers)); diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index cc2275a2..fe160857 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -13,20 +13,23 @@ public class Card { private final Requirements requirements; - private final String chainParent; - private final List<Effect> effects; + private final String chainParent; + private final List<String> chainChildren; - public Card(String name, Color color, Requirements requirements, String chainParent, List<Effect> effects, - List<String> chainChildren) { + private final String image; + + public Card(String name, Color color, Requirements requirements, List<Effect> effects, String chainParent, + List<String> chainChildren, String image) { this.name = name; this.color = color; this.requirements = requirements; this.chainParent = chainParent; this.effects = effects; this.chainChildren = chainChildren; + this.image = image; } public String getName() { diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java index f46e4b4f..48853899 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java @@ -14,10 +14,10 @@ public class CardDefinition implements Definition<Card> { private Color color; - private EffectsDefinition effect; - private Requirements requirements; + private EffectsDefinition effect; + private String chainParent; private List<String> chainChildren; @@ -28,7 +28,7 @@ public class CardDefinition implements Definition<Card> { @Override public Card create(Settings settings) { - return new Card(name, color, requirements, chainParent, effect.create(settings), chainChildren); + return new Card(name, color, requirements, effect.create(settings), chainParent, chainChildren, image); } public String getName() { diff --git a/src/test/java/org/luxons/sevenwonders/game/DecksTest.java b/src/test/java/org/luxons/sevenwonders/game/DecksTest.java new file mode 100644 index 00000000..a00e9c76 --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/DecksTest.java @@ -0,0 +1,102 @@ +package org.luxons.sevenwonders.game; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.luxons.sevenwonders.game.Decks.CardNotFoundException; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; + +import static org.junit.Assert.*; + +public class DecksTest { + + private static Decks createDecks(int nbAges, int nbCardsPerAge) { + Map<Integer, List<Card>> cardsPerAge = new HashMap<>(); + for (int age = 1; age <= nbAges; age++) { + int firstCardNumber = (age - 1) * nbCardsPerAge; + cardsPerAge.put(age, createSampleCards(firstCardNumber, nbCardsPerAge)); + } + return new Decks(cardsPerAge); + } + + private static List<Card> createSampleCards(int fromIndex, int nbCards) { + List<Card> sampleCards = new ArrayList<>(); + for (int i = fromIndex; i < fromIndex + nbCards; i++) { + sampleCards.add(new Card("Test Card " + i, Color.BLUE, null, null, null, null, null)); + } + return sampleCards; + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsOnNullNameWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard(null); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsOnEmptyNameWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard(""); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsWhenDeckIsEmpty() { + Decks decks = createDecks(0, 0); + decks.getCard("Any name"); + } + + @Test(expected = CardNotFoundException.class) + public void getCard_failsWhenCardIsNotFound() { + Decks decks = createDecks(3, 20); + decks.getCard("Unknown name"); + } + + @Test + public void getCard_succeedsWhenCardIsFound() { + Decks decks = createDecks(3, 20); + Card card = decks.getCard("Test Card 3"); + assertEquals("Test Card 3", card.getName()); + } + + @Test(expected = IllegalArgumentException.class) + public void deal_failsOnZeroPlayers() { + Decks decks = createDecks(3, 20); + decks.deal(1, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void deal_failsOnMissingAge() { + Decks decks = createDecks(2, 0); + decks.deal(4, 10); + } + + @Test(expected = IllegalArgumentException.class) + public void deal_failsWhenTooFewPlayers() { + Decks decks = createDecks(3, 28); + decks.deal(1, 3); + } + + @Test + public void deal_succeedsOnZeroCards() { + Decks decks = createDecks(3, 0); + Map<Integer, List<Card>> hands = decks.deal(1, 10); + for (List<Card> hand : hands.values()) { + assertTrue(hand.isEmpty()); + } + } + + @Test + public void deal_evenDistribution() { + int nbCardsPerAge = 12; + int nbPlayers = 4; + Decks decks = createDecks(3, nbCardsPerAge); + Map<Integer, List<Card>> hands = decks.deal(1, nbPlayers); + for (List<Card> hand : hands.values()) { + assertEquals(nbCardsPerAge / nbPlayers, hand.size()); + } + } +}
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java index 62bf376d..cbd2fd71 100644 --- a/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java @@ -39,7 +39,7 @@ public class CardTest { List<Effect> effects = Collections.singletonList(treeFarmEffect); - treeFarmCard = new Card("Tree Farm", Color.BROWN, treeFarmRequirements, "", effects, null); + treeFarmCard = new Card("Tree Farm", Color.BROWN, treeFarmRequirements, effects, "", null, null); } @Test |