diff --git a/RunThroughResults.txt b/RunThroughResults.txt index c6fbcc1..5c32a55 100644 --- a/RunThroughResults.txt +++ b/RunThroughResults.txt @@ -1,9 +1,6 @@ -Sample Run Through - ->IPA1 sample.xml You find yourself at the mouth of a cave and decide that in spite of common sense and any sense of self preservation that you're going to go exploring north into it. It's a little dark, but luckily there are some torches on the wall. >e -Can’t go that way. +Can't go that way. >N Error >n @@ -34,7 +31,7 @@ Error >attack gnome with face! Error >w -Can’t go that way. +Can't go that way. >read chest Error >attack chest with torch @@ -48,9 +45,9 @@ chest contains explosive. >take explosive Item explosive added to inventory. >open chest -chest is empty. +chest is empty >i -Inventory: torch, explosive +Inventory: explosive, torch >attack gnome with explosive Error >read explosive diff --git a/pom.xml b/pom.xml index ab8e7b2..1f8b5dd 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,14 @@ com.github.dtschust zork 1.0.0 + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + 11 diff --git a/short.txt b/short.txt new file mode 100644 index 0000000..4e2605d --- /dev/null +++ b/short.txt @@ -0,0 +1,9 @@ +You find yourself at the mouth of a cave and decide that in spite of common sense and any sense of self preservation that you're going to go exploring north into it. It's a little dark, but luckily there are some torches on the wall. +>e +Can't go that way. +>N +Error +>n +*stumble* need some light... +>i +Inventory: empty diff --git a/src/main/java/com/github/dtschust/zork/Zork.java b/src/main/java/com/github/dtschust/zork/Zork.java index bcb7465..d82fe98 100644 --- a/src/main/java/com/github/dtschust/zork/Zork.java +++ b/src/main/java/com/github/dtschust/zork/Zork.java @@ -40,7 +40,6 @@ public class Zork { /* There is no stopping in Zork, until we're done!!*/ while (true) { - skip = false; userInput = source.nextLine(); /*Now that we have the user command, check the input*/ @@ -72,7 +71,7 @@ public class Zork { /* Execute a user action or an action command from some element that is not one of the "Special Commands"*/ public void action(String input) { - int i, j, k, l, x, y, z; + int y; String tempString; ZorkContainer tempContainer; @@ -231,7 +230,7 @@ public class Zork { /*Variable initialization*/ boolean skip = false; - int i, j, k, l, x, y, z; + int x, y; ZorkTrigger tempTrigger; ZorkContainer tempContainer; diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java b/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java index 04a4b32..0d93076 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkGame.java @@ -13,6 +13,6 @@ public class ZorkGame { public HashMap Creatures = new HashMap<>(); public HashMap Inventory = new HashMap<>(); public HashMap ObjectLookup = new HashMap<>(); - public String currentRoom; - public File file; + + } diff --git a/src/test/java/com/github/dtschust/zork/ZorkTest.java b/src/test/java/com/github/dtschust/zork/ZorkTest.java new file mode 100644 index 0000000..09e8e35 --- /dev/null +++ b/src/test/java/com/github/dtschust/zork/ZorkTest.java @@ -0,0 +1,138 @@ +package com.github.dtschust.zork; + +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.util.Scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Not really nice but works + * TODO: The test result should be success instead of terminated ?WHY? + * TODO: when looking at inventory (i) we are relying on the HashMap order, so the test may fail !UNSAFE! + */ +class ZorkTest { + + @Test + public void testRunThroughResults() { + CommandReader run = new CommandReader("RunThroughResults.txt"); + GamePlayer game = new GamePlayer("sampleGame.xml", false); + while(true){ + switch(run.getInstructionType()) { + case SEND: + game.write(run.getInstruction()); + break; + case RECV: + assertEquals(run.getInstruction(), game.read()); + break; + default: + return; + } + } + } + + + /** + * CommandReader reads the .txt file containing expected input and output + */ + static class CommandReader{ + private String instruction; + private static Scanner scanner; + + public enum Type{ SEND, RECV, END } + + /** CommandReader Constructor + * @param filename file containing command sent (with leading ">") and expected responses + */ + CommandReader(String filename) { + try { + scanner = new Scanner(new File(filename)); + } catch (FileNotFoundException e){ + e.printStackTrace(); + } + } + + /** + * @return Type of the next instruction: + * - END if the game ended + * - SEND if it's th command to send + * - RECV if it's a game output + */ + public Type getInstructionType(){ + if(!scanner.hasNextLine()) + return Type.END; + instruction = scanner.nextLine(); + if(instruction.startsWith(">")) { + instruction = instruction.substring(1); + return Type.SEND; + } + + return Type.RECV; + } + + /** Returns a text line (can be both an input or output depending on the Type) + * @return The next text line + */ + public String getInstruction() { + return instruction; + } + } + + /** + * GamePlayer start the game and holds a communication interface to interact to it + */ + static class GamePlayer { + public final PrintStream console; + private PrintStream printer; + private BufferedReader reader; + private final boolean verbose; + + + /** GamePlayer Constructor + * @param filename name of the game configuration .xml file + * @param verbose should log on the console all command sent to / received by the game + */ + public GamePlayer(String filename, boolean verbose) { + this.verbose = verbose; + console = System.out; + try{ + final PipedOutputStream testInput = new PipedOutputStream(); + final PipedOutputStream out = new PipedOutputStream(); + final PipedInputStream testOutput = new PipedInputStream(out); + System.setIn(new PipedInputStream(testInput)); + System.setOut(new PrintStream(out)); + printer = new PrintStream(testInput); + reader = new BufferedReader(new InputStreamReader(testOutput)); + } catch (IOException e) { + e.printStackTrace(console); + } + + new Thread(() -> new Zork(filename)).start(); + } + + /** Sends a command to the game (and print it if verbose) + * @param line text to send to the game + */ + public void write(String line) { + printer.println(line); + if(verbose) + console.println("> " + line); + } + + /** Receive a command from the game (and print it if verbose) + * @return line of text sent by the game + */ + public String read() { + String line = null; + try{ + line = reader.readLine(); + if(verbose) + console.println("< " + line); + } catch (IOException e) { + e.printStackTrace(console); + } + return line; + } + } +} \ No newline at end of file