From ff704ec62510bbaab4daf771bea2d0a4603d68b1 Mon Sep 17 00:00:00 2001 From: Chris Davoren Date: Sat, 7 Oct 2023 18:05:45 +1000 Subject: [PATCH] Added unit tests and fixed rotate_right() incorrect check for is_initialized() --- toyrobot/robot.py | 3 +- trtests.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 trtests.py diff --git a/toyrobot/robot.py b/toyrobot/robot.py index a139d1b..c2a064b 100644 --- a/toyrobot/robot.py +++ b/toyrobot/robot.py @@ -259,8 +259,9 @@ class Robot: (initialized). """ - if self.is_initialized(): + if not self.is_initialized(): return + self._direction = (self._direction + 1) % 4 def interpret_command(self, command: str): diff --git a/trtests.py b/trtests.py new file mode 100644 index 0000000..4fee01c --- /dev/null +++ b/trtests.py @@ -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()