Преглед изворни кода

The rover can now move east

But not yet towards the north.

Also, refactored DIRECTIONS -> HEADINGS since the customer
uses this wording (and frankly, it's much better).
Oz N Tiram пре 4 година
родитељ
комит
26718f8505
2 измењених фајлова са 30 додато и 13 уклоњено
  1. 13 8
      src/rover/rover.py
  2. 17 5
      tests/test_rover.py

+ 13 - 8
src/rover/rover.py

@@ -1,24 +1,29 @@
 import random
 import warnings
 
-DIRECTIONS = ['N', 'E', 'S', 'W']
+HEADINGS = ['N', 'E', 'S', 'W']
 
 class Location:
 
-    def __init__(self, x, y, direction):
+    def __init__(self, x, y, heading):
         self.x = x
         self.y = y
-        self.direction = direction
+        self.heading = heading
 
 
 class Rover:
 
-    def __init__(self, x=None, y=None, direction=None):
-        if all((x, y, direction)):
-            self.location = Location(x, y, direction)
+    def __init__(self, x=None, y=None, heading=None):
+        if all((x, y, heading)):
+            self.location = Location(x, y, heading)
         else:
              warnings.warn("Landing at a random place")
              self.location = Location(random.randint(-10, 10),
                                       random.randint(-10, 10),
-                                      DIRECTIONS[random.randint(0,
-                                                 len(DIRECTIONS) - 1)])
+                                      HEADINGS[random.randint(0,
+                                                 len(HEADINGS) - 1)])
+
+    def move(self, command):
+        for item in command:
+            if item == 'F' and self.location.heading == 'E':
+                self.location.x += 1

+ 17 - 5
tests/test_rover.py

@@ -1,17 +1,29 @@
 from rover import Rover
-from rover.rover import DIRECTIONS
+from rover.rover import HEADINGS
 
 def test_can_land():
     assert Rover()
 
 
-def test_has_location_and_direction():
+def test_has_location_and_heading():
     rover = Rover()
     assert isinstance(rover.location.x, int)
     assert isinstance(rover.location.y, int)
-    assert rover.location.direction in DIRECTIONS
+    assert rover.location.heading in HEADINGS
 
 def test_can_land_at_specified_location():
-    rover = Rover(x=1, y=2, direction='E')
+    rover = Rover(x=1, y=2, heading='E')
     assert rover.location.x == 1
-    assert rover.location.direction == 'E'
+    assert rover.location.heading == 'E'
+
+def test_can_move_east():
+
+    rover = Rover(x=1, y=2, heading='E')
+    rover.move('FFF')
+    assert rover.location.x == 4
+
+def test_can_move_north():
+
+    rover = Rover(x=1, y=2, heading='N')
+    rover.move('FFF')
+    assert rover.location.y == 5