changes in Tests car.CarTest

setPosition, movement, crash, moveStrategy
This commit is contained in:
romanschenk37 2022-03-06 17:06:42 +01:00
parent 381ea44a9a
commit 972f011a08
1 changed files with 100 additions and 24 deletions

View File

@ -1,60 +1,136 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
import org.junit.jupiter.api.AfterEach; import ch.zhaw.pm2.racetrack.strategy.*;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class CarTest { class CarTest {
Car car; Car car;
int DEFAULT_X = 10;
int DEFAULT_Y = 10;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
car = new Car('a', new PositionVector(10, 10)); car = new Car('a', new PositionVector(DEFAULT_X, DEFAULT_Y));
} }
@AfterEach
void tearDown() {
}
@Test @Test
void setPosition() { void setPosition() {
car.setPosition(new PositionVector(20, 20)); CheckNextPosition(DEFAULT_X, DEFAULT_Y);
checkPosition(20, 20);
List<PositionVector> 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) {
car.setPosition(positionVector);
assertEquals(positionVector, car.nextPosition());
} }
@Test List<PositionVector> invalidPositions = new ArrayList<>();
void nextPosition() { invalidPositions.add(new PositionVector(0, -20));
checkPosition(10, 10); invalidPositions.add(new PositionVector(-20, 0));
invalidPositions.add(new PositionVector(-20, -20));
for(PositionVector positionVector : invalidPositions) {
boolean exception = false;
try {
car.setPosition(positionVector);
} catch (IllegalArgumentException e) {
exception = true;
}
assertTrue(exception);
CheckNextPosition(0, 0);
}
} }
@Test
void accelerate() {
}
@Test @Test
void move() { void movement() {
car.move();
CheckNextPosition(DEFAULT_X, DEFAULT_Y);
List<PositionVector.Direction> directions = Arrays.asList(PositionVector.Direction.values());
for (PositionVector.Direction direction1 : directions){
setUp();
int nextPosX = DEFAULT_X;
int nextPosY = DEFAULT_Y;
car.accelerate(direction1);
nextPosX += direction1.vector.getX();
nextPosY += direction1.vector.getY();
CheckNextPosition(nextPosX, nextPosY);
car.move();
nextPosX += direction1.vector.getX();
nextPosY += direction1.vector.getY();
CheckNextPosition(nextPosX, nextPosY);
for (PositionVector.Direction direction2 : directions) {
car.accelerate(direction2);
nextPosX += direction2.vector.getX();
nextPosY += direction2.vector.getY();
CheckNextPosition(nextPosX, nextPosY);
car.move();
nextPosX += direction1.vector.getX();
nextPosY += direction1.vector.getY();
nextPosX += direction2.vector.getX();
nextPosY += direction2.vector.getY();
CheckNextPosition(nextPosX, nextPosY);
} }
}
}
@Test @Test
void crash() { void crash() {
assertFalse(car.isCrashed());
car.crash();
assertTrue(car.isCrashed());
} }
@Test @Test
void isCrashed() { void MoveStrategy() {
MoveStrategy moveStrategy;
moveStrategy = new DoNotMoveStrategy();
car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy());
moveStrategy = new MoveListStrategy();
car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy());
moveStrategy = new PathFollowerMoveStrategy();
car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy());
moveStrategy = new UserMoveStrategy();
car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy());
} }
@Test
void setMoveStrategy() {
}
@Test
void getMoveStrategy() {
}
void checkPosition(int x, int y) { void CheckNextPosition(int x, int y) {
assertTrue(car.nextPosition().equals(new PositionVector(x, y))); assertEquals(new PositionVector(x, y), car.nextPosition());
} }
} }