1
0
Fork 0

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:
Xavier Fontanet 2024-07-05 18:02:27 +02:00
parent b50022b5b2
commit d0f420e9c0
4 changed files with 24 additions and 3 deletions

View File

@ -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);
}
}
}

View File

@ -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()));
}
}

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.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;
}
}
}

View File

@ -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");