Release v3.0
This commit is contained in:
0
OGP1718-Worms/tests/worms/model/PartialPart2FacadeTest.java
Normal file → Executable file
0
OGP1718-Worms/tests/worms/model/PartialPart2FacadeTest.java
Normal file → Executable file
42
OGP1718-Worms/tests/worms/model/PartialPart3FacadeTest.java
Executable file
42
OGP1718-Worms/tests/worms/model/PartialPart3FacadeTest.java
Executable file
@@ -0,0 +1,42 @@
|
||||
package worms.model;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import worms.facade.Facade;
|
||||
import worms.facade.IFacade;
|
||||
import worms.internal.gui.game.IActionHandler;
|
||||
import worms.model.Worm;
|
||||
import worms.programs.IProgramFactory;
|
||||
import worms.programs.ProgramParser;
|
||||
|
||||
public class PartialPart3FacadeTest {
|
||||
|
||||
private static final double EPS = 1e-4;
|
||||
|
||||
private IFacade facade;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
facade = new Facade();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testProgram() {
|
||||
World world = facade.createWorld(100.0, 100.0, new boolean[][] { {true}, {false} });
|
||||
IProgramFactory<?, ?, ?, ? extends Program> programFactory = facade.createProgramFactory();
|
||||
|
||||
Program program = ProgramParser.parseProgramFromString("x := 0; while x < 1.5: { x := x + 0.1; } turn x;", programFactory);
|
||||
Worm worm = facade.createWorm(world, new double[] { 50.0, 50.51 }, 0, 0.5, "TestWorm", null);
|
||||
IActionHandler handler = new SimpleActionHandler(facade);
|
||||
facade.loadProgramOnWorm(worm, program, handler);
|
||||
double oldOrientation = facade.getOrientation(worm);
|
||||
facade.startGame(world); // this will run the program
|
||||
double newOrientation = facade.getOrientation(worm);
|
||||
assertEquals(oldOrientation + 1.5, newOrientation, EPS);
|
||||
}
|
||||
|
||||
}
|
95
OGP1718-Worms/tests/worms/model/SimpleActionHandler.java
Executable file
95
OGP1718-Worms/tests/worms/model/SimpleActionHandler.java
Executable file
@@ -0,0 +1,95 @@
|
||||
package worms.model;
|
||||
|
||||
import worms.facade.IFacade;
|
||||
import worms.internal.gui.GUIConstants;
|
||||
import worms.internal.gui.game.IActionHandler;
|
||||
import worms.util.ModelException;
|
||||
|
||||
/**
|
||||
* A simple action handler that just calls the necessary methods on the facade.
|
||||
* Useful for writing tests; there's no other reason to use this.
|
||||
*/
|
||||
public class SimpleActionHandler implements IActionHandler {
|
||||
|
||||
private final IFacade facade;
|
||||
|
||||
public SimpleActionHandler(IFacade facade) {
|
||||
this.facade = facade;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean turn(Worm worm, double angle) {
|
||||
try {
|
||||
facade.turn(worm, angle);
|
||||
return true;
|
||||
} catch (ModelException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean move(Worm worm) {
|
||||
try {
|
||||
facade.move(worm);
|
||||
if (facade.canFall(worm)) {
|
||||
facade.fall(worm);
|
||||
}
|
||||
return true;
|
||||
} catch (ModelException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean jump(Worm worm) {
|
||||
try {
|
||||
facade.jump(worm, GUIConstants.JUMP_TIME_STEP);
|
||||
if (facade.canFall(worm)) {
|
||||
facade.fall(worm);
|
||||
}
|
||||
return true;
|
||||
} catch (ModelException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Worm worm) {
|
||||
try {
|
||||
facade.fire(worm);
|
||||
return true;
|
||||
} catch (ModelException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eat(Worm worm) {
|
||||
try {
|
||||
facade.eat(worm);
|
||||
return true;
|
||||
} catch (ModelException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
throw (RuntimeException) e.getCause();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user