From 6ea510e94ee51b221bac4ffe494c0307fc6e1064 Mon Sep 17 00:00:00 2001 From: jbion Date: Wed, 21 Dec 2016 12:36:30 +0100 Subject: Add test for Decks --- .../org/luxons/sevenwonders/game/DecksTest.java | 102 +++++++++++++++++++++ .../luxons/sevenwonders/game/cards/CardTest.java | 2 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/luxons/sevenwonders/game/DecksTest.java (limited to 'src/test') 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> 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 createSampleCards(int fromIndex, int nbCards) { + List 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> hands = decks.deal(1, 10); + for (List hand : hands.values()) { + assertTrue(hand.isEmpty()); + } + } + + @Test + public void deal_evenDistribution() { + int nbCardsPerAge = 12; + int nbPlayers = 4; + Decks decks = createDecks(3, nbCardsPerAge); + Map> hands = decks.deal(1, nbPlayers); + for (List 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 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 -- cgit