cicaprojekt/cicaprojekt/Tester.java
2016-04-25 22:53:27 +02:00

322 lines
9.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);
replicator = new PlayerBase("Replicator", (Tile) playerLocations.get("replicator"), Direction.NORTH);
return dungeon;
}
String listPlayers() {
return oneill.toString() + " " + jaffa.toString() + " " + replicator.toString();
}
void move(String param) {
switch (param) {
case "O" :
oneill.move(oneill.getFacingDirection());
break;
case "J" :
jaffa.move(jaffa.getFacingDirection());
break;
default:
//TODO error handling
break;
}
}
void boxLift(String param) {
switch (param) {
case "O" :
oneill.boxLift();
break;
case "J" :
jaffa.boxLift();
break;
}
}
void boxDrop(String param) {
switch (param) {
case "O" :
oneill.boxDrop();
break;
case "J" :
oneill.boxDrop();
break;
}
}
void shootONeillsGun(String param) {
switch (param) {
case "B" :
oneill.shootStargate(Stargate.blueStargate);
break;
case "Y" :
oneill.shootStargate(Stargate.yellowStargate);
break;
}
}
void rotate(String playerParam, String directionParam) {
switch (playerParam) {
case "O" :
switch (directionParam) {
case "L" :
oneill.rotateLeft();
break;
case "R" :
oneill.rotateRight();
break;
}
case "J" :
switch (directionParam) {
case "L" :
jaffa.rotateLeft();
break;
case "R" :
jaffa.rotateRight();
break;
}
}
}
void listStargates() {
System.out.println(Stargate.blueStargate);
System.out.println(Stargate.yellowStargate);
System.out.println(Stargate.redStargate);
System.out.println(Stargate.greenStargate);
}
/* 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() throws IOException{
boolean succes = true;
loadMap("moveTest");
String listOfPlayers = listPlayers();
if(!listOfPlayers.equals("ONeill: 1, 1 Jaffa: -666, -666 Replicator: -666, -666"))
succes = false;
move("O");
listOfPlayers = listPlayers();
if(!listOfPlayers.equals("ONeill: 1, 2 Jaffa: -666, -666 Replicator: -666, -666"))
succes = false;
System.out.print("moveTest: ");
if(succes)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return succes;
}
@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"};
loadMap("testStargates");
shootONeillsGun("B");
rotate("O", "L");
shootONeillsGun("Y");
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testStargates: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean testExtraZPM(){
String[] commands = {"loadMap testExtraZPMs\n",
"listZPMs\n",
"move O\n",
"move O\n",
"listZPMs\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testExtraZPMs: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
boolean testScalesAndGates(){
String[] commands = {"loadMap testScalesAndGates\n",
"boxLift O\n",
"rotate O L\n",
"boxDrop O\n"};
String[] expectedOutputs = {"door open\n"};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testScalesAndGates: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean gapTest(){
String[] commands = {"loadMap gapTest\n",
"move O\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("gapTest: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean ZPMTest(){
String[] commands = {"loadMap ZPMTest\n",
"move O\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("ZPMTest: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean testReplicatorPosition(){
String[] commands = {"loadMap testReplicatorPosition\n",
"move O\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testReplicatorPosition: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean timeUpTest(){
String[] commands = {"loadMap timeUpTest\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("timeUpTest: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
@Test
boolean rotationTest() {
String[] commands = {"loadMap rotationTest\n",
"Rotate O L\n",
"Rotate O R\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("rotationTest: ");
}
@Test
boolean testBoxes(){
String[] commands = {"loadMap testBoxes\n",
"boxLift\n",
"boxDrop\n"};
String[] expectedOutputs = {"boc3 lifted\n",
"box3 dropped\n"};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
System.out.print("testBoxes: ");
if(results)
System.out.println("Sikeres!");
else System.out.println("Sikertelen!");
return results;
}
}