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;
import worms.model.Worm;
import worms.util.IllegalNameException;
import worms.util.ModelException;
import worms.util.Tuple;
@@ -19,7 +20,14 @@ public class Facade implements IFacade {
@Override
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
public void move(Worm worm, int nbSteps) throws ModelException {
worm.move(nbSteps);
try {
worm.move(nbSteps);
}
catch(IllegalArgumentException e) {
throw new ModelException(e);
}
}
/**
@@ -136,7 +148,12 @@ public class Facade implements IFacade {
*/
@Override
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
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))
*/
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
* |! isValidName(name)
*/
public void setName(String name) {
public void setName(String name) throws IllegalNameException {
int validName = isValidName(name);
if (validName != -1)
@@ -501,7 +502,7 @@ public class Worm {
* then the current value of action point
* |NumberSteps < 0 || cost > this.actionPoints
*/
public void move(int numberSteps) {
public void move(int numberSteps) throws IllegalArgumentException {
if (numberSteps < 0)
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)
* @post the current action actionPoints schould be 0 after a jump
* |this.actionPoints = 0
* @throws IllegalStateException()
* @throws IllegalStateException
* if the current action actionPoints is equal to 0 or the orientation is more then
* pi the worm can not jump
* |this.actionPoints == 0 || this.orientation < Math.PI
*/
public void jump() {
public void jump() throws IllegalStateException {
if (!canJump())
throw new IllegalStateException();
@@ -593,8 +594,6 @@ public class Worm {
* |result == this.actionPoints > 0 && this.orientation < Math.PI
*/
private boolean canJump() {
System.out.println(this.actionPoints);
System.out.println(this.orientation);
return this.actionPoints > 0 && this.orientation < PI;
}

View File

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

View File

@@ -1,14 +1,12 @@
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.model.Worm;
import worms.util.ModelException;
import static org.junit.Assert.assertEquals;
public class PartialFacadeTest {
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.Test;
import static org.junit.jupiter.api.Assertions.*;
import worms.util.Tuple;
class WormTest {
private Worm worm;
@BeforeEach
void setUp() {
worm = new Worm(Tuple.create(0.0, 0.0), 0, "Test", 1);
}
@Test
void getLocation() {
assertEquals(Tuple.create(0.0, 0.0), worm.getLocation());
}
@Test
void getOrientation() {
assertEquals(0, worm.getOrientation());
}
@Test
void getRadius() {
assertEquals(1, worm.getRadius());
}
@Test