refactoring several classes

This commit is contained in:
Andrin Fassbind
2022-03-24 16:50:18 +01:00
parent bc0dd52360
commit a143a9c7d7
10 changed files with 123 additions and 103 deletions
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import static ch.zhaw.pm2.racetrack.Game.NO_WINNER;
import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*;
@@ -35,7 +36,12 @@ class GameTest {
void setup() {
userInterface = new UserInterface("Test");
game = new Game(userInterface);
track = game.selectTrack(new File(TRACK_FILE_PATH));
try {
track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) {
e.printStackTrace();
Assertions.fail();
}
game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE)));
game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO)));
@@ -93,7 +99,12 @@ class GameTest {
void setup() {
userInterface = new UserInterface("Test");
game = new Game(userInterface);
track = game.selectTrack(new File(TRACK_FILE_PATH));
try {
track = game.selectTrack(new File(TRACK_FILE_PATH));
} catch (InvalidTrackFormatException | FileNotFoundException e) {
e.printStackTrace();
Assertions.fail();
}
game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE)));
game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO)));
@@ -104,8 +115,8 @@ class GameTest {
try {
game.doCarTurn(RIGHT);
Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
}
}
@@ -114,8 +125,8 @@ class GameTest {
try {
game.doCarTurn(PositionVector.Direction.UP);
Assertions.assertTrue(game.onlyOneCarLeft());
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
}
}
}
@@ -169,23 +180,15 @@ class GameTest {
UP_RIGHT,
RIGHT,
RIGHT}));
try {
game.initPhase();
Assertions.assertEquals("a",game.gamePhase());
} catch (InvalidTrackFormatException | PositionVectorNotValid e) {
e.printStackTrace();
}
game.initPhase();
Assertions.assertEquals("a",game.gamePhase());
}
@Test
void crashA() {
game = new Game(new interFace("Test",new Integer[]{0,2,2},new PositionVector.Direction[]{UP}));
try {
game.initPhase();
Assertions.assertEquals("b",game.gamePhase());
} catch (InvalidTrackFormatException | PositionVectorNotValid e) {
e.printStackTrace();
}
game.initPhase();
Assertions.assertEquals("b",game.gamePhase());
}
}
@@ -21,10 +21,11 @@ public class TrackTest {
File file = new File(".\\tracks\\challenge.txt");
try {
trackObj = new Track(file);
} catch (Exception | PositionVectorNotValid e) {
System.err.println("Error in Test compareTrack" + e.getMessage());
} catch (FileNotFoundException | InvalidTrackFormatException e) {
e.printStackTrace();
Assertions.fail();
}
}
@Test
@@ -79,7 +80,7 @@ public class TrackTest {
track.add("##################################################");
track.add("##################################################");
Assertions.assertLinesMatch(track, trackObj.getTrack());
} catch (FileNotFoundException | InvalidTrackFormatException | PositionVectorNotValid e) {
} catch (FileNotFoundException | InvalidTrackFormatException e) {
e.printStackTrace();
}
}
@@ -115,8 +116,8 @@ public class TrackTest {
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)));
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
Assertions.fail("Test should not throw error");
}
}
@@ -126,8 +127,8 @@ public class TrackTest {
void makeCarCrash() {
try {
trackObj.carDoesCrash(0, new PositionVector(6, 22));
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
Assertions.fail("Test should not throw exception");
}
Assertions.assertEquals(Track.CRASH_INDICATOR, trackObj.getTrack().get(22).charAt(6));
@@ -146,7 +147,7 @@ public class TrackTest {
try {
trackObj = new Track(file);
} catch (Exception | PositionVectorNotValid e) {
} catch (InvalidTrackFormatException | FileNotFoundException e) {
System.err.println("Error in Test compareTrack" + e.getMessage());
}
}
@@ -168,8 +169,8 @@ public class TrackTest {
@Test
@DisplayName("Invalid Position Vector used")
void invalidPositionVector() {
Assertions.assertThrows(PositionVectorNotValid.class, () -> trackObj.willCrashAtPosition(0, new PositionVector(100, 200)));
Assertions.assertThrows(PositionVectorNotValid.class, () -> trackObj.carDoesCrash(1,new PositionVector(200,100)));
Assertions.assertThrows(PositionVectorNotValidException.class, () -> trackObj.willCrashAtPosition(0, new PositionVector(100, 200)));
Assertions.assertThrows(PositionVectorNotValidException.class, () -> trackObj.carDoesCrash(1,new PositionVector(200,100)));
}
}