test invalid command case, and btw, nice to see
"InvalidCommandNameProvided: The available commands are [MOVE_FORWARD, MOVE_BACKWARDS, TURN_LEFT, TURN_RIGHT]" in response
This commit is contained in:
parent
b50022b5b2
commit
d0f420e9c0
|
@ -9,5 +9,9 @@ public interface RoverCommandFactory {
|
|||
RoverCommandFactory provideWith(Rover rover);
|
||||
}
|
||||
|
||||
class InvalidCommandNameProvided extends RuntimeException{}
|
||||
class InvalidCommandNameProvided extends RuntimeException{
|
||||
public InvalidCommandNameProvided(String explanation) {
|
||||
super(explanation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import cat.hack3.codingtests.marsrover.api.Rover;
|
|||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public class RotableRiderRoverCommandFactory implements RoverCommandFactory {
|
||||
|
@ -26,7 +27,7 @@ public class RotableRiderRoverCommandFactory implements RoverCommandFactory {
|
|||
@Override
|
||||
public RoverCommand create(String commandName) {
|
||||
var commandType = tryResolvingCommandTypeFrom(commandName)
|
||||
.orElseThrow(InvalidCommandNameProvided::new);
|
||||
.orElseThrow(() -> new InvalidCommandNameProvided(getAvailableCommandsExplanation()));
|
||||
return switch (commandType) {
|
||||
case MOVE_FORWARD -> new MoveForwardCommand(rover);
|
||||
case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover);
|
||||
|
@ -42,4 +43,8 @@ public class RotableRiderRoverCommandFactory implements RoverCommandFactory {
|
|||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private String getAvailableCommandsExplanation() {
|
||||
return String.format("The available commands are %s", Arrays.toString(RoverCommandType.values()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.api.commands.RoverCommandFactory.InvalidCommandNameProvided;
|
||||
import cat.hack3.codingtests.marsrover.test.context.GivenRotableAndMoveCommandsOnDeployedRoverWithMap;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
@ -132,4 +133,14 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = InvalidCommandNameProvided.class)
|
||||
public void invalidCommand() {
|
||||
try {
|
||||
commandFactory.create("FLY_TILL_THE_MOON").execute();
|
||||
} catch (InvalidCommandNameProvided e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory;
|
|||
public class GivenRotableAndMoveCommandsOnDeployedRoverWithMap extends GivenMarsRoverDeployedOnMarsMap {
|
||||
|
||||
public static final Class<RoverCommandFactory.Provider> COMMAND_FACTORY_PROVIDER = RoverCommandFactory.Provider.class;
|
||||
protected RoverCommandFactory commandFactory;
|
||||
protected RoverCommand moveForwardCommand;
|
||||
protected RoverCommand moveBackwardsCommand;
|
||||
protected RoverCommand turnLeftCommand;
|
||||
|
@ -13,7 +14,7 @@ public class GivenRotableAndMoveCommandsOnDeployedRoverWithMap extends GivenMars
|
|||
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
RoverCommandFactory commandFactory = getImplProviderOf(COMMAND_FACTORY_PROVIDER)
|
||||
commandFactory = getImplProviderOf(COMMAND_FACTORY_PROVIDER)
|
||||
.provideWith(rover);
|
||||
|
||||
moveForwardCommand = commandFactory.create("MOVE_FORWARD");
|
||||
|
|
Loading…
Reference in New Issue