From 097c08df4f17ccff079677611fddee1a990a7ab8 Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 22 Dec 2016 02:30:51 +0100 Subject: Introduce Hands (pun intended) --- .../luxons/sevenwonders/game/cards/DecksTest.java | 66 +++++++++++-------- .../luxons/sevenwonders/game/cards/HandsTest.java | 77 ++++++++++++++++++++++ .../luxons/sevenwonders/game/test/TestUtils.java | 13 ++++ 3 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java (limited to 'src/test/java/org/luxons') diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java index b7fad970..06060f16 100644 --- a/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java +++ b/src/test/java/org/luxons/sevenwonders/game/cards/DecksTest.java @@ -1,34 +1,42 @@ package org.luxons.sevenwonders.game.cards; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Rule; import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +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.cards.Decks.CardNotFoundException; +import org.luxons.sevenwonders.game.test.TestUtils; import static org.junit.Assert.*; +import static org.junit.Assume.*; +@RunWith(Theories.class) public class DecksTest { + @DataPoints + public static int[] dataPoints() { + return new int[] {1, 2, 3, 5, 10}; + } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + 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)); + cardsPerAge.put(age, TestUtils.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); @@ -72,29 +80,31 @@ public class DecksTest { decks.deal(4, 10); } - @Test(expected = IllegalArgumentException.class) - public void deal_failsWhenTooFewPlayers() { - Decks decks = createDecks(3, 28); - decks.deal(1, 3); + @Theory + public void deal_failsWhenTooFewPlayers(int nbPlayers, int nbCards) { + assumeTrue(nbCards % nbPlayers != 0); + thrown.expect(IllegalArgumentException.class); + Decks decks = createDecks(1, nbCards); + decks.deal(1, nbPlayers); } - @Test - public void deal_succeedsOnZeroCards() { - Decks decks = createDecks(3, 0); - Map> hands = decks.deal(1, 10); - for (List hand : hands.values()) { - assertTrue(hand.isEmpty()); + @Theory + public void deal_succeedsOnZeroCards(int nbPlayers) { + Decks decks = createDecks(1, 0); + Hands hands = decks.deal(1, nbPlayers); + for (int i = 0; i < nbPlayers; i++) { + assertNotNull(hands.get(i)); + assertTrue(hands.get(i).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()); + @Theory + public void deal_evenDistribution(int nbPlayers, int nbCardsPerPlayer) { + int nbCardsPerAge = nbPlayers * nbCardsPerPlayer; + Decks decks = createDecks(1, nbCardsPerAge); + Hands hands = decks.deal(1, nbPlayers); + for (int i = 0; i < nbPlayers; i++) { + assertEquals(nbCardsPerPlayer, hands.get(i).size()); } } } \ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java new file mode 100644 index 00000000..97cb4625 --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java @@ -0,0 +1,77 @@ +package org.luxons.sevenwonders.game.cards; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.luxons.sevenwonders.game.cards.Hands.PlayerIndexOutOfBoundsException; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.*; + +public class HandsTest { + + private static Hands createHands(int nbPlayers, int nbCardsPerPlayer) { + Map> hands = new HashMap<>(); + for (int p = 0; p < nbPlayers; p++) { + int firstCardNumber = (p - 1) * nbCardsPerPlayer; + hands.put(p, TestUtils.createSampleCards(firstCardNumber, nbCardsPerPlayer)); + } + return new Hands(hands, nbPlayers); + } + + @Test(expected = PlayerIndexOutOfBoundsException.class) + public void get_failsOnMissingPlayer() { + Hands hands = createHands(4, 7); + hands.get(5); + } + + @Test + public void get_retrievesCorrectCards() { + List hand0 = TestUtils.createSampleCards(0, 5); + List hand1 = TestUtils.createSampleCards(5, 10); + Map> handsMap = new HashMap<>(); + handsMap.put(0, hand0); + handsMap.put(1, hand1); + Hands hands = new Hands(handsMap, 2); + assertEquals(hand0, hands.get(0)); + assertEquals(hand1, hands.get(1)); + } + + @Test + public void rotate_doesNotMoveWhenOffsetIsZero() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(0); + assertEquals(rotated.get(0), hands.get(0)); + assertEquals(rotated.get(1), hands.get(1)); + assertEquals(rotated.get(2), hands.get(2)); + } + + @Test + public void rotate_movesOfCorrectOffset_positive() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(1); + assertEquals(rotated.get(1), hands.get(0)); + assertEquals(rotated.get(2), hands.get(1)); + assertEquals(rotated.get(0), hands.get(2)); + } + + @Test + public void rotate_movesOfCorrectOffset_negative() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(-1); + assertEquals(rotated.get(2), hands.get(0)); + assertEquals(rotated.get(0), hands.get(1)); + assertEquals(rotated.get(1), hands.get(2)); + } + + @Test + public void rotate_movesOfCorrectOffset_negative2() { + Hands hands = createHands(3, 7); + Hands rotated = hands.rotate(-2); + assertEquals(rotated.get(1), hands.get(0)); + assertEquals(rotated.get(2), hands.get(1)); + assertEquals(rotated.get(0), hands.get(2)); + } +} \ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index 2a0292cd..2a3bc379 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -1,7 +1,12 @@ package org.luxons.sevenwonders.game.test; +import java.util.ArrayList; +import java.util.List; + import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.resources.Production; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; @@ -49,4 +54,12 @@ public class TestUtils { } return resources; } + + public 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; + } } -- cgit