Fixed Error in CarTest.java #20
|
@ -11,21 +11,44 @@ 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<PositionVector> validPositions = new ArrayList<>();
|
||||
validPositions.add(new PositionVector(20, 20));
|
||||
validPositions.add(new PositionVector(0, 0));
|
||||
|
@ -37,6 +60,7 @@ class CarTest {
|
|||
assertEquals(positionVector, car.nextPosition());
|
||||
}
|
||||
|
||||
// List of invalid positions.
|
||||
List<PositionVector> invalidPositions = new ArrayList<>();
|
||||
invalidPositions.add(new PositionVector(0, -20));
|
||||
invalidPositions.add(new PositionVector(-20, 0));
|
||||
|
@ -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<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values());
|
||||
|
||||
//position shouldn't be changed because velocity should be 0.
|
||||
car.move();
|
||||
CheckNextPosition(DEFAULT_X, DEFAULT_Y);
|
||||
|
||||
List<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue