Added unit tests and fixed rotate_right() incorrect check for

is_initialized()
This commit is contained in:
Chris Davoren 2023-10-07 18:05:45 +10:00
parent 730fab5287
commit ff704ec625
2 changed files with 140 additions and 1 deletions

View File

@ -259,8 +259,9 @@ class Robot:
(initialized). (initialized).
""" """
if self.is_initialized(): if not self.is_initialized():
return return
self._direction = (self._direction + 1) % 4 self._direction = (self._direction + 1) % 4
def interpret_command(self, command: str): def interpret_command(self, command: str):

138
trtests.py Normal file
View File

@ -0,0 +1,138 @@
#!/usr/bin/env python3
import unittest
import toyrobot
class ToyRobotTests(unittest.TestCase):
def test_creation(self):
robot = toyrobot.Robot()
self.assertFalse(robot.is_initialized())
def test_initialization(self):
robot = toyrobot.Robot()
robot.place(0, 0, "NORTH")
self.assertTrue(robot.is_initialized())
def test_invalid_max_x(self):
exception_thrown = False
try:
robot = toyrobot.Robot(-1, 5)
except ValueError as ve:
exception_thrown = True
self.assertTrue(exception_thrown)
def test_invalid_max_y(self):
exception_thrown = False
try:
robot = toyrobot.Robot(5, -1)
except ValueError as ve:
exception_thrown = True
self.assertTrue(exception_thrown)
def test_valid_place(self):
robot = toyrobot.Robot()
robot.place(1, 1, "NORTH")
self.assertTrue(robot.is_initialized() and \
robot.get_position() == (1, 1) and \
robot.get_direction() == "NORTH")
def test_invalid_place_position_x(self):
robot = toyrobot.Robot()
robot.place(6, 1, "NORTH")
self.assertFalse(robot.is_initialized())
def test_invalid_place_position_y(self):
robot = toyrobot.Robot()
robot.place(1, 6, "NORTH")
self.assertFalse(robot.is_initialized())
def test_invalid_place_position_both(self):
robot = toyrobot.Robot()
robot.place(6, 6, "NORTH")
self.assertFalse(robot.is_initialized())
def test_invalid_place_direction(self):
robot = toyrobot.Robot()
robot.place(1, 1, "INVALID")
self.assertFalse(robot.is_initialized())
def test_missing_place_direction(self):
robot = toyrobot.Robot()
robot.place(1, 1)
self.assertFalse(robot.is_initialized())
def test_double_place(self):
robot = toyrobot.Robot()
robot.place(5, 5, "NORTH")
robot.place(2, 2)
self.assertTrue(robot.is_initialized() and \
robot.get_position() == (2, 2) and \
robot.get_direction() == "NORTH")
def test_double_place_2(self):
robot = toyrobot.Robot()
robot.place(5, 5, "NORTH")
robot.place(2, 2, "SOUTH")
self.assertTrue(robot.is_initialized() and \
robot.get_position() == (2, 2) and \
robot.get_direction() == "SOUTH")
def test_valid_movement_x(self):
robot = toyrobot.Robot()
robot.place(2, 2, "EAST")
robot.move()
self.assertTrue(robot.get_position() == (3, 2))
def test_valid_movement_y(self):
robot = toyrobot.Robot()
robot.place(2, 2, "NORTH")
robot.move()
self.assertTrue(robot.get_position() == (2, 3))
def test_invalid_movement_north(self):
robot = toyrobot.Robot()
robot.place(1, 5, "NORTH")
robot.move()
self.assertTrue(robot.get_position() == (1, 5))
def test_invalid_movement_east(self):
robot = toyrobot.Robot()
robot.place(5, 1, "EAST")
robot.move()
self.assertTrue(robot.get_position() == (5, 1))
def test_invalid_movement_south(self):
robot = toyrobot.Robot()
robot.place(1, 0, "SOUTH")
robot.move()
self.assertTrue(robot.get_position() == (1, 0))
def test_invalid_movement_west(self):
robot = toyrobot.Robot()
robot.place(0, 1, "WEST")
robot.move()
self.assertTrue(robot.get_position() == (0, 1))
def test_rotate_left(self):
robot = toyrobot.Robot()
robot.place(0, 0, "NORTH")
robot.rotate_left()
self.assertTrue(robot.get_direction() == "WEST")
def test_rotate_right(self):
robot = toyrobot.Robot()
robot.place(0, 0, "NORTH")
robot.rotate_right()
self.assertTrue(robot.get_direction() == "EAST")
def test_rotate_right_2(self):
# Blackbox test - ensures internal numerical direction using modulus
# correctly.
robot = toyrobot.Robot()
robot.place(0, 0, "WEST")
robot.rotate_right()
self.assertTrue(robot.get_direction() == "NORTH")
if __name__ == '__main__':
unittest.main()