package cat.hack3.codingtests.marsrover; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.stream.IntStream; import static cat.hack3.codingtests.marsrover.Direction.*; import static org.testng.Assert.assertEquals; public class MarsRoverTest { private MarsRover rover; @BeforeMethod public void setUp() { int mapWidth = 10; int mapHeight = 10; int latitudeStartingPoint = 2; int longitudeStartingPoint = 3; var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates); rover = new MarsRover(marsMap, SOUTH); } @Test public void stepForward() { Direction originalDirection = rover.getCurrentDirection(); Coordinates oneStepForward = rover.moveForward(); assertEquals(oneStepForward, Coordinates.of(3, 3)); assertEquals(rover.getCurrentDirection(), originalDirection); } @Test public void stepBackwards() { Direction originalDirection = rover.getCurrentDirection(); Coordinates currentPosition = rover.moveBackwards(); assertEquals(currentPosition, Coordinates.of(1, 3)); assertEquals(rover.getCurrentDirection(), originalDirection); } @Test public void turnLef() { assertEquals(rover.turnLeft(), EAST); assertEquals(rover.turnLeft(), NORTH); assertEquals(rover.turnLeft(), WEST); assertEquals(rover.turnLeft(), SOUTH); assertEquals(rover.turnLeft(), EAST); } @Test public void turnRight() { assertEquals(rover.turnRight(), WEST); assertEquals(rover.turnRight(), NORTH); assertEquals(rover.turnRight(), EAST); assertEquals(rover.turnRight(), SOUTH); assertEquals(rover.turnRight(), WEST); } @Test public void loopTheWorldInLatitudeMovingForward() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); Direction originalDirection = rover.getCurrentDirection(); repeatAction(10, () -> rover.moveForward()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), originalDirection); } @Test public void loopTheWorldInLatitudeMovingBackwards() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); Direction originalDirection = rover.getCurrentDirection(); repeatAction(10, () -> rover.moveBackwards()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), originalDirection); } @Test public void loopTheWorldInLongitudeMovingForward() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); rover.turnLeft(); repeatAction(10, () -> rover.moveForward()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), EAST); } @Test public void loopTheWorldInLongitudeMovingBackwards() { Coordinates originalCoordinates = rover.getCurrentCoordinates(); rover.turnLeft(); repeatAction(10, () -> rover.moveBackwards()); assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentDirection(), EAST); } @Test public void crazyTour() { rover.moveForward(); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3)); assertEquals(rover.getCurrentDirection(), SOUTH); rover.turnLeft(); repeatAction(23, () -> rover.moveForward()); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6)); assertEquals(rover.getCurrentDirection(), EAST); repeatAction(3, () -> rover.turnRight()); repeatAction(9, () -> rover.moveForward()); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6)); assertEquals(rover.getCurrentDirection(), NORTH); rover.moveBackwards(); rover.turnRight(); rover.moveForward(); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7)); assertEquals(rover.getCurrentDirection(), EAST); } private void repeatAction(int times, Runnable actionToRepeat) { IntStream.rangeClosed(1, times) .forEach(i -> actionToRepeat.run()); } }