rover: add parse_exectute method to Commander

This commit is contained in:
Bruno BELANYI 2019-11-16 03:02:53 +01:00
parent 81dca25a4f
commit 556acf8095
2 changed files with 41 additions and 0 deletions

View file

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

View file

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