def test_west_turn_left(self): direction = Direction.W position = Position(0, 0) rover = Rover(position, direction) commands = ('l') rover.execute(commands) self.assertEquals(Position(0, 0), rover.get_position()) self.assertEquals(Direction.S, rover.get_direction())
def test_turn_right_and_go_forward(self): direction = Direction.N position = Position(0, 0) rover = Rover(position, direction) commands = ('r', 'f', 'f', 'f') rover.execute(commands) self.assertEquals(Position(3, 0), rover.get_position()) self.assertEquals(Direction.E, rover.get_direction())
def test_move_one_forward(self): direction = Direction.N position = Position(0, 0) rover = Rover(position, direction) commands = ('f') rover.execute(commands) self.assertEquals(Position(0, 1), rover.get_position()) self.assertEquals(Direction.N, rover.get_direction())
def test_move_one_forward_two_backward_turn_left(self): direction = Direction.N position = Position(0, 0) rover = Rover(position, direction) commands = ('f', 'b', 'b', 'l') rover.execute(commands) self.assertEquals(Position(0, -1), rover.get_position()) self.assertEquals(Direction.W, rover.get_direction())
def test_do_a_lot_of_silly_stuff(self): direction = Direction.S position = Position(0, 0) rover = Rover(position, direction) commands = ('r', 'f', 'f', 'f', 'b', 'r') rover.execute(commands) self.assertEquals(Position(0, -2), rover.get_position()) self.assertEquals(Direction.N, rover.get_direction())
def test_move_forward_to_beyond_the_borders(self, m_logger): """ If Rover exceed a border, a warning is raised. """ rover = Rover(0, 0, Heading.W, self.plateau) rover.execute(Command.M) m_logger.assert_called_once_with( 'Do you can go beyond the borders? ' 'Maybe you need to talk with mission control.')
def test_stop_at_obstacle( command, position): obstacle1_coordinate = Coordinate(0, 4) obstacle2_coordinate = Coordinate(2 ,0) grid = Grid([obstacle1_coordinate, obstacle2_coordinate]) rover_obj = Rover(grid) assert rover_obj.execute(command) == position
""" Mission simulation. """ from rover import Plateau, Rover, Heading, Command if __name__ == '__main__': instructions = open('instructions.txt', 'r') # Prepare the plateau to landings. data = instructions.readline().split() x, y = map(int, data) plateau = Plateau(x, y) # Deploy Rovers on the plateau. for data in instructions: x, y, heading = data.split() rover = Rover(int(x), int(y), getattr(Heading, heading), plateau) # Parse and run instructions. commands = instructions.readline().strip() for cmd in commands: command = getattr(Command, cmd, None) rover.execute(command) print(rover) instructions.close()
class TestRover(unittest.TestCase): """ Verifies if after landed, the rover can navigate on the plateau. """ def setUp(self): self.plateau = Plateau(5, 5) self.rover = Rover(1, 2, Heading.N, self.plateau) def test_get_str_representation(self): expected = '1 2 N' self.assertEqual(expected, str(self.rover)) def test_turn_right(self): """ Test a full turn to right when heading to north. """ self.rover.execute(Command.R) self.assertEqual(self.rover.heading, Heading.E) self.rover.execute(Command.R) self.assertEqual(self.rover.heading, Heading.S) self.rover.execute(Command.R) self.assertEqual(self.rover.heading, Heading.W) self.rover.execute(Command.R) self.assertEqual(self.rover.heading, Heading.N) def test_turn_left(self): """ Test a full turn to left when heading to north. """ self.rover.execute(Command.L) self.assertEqual(self.rover.heading, Heading.W) self.rover.execute(Command.L) self.assertEqual(self.rover.heading, Heading.S) self.rover.execute(Command.L) self.assertEqual(self.rover.heading, Heading.E) self.rover.execute(Command.L) self.assertEqual(self.rover.heading, Heading.N) def test_move(self): """ Test a move forward in all directions, starting from north. """ self.rover.execute(Command.M) self.assertEqual((1, 3), self.rover.coordinates) self.rover.execute(Command.R) self.rover.execute(Command.M) self.assertEqual((2, 3), self.rover.coordinates) self.rover.execute(Command.R) self.rover.execute(Command.M) self.assertEqual((2, 2), self.rover.coordinates) self.rover.execute(Command.R) self.rover.execute(Command.M) self.assertEqual((1, 2), self.rover.coordinates) def test_execute_with_unknown_command(self): rover = Rover(1, 2, Heading.N, self.plateau) self.assertRaises(ValueError, rover.execute, True) @mock.patch('rover.logger.warning') def test_move_forward_to_beyond_the_borders(self, m_logger): """ If Rover exceed a border, a warning is raised. """ rover = Rover(0, 0, Heading.W, self.plateau) rover.execute(Command.M) m_logger.assert_called_once_with( 'Do you can go beyond the borders? ' 'Maybe you need to talk with mission control.')