cicaprojekt/cicaprojekt/Tester.java

103 lines
3.3 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"};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("moveTest: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean testStargates(){
String[] commands = {"loadMap testStargates\n",
"shootONeillsGun B\n",
"rotate O L\n",
"shootOneillsGun Y\n",
"listStargates\n",
"move\n",
"listPlayers\n"};
String[] expectedOutputs = {"BlueStargate: 5, 10\n",
"YellowStargate: 5, 9\n",
"ONeill: 5, 9\n"};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testStargates: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
}