diff options
-rw-r--r-- | src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 107 | ||||
-rw-r--r-- | test/eu/equalparts/cardbase/cli/CardbaseCLITest.java | 402 | ||||
-rw-r--r-- | todo | 1 |
3 files changed, 325 insertions, 185 deletions
diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index 03351da..98cce21 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -150,7 +150,7 @@ public final class CardbaseCLI { * @param input the raw input from the user. * @return an array of strings, where the first element is the command and subsequent elements are the arguments. */ - String[] sanitiseInput(String input) { + private String[] sanitiseInput(String input) { return input.trim().split("[ \t]+"); } @@ -158,54 +158,65 @@ public final class CardbaseCLI { /** * Read stdin for user input, sanitise and interpret any commands entered. */ - void startInterface() { + private void startInterface() { BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); try { // the main loop while (!exit) { // print prompt System.out.print(selectedSet == null ? "> " : selectedSet.code + " > "); - // condition input and interpret - String[] input = sanitiseInput(consoleReader.readLine()); - String command = input[0]; - String[] args = Arrays.copyOfRange(input, 1, input.length); - - if (command.equalsIgnoreCase("help")) { - help(); - } else if (command.equalsIgnoreCase("write") - || command.equalsIgnoreCase("save")) { - write(args); - } else if (command.equalsIgnoreCase("version")) { - version(); - } else if (command.equalsIgnoreCase("exit")) { - exit(); - } else if (command.equalsIgnoreCase("sets")) { - sets(); - } else if (command.equalsIgnoreCase("set")) { - set(args); - } else if (command.equalsIgnoreCase("glance")) { - glance(); - } else if (command.equalsIgnoreCase("peruse")) { - peruse(args); - } else if (command.equalsIgnoreCase("undo")) { - undo(); - } else if (command.equalsIgnoreCase("remove") - || command.equalsIgnoreCase("rm")) { - remove(args); - } else { - add(command, args); - } + // interpret input + interpretInput(consoleReader.readLine()); } } catch (IOException e) { System.out.println("Error: something went wrong with stdin, exiting..."); if (Cardbase.DEBUG) e.printStackTrace(); } } + + /** + * Sanitises and interprets raw input as it is read from stdin. This method has default-visibility for unit testing. + * + * @param rawInput + */ + void interpretInput(String rawInput) { + // condition input + String[] input = sanitiseInput(rawInput); + String command = input[0]; + String[] args = Arrays.copyOfRange(input, 1, input.length); + + // interpret + if (command.equalsIgnoreCase("help")) { + help(); + } else if (command.equalsIgnoreCase("write") + || command.equalsIgnoreCase("save")) { + write(args); + } else if (command.equalsIgnoreCase("version")) { + version(); + } else if (command.equalsIgnoreCase("exit")) { + exit(); + } else if (command.equalsIgnoreCase("sets")) { + sets(); + } else if (command.equalsIgnoreCase("set")) { + set(args); + } else if (command.equalsIgnoreCase("glance")) { + glance(); + } else if (command.equalsIgnoreCase("peruse")) { + peruse(args); + } else if (command.equalsIgnoreCase("undo")) { + undo(); + } else if (command.equalsIgnoreCase("remove") + || command.equalsIgnoreCase("rm")) { + remove(args); + } else { + add(command, args); + } + } /** * Print help to console. */ - void help() { + private void help() { System.out.println(help); } @@ -214,7 +225,7 @@ public final class CardbaseCLI { * * @param args optionally the file to which to write. */ - void write(String... args) { + private void write(String... args) { File outputFile; // user-provided file overrides everything else if (args != null && args.length > 0) { @@ -239,7 +250,7 @@ public final class CardbaseCLI { System.out.println("Error: something terrible happened to the internal cardbase data structure. Oops."); if (Cardbase.DEBUG) e.printStackTrace(); } catch (IOException e) { - System.out.println("Error: lost contact with the output file, try again?"); + System.out.println("Error: lost contact with the output file."); if (Cardbase.DEBUG) e.printStackTrace(); } } @@ -251,14 +262,14 @@ public final class CardbaseCLI { /** * Print program version. */ - void version() { + private void version() { System.out.println("Cardbase v" + VERSION); } /** * Exit procedure. */ - void exit() { + private void exit() { if (savePrompt) { System.out.println("Don't forget to save. If you really wish to quit without saving, type \"exit\" again."); savePrompt = false; @@ -270,7 +281,7 @@ public final class CardbaseCLI { /** * Print a list of valid set codes. */ - void sets() { + private void sets() { for (CardSetInformation set : mtgUniverse.getCardSetList()) { // CardSet has an overridden toString() System.out.println(set); @@ -282,7 +293,7 @@ public final class CardbaseCLI { * * @param args the code of the chosen set. */ - void set(String... args) { + private void set(String... args) { if (args != null && args.length > 0) { try { selectedSet = mtgUniverse.getFullCardSet(args[0]); @@ -312,7 +323,7 @@ public final class CardbaseCLI { /** * Print a brief list of the whole cardbase. */ - void glance() { + private void glance() { int total = 0; for (Card card : cardbase.getCards()) { printGlance(card); @@ -326,7 +337,7 @@ public final class CardbaseCLI { * * @param args optionally a card within the set (by number) to peruse. */ - void peruse(String... args) { + private void peruse(String... args) { // if a card is specified, peruse only that if (args != null && args.length > 0) { if (selectedSet != null) { @@ -355,7 +366,7 @@ public final class CardbaseCLI { * * @param args the set number of the card to remove and optionally the count to be removed. */ - void remove(String... args) { + private void remove(String... args) { if (selectedSet != null) { if (args != null && args.length > 0) { // Card cardToRemove = selectedSet.getCardByNumber(args[0]); @@ -372,13 +383,13 @@ public final class CardbaseCLI { cardToRemove.count = count; removeCard(cardToRemove); } else { - System.out.println(args[0] + " is not in the cardbase."); + System.out.println("Card " + selectedSet.code + " " + args[0] + " is not in the cardbase."); } } else { System.out.println("Please specify a card number to remove."); } } else { - System.out.println("Select a set before removing cards."); + System.out.println("Please select a set before removing cards."); } } @@ -388,7 +399,7 @@ public final class CardbaseCLI { * @param number the number of the card to add. * @param args optionally the count to add. */ - void add(String number, String... args) { + private void add(String number, String... args) { if (selectedSet != null) { // a blank line after adding a card repeats the addition unlimitedly if (number.isEmpty()) { @@ -412,14 +423,14 @@ public final class CardbaseCLI { } } } else { - System.out.println("Select a set before adding cards."); + System.out.println("Please select a set before adding cards."); } } /** * Undo previous action. */ - void undo() { + private void undo() { if (lastAction != null) { if (lastAction == Action.ADD) { removeCard(lastAction.card); @@ -474,7 +485,7 @@ public final class CardbaseCLI { * @param name the file name candidate to sanitise. * @return the sanitised name. */ - String sanitiseFileName(String name) { + private String sanitiseFileName(String name) { // POSIX-compliant valid filename characters name = name.replaceAll("[^-_./A-Za-z0-9]", ""); // extension is not indispensable, but good practice diff --git a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java index 7332c3b..5ecc16e 100644 --- a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java +++ b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java @@ -4,7 +4,9 @@ import static org.junit.Assert.*; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; import java.io.PrintStream; +import java.util.HashMap; import java.util.Scanner; import org.junit.After; @@ -56,98 +58,6 @@ public class CardbaseCLITest { } /*********************************************************************************** - * User input tests, happy path - ***********************************************************************************/ - @Test - public void commandAndArgumentsSeparatedBySpaces() throws Exception { - String[] processedInput = uut.sanitiseInput("cOmMand5 argumEnt1 argument2"); - - assertEquals("Wrong array length.", 3, processedInput.length); - assertEquals("cOmMand5", processedInput[0]); - assertEquals("argumEnt1", processedInput[1]); - assertEquals("argument2", processedInput[2]); - } - - @Test - public void commandAndNoArguments() throws Exception { - String[] processedInput = uut.sanitiseInput("Someth1ng"); - - assertEquals("Wrong array length.", 1, processedInput.length); - assertEquals("Someth1ng", processedInput[0]); - } - - /* - * Edge cases - */ - @Test - public void blankInput() throws Exception { - String[] processedInput = uut.sanitiseInput(""); - - assertEquals("Wrong array length.", 1, processedInput.length); - assertEquals("", processedInput[0]); - } - - @Test - public void onlyWhiteSpace() throws Exception { - String[] processedInput = uut.sanitiseInput(" "); - - assertEquals("Wrong array length.", 1, processedInput.length); - assertEquals("", processedInput[0]); - } - - @Test - public void leadingTrailingAndIntermediaryWhiteSpace() throws Exception { - String[] processedInput = uut.sanitiseInput(" \t this \twas \t \t a triumph \t\t "); - - assertEquals("Wrong array length.", 4, processedInput.length); - assertEquals("this", processedInput[0]); - assertEquals("was", processedInput[1]); - assertEquals("a", processedInput[2]); - assertEquals("triumph", processedInput[3]); - } - - /*********************************************************************************** - * File name sanity tests, happy path - ***********************************************************************************/ - @Test - public void reasonableFileNameWithoutExtension() throws Exception { - String processedName = uut.sanitiseFileName("f1lename"); - - assertEquals("f1lename.cb", processedName); - } - - @Test - public void reasonableFileNameWithExtension() throws Exception { - String processedName = uut.sanitiseFileName("f1lename.cb"); - - assertEquals("f1lename.cb", processedName); - } - - /* - * Edge cases - */ - @Test - public void nameWithBrokenExtension() throws Exception { - String processedName = uut.sanitiseFileName("f1lename.c b"); - - assertEquals("f1lename.cb", processedName); - } - - @Test - public void nameWithWrongExtension() throws Exception { - String processedName = uut.sanitiseFileName("f1lename.tar"); - - assertEquals("f1lename.tar.cb", processedName); - } - - @Test - public void nameWithIllegalCharacters() throws Exception { - String processedName = uut.sanitiseFileName("f1lEnämẽ\n\t\"--._-//? \t^|#ŧ@fhw9vLL'''"); - - assertEquals("f1lEnm--._-//fhw9vLL.cb", processedName); - } - - /*********************************************************************************** * Constructor tests, happy path ***********************************************************************************/ @Test @@ -216,7 +126,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.help(); + uut.interpretInput("help"); } finally { System.setOut(console); } @@ -240,7 +150,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.write(testFile.getAbsolutePath()); + uut.interpretInput("write " + testFile.getAbsolutePath()); } finally { System.setOut(console); } @@ -259,7 +169,7 @@ public class CardbaseCLITest { try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json")); Scanner scanner2 = new Scanner(testFile)) { - uut.write(testFile.getAbsolutePath()); + uut.interpretInput("write " + testFile.getAbsolutePath()); String cardJSON = scanner.useDelimiter("\\Z").next(); Card testCard = new ObjectMapper().readValue(cardJSON, Card.class); testCard.count = 1; @@ -267,7 +177,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.write(); + uut.interpretInput("write"); } finally { System.setOut(console); } @@ -284,13 +194,39 @@ public class CardbaseCLITest { public void noFileIsProvidedAndNoDefaultIsAvailable() throws Exception { try { System.setOut(new PrintStream(testOutput)); - uut.write(); + uut.interpretInput("write"); } finally { System.setOut(console); } assertEquals("Please provide a file name." + EOL, testOutput.toString()); } + @Test + public void reasonableFileNameWithoutExtension() throws Exception { + File testFile = tempFolder.newFile("saveTest"); + + try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json")); + Scanner scanner2 = new Scanner(new File(tempFolder.getRoot().getAbsolutePath() + "/saveTest.cb"))) { + String cardJSON = scanner.useDelimiter("\\Z").next(); + Card testCard = new ObjectMapper().readValue(cardJSON, Card.class); + testCard.count = 1; + uut.cardbase.addCard(testCard); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("write " + testFile.getAbsolutePath()); + } finally { + System.setOut(console); + } + + String save = scanner2.useDelimiter("\\Z").next(); + assertTrue(save.contains(cardJSON)); + + assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "saveTest.cb\". " + + "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString()); + } + } + /* * Edge cases */ @@ -299,13 +235,82 @@ public class CardbaseCLITest { File directory = tempFolder.newFolder("testdirectory.cb"); try { System.setOut(new PrintStream(testOutput)); - uut.write(directory.getAbsolutePath()); + uut.interpretInput("write " + directory.getAbsolutePath()); } finally { System.setOut(console); } assertEquals("Could not write to \"" + directory.getAbsolutePath() + "\"." + EOL, testOutput.toString()); } + @Test + public void nameWithBrokenExtension() throws Exception { + try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"))) { + String cardJSON = scanner.useDelimiter("\\Z").next(); + Card testCard = new ObjectMapper().readValue(cardJSON, Card.class); + testCard.count = 1; + uut.cardbase.addCard(testCard); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/saveTest.c b"); + } finally { + System.setOut(console); + } + + try (Scanner scanner2 = new Scanner(new File(tempFolder.getRoot().getAbsolutePath() + "/saveTest.c.cb"))) { + String save = scanner2.useDelimiter("\\Z").next(); + assertTrue(save.contains(cardJSON)); + + assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "/saveTest.c.cb\". " + + "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString()); + } + } + } + + @Test + public void nameWithWrongExtension() throws Exception { + try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"))) { + String cardJSON = scanner.useDelimiter("\\Z").next(); + Card testCard = new ObjectMapper().readValue(cardJSON, Card.class); + testCard.count = 1; + uut.cardbase.addCard(testCard); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/saveTest.tar"); + } finally { + System.setOut(console); + } + + try (Scanner scanner2 = new Scanner(new File(tempFolder.getRoot().getAbsolutePath() + "/saveTest.tar.cb"))) { + String save = scanner2.useDelimiter("\\Z").next(); + assertTrue(save.contains(cardJSON)); + + assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "/saveTest.tar.cb\". " + + "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString()); + } + } + } + + @Test + public void nameWithIllegalCharacters() throws Exception { + try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"))) { + String cardJSON = scanner.useDelimiter("\\Z").next(); + Card testCard = new ObjectMapper().readValue(cardJSON, Card.class); + testCard.count = 1; + uut.cardbase.addCard(testCard); + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/f1lEnämẽ\"--._-//"); + } finally { + System.setOut(console); + } + + assertEquals("Error: lost contact with the output file." + EOL, testOutput.toString()); + } + } + /*********************************************************************************** * version() tests, happy path ***********************************************************************************/ @@ -313,7 +318,7 @@ public class CardbaseCLITest { public void correctVersionIsPrinted() throws Exception { try { System.setOut(new PrintStream(testOutput)); - uut.version(); + uut.interpretInput("version"); } finally { System.setOut(console); } @@ -325,7 +330,7 @@ public class CardbaseCLITest { ***********************************************************************************/ @Test public void exitFlagIsRaised() throws Exception { - uut.exit(); + uut.interpretInput("exit"); assertEquals("Incorrect state for exit flag.", true, uut.exit); } @@ -335,7 +340,7 @@ public class CardbaseCLITest { uut.savePrompt = true; try { System.setOut(new PrintStream(testOutput)); - uut.exit(); + uut.interpretInput("exit"); } finally { System.setOut(console); } @@ -347,12 +352,12 @@ public class CardbaseCLITest { public void exitFlagIsRaisedAfterSavePromptIsAcknowledged() throws Exception { uut.savePrompt = true; - uut.exit(); + uut.interpretInput("exit"); assertEquals("Incorrect state for exit flag.", false, uut.exit); assertEquals("Incorrect state for save flag.", false, uut.savePrompt); - uut.exit(); + uut.interpretInput("exit"); assertEquals("Incorrect state for exit flag.", true, uut.exit); } @@ -366,7 +371,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.sets(); + uut.interpretInput("sets"); } finally { System.setOut(console); } @@ -388,7 +393,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.sets(); + uut.interpretInput("sets"); } finally { System.setOut(console); } @@ -408,7 +413,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.set("M15"); + uut.interpretInput("set M15"); } finally { System.setOut(console); } @@ -426,12 +431,12 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.set("not a set"); + uut.interpretInput("set not_a_set"); } finally { System.setOut(console); } - assertEquals("\"not a set\" does not correspond to any set (use \"sets\" to see all valid set codes)." + assertEquals("\"not_a_set\" does not correspond to any set (use \"sets\" to see all valid set codes)." + EOL, testOutput.toString()); assertNull(uut.selectedSet); } @@ -447,7 +452,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.glance(); + uut.interpretInput("glance"); } finally { System.setOut(console); } @@ -459,7 +464,7 @@ public class CardbaseCLITest { public void glanceIsPrintedWithZeroCards() throws Exception { try { System.setOut(new PrintStream(testOutput)); - uut.glance(); + uut.interpretInput("glance"); } finally { System.setOut(console); } @@ -473,7 +478,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.glance(); + uut.interpretInput("glance"); } finally { System.setOut(console); } @@ -498,7 +503,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.peruse(); + uut.interpretInput("peruse"); } finally { System.setOut(console); } @@ -512,7 +517,7 @@ public class CardbaseCLITest { public void perusalIsPrintedWithZeroCards() throws Exception { try { System.setOut(new PrintStream(testOutput)); - uut.peruse(); + uut.interpretInput("peruse"); } finally { System.setOut(console); } @@ -526,7 +531,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.peruse(); + uut.interpretInput("peruse"); } finally { System.setOut(console); } @@ -546,7 +551,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.peruse("129"); + uut.interpretInput("peruse 129"); } finally { System.setOut(console); } @@ -569,7 +574,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.peruse("100"); + uut.interpretInput("peruse 100"); } finally { System.setOut(console); } @@ -584,7 +589,7 @@ public class CardbaseCLITest { try { System.setOut(new PrintStream(testOutput)); - uut.peruse("100"); + uut.interpretInput("peruse 100"); } finally { System.setOut(console); } @@ -598,12 +603,12 @@ public class CardbaseCLITest { @Test public void removeValidAmountOfExistingCard() throws Exception { uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); - // dummy set just so the uut knows the set to peruse from + // dummy set just so the uut knows the set to remove from FullCardSet fcs = new FullCardSet(); fcs.code = "FRF"; uut.selectedSet = fcs; - uut.remove("129", "3"); + uut.interpretInput("remove 129 3"); assertEquals("Wrong number of cards was removed.", uut.cardbase.getCard("FRF", "129").count, new Integer(6)); } @@ -611,12 +616,12 @@ public class CardbaseCLITest { @Test public void removeExceedingAmountOfExistingCard() throws Exception { uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); - // dummy set just so the uut knows the set to peruse from + // dummy set just so the uut knows the set to remove from FullCardSet fcs = new FullCardSet(); fcs.code = "FRF"; uut.selectedSet = fcs; - uut.remove("128", "3"); + uut.interpretInput("remove 128 3"); assertNull("Card was not removed successfully.", uut.cardbase.getCard("FRF", "128")); } @@ -624,12 +629,12 @@ public class CardbaseCLITest { @Test public void removeExactAmountOfExistingCard() throws Exception { uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); - // dummy set just so the uut knows the set to peruse from + // dummy set just so the uut knows the set to remove from FullCardSet fcs = new FullCardSet(); fcs.code = "FRF"; uut.selectedSet = fcs; - uut.remove("128", "1"); + uut.interpretInput("remove 128 1"); assertNull("Card was not removed successfully.", uut.cardbase.getCard("FRF", "128")); } @@ -637,12 +642,12 @@ public class CardbaseCLITest { @Test public void removeSingleExistingCardWithoutAmount() throws Exception { uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); - // dummy set just so the uut knows the set to peruse from + // dummy set just so the uut knows the set to remove from FullCardSet fcs = new FullCardSet(); fcs.code = "FRF"; uut.selectedSet = fcs; - uut.remove("128"); + uut.interpretInput("remove 128"); assertNull("Card was not removed successfully.", uut.cardbase.getCard("FRF", "128")); } @@ -650,12 +655,12 @@ public class CardbaseCLITest { @Test public void removeMultipleExistingCardWithoutAmount() throws Exception { uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); - // dummy set just so the uut knows the set to peruse from + // dummy set just so the uut knows the set to remove from FullCardSet fcs = new FullCardSet(); fcs.code = "FRF"; uut.selectedSet = fcs; - uut.remove("129"); + uut.interpretInput("remove 129"); assertNull("Card was not removed successfully.", uut.cardbase.getCard("FRF", "129")); } @@ -663,27 +668,150 @@ public class CardbaseCLITest { /* * Edge cases */ - // attempt to remove nonexistent card without amount - - // attempt to remove nonexistent card with amount + @Test + public void removeNonExistentCardWithoutAmount() throws Exception { + // dummy set just so the uut knows the set to remove from + FullCardSet fcs = new FullCardSet(); + fcs.code = "FRF"; + uut.selectedSet = fcs; + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("remove 128"); + } finally { + System.setOut(console); + } + + assertEquals("Card FRF 128 is not in the cardbase." + EOL, testOutput.toString()); + } - // remove 0 of existing card + @Test + public void removeNonExistentCardWithAmount() throws Exception { + // dummy set just so the uut knows the set to remove from + FullCardSet fcs = new FullCardSet(); + fcs.code = "FRF"; + uut.selectedSet = fcs; + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("remove 128 2"); + } finally { + System.setOut(console); + } + + assertEquals("Card FRF 128 is not in the cardbase." + EOL, testOutput.toString()); + } - // remove negative number of existing card + @Test + public void removeZeroOfExistingCard() throws Exception { + uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); + // dummy set just so the uut knows the set to remove from + FullCardSet fcs = new FullCardSet(); + fcs.code = "FRF"; + uut.selectedSet = fcs; + + uut.interpretInput("remove 129 0"); + + assertEquals("Card amount should not have changed.", uut.cardbase.getCard("FRF", "129").count, new Integer(8)); + } - // remove card without selected set + @Test + public void removeNegativeAmountOfExistingCard() throws Exception { + uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); + // dummy set just so the uut knows the set to remove from + FullCardSet fcs = new FullCardSet(); + fcs.code = "FRF"; + uut.selectedSet = fcs; + + uut.interpretInput("remove 129 -10"); + + assertEquals("Card amount should not have changed.", uut.cardbase.getCard("FRF", "129").count, new Integer(8)); + } + @Test + public void removeWithNoSetSelected() throws Exception { + uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile()); + uut.selectedSet = null; + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("remove 100"); + } finally { + System.setOut(console); + } + + assertEquals("Please select a set before removing cards." + EOL, testOutput.toString()); + } /*********************************************************************************** * add() tests, happy path ***********************************************************************************/ - + // add - /*********************************************************************************** - * undo() tests, happy path - ***********************************************************************************/ + /* + * Edge cases + */ + @Test + public void invalidCommandWithNoSelectedSet() throws Exception { + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("cOmMand5 argumEnt1 argument2"); + } finally { + System.setOut(console); + } + + assertEquals("Please select a set before adding cards." + EOL, testOutput.toString()); + } + + @Test + public void invalidCommandWithSelectedSet() throws Exception { + // dummy set just so the uut knows the set to remove from + FullCardSet fcs = new FullCardSet(); + fcs.name = "Fate Reforged"; + fcs.cards = new HashMap<String, Card>(); + uut.selectedSet = fcs; + + try { + System.setOut(new PrintStream(testOutput)); + uut.interpretInput("cOmMand5 argumEnt1 argument2"); + } finally { + System.setOut(console); + } + + assertEquals("cOmMand5 does not correspond to a card in Fate Reforged." + EOL, testOutput.toString()); + } + +// @Test +// public void blankInput() throws Exception { +// String[] processedInput = uut.sanitiseInput(""); +// +// assertEquals("Wrong array length.", 1, processedInput.length); +// assertEquals("", processedInput[0]); +// } +// +// @Test +// public void onlyWhiteSpace() throws Exception { +// String[] processedInput = uut.sanitiseInput(" "); +// +// assertEquals("Wrong array length.", 1, processedInput.length); +// assertEquals("", processedInput[0]); +// } +// +// @Test +// public void leadingTrailingAndIntermediaryWhiteSpace() throws Exception { +// String[] processedInput = uut.sanitiseInput(" \t this \twas \t \t a triumph \t\t "); +// +// assertEquals("Wrong array length.", 4, processedInput.length); +// assertEquals("this", processedInput[0]); +// assertEquals("was", processedInput[1]); +// assertEquals("a", processedInput[2]); +// assertEquals("triumph", processedInput[3]); +// } + /*********************************************************************************** + * undo() tests, happy path + ***********************************************************************************/ } @@ -5,5 +5,6 @@ Cardbase: CardbaseCLI: * undo, add, remove + * refactor exit tests, shouldnt need to look at flag To pass: happy path remove
\ No newline at end of file |