1
0
Fork 0

final test mix

This commit is contained in:
Xavier Fontanet 2024-06-20 23:17:50 +02:00
parent ac72870894
commit 303b1c6df5
1 changed files with 29 additions and 7 deletions

View File

@ -3,7 +3,6 @@ package cat.hack3.codingtests.marsrover;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
import static cat.hack3.codingtests.marsrover.Direction.*;
@ -67,7 +66,7 @@ public class MarsRoverTest {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection();
repeatAction(10, i -> rover.moveForward());
repeatAction(10, () -> rover.moveForward());
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection);
@ -78,7 +77,7 @@ public class MarsRoverTest {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection();
repeatAction(10, i -> rover.moveBackwards());
repeatAction(10, () -> rover.moveBackwards());
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection);
@ -89,7 +88,7 @@ public class MarsRoverTest {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
rover.turnLeft();
repeatAction(10, i -> rover.moveForward());
repeatAction(10, () -> rover.moveForward());
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), EAST);
@ -100,14 +99,37 @@ public class MarsRoverTest {
Coordinates originalCoordinates = rover.getCurrentCoordinates();
rover.turnLeft();
repeatAction(10, i -> rover.moveBackwards());
repeatAction(10, () -> rover.moveBackwards());
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), EAST);
}
private void repeatAction(int times, IntConsumer actionToRepeat) {
@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(actionToRepeat);
.forEach(i -> actionToRepeat.run());
}
}