1
0
Fork 0

hierarchical tests

This commit is contained in:
Xavier Fontanet 2024-06-29 21:38:30 +02:00
parent 35d570241a
commit 042aec2621
7 changed files with 107 additions and 77 deletions

View File

@ -1,64 +0,0 @@
package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.IntStream;
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH;
import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*;
public class CommonTests {
public static final Direction STARTING_DIRECTION = SOUTH;
protected RotableRiderRover rover;
protected RoverCommand moveForwardCommand;
protected RoverCommand moveBackwardsCommand;
protected RoverCommand turnLeftCommand;
protected RoverCommand turnRightCommand;
protected void setup() {
int mapWidth = 10;
int mapHeight = 10;
int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
var marsMap = getImplProviderOf(PlanetMap.Provider.class)
.provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles());
rover = getImplProviderOf(RotableRiderRover.Provider.class)
.provideWith(marsMap, CommonTests.STARTING_DIRECTION);
var commandFactory = getImplProviderOf(RoverCommandFactory.Provider.class)
.provideWith(rover);
moveForwardCommand = commandFactory.create(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.create(TURN_LEFT);
turnRightCommand = commandFactory.create(TURN_RIGHT);
}
private <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader
.load(interfaceProvider)
.findFirst()
.orElseThrow();
}
protected List<Coordinates> getObstacles() {
return List.of();
}
protected void repeatAction(int times, Runnable actionToRepeat) {
IntStream.rangeClosed(1, times)
.forEach(i -> actionToRepeat.run());
}
}

View File

@ -2,17 +2,18 @@ package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.test.context.GivenRotableAndMoveCommandsOnDeployedRoverWithMap;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.*;
import static org.testng.Assert.assertEquals;
public class MarsRoverTest extends CommonTests {
public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWithMap {
@BeforeMethod
public void setUp() {
setup();
super.setUp();
}
@Test
@ -109,7 +110,7 @@ public class MarsRoverTest extends CommonTests {
}
@Test
public void crazyTour() {
public void acceptanceTest() {
moveForwardCommand.execute();
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
assertEquals(rover.getCurrentDirection(), SOUTH);

View File

@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.test;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.test.context.GivenRotableAndMoveCommandsOnDeployedRoverWithMap;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@ -10,11 +11,19 @@ import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
public class MarsRoverWithObstaclesTest extends CommonTests {
public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDeployedRoverWithMap {
@Override
protected List<Coordinates> getObstacles() {
return List.of(
Coordinates.of(3, 3),
Coordinates.of(5, 5)
);
}
@BeforeMethod
public void setUp() {
super.setup();
super.setUp();
}
@Test
@ -44,12 +53,4 @@ public class MarsRoverWithObstaclesTest extends CommonTests {
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
}
@Override
protected List<Coordinates> getObstacles() {
return List.of(
Coordinates.of(3, 3),
Coordinates.of(5, 5)
);
}
}

View File

@ -0,0 +1,29 @@
package cat.hack3.codingtests.marsrover.test.context;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import java.util.List;
public abstract class GivenMarsMapSquaredWithInitialCoordinates implements TestingCommons {
public static final Class<PlanetMap.Provider> PLANET_MAP_PROVIDER = PlanetMap.Provider.class;
public static final List<Coordinates> NONE_OBSTACLE = List.of();
protected PlanetMap marsMap;
public void setUp() {
int mapWidth = 10;
int mapHeight = 10;
int latitudeStartingPoint = 2;
int longitudeStartingPoint = 3;
var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint);
marsMap = getImplProviderOf(PLANET_MAP_PROVIDER)
.provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles());
}
protected List<Coordinates> getObstacles() {
return NONE_OBSTACLE;
}
}

View File

@ -0,0 +1,18 @@
package cat.hack3.codingtests.marsrover.test.context;
import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH;
public abstract class GivenMarsRoverDeployedOnMarsMap extends GivenMarsMapSquaredWithInitialCoordinates{
static final Direction STARTING_DIRECTION = SOUTH;
protected RotableRiderRover rover;
public void setUp() {
super.setUp();
rover = getImplProviderOf(RotableRiderRover.Provider.class)
.provideWith(marsMap, STARTING_DIRECTION);
}
}

View File

@ -0,0 +1,26 @@
package cat.hack3.codingtests.marsrover.test.context;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*;
public class GivenRotableAndMoveCommandsOnDeployedRoverWithMap extends GivenMarsRoverDeployedOnMarsMap {
public static final Class<RoverCommandFactory.Provider> COMMAND_FACTORY_PROVIDER = RoverCommandFactory.Provider.class;
protected RoverCommand moveForwardCommand;
protected RoverCommand moveBackwardsCommand;
protected RoverCommand turnLeftCommand;
protected RoverCommand turnRightCommand;
public void setUp() {
super.setUp();
var commandFactory = getImplProviderOf(COMMAND_FACTORY_PROVIDER)
.provideWith(rover);
moveForwardCommand = commandFactory.create(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.create(TURN_LEFT);
turnRightCommand = commandFactory.create(TURN_RIGHT);
}
}

View File

@ -0,0 +1,19 @@
package cat.hack3.codingtests.marsrover.test.context;
import java.util.ServiceLoader;
import java.util.stream.IntStream;
public interface TestingCommons {
default <T> T getImplProviderOf(Class<T> interfaceProvider) {
return ServiceLoader
.load(interfaceProvider)
.findFirst()
.orElseThrow();
}
default void repeatAction(int times, Runnable actionToRepeat) {
IntStream.rangeClosed(1, times)
.forEach(i -> actionToRepeat.run());
}
}