From 652a34e309a1b241ffb79a4fe86cd66535eef531 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Thu, 8 Mar 2018 17:50:53 +0100 Subject: [PATCH] improved test --- OGP1718-Worms/src/worms/facade/Facade.java | 32 ++++++++++++++++--- OGP1718-Worms/src/worms/model/Worm.java | 15 ++++----- OGP1718-Worms/src/worms/util/Tuple.java | 10 +++--- .../tests/worms/model/PartialFacadeTest.java | 6 ++-- OGP1718-Worms/tests/worms/model/WormTest.java | 10 +++++- 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 2e49986..82b59f6 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -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); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 0a92d51..c107c32 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -170,7 +170,8 @@ public class Worm { * |result == (location != null) && (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2)) */ private boolean isValidLocation(Tuplelocation) { - 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) @@ -479,7 +480,7 @@ public class Worm { * * @param numberSteps * the number of steps the worm should take - * + * * @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 * 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 * |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; } diff --git a/OGP1718-Worms/src/worms/util/Tuple.java b/OGP1718-Worms/src/worms/util/Tuple.java index 3646ebb..1eaf84c 100644 --- a/OGP1718-Worms/src/worms/util/Tuple.java +++ b/OGP1718-Worms/src/worms/util/Tuple.java @@ -31,12 +31,12 @@ public class Tuple { * @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 { } @Immutable - public static Tuple create(double[] arr) { - if (arr == null || arr.length > 2) + public static Tuple create(double[] arr) throws IllegalArgumentException { + if (arr == null || arr.length != 2) throw new IllegalArgumentException("Invalid parameter arr"); return new Tuple<>(arr[0], arr[1]); } diff --git a/OGP1718-Worms/tests/worms/model/PartialFacadeTest.java b/OGP1718-Worms/tests/worms/model/PartialFacadeTest.java index f0f2790..56f1df2 100644 --- a/OGP1718-Worms/tests/worms/model/PartialFacadeTest.java +++ b/OGP1718-Worms/tests/worms/model/PartialFacadeTest.java @@ -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; diff --git a/OGP1718-Worms/tests/worms/model/WormTest.java b/OGP1718-Worms/tests/worms/model/WormTest.java index 72fb353..a602556 100644 --- a/OGP1718-Worms/tests/worms/model/WormTest.java +++ b/OGP1718-Worms/tests/worms/model/WormTest.java @@ -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