#!/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: toyrobot.Robot(-1, 5) except ValueError: exception_thrown = True self.assertTrue(exception_thrown) def test_invalid_max_y(self): exception_thrown = False try: toyrobot.Robot(5, -1) except ValueError: 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, "SOUTH") robot.rotate_left() self.assertTrue(robot.get_direction() == "EAST") def test_rotate_left_2(self): # Blackbox test - ensures internal numerical direction using modulus # correctly on decrement. 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 on increment. robot = toyrobot.Robot() robot.place(0, 0, "WEST") robot.rotate_right() self.assertTrue(robot.get_direction() == "NORTH") def test_example_d_calls(self): robot = toyrobot.Robot() robot.place(1, 2, "EAST") robot.move() robot.rotate_left() robot.move() robot.place(3, 1) robot.move() self.assertTrue( robot.is_initialized() and robot.get_position() == (3, 2) and robot.get_direction() == "NORTH" ) def test_example_d_commands(self): robot = toyrobot.Robot() robot.interpret_command("PLACE 1,2,EAST") robot.interpret_command("MOVE") robot.interpret_command("LEFT") robot.interpret_command("MOVE") robot.interpret_command("PLACE 3,1") robot.interpret_command("MOVE") self.assertTrue(str(robot) == "3,2,NORTH") if __name__ == "__main__": unittest.main()