From 4487269ab64a3ea18ae192366cf8c0031ba46b38 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 28 Nov 2019 16:21:40 +0100 Subject: [PATCH] rover: add exception on unknown Commander command --- rover/rover.py | 4 +++- rover/test_rover.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rover/rover.py b/rover/rover.py index aaf9d2c..b4eb1ca 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -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 diff --git a/rover/test_rover.py b/rover/test_rover.py index 5f198c7..ac7a57a 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -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):