hierarchical tests
This commit is contained in:
parent
35d570241a
commit
042aec2621
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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.Coordinates;
|
||||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
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.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.*;
|
import static cat.hack3.codingtests.marsrover.api.cartography.Direction.*;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
public class MarsRoverTest extends CommonTests {
|
public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWithMap {
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
setup();
|
super.setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -109,7 +110,7 @@ public class MarsRoverTest extends CommonTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void crazyTour() {
|
public void acceptanceTest() {
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
||||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||||
|
|
|
@ -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.Coordinates;
|
||||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
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.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
@ -10,11 +11,19 @@ import java.util.List;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertNotEquals;
|
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
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
super.setup();
|
super.setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -44,12 +53,4 @@ public class MarsRoverWithObstaclesTest extends CommonTests {
|
||||||
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
|
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected List<Coordinates> getObstacles() {
|
|
||||||
return List.of(
|
|
||||||
Coordinates.of(3, 3),
|
|
||||||
Coordinates.of(5, 5)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue