rover: add exception on unknown Commander command

This commit is contained in:
Bruno BELANYI 2019-11-28 16:21:40 +01:00
parent b4ecee8c44
commit 4487269ab6
2 changed files with 9 additions and 2 deletions

View file

@ -101,7 +101,7 @@ class Commander(BaseModel):
return values
def parse_execute(self, commands: str) -> None:
for command in commands:
for i, command in enumerate(commands):
save: Vector = deepcopy(self.rover.pos)
if command == "F":
self.rover.forward()
@ -111,6 +111,8 @@ class Commander(BaseModel):
self.rover.turn_left()
elif command == "R":
self.rover.turn_right()
else:
raise ValueError(f"Unknown command: '{command}' (index {i})")
if self.rover.pos in self.obstacles:
self.rover.pos = save
raise ObstacleError

View file

@ -1,6 +1,5 @@
import pytest
from pydantic import ValidationError
from rover import Commander, Direction, ObstacleError, Rover, Vector
@ -222,6 +221,12 @@ def test_commander_complex_command():
assert com.rover == Rover(dir=Direction.EAST)
def test_commander_unknown_command():
com = Commander()
with pytest.raises(ValueError):
com.parse_execute("A")
def test_commander_command_with_obstacles():
com = Commander(obstacles=[Vector(x=1, y=0), Vector(x=1, y=2)])
with pytest.raises(ObstacleError):