From 556acf809563a7df9468e06b43e4f09f38fffeee Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 16 Nov 2019 03:02:53 +0100 Subject: [PATCH] rover: add parse_exectute method to Commander --- rover/rover.py | 11 +++++++++++ rover/test_rover.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/rover/rover.py b/rover/rover.py index eaae800..babb4ea 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -60,3 +60,14 @@ class Rover(BaseModel): class Commander(BaseModel): rover: Rover = Rover() + + def parse_execute(self, commands: str): + for command in commands: + if command == "F": + self.rover.forward() + elif command == "B": + self.rover.backward() + elif command == "L": + self.rover.turn_left() + elif command == "R": + self.rover.turn_right() diff --git a/rover/test_rover.py b/rover/test_rover.py index a43634b..90d44b1 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -187,3 +187,33 @@ def test_commander_default_values(): com = Commander() assert com.rover == Rover() + + +def test_commander_can_parse_left(): + com = Commander() + com.parse_execute("L") + assert com.rover == Rover(dir=Direction.WEST) + + +def test_commander_can_parse_right(): + com = Commander() + com.parse_execute("R") + assert com.rover == Rover(dir=Direction.EAST) + + +def test_commmander_can_parse_forward(): + com = Commander() + com.parse_execute("F") + assert com.rover == Rover(pos=Vector(x=0, y=1)) + + +def test_commmander_can_parse_backward(): + com = Commander(rover=Rover(pos=Vector(x=0, y=1))) + com.parse_execute("B") + assert com.rover == Rover() + + +def test_commander_complex_command(): + com = Commander() + com.parse_execute("FRFRFLB") + assert com.rover == Rover(dir=Direction.EAST)