Move tests to the Mage directory and stub out looking up cards by names for tests.

This commit is contained in:
John Hitchings 2019-01-10 09:51:53 -08:00
parent 58e629dca6
commit 9372ba63e2
12 changed files with 268 additions and 75 deletions

View file

@ -1,44 +0,0 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
public class CodDeckImportTest {
@Test
public void testImportCod() {
StringBuilder errors = new StringBuilder();
DeckCardLists deck = new CodDeckImporter().importDeck(
"src/test/java/mage/cards/decks/importer/testdeck.cod", errors);
assertEquals("Deck Name", deck.getName());
assertEquals(113, deck.getCards().size());
assertEquals(3, deck.getSideboard().size());
int index = 0;
for (int i = 0; i < 12; i++) {
assertCardSame("Forest", deck.getCards().get(index++));
}
for (int i = 0; i < 100; i++) {
assertCardSame("Razorverge Thicket", deck.getCards().get(index++));
}
assertCardSame("Avacyn's Pilgrim", deck.getCards().get(index++));
assertEquals(index, deck.getCards().size());
index = 0;
for (int i = 0; i < 3; i++) {
assertCardSame("War Priest of Thune", deck.getSideboard().get(index++));
}
assertEquals(index, deck.getSideboard().size());
assertEquals("Could not find card: '@#$NOT A REAL CARD NAME@#$'\n", errors.toString());
}
private static void assertCardSame(String name, DeckCardInfo card) {
assertEquals(name, card.getCardName());
assertEquals(1, card.getQuantity());
}
}

View file

@ -1,20 +0,0 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import mage.cards.decks.DeckCardLists;
public class DecDeckImportTest {
@Test
public void testImport() {
StringBuilder errors = new StringBuilder();
DeckCardLists deck = new DecDeckImporter().importDeck(
"src/test/java/mage/cards/decks/importer/testdeck.dec", errors);
assertEquals(60, deck.getCards().size());
assertEquals(15, deck.getSideboard().size());
}
}

View file

@ -0,0 +1,16 @@
package mage.cards.decks.importer;
import java.util.Optional;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
public class CardLookup {
public static final CardLookup instance = new CardLookup();
public Optional<CardInfo> lookupCardInfo(String name) {
return Optional.ofNullable(CardRepository.instance.findPreferedCoreExpansionCard(name, true));
}
}

View file

@ -7,6 +7,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -40,12 +41,12 @@ public class CodDeckImporter extends DeckImporter {
List<Node> mainCards = getNodes(doc, "/cockatrice_deck/zone[@name='main']/card");
decklist.setCards(mainCards.stream()
.flatMap(toDeckCardInfo(errorMessages))
.flatMap(toDeckCardInfo(getCardLookup(), errorMessages))
.collect(Collectors.toList()));
List<Node> sideboardCards = getNodes(doc, "/cockatrice_deck/zone[@name='side']/card");
decklist.setSideboard(sideboardCards.stream()
.flatMap(toDeckCardInfo(errorMessages))
.flatMap(toDeckCardInfo(getCardLookup(), errorMessages))
.collect(Collectors.toList()));
getNodes(doc, "/cockatrice_deck/deckname")
@ -65,25 +66,25 @@ public class CodDeckImporter extends DeckImporter {
return 1;
}
try {
return Math.min(100, Math.max(0, Integer.parseInt(numberNode.getNodeValue())));
return Math.min(100, Math.max(1, Integer.parseInt(numberNode.getNodeValue())));
} catch (NumberFormatException e) {
return 1;
}
}
private static Function<Node, Stream<DeckCardInfo>> toDeckCardInfo(StringBuilder errors) {
private static Function<Node, Stream<DeckCardInfo>> toDeckCardInfo(CardLookup lookup, StringBuilder errors) {
return node -> {
String name = node.getAttributes().getNamedItem("name").getNodeValue().trim();
CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(name, true);
if (cardInfo != null) {
Optional<CardInfo> cardInfo = lookup.lookupCardInfo(name);
if (cardInfo.isPresent()) {
CardInfo info = cardInfo.get();
return Collections.nCopies(
getQuantityFromNode(node),
new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()))
.stream();
new DeckCardInfo(info.getName(), info.getCardNumber(), info.getSetCode())).stream();
} else {
errors.append("Could not find card: '").append(name).append("'\n");
return Stream.empty();
}
return Stream.empty();
};
}

View file

