Added Javadoc and comments in CarTest.java

This commit is contained in:
romanschenk37 2022-03-06 18:57:01 +01:00
parent 8086111772
commit 40cd4f7b72
1 changed files with 60 additions and 31 deletions

View File

@ -11,38 +11,62 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for Class Car
*/
class CarTest { class CarTest {
Car car; Car car;
// Default coordinates for tests
int DEFAULT_X = 10; int DEFAULT_X = 10;
int DEFAULT_Y = 10; int DEFAULT_Y = 10;
/**
* Create a new Car Object and set Position to a defined Default Position
*/
@BeforeEach @BeforeEach
void setUp() { void setUp() {
car = new Car('a', new PositionVector(DEFAULT_X, DEFAULT_Y)); 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 @Test
void setPosition() { void setPosition() {
CheckNextPosition(DEFAULT_X, DEFAULT_Y); CheckNextPosition(DEFAULT_X, DEFAULT_Y);
// List of valid Positions
List<PositionVector> validPositions = new ArrayList<>(); List<PositionVector> validPositions = new ArrayList<>();
validPositions.add(new PositionVector(20, 20)); validPositions.add(new PositionVector(20, 20));
validPositions.add(new PositionVector(0, 0)); validPositions.add(new PositionVector(0, 0));
validPositions.add(new PositionVector(20, 0)); validPositions.add(new PositionVector(20, 0));
validPositions.add(new PositionVector(0, 20)); validPositions.add(new PositionVector(0, 20));
for(PositionVector positionVector : validPositions) { for (PositionVector positionVector : validPositions) {
car.setPosition(positionVector); car.setPosition(positionVector);
assertEquals(positionVector, car.nextPosition()); assertEquals(positionVector, car.nextPosition());
} }
// List of invalid positions.
List<PositionVector> invalidPositions = new ArrayList<>(); List<PositionVector> invalidPositions = new ArrayList<>();
invalidPositions.add(new PositionVector(0, -20)); invalidPositions.add(new PositionVector(0, -20));
invalidPositions.add(new PositionVector(-20, 0)); invalidPositions.add(new PositionVector(-20, 0));
invalidPositions.add(new PositionVector(-20, -20)); invalidPositions.add(new PositionVector(-20, -20));
for(PositionVector positionVector : invalidPositions) { for (PositionVector positionVector : invalidPositions) {
boolean exception = false; boolean exception = false;
try { try {
car.setPosition(positionVector); car.setPosition(positionVector);
@ -51,62 +75,73 @@ class CarTest {
} }
assertTrue(exception); assertTrue(exception);
// position should keep unchanged
CheckNextPosition(DEFAULT_X, DEFAULT_Y); 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 @Test
void movement() { void movement() {
// add all possible directions in a List
List<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values());
//position shouldn't be changed because velocity should be 0.
car.move(); car.move();
CheckNextPosition(DEFAULT_X, DEFAULT_Y); CheckNextPosition(DEFAULT_X, DEFAULT_Y);
List<PositionVector.Direction> 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(); 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); car.accelerate(direction1);
nextPosX += direction1.vector.getX(); ExpectedNextPosX += direction1.vector.getX();
nextPosY += direction1.vector.getY(); ExpectedNextPosY += direction1.vector.getY();
CheckNextPosition(nextPosX, nextPosY); CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY);
car.move(); car.move();
nextPosX += direction1.vector.getX(); ExpectedNextPosX += direction1.vector.getX();
nextPosY += direction1.vector.getY(); ExpectedNextPosY += direction1.vector.getY();
CheckNextPosition(nextPosX, nextPosY); CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY);
for (PositionVector.Direction direction2 : directions) { for (PositionVector.Direction direction2 : directions) {
car.accelerate(direction2); car.accelerate(direction2);
nextPosX += direction2.vector.getX(); ExpectedNextPosX += direction2.vector.getX();
nextPosY += direction2.vector.getY(); ExpectedNextPosY += direction2.vector.getY();
CheckNextPosition(nextPosX, nextPosY); CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY);
car.move(); car.move();
nextPosX += direction1.vector.getX(); ExpectedNextPosX += (direction1.vector.getX() + direction2.vector.getX());
nextPosY += direction1.vector.getY(); ExpectedNextPosY += (direction1.vector.getY() + direction2.vector.getY());
nextPosX += direction2.vector.getX(); CheckNextPosition(ExpectedNextPosX, ExpectedNextPosY);
nextPosY += direction2.vector.getY();
CheckNextPosition(nextPosX, nextPosY);
} }
} }
} }
/**
* test for methods crash and isCrashed. checks if state crashed is set and returned correctly.
*/
@Test @Test
void crash() { void crash() {
assertFalse(car.isCrashed()); assertFalse(car.isCrashed());
car.crash(); car.crash();
assertTrue(car.isCrashed()); assertTrue(car.isCrashed());
} }
/**
* test for methods setMoveStrategy. Checks if the MoveStrategy Object is saved and returned correctly
* with all Types of MoveStrategy.
*/
@Test @Test
void MoveStrategy() { void MoveStrategy() {
MoveStrategy moveStrategy; MoveStrategy moveStrategy;
@ -127,10 +162,4 @@ class CarTest {
car.setMoveStrategy(moveStrategy); car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy()); assertEquals(moveStrategy, car.getMoveStrategy());
} }
void CheckNextPosition(int x, int y) {
assertEquals(new PositionVector(x, y), car.nextPosition());
}
} }