diff options
-rw-r--r-- | src/eu/equalparts/cardbase/cardfield/IntegerCardField.java | 28 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/cardfield/StringCardField.java | 47 | ||||
-rw-r--r-- | test/eu/equalparts/cardbase/filtering/CardFilteringTest.java | 634 | ||||
-rw-r--r-- | todo | 3 |
4 files changed, 569 insertions, 143 deletions
diff --git a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java index 8526640..604880a 100644 --- a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java @@ -9,18 +9,22 @@ public class IntegerCardField extends CardField<Integer> { @Override public boolean filter(Filter filter) throws NumberFormatException { - switch (filter.type) { - case CONTAINS: - return get().toString().contains(filter.value); - case EQUALS: - return get().toString().equalsIgnoreCase(filter.value); - case REGEX: - return get().toString().matches(filter.value); - case GREATER_THAN: - return get() > Integer.parseInt(filter.value); - case SMALLER_THAN: - return get() < Integer.parseInt(filter.value); - default: + if (get() != null) { + switch (filter.type) { + case CONTAINS: + return get().toString().contains(filter.value); + case EQUALS: + return get().toString().equalsIgnoreCase(filter.value); + case REGEX: + return get().toString().matches(filter.value); + case GREATER_THAN: + return get() > Integer.parseInt(filter.value); + case SMALLER_THAN: + return get() < Integer.parseInt(filter.value); + default: + return false; + } + } else { return false; } } diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java index 8239138..d33cb53 100644 --- a/src/eu/equalparts/cardbase/cardfield/StringCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -1,23 +1,50 @@ package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.filtering.Filter; +import eu.equalparts.cardbase.utils.Utils; public class StringCardField extends CardField<String> { - + public StringCardField(String name, String value) { super(name, value); } @Override public boolean filter(Filter filter) { - switch (filter.type) { - case CONTAINS: - return get().toLowerCase().contains(filter.value.toLowerCase()); - case EQUALS: - return get().equalsIgnoreCase(filter.value); - case REGEX: - return get().matches(filter.value); - default: - throw new IllegalArgumentException(); + if (get() != null) { + switch (filter.type) { + case CONTAINS: + return get().toLowerCase().contains(filter.value.toLowerCase()); + case EQUALS: + return get().equalsIgnoreCase(filter.value); + case REGEX: + return get().matches(filter.value); + case GREATER_THAN: + try { + if (Utils.hasAnnotation(Card.class.getField(filter.fieldName), DirtyNumber.class)) { + return Integer.parseInt(get().replaceAll("[^0-9]+", "")) > Integer.parseInt(filter.value); + } else { + throw new IllegalArgumentException(); + } + } catch (NumberFormatException | NoSuchFieldException | SecurityException e) { + return false; + } + case SMALLER_THAN: + try { + if (Utils.hasAnnotation(Card.class.getField(filter.fieldName), DirtyNumber.class)) { + return Integer.parseInt(get().replaceAll("[^0-9]+", "")) < Integer.parseInt(filter.value); + } else { + throw new IllegalArgumentException(); + } + } catch (NumberFormatException | NoSuchFieldException | SecurityException e) { + return false; + } + default: + throw new IllegalArgumentException(); + } + } else { + return false; } } } diff --git a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java index ed3d3f2..d4839a6 100644 --- a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java +++ b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java @@ -24,12 +24,12 @@ public class CardFilteringTest { @Rule public final ExpectedException exception = ExpectedException.none(); - + @BeforeClass public static void setUpBeforeClass() throws Exception { allTestCards = JSON.mapper.readValue(CardFilteringTest.class.getResourceAsStream("/testcards.json"), new TypeReference<List<Card>>() {}); } - + @Before public void setUp() { testCards = new LinkedList<>(allTestCards); @@ -41,19 +41,19 @@ public class CardFilteringTest { @Test public void filterByNameEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "name", "Callow jushi"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); } - + @Test public void filterByNameContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "name", "sh"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; String[] names = { @@ -66,13 +66,13 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByNameRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "name", ".+?n"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 2, testCards.size()); int i = 0; String[] names = { @@ -84,39 +84,39 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByNameGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByNameSmallerThan() throws Exception { - Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); + Filter filter = new Filter(FilterType.SMALLER_THAN, "name", "0"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } @Test public void filterByLayoutEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "layout", "flip"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); } - + @Test public void filterByLayoutContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "layout", "l"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 8, testCards.size()); int i = 0; String[] names = { @@ -134,49 +134,49 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByLayoutRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "layout", "fl[a-z]p"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name.get()); } - + @Test public void filterByLayoutGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "layout", "5"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByLayoutSmallerThan() throws Exception { Filter filter = new Filter(FilterType.SMALLER_THAN, "layout", "5"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByManaCostEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "manaCost", "{X}{U}{U}"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Disrupting Shoal", testCards.get(0).name.get()); } - + @Test public void filterByManaCostContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "manaCost", "B"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; String[] names = { @@ -189,66 +189,66 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByManaCostRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "manaCost", "(\\{G\\}){8}"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Khalni Hydra", testCards.get(0).name.get()); } - + @Test public void filterByManaCostGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "manaCost", "5"); - + exception.expect(IllegalArgumentException.class); CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByManaCostSmallerThan() throws Exception { - Filter filter = new Filter(FilterType.GREATER_THAN, "manaCost", "5"); + Filter filter = new Filter(FilterType.SMALLER_THAN, "manaCost", "5"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByCMCEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "cmc", "5"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name.get()); } - + @Test public void filterByCMCContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "cmc", "5"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name.get()); } - + @Test public void filterByCMCRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "cmc", "5"); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByCMCGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "cmc", "5"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 4, testCards.size()); int i = 0; String[] names = { @@ -262,13 +262,13 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByCMCSmallerThan() throws Exception { Filter filter = new Filter(FilterType.SMALLER_THAN, "cmc", "5"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; String[] names = { @@ -285,19 +285,19 @@ public class CardFilteringTest { @Test public void filterByTypeEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "type", "Sorcery"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name.get()); } - + @Test public void filterByTypeContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "type", "Creature"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 5, testCards.size()); int i = 0; String[] names = { @@ -312,13 +312,13 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByTypeRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "type", "[^ ]+? ā.*"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 6, testCards.size()); int i = 0; String[] names = { @@ -334,29 +334,29 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByTypeGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "type", "0"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByTypeSmallerThan() throws Exception { - Filter filter = new Filter(FilterType.GREATER_THAN, "name", "0"); + Filter filter = new Filter(FilterType.SMALLER_THAN, "name", "0"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } @Test public void filterByRarityEquals() throws Exception { Filter filter = new Filter(FilterType.EQUALS, "rarity", "Mythic Rare"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 2, testCards.size()); int i = 0; String[] names = { @@ -368,13 +368,13 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByRarityContains() throws Exception { Filter filter = new Filter(FilterType.CONTAINS, "rarity", "Rare"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 5, testCards.size()); int i = 0; String[] names = { @@ -389,13 +389,13 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByRarityRegex() throws Exception { Filter filter = new Filter(FilterType.REGEX, "rarity", "[^ ]+"); - + CardFiltering.filterByField(testCards, filter); - + assertEquals("Wrong list size.", 6, testCards.size()); int i = 0; String[] names = { @@ -411,160 +411,552 @@ public class CardFilteringTest { i++; } } - + @Test public void filterByRarityGreaterThan() throws Exception { Filter filter = new Filter(FilterType.GREATER_THAN, "rarity", "0"); exception.expect(IllegalArgumentException.class); - + CardFiltering.filterByField(testCards, filter); } - + @Test public void filterByRaritySmallerThan() throws Exception { - Filter filter = new Filter(FilterType.GREATER_THAN, "rarity", "0"); + Filter filter = new Filter(FilterType.SMALLER_THAN, "rarity", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByTextEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "text", "When Ugin's Construct enters the battlefield, sacrifice a permanent that's one or more colors."); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Ugin's Construct", testCards.get(0).name.get()); + } + + @Test + public void filterByTextContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "text", "Trample"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); + } + + @Test + public void filterByTextRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "text", "^Whenever [\\S\\s]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name.get()); + } + + @Test + public void filterByTextGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "text", "0"); exception.expect(IllegalArgumentException.class); - + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByTextSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "text", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByFlavorEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "flavor", "The thunder of its hooves beats dreams into despair."); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Nightmare", testCards.get(0).name.get()); + } + + @Test + public void filterByFlavorContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "flavor", "of"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Nightmare", + "Shivan Dragon", + "Ugin's Construct", + + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByFlavorRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "flavor", ".*?\"\nā[^\"]+$"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Khalni Hydra", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByFlavorGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "flavor", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByFlavorSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "flavor", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByArtistEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "artist", "Todd Lockwood"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); + } + + @Test + public void filterByArtistContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "artist", "e"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 5, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Nightmare", + "Disrupting Shoal", + "Sorin Markov", + "Ugin's Construct", + + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByArtistRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "artist", "[^ ]+ [^ ]+ [^ ]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Disrupting Shoal", testCards.get(0).name.get()); + } + + @Test + public void filterByArtistGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "artist", "0"); + exception.expect(IllegalArgumentException.class); + + CardFiltering.filterByField(testCards, filter); + } + + @Test + public void filterByArtistSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "artist", "0"); + exception.expect(IllegalArgumentException.class); + CardFiltering.filterByField(testCards, filter); } @Test - public void filterByText() throws Exception { + public void filterByNumberEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "number", "192"); + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); } - + @Test - public void filterByFlavor() throws Exception { + public void filterByNumberContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "number", "3"); + + CardFiltering.filterByField(testCards, filter); + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Disrupting Shoal", + + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } } - + @Test - public void filterByArtist() throws Exception { + public void filterByNumberRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "number", "[0-9]{1,3}[a-z]"); + + CardFiltering.filterByField(testCards, filter); + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name.get()); } - + @Test - public void filterByNumber() throws Exception { + public void filterByNumberGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "number", "192"); + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 3, testCards.size()); + int i = 0; + String[] names = { + "Coerced Confession", + "Nightmare", + "Shivan Dragon", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } } - + @Test - public void filterByPower() throws Exception { + public void filterByNumberSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "number", "109"); + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Disrupting Shoal", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } } - + @Test - public void filterByToughness() throws Exception { + public void filterByPowerEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "power", "*"); + + CardFiltering.filterByField(testCards, filter); + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Nightmare", testCards.get(0).name.get()); } - + @Test - public void filterByLoyalty() throws Exception { + public void filterByPowerContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "power", "8"); + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); + } + + @Test + public void filterByPowerRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "power", "[0-9]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 4, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Khalni Hydra", + "Shivan Dragon", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByPowerGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "power", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); } - + + @Test + public void filterByPowerSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "power", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByToughnessEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "toughness", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 2, testCards.size()); + int i = 0; + String[] names = { + "Shivan Dragon", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByToughnessContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "toughness", "*"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Nightmare", testCards.get(0).name.get()); + } + + @Test + public void filterByToughnessRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "toughness", "[0-9]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 4, testCards.size()); + int i = 0; + String[] names = { + "Callow Jushi", + "Khalni Hydra", + "Shivan Dragon", + "Ugin's Construct", + }; + for (Card card : testCards) { + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); + i++; + } + } + + @Test + public void filterByToughnessGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "toughness", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Khalni Hydra", testCards.get(0).name.get()); + } + + @Test + public void filterByToughnessSmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "toughness", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Callow Jushi", testCards.get(0).name.get()); + } + + @Test + public void filterByLoyaltyEquals() throws Exception { + Filter filter = new Filter(FilterType.EQUALS, "loyalty", "5"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 0, testCards.size()); + } + + @Test + public void filterByLoyaltyContains() throws Exception { + Filter filter = new Filter(FilterType.CONTAINS, "loyalty", "test"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 0, testCards.size()); + } + + @Test + public void filterByLoyaltyRegex() throws Exception { + Filter filter = new Filter(FilterType.REGEX, "loyalty", "[0-9]+"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Sorin Markov", testCards.get(0).name.get()); + } + + @Test + public void filterByLoyaltyGreaterThan() throws Exception { + Filter filter = new Filter(FilterType.GREATER_THAN, "loyalty", "2"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 1, testCards.size()); + assertEquals("Sorin Markov", testCards.get(0).name.get()); + } + + @Test + public void filterByLoyaltySmallerThan() throws Exception { + Filter filter = new Filter(FilterType.SMALLER_THAN, "loyalty", "1"); + + CardFiltering.filterByField(testCards, filter); + + assertEquals("Wrong list size.", 0, testCards.size()); + } + @Test public void filterByMultiverseID() throws Exception { } - + @Test public void filterByImageName() throws Exception { } - + @Test public void filterBySetCode() throws Exception { } - + /* * Filter validation tests, happy path */ @Test public void validateNameEqualsSomethingFilter() throws Exception { Filter testFilter = new Filter(FilterType.EQUALS, "name", "Card name"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateFlavourEqualsNothingFilter() throws Exception { Filter testFilter = new Filter(FilterType.EQUALS, "flavor", ""); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateTextContainsVigilanceFilter() throws Exception { Filter testFilter = new Filter(FilterType.CONTAINS, "text", "vigilance"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateCMCContainsNumberFilter() throws Exception { Filter testFilter = new Filter(FilterType.CONTAINS, "cmc", "5"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateTypeRegexFilter() throws Exception { Filter testFilter = new Filter(FilterType.REGEX, "type", "Legendary [A-z]+? Creature.*"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateManaCostRegexFilter() throws Exception { Filter testFilter = new Filter(FilterType.REGEX, "manaCost", ".*?{./.}.*?"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateCMCGreaterThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.GREATER_THAN, "cmc", "5"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validatePowerGreaterThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.GREATER_THAN, "power", "3"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateLoyaltySmallerThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.SMALLER_THAN, "loyalty", "3"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateToughnessSmallerThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.SMALLER_THAN, "toughness", "5"); - + assertTrue(CardFiltering.isFilterValid(testFilter)); } - + /* * Edge cases */ @Test public void validateNonIntegerFieldGreaterThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.GREATER_THAN, "name", "5"); - + assertTrue(!CardFiltering.isFilterValid(testFilter)); } - + @Test public void validateNonIntegerValueGreaterThanFilter() throws Exception { Filter testFilter = new Filter(FilterType.GREATER_THAN, "cmc", "test"); - + assertTrue(!CardFiltering.isFilterValid(testFilter)); } } @@ -7,4 +7,7 @@ "Sorin Markov", "Ugin's Construct", +assertEquals("Wrong list size.", 1, testCards.size()); +assertEquals("Callow Jushi", testCards.get(0).name.get()); + cat test/testcards.json | sed -e "s/,/,\n/g" | awk '/cmc/ { r = gensub(/.+?\":\"(.+?)\".+/, "\"\\1\",", "g"); print r }'
\ No newline at end of file |