kind of test

This commit is contained in:
RaffaeleMorganti 2022-11-17 00:51:58 +01:00
parent 6b3d2d142c
commit 4b01c0a85c
6 changed files with 163 additions and 12 deletions

View file

@ -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. 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 >e
Cant go that way. Can't go that way.
>N >N
Error Error
>n >n
@ -34,7 +31,7 @@ Error
>attack gnome with face! >attack gnome with face!
Error Error
>w >w
Cant go that way. Can't go that way.
>read chest >read chest
Error Error
>attack chest with torch >attack chest with torch
@ -48,9 +45,9 @@ chest contains explosive.
>take explosive >take explosive
Item explosive added to inventory. Item explosive added to inventory.
>open chest >open chest
chest is empty. chest is empty
>i >i
Inventory: torch, explosive Inventory: explosive, torch
>attack gnome with explosive >attack gnome with explosive
Error Error
>read explosive >read explosive

View file

@ -7,6 +7,14 @@
<groupId>com.github.dtschust</groupId> <groupId>com.github.dtschust</groupId>
<artifactId>zork</artifactId> <artifactId>zork</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>

9
short.txt Normal file
View file

@ -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

View file

@ -40,7 +40,6 @@ public class Zork {
/* There is no stopping in Zork, until we're done!!*/ /* There is no stopping in Zork, until we're done!!*/
while (true) { while (true) {
skip = false;
userInput = source.nextLine(); userInput = source.nextLine();
/*Now that we have the user command, check the input*/ /*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 <action> element that is not one of the "Special Commands"*/ /* Execute a user action or an action command from some <action> element that is not one of the "Special Commands"*/
public void action(String input) { public void action(String input) {
int i, j, k, l, x, y, z; int y;
String tempString; String tempString;
ZorkContainer tempContainer; ZorkContainer tempContainer;
@ -231,7 +230,7 @@ public class Zork {
/*Variable initialization*/ /*Variable initialization*/
boolean skip = false; boolean skip = false;
int i, j, k, l, x, y, z; int x, y;
ZorkTrigger tempTrigger; ZorkTrigger tempTrigger;
ZorkContainer tempContainer; ZorkContainer tempContainer;

View file

@ -13,6 +13,6 @@ public class ZorkGame {
public HashMap<String, ZorkCreature> Creatures = new HashMap<>(); public HashMap<String, ZorkCreature> Creatures = new HashMap<>();
public HashMap<String, String> Inventory = new HashMap<>(); public HashMap<String, String> Inventory = new HashMap<>();
public HashMap<String, String> ObjectLookup = new HashMap<>(); public HashMap<String, String> ObjectLookup = new HashMap<>();
public String currentRoom;
public File file;
} }

View file

@ -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;
}
}
}