From f7fe057b745d3f0e19b5dd6bd1819b11fc89c551 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 7 Aug 2016 22:18:55 +0200 Subject: Added filtering, still not working fully --- .../cardstorage/ReferenceCardContainerTest.java | 168 -------------- .../cardstorage/StandaloneCardContainerTest.java | 171 -------------- .../containers/ReferenceCardContainerTest.java | 167 ++++++++++++++ .../containers/StandaloneCardContainerTest.java | 170 ++++++++++++++ .../cardbase/decks/ReferenceDeckTest.java | 2 +- .../cardbase/filtering/CardFilteringTest.java | 252 +++++++++++++++++++++ .../cardbase/sorting/CardSortingTest.java | 88 ++++--- 7 files changed, 641 insertions(+), 377 deletions(-) delete mode 100644 test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java delete mode 100644 test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java create mode 100644 test/eu/equalparts/cardbase/filtering/CardFilteringTest.java (limited to 'test/eu/equalparts') diff --git a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java deleted file mode 100644 index bf49e2f..0000000 --- a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java +++ /dev/null @@ -1,168 +0,0 @@ -package eu.equalparts.cardbase.cardstorage; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import eu.equalparts.cardbase.CardbaseTest; -import eu.equalparts.cardbase.cards.Card; - -public class ReferenceCardContainerTest { - - private ReferenceCardContainer uut; - private static Card testCard; - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Rule - public final TemporaryFolder tempFolder = new TemporaryFolder(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class); - } - - @Before - public void setUp() throws Exception { - uut = new ReferenceCardContainer(); - } - - /*********************************************************************************** - * Adding card tests, happy path - ***********************************************************************************/ - @Test - public void newCardIsAdded() throws Exception { - assertEquals("Container should not contain the test card to begin with.", 0, uut.getCount(testCard)); - - uut.addCard(testCard, 1); - - assertEquals("Container should contain 1 test card.", 1, uut.getCount(testCard)); - } - - @Test - public void existingCardIsIncremented() throws Exception { - uut.addCard(testCard, 2); - uut.addCard(testCard, 4); - - assertEquals("Card count was not updated correctly.", 6, uut.getCount(testCard)); - } - - /* - * Edge cases - */ - @Test - public void cardAddedIsNull() throws Exception { - exception.expect(NullPointerException.class); - uut.addCard(null, 0); - } - - /*********************************************************************************** - * Removing card tests, happy path - ***********************************************************************************/ - @Test - public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception { - uut.addCard(testCard, 5); - - int removed = uut.removeCard(testCard, 3); - - assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 3, removed); - } - - @Test - public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception { - uut.addCard(testCard, 5); - - int removed = uut.removeCard(testCard, 5); - - assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 5, removed); - } - - @Test - public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception { - uut.addCard(testCard, 3); - - int removed = uut.removeCard(testCard, 5); - - assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 3, removed); - } - - /* - * Edge cases - */ - @Test - public void removedCardIsNull() throws Exception { - exception.expect(NullPointerException.class); - uut.removeCard(null, 0); - } - - @Test - public void removedCardIsNotInContainer() throws Exception { - assertEquals("Card is not initially missing from container.", 0, uut.getCount(testCard)); - - int removed = uut.removeCard(testCard, 1); - - assertEquals("Removed count should be 0.", 0, removed); - assertEquals("Card should still be missing from container.", 0, uut.getCount(testCard)); - } - - @Test - public void removedCountIsLessThanZero() throws Exception { - uut.addCard(testCard, 3); - - int removed = uut.removeCard(testCard, -4); - - assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 0, removed); - } - - /*********************************************************************************** - * Land tests - ***********************************************************************************/ - @Test - public void containsIslands() throws Exception { - assertEquals(0, uut.getIslands()); - uut.setIslands(5); - assertEquals(5, uut.getIslands()); - } - - @Test - public void containsPlains() throws Exception { - assertEquals(0, uut.getPlains()); - uut.setPlains(5); - assertEquals(5, uut.getPlains()); - } - - @Test - public void containsSwamps() throws Exception { - assertEquals(0, uut.getSwamps()); - uut.setSwamps(5); - assertEquals(5, uut.getSwamps()); - } - - @Test - public void containsMountains() throws Exception { - assertEquals(0, uut.getMountains()); - uut.setMountains(5); - assertEquals(5, uut.getMountains()); - } - - @Test - public void containsForests() throws Exception { - assertEquals(0, uut.getForests()); - uut.setForests(5); - assertEquals(5, uut.getForests()); - } - -} diff --git a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java deleted file mode 100644 index 6071e09..0000000 --- a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java +++ /dev/null @@ -1,171 +0,0 @@ -package eu.equalparts.cardbase.cardstorage; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.lang.reflect.Field; - -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import eu.equalparts.cardbase.CardbaseTest; -import eu.equalparts.cardbase.cards.Card; - -public class StandaloneCardContainerTest { - private StandaloneCardContainer uut; - private static Card testCard; - - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Rule - public final TemporaryFolder tempFolder = new TemporaryFolder(); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class); - } - - @Before - public void setUp() throws Exception { - uut = new StandaloneCardContainer(); - } - - /*********************************************************************************** - * Adding card tests, happy path - ***********************************************************************************/ - @Test - public void newCardIsAdded() throws Exception { - assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode, testCard.number)); - - uut.addCard(testCard, 1); - - assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode, testCard.number)); - assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard)); - } - - @Test - public void existingCardIsIncremented() throws Exception { - uut.addCard(testCard, 2); - uut.addCard(testCard, 4); - - Card addedCard = uut.getCard(testCard.setCode, testCard.number); - assertNotNull("Card was not found in cardbase.", addedCard); - assertEquals("Card count was not updated correctly.", 6, uut.getCount(addedCard)); - } - - /* - * Edge cases - */ - @Test - public void cardAddedIsNull() throws Exception { - exception.expect(NullPointerException.class); - uut.addCard(null, 0); - } - - /*********************************************************************************** - * Removing card tests, happy path - ***********************************************************************************/ - @Test - public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception { - uut.addCard(testCard, 5); - - int removed = uut.removeCard(testCard, 3); - - assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 3, removed); - assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); - } - - @Test - public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception { - uut.addCard(testCard, 5); - - int removed = uut.removeCard(testCard, 5); - - assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 5, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); - } - - @Test - public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception { - uut.addCard(testCard, 3); - - int removed = uut.removeCard(testCard, 5); - - assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 3, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); - } - - /* - * Edge cases - */ - @Test - public void removedCardIsNull() throws Exception { - exception.expect(NullPointerException.class); - uut.removeCard(null, 0); - } - - @Test - public void removedCardIsNotInContainer() throws Exception { - assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode, testCard.number)); - - int removed = uut.removeCard(testCard, 1); - - assertEquals("Removed count should be 0.", 0, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); - } - - @Test - public void removedCountIsLessThanZero() throws Exception { - uut.addCard(testCard, 3); - - int removed = uut.removeCard(testCard, -4); - - assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard)); - assertEquals("Container reports wrong removed count.", 0, removed); - assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); - } - - /*********************************************************************************** - * Card getter tests, happy path - ***********************************************************************************/ - @Test - public void correctCardIsReturnedByGetter() throws Exception { - uut.addCard(testCard, 1); - - Card card = uut.getCard(testCard.setCode, testCard.number); - - for (Field field : Card.class.getFields()) { - assertEquals("Field " + field.getName(), field.get(testCard), field.get(card)); - } - } - - @Test - public void correctCardCollectionIsReturnedByGetter() throws Exception { - uut.addCard(testCard, 1); - - assertTrue("Not all cards were returned by the getter.", uut.getCards().contains(testCard)); - } - - @Test - public void cardCollectionWhenContainerIsEmpty() throws Exception { - assertEquals("Returned collection size should have been 0.", 0, uut.getCards().size()); - } - - @Test - public void getCardIsNotInCardbase() throws Exception { - assertNull("Method should have returned null", uut.getCard(testCard.setCode, testCard.number)); - } -} diff --git a/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java new file mode 100644 index 0000000..849d84c --- /dev/null +++ b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java @@ -0,0 +1,167 @@ +package eu.equalparts.cardbase.containers; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import eu.equalparts.cardbase.cards.Card; + +public class ReferenceCardContainerTest { + + private ReferenceCardContainer uut; + private static Card testCard; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Rule + public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + testCard = mapper.readValue(ReferenceCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); + } + + @Before + public void setUp() throws Exception { + uut = new ReferenceCardContainer(); + } + + /*********************************************************************************** + * Adding card tests, happy path + ***********************************************************************************/ + @Test + public void newCardIsAdded() throws Exception { + assertEquals("Container should not contain the test card to begin with.", 0, uut.getCount(testCard)); + + uut.addCard(testCard, 1); + + assertEquals("Container should contain 1 test card.", 1, uut.getCount(testCard)); + } + + @Test + public void existingCardIsIncremented() throws Exception { + uut.addCard(testCard, 2); + uut.addCard(testCard, 4); + + assertEquals("Card count was not updated correctly.", 6, uut.getCount(testCard)); + } + + /* + * Edge cases + */ + @Test + public void cardAddedIsNull() throws Exception { + exception.expect(NullPointerException.class); + uut.addCard(null, 0); + } + + /*********************************************************************************** + * Removing card tests, happy path + ***********************************************************************************/ + @Test + public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception { + uut.addCard(testCard, 5); + + int removed = uut.removeCard(testCard, 3); + + assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 3, removed); + } + + @Test + public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception { + uut.addCard(testCard, 5); + + int removed = uut.removeCard(testCard, 5); + + assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 5, removed); + } + + @Test + public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception { + uut.addCard(testCard, 3); + + int removed = uut.removeCard(testCard, 5); + + assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 3, removed); + } + + /* + * Edge cases + */ + @Test + public void removedCardIsNull() throws Exception { + exception.expect(NullPointerException.class); + uut.removeCard(null, 0); + } + + @Test + public void removedCardIsNotInContainer() throws Exception { + assertEquals("Card is not initially missing from container.", 0, uut.getCount(testCard)); + + int removed = uut.removeCard(testCard, 1); + + assertEquals("Removed count should be 0.", 0, removed); + assertEquals("Card should still be missing from container.", 0, uut.getCount(testCard)); + } + + @Test + public void removedCountIsLessThanZero() throws Exception { + uut.addCard(testCard, 3); + + int removed = uut.removeCard(testCard, -4); + + assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 0, removed); + } + + /*********************************************************************************** + * Land tests + ***********************************************************************************/ + @Test + public void containsIslands() throws Exception { + assertEquals(0, uut.getIslands()); + uut.setIslands(5); + assertEquals(5, uut.getIslands()); + } + + @Test + public void containsPlains() throws Exception { + assertEquals(0, uut.getPlains()); + uut.setPlains(5); + assertEquals(5, uut.getPlains()); + } + + @Test + public void containsSwamps() throws Exception { + assertEquals(0, uut.getSwamps()); + uut.setSwamps(5); + assertEquals(5, uut.getSwamps()); + } + + @Test + public void containsMountains() throws Exception { + assertEquals(0, uut.getMountains()); + uut.setMountains(5); + assertEquals(5, uut.getMountains()); + } + + @Test + public void containsForests() throws Exception { + assertEquals(0, uut.getForests()); + uut.setForests(5); + assertEquals(5, uut.getForests()); + } + +} diff --git a/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java new file mode 100644 index 0000000..6e9b31a --- /dev/null +++ b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java @@ -0,0 +1,170 @@ +package eu.equalparts.cardbase.containers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Field; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import eu.equalparts.cardbase.cards.Card; + +public class StandaloneCardContainerTest { + private StandaloneCardContainer uut; + private static Card testCard; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Rule + public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + testCard = mapper.readValue(StandaloneCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); + } + + @Before + public void setUp() throws Exception { + uut = new StandaloneCardContainer(); + } + + /*********************************************************************************** + * Adding card tests, happy path + ***********************************************************************************/ + @Test + public void newCardIsAdded() throws Exception { + assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode, testCard.number)); + + uut.addCard(testCard, 1); + + assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode, testCard.number)); + assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard)); + } + + @Test + public void existingCardIsIncremented() throws Exception { + uut.addCard(testCard, 2); + uut.addCard(testCard, 4); + + Card addedCard = uut.getCard(testCard.setCode, testCard.number); + assertNotNull("Card was not found in cardbase.", addedCard); + assertEquals("Card count was not updated correctly.", 6, uut.getCount(addedCard)); + } + + /* + * Edge cases + */ + @Test + public void cardAddedIsNull() throws Exception { + exception.expect(NullPointerException.class); + uut.addCard(null, 0); + } + + /*********************************************************************************** + * Removing card tests, happy path + ***********************************************************************************/ + @Test + public void cardIsStillPresentIfRemoveCountIsLessThanCardCount() throws Exception { + uut.addCard(testCard, 5); + + int removed = uut.removeCard(testCard, 3); + + assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 3, removed); + assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); + } + + @Test + public void cardIsRemovedIfRemoveCountIsEqualToCardCount() throws Exception { + uut.addCard(testCard, 5); + + int removed = uut.removeCard(testCard, 5); + + assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 5, removed); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + } + + @Test + public void cardIsRemovedIfRemoveCountIsGreaterThanCardCount() throws Exception { + uut.addCard(testCard, 3); + + int removed = uut.removeCard(testCard, 5); + + assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 3, removed); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + } + + /* + * Edge cases + */ + @Test + public void removedCardIsNull() throws Exception { + exception.expect(NullPointerException.class); + uut.removeCard(null, 0); + } + + @Test + public void removedCardIsNotInContainer() throws Exception { + assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode, testCard.number)); + + int removed = uut.removeCard(testCard, 1); + + assertEquals("Removed count should be 0.", 0, removed); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + } + + @Test + public void removedCountIsLessThanZero() throws Exception { + uut.addCard(testCard, 3); + + int removed = uut.removeCard(testCard, -4); + + assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard)); + assertEquals("Container reports wrong removed count.", 0, removed); + assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); + } + + /*********************************************************************************** + * Card getter tests, happy path + ***********************************************************************************/ + @Test + public void correctCardIsReturnedByGetter() throws Exception { + uut.addCard(testCard, 1); + + Card card = uut.getCard(testCard.setCode, testCard.number); + + for (Field field : Card.class.getFields()) { + assertEquals("Field " + field.getName(), field.get(testCard), field.get(card)); + } + } + + @Test + public void correctCardCollectionIsReturnedByGetter() throws Exception { + uut.addCard(testCard, 1); + + assertTrue("Not all cards were returned by the getter.", uut.getCards().contains(testCard)); + } + + @Test + public void cardCollectionWhenContainerIsEmpty() throws Exception { + assertEquals("Returned collection size should have been 0.", 0, uut.getCards().size()); + } + + @Test + public void getCardIsNotInCardbase() throws Exception { + assertNull("Method should have returned null", uut.getCard(testCard.setCode, testCard.number)); + } +} diff --git a/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java b/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java index 928c167..2a6f482 100644 --- a/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java +++ b/test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java @@ -6,7 +6,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; -import eu.equalparts.cardbase.cardstorage.ReferenceCardContainer; +import eu.equalparts.cardbase.containers.ReferenceCardContainer; public class ReferenceDeckTest { private ReferenceDeck uut; diff --git a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java new file mode 100644 index 0000000..4f5579d --- /dev/null +++ b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java @@ -0,0 +1,252 @@ +package eu.equalparts.cardbase.filtering; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedList; +import java.util.List; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.filtering.CardFiltering.FilterType; + +public class CardFilteringTest { + private static List allTestCards, testCards; + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + allTestCards = mapper.readValue(CardFilteringTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); + } + + @Before + public void setUp() { + testCards = new LinkedList<>(allTestCards); + } + + @Test + public void filterByNameEquals() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.EQUALS, "Callow jushi"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByNameContains() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.CONTAINS, "sh"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Shivan Dragon", + "Disrupting Shoal", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByNameRegex() throws Exception { + CardFiltering.filterByField(testCards, "name", FilterType.REGEX, ".+?n"); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Shivan Dragon", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByLayoutEquals() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.EQUALS, "flip"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByLayoutContains() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.CONTAINS, "l"); + + assertEquals("Wrong list size.", 8, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Coerced Confession", + "Khalni Hydra", + "Nightmare", + "Shivan Dragon", + "Disrupting Shoal", + "Sorin Markov", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByLayoutRegex() throws Exception { + CardFiltering.filterByField(testCards, "layout", FilterType.REGEX, "fl[a-z]p"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name); + } + + @Test + public void filterByManaCostEquals() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.EQUALS, "{X}{U}{U}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Disrupting Shoal", testCards.get(0).name); + } + + @Test + public void filterByManaCostContains() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.CONTAINS, "B"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Nightmare", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByManaCostRegex() throws Exception { + CardFiltering.filterByField(testCards, "manaCost", FilterType.REGEX, "(\\{G\\}){8}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name); + } + + @Test + public void filterByCMCEquals() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.EQUALS, "5"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Coerced Confession", testCards.get(0).name); + } + + @Test + public void filterByCMCContains() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.CONTAINS, "B"); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Nightmare", + "Sorin Markov", + }; + for (Card card : testCards) { + assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + i++; + } + } + + @Test + public void filterByCMCRegex() throws Exception { + CardFiltering.filterByField(testCards, "cmc", FilterType.REGEX, "(\\{G\\}){8}"); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name); + } + + @Test + public void filterByType() throws Exception { + + } + + @Test + public void filterByRarity() throws Exception { + + } + + @Test + public void filterByText() throws Exception { + + } + + @Test + public void filterByFlavor() throws Exception { + + } + + @Test + public void filterByArtist() throws Exception { + + } + + @Test + public void filterByNumber() throws Exception { + + } + + @Test + public void filterByPower() throws Exception { + + } + + @Test + public void filterByToughness() throws Exception { + + } + + @Test + public void filterByLoyalty() throws Exception { + + } + + @Test + public void filterByMultiverseID() throws Exception { + + } + + @Test + public void filterByImageName() throws Exception { + + } + + @Test + public void filterByWatermark() throws Exception { + + } + + @Test + public void filterBySetCode() throws Exception { + + } + + @Test + public void filterByImageCode() throws Exception { + + } +} diff --git a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java index 64a33b5..4ec0f5e 100644 --- a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java +++ b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java @@ -2,11 +2,12 @@ package eu.equalparts.cardbase.sorting; import static org.junit.Assert.assertTrue; -import java.util.Collection; import java.util.List; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -21,6 +22,9 @@ import eu.equalparts.cardbase.cards.Card; */ public class CardSortingTest { private static List testCards; + + @Rule + public final ExpectedException exception = ExpectedException.none(); @BeforeClass public static void setUpBeforeClass() throws Exception { @@ -30,7 +34,7 @@ public class CardSortingTest { @Test public void sortByName() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "name"); + CardSorting.sortByField(testCards, "name"); int i = 0; String[] names = { "Callow Jushi", @@ -42,7 +46,7 @@ public class CardSortingTest { "Sorin Markov", "Ugin's Construct", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); i++; } @@ -50,7 +54,7 @@ public class CardSortingTest { @Test public void sortByLayout() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "layout"); + CardSorting.sortByField(testCards, "layout"); int i = 0; String[] layouts = { "flip", @@ -62,7 +66,7 @@ public class CardSortingTest { "normal", "normal", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.layout + " should have been " + layouts[i] + ", i = " + i, card.layout.equals(layouts[i])); i++; } @@ -70,7 +74,7 @@ public class CardSortingTest { @Test public void sortByManaCost() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "manaCost"); + CardSorting.sortByField(testCards, "manaCost"); int i = 0; String[] costs = { "{1}{U}{U}", @@ -82,7 +86,7 @@ public class CardSortingTest { "{G}{G}{G}{G}{G}{G}{G}{G}", "{X}{U}{U}", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.manaCost + " should have been " + costs[i] + ", i = " + i, card.manaCost.equals(costs[i])); i++; } @@ -90,10 +94,10 @@ public class CardSortingTest { @Test public void sortByCMC() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "cmc"); + CardSorting.sortByField(testCards, "cmc"); int i = 0; Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8}; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.cmc + " should have been " + cmcs[i] + ", i = " + i, card.cmc.equals(cmcs[i])); i++; } @@ -101,7 +105,7 @@ public class CardSortingTest { @Test public void sortByType() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "type"); + CardSorting.sortByField(testCards, "type"); int i = 0; String[] types = { "Artifact Creature ā€” Construct", @@ -113,7 +117,7 @@ public class CardSortingTest { "Planeswalker ā€” Sorin", "Sorcery", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.type + " should have been " + types[i] + ", i = " + i, card.type.equals(types[i])); i++; } @@ -121,7 +125,7 @@ public class CardSortingTest { @Test public void sortByRarity() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "rarity"); + CardSorting.sortByField(testCards, "rarity"); int i = 0; String[] rarities = { "Uncommon", @@ -133,7 +137,7 @@ public class CardSortingTest { "Mythic Rare", "Mythic Rare", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.rarity + " should have been " + rarities[i] + ", i = " + i, card.rarity.equals(rarities[i])); i++; } @@ -141,7 +145,7 @@ public class CardSortingTest { @Test public void sortByText() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "text"); + CardSorting.sortByField(testCards, "text"); int i = 0; String[] texts = { "+2: Sorin Markov deals 2 damage to target creature or player and you gain 2 life.\nāˆ’3: Target opponent's life total becomes 10.\nāˆ’7: You control target player during that player's next turn.", @@ -153,7 +157,7 @@ public class CardSortingTest { "Whenever you cast a Spirit or Arcane spell, you may put a ki counter on Callow Jushi.\nAt the beginning of the end step, if there are two or more ki counters on Callow Jushi, you may flip it.", "You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.\nCounter target spell if its converted mana cost is X.", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.text + " should have been " + texts[i] + ", i = " + i, card.text.equals(texts[i])); i++; } @@ -161,7 +165,7 @@ public class CardSortingTest { @Test public void sortByFlavor() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "flavor"); + CardSorting.sortByField(testCards, "flavor"); int i = 0; String[] flavors = { "", @@ -173,7 +177,7 @@ public class CardSortingTest { "The undisputed master of the mountains of Shiv.", "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String flavor = card.flavor != null ? card.flavor : ""; assertTrue(flavor + " should have been " + flavors[i] + ", i = " + i, flavor.equals(flavors[i])); i++; @@ -182,7 +186,7 @@ public class CardSortingTest { @Test public void sortByArtist() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "artist"); + CardSorting.sortByField(testCards, "artist"); int i = 0; String[] artists = { "Donato Giancola", @@ -194,7 +198,7 @@ public class CardSortingTest { "Tsutomu Kawade", "Vance Kovacs", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.artist + " should have been " + artists[i] + ", i = " + i, card.artist.equals(artists[i])); i++; } @@ -202,7 +206,7 @@ public class CardSortingTest { @Test public void sortByNumber() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "number"); + CardSorting.sortByField(testCards, "number"); int i = 0; String[] numbers = { "31a", @@ -214,7 +218,7 @@ public class CardSortingTest { "276", "281", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.number + " should have been " + numbers[i] + ", i = " + i, card.number.equals(numbers[i])); i++; } @@ -222,7 +226,7 @@ public class CardSortingTest { @Test public void sortByPower() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "power"); + CardSorting.sortByField(testCards, "power"); int i = 0; String[] powers = { "", @@ -234,7 +238,7 @@ public class CardSortingTest { "5", "8", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String power = card.power != null ? card.power : ""; assertTrue(power + " should have been " + powers[i] + ", i = " + i, power.equals(powers[i])); i++; @@ -243,7 +247,7 @@ public class CardSortingTest { @Test public void sortByToughness() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "power"); + CardSorting.sortByField(testCards, "power"); int i = 0; String[] toughnesses = { "", @@ -255,7 +259,7 @@ public class CardSortingTest { "5", "8", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String toughness = card.toughness != null ? card.toughness : ""; assertTrue(toughness + " should have been " + toughnesses[i] + ", i = " + i, toughness.equals(toughnesses[i])); i++; @@ -264,10 +268,10 @@ public class CardSortingTest { @Test public void sortByLoyalty() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "loyalty"); + CardSorting.sortByField(testCards, "loyalty"); int i = 0; Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4}; - for (Card card : sortedCards) { + for (Card card : testCards) { Integer loyalty = card.loyalty != null ? card.loyalty : 0; assertTrue(loyalty + " should have been " + loyalties[i] + ", i = " + i, loyalty.equals(loyalties[i])); i++; @@ -276,10 +280,10 @@ public class CardSortingTest { @Test public void sortByMultiverseID() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "multiverseid"); + CardSorting.sortByField(testCards, "multiverseid"); int i = 0; Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 }; - for (Card card : sortedCards) { + for (Card card : testCards) { Integer id = card.multiverseid != null ? card.multiverseid : 0; assertTrue(id + " should have been " + ids[i] + ", i = " + i, id.equals(ids[i])); i++; @@ -288,7 +292,7 @@ public class CardSortingTest { @Test public void sortByImageName() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "imageName"); + CardSorting.sortByField(testCards, "imageName"); int i = 0; String[] names = { "callow jushi", @@ -300,7 +304,7 @@ public class CardSortingTest { "sorin markov", "ugin's construct", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.imageName + " should have been " + names[i] + ", i = " + i, card.imageName.equals(names[i])); i++; } @@ -308,7 +312,7 @@ public class CardSortingTest { @Test public void sortByWatermark() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "watermark"); + CardSorting.sortByField(testCards, "watermark"); int i = 0; String[] watermarks = { "", @@ -320,7 +324,7 @@ public class CardSortingTest { "", "Dimir", }; - for (Card card : sortedCards) { + for (Card card : testCards) { String watermark = card.watermark != null ? card.watermark : ""; assertTrue(watermark + " should have been " + watermarks[i] + ", i = " + i, watermark.equals(watermarks[i])); i++; @@ -329,7 +333,7 @@ public class CardSortingTest { @Test public void sortBySetCode() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "setCode"); + CardSorting.sortByField(testCards, "setCode"); int i = 0; String[] sets = { "BOK", @@ -341,14 +345,15 @@ public class CardSortingTest { "M15", "ROE", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.setCode + " should have been " + sets[i] + ", i = " + i, card.setCode.equals(sets[i])); i++; } } + @Test public void sortByImageCode() throws Exception { - Collection sortedCards = CardSorting.sortByField(testCards, "imageCode"); + CardSorting.sortByField(testCards, "imageCode"); int i = 0; String[] codes = { "bok", @@ -360,9 +365,18 @@ public class CardSortingTest { "m15", "roe", }; - for (Card card : sortedCards) { + for (Card card : testCards) { assertTrue(card.imageCode + " should have been " + codes[i] + ", i = " + i, card.imageCode.equals(codes[i])); i++; } } + + /* + * Edge cases + */ + @Test + public void sortFieldDoesNotExist() throws Exception { + exception.expect(NoSuchFieldException.class); + CardSorting.sortByField(testCards, "not a field name"); + } } -- cgit v1.2.3