improved test

This commit is contained in:
2018-03-08 17:50:53 +01:00
parent 5ce16f8793
commit 652a34e309
5 changed files with 50 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
package worms.facade; package worms.facade;
import worms.model.Worm; import worms.model.Worm;
import worms.util.IllegalNameException;
import worms.util.ModelException; import worms.util.ModelException;
import worms.util.Tuple; import worms.util.Tuple;
@@ -19,7 +20,14 @@ public class Facade implements IFacade {
@Override @Override
public Worm createWorm(double[] location, double direction, double radius, String name) throws ModelException { public Worm createWorm(double[] location, double direction, double radius, String name) throws ModelException {
return new Worm(Tuple.create(location), direction, name, radius); try {
return new Worm(Tuple.create(location), direction, name, radius);
}
catch(IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -30,8 +38,12 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void move(Worm worm, int nbSteps) throws ModelException { public void move(Worm worm, int nbSteps) throws ModelException {
try {
worm.move(nbSteps); worm.move(nbSteps);
}
catch(IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -136,7 +148,12 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void setRadius(Worm worm, double newRadius) throws ModelException { public void setRadius(Worm worm, double newRadius) throws ModelException {
worm.setRadius(newRadius); try {
worm.setRadius(newRadius);
}
catch(IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**
@@ -189,7 +206,12 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void rename(Worm worm, String newName) throws ModelException { public void rename(Worm worm, String newName) throws ModelException {
worm.setName(newName); try {
worm.setName(newName);
}
catch(IllegalNameException e) {
throw new ModelException(e);
}
} }
/** /**

View File

@@ -170,7 +170,8 @@ public class Worm {
* |result == (location != null) && (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2)) * |result == (location != null) && (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2))
*/ */
private boolean isValidLocation(Tuple<Double, Double>location) { private boolean isValidLocation(Tuple<Double, Double>location) {
return location != null && !Double.isNaN(location.item1) && !Double.isNaN(location.item2); return location != null && location.item1 != null && location.item2 != null
&& !Double.isNaN(location.item1) && !Double.isNaN(location.item2);
} }
//=================================================================================== //===================================================================================
@@ -428,7 +429,7 @@ public class Worm {
* the given name is not a valid name for any worm * the given name is not a valid name for any worm
* |! isValidName(name) * |! isValidName(name)
*/ */
public void setName(String name) { public void setName(String name) throws IllegalNameException {
int validName = isValidName(name); int validName = isValidName(name);
if (validName != -1) if (validName != -1)
@@ -479,7 +480,7 @@ public class Worm {
* *
* @param numberSteps * @param numberSteps
* the number of steps the worm should take * the number of steps the worm should take
* *
* @post the x-coordinate of the new location of the worm schould be the location of * @post the x-coordinate of the new location of the worm schould be the location of
* the old x-coordinate plus the number of steps multiplied with the distance * the old x-coordinate plus the number of steps multiplied with the distance
* that the x-coordinate schould move (distance is equal to the radius multiplied * that the x-coordinate schould move (distance is equal to the radius multiplied
@@ -501,7 +502,7 @@ public class Worm {
* then the current value of action point * then the current value of action point
* |NumberSteps < 0 || cost > this.actionPoints * |NumberSteps < 0 || cost > this.actionPoints
*/ */
public void move(int numberSteps) { public void move(int numberSteps) throws IllegalArgumentException {
if (numberSteps < 0) if (numberSteps < 0)
throw new IllegalArgumentException(); // TODO add decent exception msg throw new IllegalArgumentException(); // TODO add decent exception msg
@@ -568,12 +569,12 @@ public class Worm {
* |worms.util.Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2) * |worms.util.Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2)
* @post the current action actionPoints schould be 0 after a jump * @post the current action actionPoints schould be 0 after a jump
* |this.actionPoints = 0 * |this.actionPoints = 0
* @throws IllegalStateException() * @throws IllegalStateException
* if the current action actionPoints is equal to 0 or the orientation is more then * if the current action actionPoints is equal to 0 or the orientation is more then
* pi the worm can not jump * pi the worm can not jump
* |this.actionPoints == 0 || this.orientation < Math.PI * |this.actionPoints == 0 || this.orientation < Math.PI
*/ */
public void jump() { public void jump() throws IllegalStateException {
if (!canJump()) if (!canJump())
throw new IllegalStateException(); throw new IllegalStateException();
@@ -593,8 +594,6 @@ public class Worm {
* |result == this.actionPoints > 0 && this.orientation < Math.PI * |result == this.actionPoints > 0 && this.orientation < Math.PI
*/ */
private boolean canJump() { private boolean canJump() {
System.out.println(this.actionPoints);
System.out.println(this.orientation);
return this.actionPoints > 0 && this.orientation < PI; return this.actionPoints > 0 && this.orientation < PI;
} }

View File

@@ -31,12 +31,12 @@ public class Tuple<T1, T2> {
* @param item2 * @param item2
* The second element for this tuple * The second element for this tuple
*/ */
private Tuple(T1 item1, T2 item2) { public Tuple(T1 item1, T2 item2) throws IllegalArgumentException {
if (item1 == null) /*if (item1 == null)
throw new IllegalArgumentException("item1 may not be null"); throw new IllegalArgumentException("item1 may not be null");
if (item2 == null) if (item2 == null)
throw new IllegalArgumentException("item2 may not be null"); throw new IllegalArgumentException("item2 may not be null");*/
this.item1 = item1; this.item1 = item1;
this.item2 = item2; this.item2 = item2;
@@ -48,8 +48,8 @@ public class Tuple<T1, T2> {
} }
@Immutable @Immutable
public static Tuple<Double, Double> create(double[] arr) { public static Tuple<Double, Double> create(double[] arr) throws IllegalArgumentException {
if (arr == null || arr.length > 2) if (arr == null || arr.length != 2)
throw new IllegalArgumentException("Invalid parameter arr"); throw new IllegalArgumentException("Invalid parameter arr");
return new Tuple<>(arr[0], arr[1]); return new Tuple<>(arr[0], arr[1]);
} }

View File

@@ -1,14 +1,12 @@
package worms.model; package worms.model;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import worms.facade.Facade; import worms.facade.Facade;
import worms.facade.IFacade; import worms.facade.IFacade;
import worms.model.Worm;
import worms.util.ModelException; import worms.util.ModelException;
import static org.junit.Assert.assertEquals;
public class PartialFacadeTest { public class PartialFacadeTest {
private static final double EPS = 1e-4; private static final double EPS = 1e-4;

View File

@@ -2,24 +2,32 @@ package worms.model;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import worms.util.Tuple;
class WormTest { class WormTest {
private Worm worm;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1);
} }
@Test @Test
void getLocation() { void getLocation() {
assertEquals(Tuple.create(0.0, 0.0), worm.getLocation());
} }
@Test @Test
void getOrientation() { void getOrientation() {
assertEquals(0, worm.getOrientation());
} }
@Test @Test
void getRadius() { void getRadius() {
assertEquals(1, worm.getRadius());
} }
@Test @Test