Game test #29

Merged
fassband merged 4 commits from GameTest into main 2022-03-23 09:19:00 +01:00
2 changed files with 153 additions and 25 deletions

View File

@ -222,10 +222,9 @@ public class Game implements GameSpecification {
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
if (direction == null) { if (direction == null) {
track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); //TODO: Entfernen? direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
}else {
doCarTurn(direction);
} }
doCarTurn(direction);
switchToNextActiveCar(); switchToNextActiveCar();
} }
userInterface.printTrack(track); userInterface.printTrack(track);

View File

@ -1,59 +1,74 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.DisplayName;
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.File;
import java.util.List;
import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; import static ch.zhaw.pm2.racetrack.Game.NO_WINNER;
import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*;
/**
* Test for Class Game
*/
class GameTest { class GameTest {
private UserInterface userInterface; private UserInterface userInterface;
private Game game; private Game game;
private Track track; private Track track;
private String TRACK_FILE_PATH = ".\\tracks\\challenge.txt";
private int CAR_INDEX_ONE = 0;
private int CAR_INDEX_TWO = 1;
/**
* This nested Class tests if the game gets initiatet correctly
*/
@Nested @Nested
@DisplayName("Test correct Setup") @DisplayName("Test correct Setup")
class Setup { class Setup {
@BeforeEach @BeforeEach
void setup() { void setup() {
userInterface = new UserInterface("Test"); userInterface = new UserInterface("Test");
game = new Game(userInterface); game = new Game(userInterface);
track = game.selectTrack(new File(".\\tracks\\challenge.txt")); track = game.selectTrack(new File(TRACK_FILE_PATH));
game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE)));
game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO)));
} }
@Test @Test
void getCurrentCarIndex() { void getCurrentCarIndex() {
Assertions.assertEquals(0,game.getCurrentCarIndex()); Assertions.assertEquals(CAR_INDEX_ONE, game.getCurrentCarIndex());
game.switchToNextActiveCar(); game.switchToNextActiveCar();
Assertions.assertEquals(1,game.getCurrentCarIndex()); Assertions.assertEquals(CAR_INDEX_TWO, game.getCurrentCarIndex());
} }
@Test @Test
void getCarId() { void getCarId() {
Assertions.assertEquals('a',game.getCarId(0)); Assertions.assertEquals('a', game.getCarId(0));
Assertions.assertEquals('b',game.getCarId(1)); Assertions.assertEquals('b', game.getCarId(1));
} }
@Test @Test
void getCarPosition() { void getCarPosition() {
Assertions.assertEquals(new PositionVector(24,22),game.getCarPosition(0)); Assertions.assertEquals(new PositionVector(24, 22), game.getCarPosition(0));
Assertions.assertEquals(new PositionVector(24,24),game.getCarPosition(1)); Assertions.assertEquals(new PositionVector(24, 24), game.getCarPosition(1));
} }
@Test @Test
void getCarVelocity() { void getCarVelocity() {
Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(0)); Assertions.assertEquals(new PositionVector(0, 0), game.getCarVelocity(0));
Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(1)); Assertions.assertEquals(new PositionVector(0, 0), game.getCarVelocity(1));
} }
@Test @Test
void getWinner() { void getWinner() {
Assertions.assertEquals(NO_WINNER,game.getWinner()); Assertions.assertEquals(NO_WINNER, game.getWinner());
} }
@Test @Test
@ -67,24 +82,28 @@ class GameTest {
} }
} }
/**
* This nested Class makes basic manipulation after Game init.
*/
@Nested @Nested
@DisplayName("Basic manipulation") @DisplayName("Basic manipulation")
class manipulation { class Manipulation {
@BeforeEach @BeforeEach
void setup() { void setup() {
userInterface = new UserInterface("Test"); userInterface = new UserInterface("Test");
game = new Game(userInterface); game = new Game(userInterface);
track = game.selectTrack(new File(".\\tracks\\challenge.txt")); track = game.selectTrack(new File(TRACK_FILE_PATH));
game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE)));
game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO)));
} }
@Test @Test
void carTurnCorrect() { void carTurnCorrect() {
try { try {
game.doCarTurn(PositionVector.Direction.RIGHT); game.doCarTurn(RIGHT);
Assertions.assertEquals(new PositionVector(1,0),game.getCarVelocity(0)); Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
} catch (PositionVectorNotValid positionVectorNotValid) { } catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace(); positionVectorNotValid.printStackTrace();
} }
@ -99,6 +118,116 @@ class GameTest {
positionVectorNotValid.printStackTrace(); positionVectorNotValid.printStackTrace();
} }
} }
}
/**
* This nested Class tests a Playtrough. And implements a UserInterface interagtion to pretend a real player
*/
@Nested
@DisplayName("Playtrough")
class Play {
private Game game;
@Test
void winner() {
game = new Game(new interFace("Test",new Integer[]{0,2,1},new PositionVector.Direction[]{RIGHT,
RIGHT,
RIGHT,
NONE,
NONE,
NONE,
NONE,
UP,
LEFT,
LEFT,
LEFT,
LEFT,
UP_LEFT,
NONE,
RIGHT,
RIGHT,
RIGHT,
NONE,
LEFT,
DOWN_LEFT,
DOWN_LEFT,
LEFT,
LEFT,
NONE,
RIGHT,
NONE,
DOWN,
DOWN,
RIGHT,
NONE,
RIGHT,
DOWN,
NONE,
UP_RIGHT,
RIGHT,
UP_RIGHT,
UP_RIGHT,
RIGHT,
RIGHT}));
try {
game.initPhase();
Assertions.assertEquals("a",game.gamePhase());
} catch (InvalidTrackFormatException | PositionVectorNotValid e) {
e.printStackTrace();
}
}
@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();
}
}
}
private class interFace extends UserInterface {
private final PositionVector.Direction[] directions;
private final Integer[] instructions;
private int pointerDir,pointerInstruction;
public interFace(String welcometxt, Integer[] instructions, PositionVector.Direction[] directions) {
super(welcometxt);
pointerDir = -1;
pointerInstruction = -1;
this.instructions = instructions;
this.directions = directions;
}
@Override
public int selectOption(String text, List<String> options) {
pointerInstruction++;
return instructions[pointerInstruction];
}
public void printInformation(String text) {
}
public void printTrack(Track track) {
}
public void quit(String text) {
}
public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) {
pointerDir += 1;
if(pointerDir < directions.length) {
return directions[pointerDir];
}
return NONE;
}
} }
} }