toyrobot/trtests.py

139 lines
4.3 KiB
Python
Raw Normal View History

#!/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()