2015-11-08 09:31:58 +01:00
|
|
|
import unittest
|
|
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from robot_simulator import (
|
|
|
|
|
Robot,
|
|
|
|
|
NORTH,
|
|
|
|
|
EAST,
|
|
|
|
|
SOUTH,
|
|
|
|
|
WEST,
|
|
|
|
|
)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2020-10-15 12:46:24 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
2017-10-27 20:46:14 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
|
2018-06-13 09:12:09 -04:00
|
|
|
class RobotSimulatorTest(unittest.TestCase):
|
2018-10-23 14:45:49 +02:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
# Test create robot
|
|
|
|
|
def test_at_origin_facing_north(self):
|
2018-10-23 14:45:49 +02:00
|
|
|
robot = Robot(NORTH, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, NORTH)
|
2015-11-16 17:36:54 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_at_negative_position_facing_south(self):
|
2019-07-11 14:42:59 +02:00
|
|
|
robot = Robot(SOUTH, -1, -1)
|
2019-10-14 18:19:20 +02:00
|
|
|
|
2019-07-11 14:42:59 +02:00
|
|
|
self.assertEqual(robot.coordinates, (-1, -1))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, SOUTH)
|
|
|
|
|
|
|
|
|
|
# Test rotating clockwise
|
|
|
|
|
def test_changes_north_to_east(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(NORTH, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("R")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, EAST)
|
|
|
|
|
|
|
|
|
|
def test_changes_east_to_south(self):
|
|
|
|
|
robot = Robot(EAST, 0, 0)
|
|
|
|
|
robot.move("R")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, SOUTH)
|
|
|
|
|
|
|
|
|
|
def test_changes_south_to_west(self):
|
|
|
|
|
robot = Robot(SOUTH, 0, 0)
|
|
|
|
|
robot.move("R")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, WEST)
|
|
|
|
|
|
|
|
|
|
def test_changes_west_to_north(self):
|
|
|
|
|
robot = Robot(WEST, 0, 0)
|
|
|
|
|
robot.move("R")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, NORTH)
|
|
|
|
|
|
|
|
|
|
# Test rotating counter-clockwise
|
|
|
|
|
def test_changes_north_to_west(self):
|
|
|
|
|
robot = Robot(NORTH, 0, 0)
|
|
|
|
|
robot.move("L")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, WEST)
|
|
|
|
|
|
|
|
|
|
def test_changes_west_to_south(self):
|
|
|
|
|
robot = Robot(WEST, 0, 0)
|
|
|
|
|
robot.move("L")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, SOUTH)
|
|
|
|
|
|
|
|
|
|
def test_changes_south_to_east(self):
|
|
|
|
|
robot = Robot(SOUTH, 0, 0)
|
|
|
|
|
robot.move("L")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, EAST)
|
|
|
|
|
|
|
|
|
|
def test_changes_east_to_north(self):
|
|
|
|
|
robot = Robot(EAST, 0, 0)
|
|
|
|
|
robot.move("L")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(robot.coordinates, (0, 0))
|
|
|
|
|
self.assertEqual(robot.direction, NORTH)
|
|
|
|
|
|
|
|
|
|
# Test moving forward one
|
|
|
|
|
def test_facing_north_increments_y(self):
|
|
|
|
|
robot = Robot(NORTH, 0, 0)
|
|
|
|
|
robot.move("A")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, 1))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, NORTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_facing_south_decrements_y(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(SOUTH, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("A")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (0, -1))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, SOUTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_facing_east_increments_x(self):
|
2017-10-27 20:46:14 +01:00
|
|
|
robot = Robot(EAST, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("A")
|
|
|
|
|
|
2017-10-27 20:46:14 +01:00
|
|
|
self.assertEqual(robot.coordinates, (1, 0))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, EAST)
|
2017-10-27 20:46:14 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_facing_west_decrements_x(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(WEST, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("A")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-1, 0))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, WEST)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
# Test follow series of instructions
|
|
|
|
|
def test_moving_east_and_north_from_readme(self):
|
2018-10-23 14:45:49 +02:00
|
|
|
robot = Robot(NORTH, 7, 3)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("RAALAL")
|
|
|
|
|
|
2018-10-23 14:45:49 +02:00
|
|
|
self.assertEqual(robot.coordinates, (9, 4))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, WEST)
|
2018-10-23 14:45:49 +02:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_moving_west_and_north(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(NORTH, 0, 0)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("LAAARALA")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-4, 1))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, WEST)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_moving_west_and_south(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(EAST, 2, -7)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("RRAAAAALA")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (-3, -8))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, SOUTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
def test_moving_east_and_north(self):
|
2015-11-16 17:36:54 +01:00
|
|
|
robot = Robot(SOUTH, 8, 4)
|
2019-10-14 18:19:20 +02:00
|
|
|
robot.move("LAAARRRALLLL")
|
|
|
|
|
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(robot.coordinates, (11, 5))
|
2019-10-14 18:19:20 +02:00
|
|
|
self.assertEqual(robot.direction, NORTH)
|
2015-11-08 09:31:58 +01:00
|
|
|
|
2021-05-09 02:32:12 -07:00
|
|
|
# Utility functions
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
2016-11-29 09:44:47 +01:00
|
|
|
|
2019-10-14 18:19:20 +02:00
|
|
|
if __name__ == "__main__":
|
2015-11-08 09:31:58 +01:00
|
|
|
unittest.main()
|