aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java2
-rw-r--r--src/eu/equalparts/cardbase/cards/OldCard.java95
-rw-r--r--src/eu/equalparts/cardbase/comparator/SpecialFields.java1
-rw-r--r--src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java (renamed from src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java)2
-rw-r--r--src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java (renamed from src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java)6
-rw-r--r--src/eu/equalparts/cardbase/decks/ReferenceDeck.java2
-rw-r--r--src/eu/equalparts/cardbase/filtering/CardFiltering.java57
-rw-r--r--src/eu/equalparts/cardbase/sorting/CardSorting.java4
-rw-r--r--test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java (renamed from test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java)5
-rw-r--r--test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java (renamed from test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java)5
-rw-r--r--test/eu/equalparts/cardbase/decks/ReferenceDeckTest.java2
-rw-r--r--test/eu/equalparts/cardbase/filtering/CardFilteringTest.java252
-rw-r--r--test/eu/equalparts/cardbase/sorting/CardSortingTest.java88
-rw-r--r--todo11
14 files changed, 477 insertions, 55 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java
index 9c630bd..90e1ffe 100644
--- a/src/eu/equalparts/cardbase/Cardbase.java
+++ b/src/eu/equalparts/cardbase/Cardbase.java
@@ -7,7 +7,7 @@ import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import eu.equalparts.cardbase.cardstorage.StandaloneCardContainer;
+import eu.equalparts.cardbase.containers.StandaloneCardContainer;
import eu.equalparts.cardbase.utils.JSON;
/**
diff --git a/src/eu/equalparts/cardbase/cards/OldCard.java b/src/eu/equalparts/cardbase/cards/OldCard.java
new file mode 100644
index 0000000..daa831e
--- /dev/null
+++ b/src/eu/equalparts/cardbase/cards/OldCard.java
@@ -0,0 +1,95 @@
+package eu.equalparts.cardbase.cards;
+
+import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber;
+import eu.equalparts.cardbase.comparator.SpecialFields.Rarity;
+
+public class OldCard {
+
+ public String name;
+ public String layout;
+ public String manaCost;
+ public Integer cmc;
+ public String type;
+ @Rarity
+ public String rarity;
+ public String text;
+ public String flavor;
+ public String artist;
+ @DirtyNumber
+ public String number;
+ @DirtyNumber
+ public String power;
+ @DirtyNumber
+ public String toughness;
+ public Integer loyalty;
+ public Integer multiverseid;
+ public String imageName;
+ public String watermark;
+
+ // Not part of upstream JSON
+ public String setCode;
+ public String imageCode;
+ public Integer count;
+
+ @Override
+ public OldCard clone() {
+ OldCard clone = new OldCard();
+
+ clone.name = this.name;
+ clone.layout = this.layout;
+ clone.manaCost = this.manaCost;
+ clone.cmc = this.cmc;
+ clone.type = this.type;
+ clone.rarity = this.rarity;
+ clone.text = this.text;
+ clone.flavor = this.flavor;
+ clone.artist = this.artist;
+ clone.number = this.number;
+ clone.power = this.power;
+ clone.toughness = this.toughness;
+ clone.loyalty = this.loyalty;
+ clone.multiverseid = this.multiverseid;
+ clone.imageName = this.imageName;
+ clone.watermark = this.watermark;
+ clone.setCode = this.setCode;
+ clone.imageCode = this.imageCode;
+ clone.count = this.count;
+
+ return clone;
+ }
+
+ public static int makeHash(String setCode, String number) {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((number == null) ? 0 : number.hashCode());
+ result = prime * result + ((setCode == null) ? 0 : setCode.hashCode());
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ return makeHash(setCode, number);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ OldCard other = (OldCard) obj;
+ if (number == null) {
+ if (other.number != null)
+ return false;
+ } else if (!number.equals(other.number))
+ return false;
+ if (setCode == null) {
+ if (other.setCode != null)
+ return false;
+ } else if (!setCode.equals(other.setCode))
+ return false;
+ return true;
+ }
+} \ No newline at end of file
diff --git a/src/eu/equalparts/cardbase/comparator/SpecialFields.java b/src/eu/equalparts/cardbase/comparator/SpecialFields.java
index 692f774..433c8ab 100644
--- a/src/eu/equalparts/cardbase/comparator/SpecialFields.java
+++ b/src/eu/equalparts/cardbase/comparator/SpecialFields.java
@@ -13,5 +13,4 @@ public final class SpecialFields {
@Retention(RetentionPolicy.RUNTIME)
public @interface Rarity {}
-
}
diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java
index f43b76b..41c4224 100644
--- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
+++ b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import java.util.HashMap;
import java.util.Map;
diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java
index 53d248d..e5a514e 100644
--- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
+++ b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import java.util.HashMap;
import java.util.LinkedList;
@@ -38,8 +38,8 @@ public class StandaloneCardContainer extends ReferenceCardContainer {
/**
* This method is intended to allow iteration directly on the list of cards,
* while at the same time retaining control over the insert and remove procedures.
- * The returned {@code List} is a read-only; trying to modify its structure will
- * result in an {@code UnsupportedOperationException}.
+ * The returned {@code List} is a copy of the list kept internally by {@code StandaloneCardContainer};
+ * modifying its structure is possible but will not affect the container instance from which it came.
*
* @return an unmodifiable list of all the cards in the cardbase.
*/
diff --git a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
index 8c8e227..9875796 100644
--- a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
+++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
@@ -1,6 +1,6 @@
package eu.equalparts.cardbase.decks;
-import eu.equalparts.cardbase.cardstorage.ReferenceCardContainer;
+import eu.equalparts.cardbase.containers.ReferenceCardContainer;
public class ReferenceDeck extends ReferenceCardContainer {
private String name = "";
diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java
new file mode 100644
index 0000000..23f439d
--- /dev/null
+++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java
@@ -0,0 +1,57 @@
+package eu.equalparts.cardbase.filtering;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import eu.equalparts.cardbase.cards.Card;
+
+/**
+ * Contains and equals are not case sensitive. Regex is.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class CardFiltering {
+
+ enum FilterType {
+ EQUALS {
+ @Override
+ public boolean doFilter(String value, String filter) {
+ return value.equalsIgnoreCase(filter);
+ }
+ }, CONTAINS {
+ @Override
+ public boolean doFilter(String value, String filter) {
+ return value.toLowerCase().contains(filter.toLowerCase());
+ }
+ }, REGEX {
+ @Override
+ public boolean doFilter(String value, String filter) {
+ return value.matches(filter);
+ }
+ };
+
+ public abstract boolean doFilter(String value, String filter);
+ }
+
+ public static Collection<Card> filterByField(List<Card> cards, String fieldName, FilterType filterType, String filterValue) throws NoSuchFieldException {
+ Field fieldToFilter = Card.class.getDeclaredField(fieldName);
+ try {
+ for (Iterator<Card> iterator = cards.iterator(); iterator.hasNext();) {
+ if (!filterType.doFilter((String) fieldToFilter.get(iterator.next()), filterValue)) {
+ iterator.remove();
+ }
+ }
+ } catch (IllegalArgumentException e) {
+ System.out.println("Error: class Card does not define field \"" + fieldToFilter.getName() + "\".");
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible.");
+ e.printStackTrace();
+ }
+ return cards;
+ }
+
+}
diff --git a/src/eu/equalparts/cardbase/sorting/CardSorting.java b/src/eu/equalparts/cardbase/sorting/CardSorting.java
index 04413b9..e7f7c96 100644
--- a/src/eu/equalparts/cardbase/sorting/CardSorting.java
+++ b/src/eu/equalparts/cardbase/sorting/CardSorting.java
@@ -10,11 +10,9 @@ public abstract class CardSorting {
/**
* @param cards
* @param fieldName the name of the field by which to sort.
- * @return an unmodifiable collection representing the cardbase sorted in the required order.
* @throws NoSuchFieldException if the field provided is invalid.
*/
- public static List<Card> sortByField(List<Card> cards, String fieldName) throws NoSuchFieldException {
+ public static void sortByField(List<Card> cards, String fieldName) throws NoSuchFieldException {
cards.sort(new CardComparator(Card.class.getDeclaredField(fieldName)));
- return cards;
}
}
diff --git a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java
index bf49e2f..849d84c 100644
--- a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
+++ b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import static org.junit.Assert.assertEquals;
@@ -11,7 +11,6 @@ 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 {
@@ -28,7 +27,7 @@ public class ReferenceCardContainerTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ObjectMapper mapper = new ObjectMapper();
- testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
+ testCard = mapper.readValue(ReferenceCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
}
@Before
diff --git a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java
index 6071e09..6e9b31a 100644
--- a/test/eu/equalparts/cardbase/cardstorage/StandaloneCardContainerTest.java
+++ b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -16,7 +16,6 @@ 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 {
@@ -32,7 +31,7 @@ public class StandaloneCardContainerTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ObjectMapper mapper = new ObjectMapper();
- testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
+ testCard = mapper.readValue(StandaloneCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
}
@Before
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<Card> 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<List<Card>>() {});
+ }
+
+ @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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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<Card> 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");
+ }
}
diff --git a/todo b/todo
index 02cbef1..773f365 100644
--- a/todo
+++ b/todo
@@ -1 +1,10 @@
-sorting and filtering should work on StandaloneCardContainer, not Cardbase. \ No newline at end of file
+"Callow Jushi",
+"Coerced Confession",
+"Khalni Hydra",
+"Nightmare",
+"Shivan Dragon",
+"Disrupting Shoal",
+"Sorin Markov",
+"Ugin's Construct",
+
+cat test/testcards.json | sed -e "s/,/,\n/g" | awk '/cmc/ { r = gensub(/.+?\":\"(.+?)\".+/, "\"\\1\",", "g"); print r }' \ No newline at end of file