From 584b53259818f888af960d1fc0a7841b6dba9512 Mon Sep 17 00:00:00 2001 From: Neil Gentleman Date: Wed, 28 Oct 2015 23:25:52 -0700 Subject: [PATCH] check card source for unexpected types needs more tuning - very noisy right now --- .../mage/verify/CompareWithMtgjsonTest.java | 86 +++++++++++++++---- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java b/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java index b50370afdb..5f1c8a07a6 100644 --- a/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java +++ b/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java @@ -10,19 +10,17 @@ import mage.cards.ExpansionSet; import mage.cards.Sets; import mage.cards.SplitCard; import mage.constants.CardType; -import mage.util.ClassScanner; import org.junit.Test; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Paths; import java.text.Normalizer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.ListIterator; -import java.util.Map; -import java.util.Objects; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class CompareWithMtgjsonTest { @@ -47,25 +45,67 @@ public class CompareWithMtgjsonTest { for (ExpansionSet set : sets) { for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) { + Set tokens = findSourceTokens(setInfo.getCardClass()); Card card = CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(), setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo())); if (card.isSplitCard()) { - check(reference, ((SplitCard) card).getLeftHalfCard()); - check(reference, ((SplitCard) card).getRightHalfCard()); + check(reference, ((SplitCard) card).getLeftHalfCard(), null); + check(reference, ((SplitCard) card).getRightHalfCard(), null); } else { - check(reference, card); + check(reference, card, tokens); } } } } + private static final Pattern SHORT_JAVA_STRING = Pattern.compile("(?<=\")[A-Z][a-z]+(?=\")"); + + private Set findSourceTokens(Class c) throws IOException { + String path = "../Mage.Sets/src/" + c.getName().replace(".", "/") + ".java"; + try { + String source = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); + Matcher matcher = SHORT_JAVA_STRING.matcher(source); + Set tokens = new HashSet<>(); + while (matcher.find()) { + tokens.add(matcher.group()); + } + return tokens; + } catch (NoSuchFileException e) { + System.out.println("failed to read " + path); + return Collections.emptySet(); + } + } + private String stripAccents(String str) { String decomposed = Normalizer.normalize(str, Normalizer.Form.NFKD); return decomposed.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); } - private void check(Map> reference, Card card) { - String name = card.getName(); + private void check(Map> reference, Card card, Set tokens) { + Map ref = findReference(reference, card.getName()); + if (ref == null) { + System.out.println("Missing card reference for " + card); + return; + } + checkAll(card, ref); + if (tokens != null) { + Map ref2 = null; + if (card.isFlipCard()) { + ref2 = findReference(reference, card.getFlipCardName()); + } + for (String token : tokens) { + if (!(token.equals(card.getName()) + || containsInTypesOrText(ref, token) + || containsInTypesOrText(ref, token.toLowerCase()) + || (ref2 != null && (containsInTypesOrText(ref2, token) || containsInTypesOrText(ref2, token.toLowerCase()))) + )) { + System.out.println("unexpected token " + token + " in " + card); + } + } + } + } + + private Map findReference(Map> reference, String name) { Map ref = reference.get(name); if (ref == null) { name = name.replaceFirst("\\bA[Ee]", "Æ"); @@ -75,11 +115,19 @@ public class CompareWithMtgjsonTest { name = name.replace("'", "\""); // for Kongming, "Sleeping Dragon" & Pang Tong, "Young Phoenix" ref = reference.get(name); } - if (ref == null) { - System.out.println("Missing card reference for " + card); - return; - } - checkAll(card, ref); + return ref; + } + + private boolean containsInTypesOrText(Map ref, String token) { + return contains(ref, "types", token) + || contains(ref, "subtypes", token) + || contains(ref, "supertypes", token) + || ((String) ref.get("text")).contains(token); + } + + private boolean contains(Map ref, String key, String value) { + Collection options = (Collection) ref.get(key); + return options != null && options.contains(value); } private void checkAll(Card card, Map ref) {