From 40cd4f7b72e59e83fbcf60f9d7ad2759d6015fab Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Sun, 6 Mar 2022 18:57:01 +0100 Subject: [PATCH] Added Javadoc and comments in CarTest.java --- .../java/ch/zhaw/pm2/racetrack/CarTest.java | 91 ++++++++++++------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java index 5f0c462..3468cbf 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java @@ -11,38 +11,62 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; +/** + * Tests for Class Car + */ class CarTest { + Car car; + + // Default coordinates for tests int DEFAULT_X = 10; int DEFAULT_Y = 10; + /** + * Create a new Car Object and set Position to a defined Default Position + */ @BeforeEach void setUp() { car = new Car('a', new PositionVector(DEFAULT_X, DEFAULT_Y)); } + /** + * Method to check nextPosition with coordinates as int + * + * @param x the expected value for x coordinate + * @param y the expected value for y coordinate + */ + void CheckNextPosition(int x, int y) { + assertEquals(new PositionVector(x, y), car.nextPosition()); + } + /** + * - checks if the position of the car can be set and saved correctly with valid positions. + * - checks if an exception is throwed and position keeps unchanged if invalid coordinates are entered. + */ @Test void setPosition() { CheckNextPosition(DEFAULT_X, DEFAULT_Y); + // List of valid Positions List validPositions = new ArrayList<>(); validPositions.add(new PositionVector(20, 20)); validPositions.add(new PositionVector(0, 0)); validPositions.add(new PositionVector(20, 0)); validPositions.add(new PositionVector(0, 20)); - for(PositionVector positionVector : validPositions) { + for (PositionVector positionVector : validPositions) { car.setPosition(positionVector); assertEquals(positionVector, car.nextPosition()); } + // List of invalid positions. List invalidPositions = new ArrayList<>(); invalidPositions.add(new PositionVector(0, -20)); invalidPositions.add(new PositionVector(-20, 0)); invalidPositions.add(new PositionVector(-20, -20)); - for(PositionVector positionVector : invalidPositions) { + for (PositionVector positionVector : invalidPositions) { boolean exception = false; try { car.setPosition(positionVector); @@ -51,62 +75,73 @@ class CarTest { } assertTrue(exception); + // position should keep unchanged CheckNextPosition(DEFAULT_X, DEFAULT_Y); } } - + /** + * Checks if the methods accelerate and move are working correctly with acceleration in all directions. + * Checks also if velocity is calculated correctly if method accelerate is called a second time. + */ @Test void movement() { + // add all possible directions in a List + List directions = Arrays.asList(PositionVector.Direction.values()); + + //position shouldn't be changed because velocity should be 0. car.move(); CheckNextPosition(DEFAULT_X, DEFAULT_Y); - List directions = Arrays.asList(PositionVector.Direction.values()); - - for (PositionVector.Direction direction1 : directions){ + for (PositionVector.Direction direction1 : directions) { + //create a new instance of Car with default coordinates and velocity 0. setUp(); - int nextPosX = DEFAULT_X; - int nextPosY = DEFAULT_Y; + + //variables to save the actual expected result of method nextPosition + int ExpectedNextPosX = DEFAULT_X; + int ExpectedNextPosY = DEFAULT_Y; car.accelerate(direction1); - nextPosX += direction1.vector.getX(); - nextPosY += direction1.vector.getY(); - CheckNextPosition(nextPosX, nextPosY); + ExpectedNextPosX += direction1.vector.getX(); + ExpectedNextPosY += direction1.vector.getY(); + CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY); car.move(); - nextPosX += direction1.vector.getX(); - nextPosY += direction1.vector.getY(); - CheckNextPosition(nextPosX, nextPosY); + ExpectedNextPosX += direction1.vector.getX(); + ExpectedNextPosY += direction1.vector.getY(); + CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY); for (PositionVector.Direction direction2 : directions) { car.accelerate(direction2); - nextPosX += direction2.vector.getX(); - nextPosY += direction2.vector.getY(); - CheckNextPosition(nextPosX, nextPosY); + ExpectedNextPosX += direction2.vector.getX(); + ExpectedNextPosY += direction2.vector.getY(); + CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY); car.move(); - nextPosX += direction1.vector.getX(); - nextPosY += direction1.vector.getY(); - nextPosX += direction2.vector.getX(); - nextPosY += direction2.vector.getY(); - CheckNextPosition(nextPosX, nextPosY); - + ExpectedNextPosX += (direction1.vector.getX() + direction2.vector.getX()); + ExpectedNextPosY += (direction1.vector.getY() + direction2.vector.getY()); + CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY); } - } } + /** + * test for methods crash and isCrashed. checks if state crashed is set and returned correctly. + */ @Test void crash() { assertFalse(car.isCrashed()); car.crash(); assertTrue(car.isCrashed()); - } + /** + * test for methods setMoveStrategy. Checks if the MoveStrategy Object is saved and returned correctly + * with all Types of MoveStrategy. + */ @Test void MoveStrategy() { MoveStrategy moveStrategy; @@ -127,10 +162,4 @@ class CarTest { car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); } - - - - void CheckNextPosition(int x, int y) { - assertEquals(new PositionVector(x, y), car.nextPosition()); - } }