cicaprojekt/cicaprojekt/Tester.java
2016-04-25 20:02:48 +02:00

73 lines
2.2 KiB
Java

package cicaprojekt;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class Tester {
private Player oneill;
private Player jaffa;
private PlayerBase replicator;
Dungeon loadMap(String param) throws IOException{
Dungeon dungeon = new Dungeon();
Map playerLocations = dungeon.buildDungeon(new File(param));
oneill = new Player("ONeill", (Tile) playerLocations.get("oneill"), Direction.NORTH);
jaffa = new Player("Jaffa", (Tile) playerLocations.get("jaffa"), Direction.NORTH);
return dungeon;
}
void listPlayers() {
System.out.println(oneill);
System.out.println(jaffa);
System.out.println(replicator);
}
/* custom Test annotation */
@Target(ElementType.METHOD) // it's for methods
@Retention(RetentionPolicy.RUNTIME) // we want to retain annotations in runtime
private @interface Test {}
/* run all methods annotated with @Test */
public boolean runAllTests() throws InvocationTargetException, IllegalAccessException
{
boolean testspassed = true;
for (Method m : this.getClass().getMethods()) // iterate over all methods of this
{
if (m.isAnnotationPresent(Test.class)) // if its annotated with @Test...
{
Boolean testresult = (Boolean) m.invoke(null); // call it!
if (!testresult)
{
testspassed = false;
}
}
}
return testspassed;
}
@Test
boolean moveTest(){
String[] commands = {"loadMap movetest\n",
"listPlayers\n",
"move O",
"listPlayers\n"};
String[] expectedOutputs = {"ONeill: 1,1\n",
"ONeill: 1,2\n"};
return testOnSequenceOfCommands(commands, expectedOutputs);
}
}