@ -1,6 +1,8 @@
package mage.cards.decks.importer;
import java.util.Optional;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
@ -29,10 +31,11 @@ public class DecDeckImporter extends PlainTextDeckImporter {
String lineName = line.substring(delim).trim();
try {
int num = Integer.parseInt(lineNum);
CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(lineName, true);
if (cardInfo == null) {
Optional<CardInfo> cardLookup = getCardLookup().lookupCardInfo(lineName);
if (!cardLookup.isPresent()) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append('\n');
} else {
CardInfo cardInfo = cardLookup.get();
for (int i = 0; i < num; i++) {
if (!sideboard) {
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));

View file

@ -3,10 +3,13 @@ package mage.cards.decks.importer;
import org.apache.log4j.Logger;
import java.io.File;
import java.util.Locale;
import java.util.Optional;
import java.util.Scanner;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
public abstract class DeckImporter {
@ -53,6 +56,10 @@ public abstract class DeckImporter {
return importDeck(file, new StringBuilder());
}
public CardLookup getCardLookup() {
return CardLookup.instance;
}
private static boolean haveSideboardSection(String file) {
// search for sideboard section:
// or //sideboard

View file

@ -0,0 +1,56 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
public class CodDeckImportTest {
private static final FakeCardLookup LOOKUP = new FakeCardLookup()
.addCard("Forest")
.addCard("Razorverge Thicket")
.addCard("Avacyn's Pilgrim")
.addCard("War Priest of Thune");
@Test
public void testImportCod() {
CodDeckImporter importer = new CodDeckImporter() {
@Override
public CardLookup getCardLookup() {
return LOOKUP;
}
};
StringBuilder errors = new StringBuilder();
DeckCardLists deck = importer.importDeck(
"src/test/java/mage/cards/decks/importer/testdeck.cod", errors);
assertEquals("Deck Name", deck.getName());
TestDeckChecker.checker()
.addMain("Forest", 12)
.addMain("Razorverge Thicket", 100)
.addMain("Avacyn's Pilgrim", 1)
.addSide("War Priest of Thune", 3)
.verify(deck, 113, 3);
assertEquals("Could not find card: '@#$NOT A REAL CARD NAME@#$'\n", errors.toString());
}
private static void assertCardSame(String name, DeckCardInfo card) {
assertEquals(name, card.getCardName());
assertEquals(1, card.getQuantity());
}
}

View file

@ -0,0 +1,91 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
public class DecDeckImportTest {
private static final FakeCardLookup LOOKUP = new FakeCardLookup()
.addCard("Masticore")
.addCard("Metalworker")
.addCard("Phyrexian Colossus")
.addCard("Crumbling Sanctuary")
.addCard("Grim Monolith")
.addCard("Mishra's Helix")
.addCard("Phyrexian Processor")
.addCard("Tangle Wire")
.addCard("Thran Dynamo")
.addCard("Voltaic Key")
.addCard("Tinker")
.addCard("Brainstorm")
.addCard("Crystal Vein")
.addCard("Island")
.addCard("Rishadan Port")
.addCard("Saprazzan Skerry")
.addCard("Annul")
.addCard("Chill")
.addCard("Miscalculation")
.addCard("Mishra's Helix")
.addCard("Rising Waters");
@Test
public void testImport() {
StringBuilder errors = new StringBuilder();
DecDeckImporter importer = new DecDeckImporter() {
@Override
public CardLookup getCardLookup() {
return LOOKUP;
}
};
DeckCardLists deck = importer.importDeck(
"src/test/java/mage/cards/decks/importer/testdeck.dec", errors);
TestDeckChecker.checker()
.addMain("Masticore", 4)
.addMain("Metalworker", 4)
.addMain("Phyrexian Colossus", 1)
.addMain("Crumbling Sanctuary", 1)
.addMain("Grim Monolith", 4)
.addMain("Mishra's Helix", 1)
.addMain("Phyrexian Processor", 4)
.addMain("Tangle Wire", 4)
.addMain("Thran Dynamo", 4)
.addMain("Voltaic Key", 4)
.addMain("Tinker", 4)
.addMain("Brainstorm", 4)
.addMain("Crystal Vein", 4)
.addMain("Island", 9)
.addMain("Rishadan Port", 4)
.addMain("Saprazzan Skerry", 4)
.addSide("Annul", 4)
.addSide("Chill", 4)
.addSide("Miscalculation", 4)
.addSide("Mishra's Helix", 1)
.addSide("Rising Waters", 2)
.verify(deck, 60, 15);
assertEquals("", errors.toString());
}
private static FakeCardLookup getFakeCardLookup() {
FakeCardLookup lookup = new FakeCardLookup() {
@Override
public Optional<CardInfo> lookupCardInfo(String name) {
System.out.println(name);
return super.lookupCardInfo(name);
}
};
return lookup;
}
}

View file

@ -0,0 +1,29 @@
package mage.cards.decks.importer;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import mage.cards.repository.CardInfo;
public class FakeCardLookup extends CardLookup {
private final Map<String, CardInfo> lookup = new HashMap<>();
public FakeCardLookup addCard(String cardName) {
lookup.put(cardName, new CardInfo() {{
name = cardName;
}});
return this;
}
@Override
public Optional<CardInfo> lookupCardInfo(String name) {
CardInfo card = lookup.get(name);
if (card == null) {
System.out.println("Couldn't find: " + name);
}
return Optional.ofNullable(card);
}
}

View file

@ -0,0 +1,54 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import mage.cards.decks.DeckCardLists;
public class TestDeckChecker {
private final List<String> main = new ArrayList<>();
private final List<String> side = new ArrayList<>();
public TestDeckChecker addMain(String name) {
return addMain(name, 1);
}
public TestDeckChecker addMain(String name, int quantity) {
main.addAll(Collections.nCopies(quantity, name));
return this;
}
public TestDeckChecker addSide(String name) {
return addSide(name, 1);
}
public TestDeckChecker addSide(String name, int quantity) {
side.addAll(Collections.nCopies(quantity, name));
return this;
}
public void verify(DeckCardLists deck, int nMain, int nSide) {
assertEquals(nMain, main.size());
assertEquals(nSide, side.size());
assertEquals(nMain, deck.getCards().size());
assertEquals(nSide, deck.getSideboard().size());
for (int i = 0; i < main.size(); i++) {
String expected = main.get(i);
String actual = deck.getCards().get(i).getCardName();
assertEquals(String.format("Expected: '%s' Actual: '%s' at index: %s",
expected, actual, i), expected, actual);
}
}
public static TestDeckChecker checker() {
return new TestDeckChecker();
}
}