From 95e4e3ea29d64619715b9a187914374bc776ada8 Mon Sep 17 00:00:00 2001 From: Alex Marcotte Date: Mon, 30 May 2016 23:01:57 -0400 Subject: [PATCH] Text import will automatically start sideboard after a single blank line. Added a JUnit test for TxtDeckImporter. --- Mage.Tests/JustLands.txt | 6 +++ .../decks/importer/TxtDeckImporterTest.java | 43 +++++++++++++++++++ .../cards/decks/importer/TxtDeckImporter.java | 21 ++++----- 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 Mage.Tests/JustLands.txt create mode 100644 Mage.Tests/src/test/java/org/mage/test/decks/importer/TxtDeckImporterTest.java diff --git a/Mage.Tests/JustLands.txt b/Mage.Tests/JustLands.txt new file mode 100644 index 0000000000..9d668492ee --- /dev/null +++ b/Mage.Tests/JustLands.txt @@ -0,0 +1,6 @@ +1 Forest +1 Plains +1 Island + +1 Swamp +1 Mountain \ No newline at end of file diff --git a/Mage.Tests/src/test/java/org/mage/test/decks/importer/TxtDeckImporterTest.java b/Mage.Tests/src/test/java/org/mage/test/decks/importer/TxtDeckImporterTest.java new file mode 100644 index 0000000000..aa351e47f8 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/decks/importer/TxtDeckImporterTest.java @@ -0,0 +1,43 @@ +package org.mage.test.decks.importer; + +import mage.cards.decks.DeckCardInfo; +import mage.cards.decks.DeckCardLists; +import mage.cards.decks.importer.TxtDeckImporter; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; +import org.junit.Assert; +import org.junit.Test; + +public class TxtDeckImporterTest { + + @Test + public void testImportWithBlankLineAboveSideboard() { + TxtDeckImporter importer = new TxtDeckImporter(); + + CardInfo card; + DeckCardLists deck = new DeckCardLists(); + + String[] cards = {"Plains", "Forest", "Island"}; + String[] sideboard = {"Swamp", "Mountain"}; + + for (String c : cards) { + card = CardRepository.instance.findPreferedCoreExpansionCard(c, true); + assert card != null; + deck.getCards().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode())); + } + + for (String s : sideboard) { + card = CardRepository.instance.findPreferedCoreExpansionCard(s, true); + assert card != null; + deck.getSideboard().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode())); + } + + Assert.assertEquals("Deck does not contain 3 cards, found " + deck.getCards().size(), 3, deck.getCards().size()); + Assert.assertEquals("Sideboard does not contain 2 cards, found " + deck.getSideboard().size(), 2, deck.getSideboard().size()); + + DeckCardLists imported = importer.importDeck("JustLands.txt"); + + Assert.assertEquals("Imported deck does not contain 3 cards, found " + imported.getCards().size(), 3, imported.getCards().size()); + Assert.assertEquals("Imported sideboard does not contain 2 cards, found " + imported.getSideboard().size(), 2, imported.getSideboard().size()); + } +} diff --git a/Mage/src/main/java/mage/cards/decks/importer/TxtDeckImporter.java b/Mage/src/main/java/mage/cards/decks/importer/TxtDeckImporter.java index 3f5cf742f0..59ace12525 100644 --- a/Mage/src/main/java/mage/cards/decks/importer/TxtDeckImporter.java +++ b/Mage/src/main/java/mage/cards/decks/importer/TxtDeckImporter.java @@ -27,14 +27,15 @@ */ package mage.cards.decks.importer; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; import mage.cards.decks.DeckCardInfo; import mage.cards.decks.DeckCardLists; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + /** * * @author BetaSteward_at_googlemail.com @@ -46,7 +47,7 @@ public class TxtDeckImporter extends DeckImporter { public static final Set IGNORE_NAMES = new HashSet<>(Arrays.asList(SET_VALUES)); private boolean sideboard = false; - private int emptyLinesInARow = 0; + private int nonEmptyLinesTotal = 0; @Override protected void readLine(String line, DeckCardLists deckList) { @@ -57,14 +58,14 @@ public class TxtDeckImporter extends DeckImporter { if (line.startsWith("//")) { return; } - if (line.length() == 0) { - emptyLinesInARow++; - if (emptyLinesInARow > 1) { - sideboard = true; - } + + // Start the sideboard on empty line that follows + // at least 1 non-empty line + if (line.length() == 0 && nonEmptyLinesTotal > 0) { + sideboard = true; return; } else { - emptyLinesInARow = 0; + nonEmptyLinesTotal++; } line = line.replace("\t", " "); // changing tabs to blanks as delimiter