rover: add parse_exectute method to Commander
This commit is contained in:
parent
81dca25a4f
commit
556acf8095
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue