improved test
This commit is contained in:
@@ -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,8 +20,15 @@ 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 {
|
||||||
|
|
||||||
|
try {
|
||||||
return new Worm(Tuple.create(location), direction, name, radius);
|
return new Worm(Tuple.create(location), direction, name, radius);
|
||||||
}
|
}
|
||||||
|
catch(IllegalArgumentException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves the given worm by the given number of steps.
|
* Moves the given worm by the given number of steps.
|
||||||
@@ -30,9 +38,13 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns the given worm by the given angle.
|
* Turns the given worm by the given angle.
|
||||||
@@ -136,8 +148,13 @@ 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 {
|
||||||
|
try {
|
||||||
worm.setRadius(newRadius);
|
worm.setRadius(newRadius);
|
||||||
}
|
}
|
||||||
|
catch(IllegalArgumentException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current number of action points of the given worm.
|
* Returns the current number of action points of the given worm.
|
||||||
@@ -189,8 +206,13 @@ 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 {
|
||||||
|
try {
|
||||||
worm.setName(newName);
|
worm.setName(newName);
|
||||||
}
|
}
|
||||||
|
catch(IllegalNameException e) {
|
||||||
|
throw new ModelException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the mass of the given worm.
|
* Returns the mass of the given worm.
|
||||||
|
@@ -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)
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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]);
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user