team02-AngryNerds-projekt1-.../src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java

160 lines
6.0 KiB
Java

package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
import org.junit.jupiter.api.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
public class TrackTest {
Track trackObj;
@Nested
@DisplayName("Positiv Test Cases")
class positivClass {
@BeforeEach
void setup() {
File file = new File(".\\tracks\\challenge.txt");
try {
trackObj = new Track(file);
} catch (FileNotFoundException | InvalidTrackFormatException e) {
e.printStackTrace();
Assertions.fail();
}
}
@Test
@DisplayName("Create correct amount of Car instance")
void checkCars() {
Assertions.assertEquals(2, trackObj.getCarCount());
}
@Test
@DisplayName("Create Car instance with correct Symbols / Id")
void checkCarId() {
Assertions.assertEquals('a', trackObj.getCarId(0));
Assertions.assertEquals('b', trackObj.getCarId(1));
}
@Test
@DisplayName("Check getSpaceTyp")
void getSpaceTyp() {
Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT, trackObj.getSpaceType(new PositionVector(22, 24)));
}
@Test
@DisplayName("Find FinishLine")
void findFinish() {
List<PositionVector> expected = new ArrayList<>();
expected.add(new PositionVector(22, 22));
expected.add(new PositionVector(22, 23));
expected.add(new PositionVector(22, 24));
Assertions.assertEquals(expected, trackObj.getFinishLine());
}
@Test
@DisplayName("Converts Trackfile correctly to List<String>")
void checkTrack() {
Track trackObj;
try {
trackObj = new Track(new File(".\\tracks\\oval-anticlock-right.txt"));
List<String> track = new ArrayList<>();
track.add("##################################################");
track.add("##################################################");
track.add("############## #############");
track.add("########## ##########");
track.add("####### #######");
track.add("###### ################# ######");
track.add("##### ################### #####");
track.add("##### ################### #####");
track.add("###### ################# ######");
track.add("####### > a #######");
track.add("########## > ##########");
track.add("############## > b ##############");
track.add("##################################################");
track.add("##################################################");
Assertions.assertLinesMatch(track, trackObj.getTrack());
} catch (FileNotFoundException | InvalidTrackFormatException e) {
e.printStackTrace();
}
}
@Test
@DisplayName("Make Car move down on track")
void makeCarMoveDown() {
PositionVector beforeMove = trackObj.getCarPos(0);
trackObj.getCar(0).accelerate(PositionVector.Direction.DOWN);
trackObj.moveCar(0);
PositionVector afterMove = trackObj.getCarPos(0);
Assertions.assertEquals(beforeMove.getY() + 1, afterMove.getY());
Assertions.assertEquals(beforeMove.getX(), afterMove.getX());
}
@Test
@DisplayName("Make Car move with (0,0) acceleration on track")
void makeCarStay() {
PositionVector beforeMove = trackObj.getCarPos(0);
trackObj.moveCar(0);
PositionVector afterMove = trackObj.getCarPos(0);
Assertions.assertEquals(beforeMove.getY(), afterMove.getY());
Assertions.assertEquals(beforeMove.getX(), afterMove.getX());
}
@Test
@DisplayName("Will Car Crash")
void willCarCrash() {
//Car will Crash
Assertions.assertTrue(trackObj.willCrashAtPosition(0, new PositionVector(25, 21)));
//Car will not Crash and is on track
Assertions.assertFalse(trackObj.willCrashAtPosition(0, new PositionVector(7, 22)));
//Car will not Crash and is on finishLine
Assertions.assertFalse(trackObj.willCrashAtPosition(0, trackObj.getFinishLine().get(0)));
}
@Test
@DisplayName("Make Car Crash")
void makeCarCrash() {
trackObj.carDoesCrash(0, new PositionVector(6, 22));
Assertions.assertEquals(Track.CRASH_INDICATOR, trackObj.getTrack().get(22).charAt(6));
Assertions.assertTrue(trackObj.getCar(0).isCrashed());
}
}
@Nested
@DisplayName("Negative TestCase")
class negativeClass {
File file;
@BeforeEach
void setup() {
file = new File(".\\tracks\\challenge.txt");
try {
trackObj = new Track(file);
} catch (InvalidTrackFormatException | FileNotFoundException e) {
System.err.println("Error in Test compareTrack" + e.getMessage());
}
}
@Test
@DisplayName("Throw error if File not found")
void canReadFile() {
file = new File(".\\tracks\\NotExisting.txt");
Assertions.assertThrows(FileNotFoundException.class, () -> new Track(file));
}
@Test
@DisplayName("Throw error if File is invalid")
void invalidTrackFile() {
File testfile = new File(".\\src\\test\\InvalidTracks\\sameCar.txt");
Assertions.assertThrows(InvalidTrackFormatException.class, () -> new Track(testfile));
}
}
}