Example #1
0
    def test_is_in_path(self, name, direction, r2x, r2y, expected):
        rover1 = Rover("Rover 1", 1, 1, direction)
        rover2 = Rover("Rover 2", r2x, r2y, 'N')

        result = MissionControl._is_in_path(rover1, rover2)
        self.assertEquals(
            result, expected, f'Test in path {name} failed.\n'
            f'Expected: {expected}\n'
            f'Got: {result}')
    def deploy_rovers(self):
        max_rovers = 4 if self.max_x > 4 else self.max_x
        while True:
            number_of_rovers = input(
                f"\nNumber of rovers (1-{max_rovers}) to deploy?\n")
            if number_of_rovers.isdigit():
                if int(number_of_rovers) > max_rovers:
                    MissionComms.print_fail(
                        f"Can't deploy more than {max_rovers} rovers!")
                    continue
                if int(number_of_rovers) <= 0:
                    MissionComms.print_fail(
                        "We need some rovers, choose a number between 1 and 4")
                    continue
                break
            MissionComms.print_fail(
                f"Expecting a number between 1 and {max_rovers}")
            continue

        for r in range(int(number_of_rovers)):
            rover = Rover(ROVER_NAMES[r], r, 0, 'N')
            self.rovers.append(rover)
            MissionComms.print_info(
                f"Rover {rover.name} deployed at position:\t {rover.get_rover_position()}"
            )
    def deploy_rover(self):
        prompt = "\nWhere should the rover be deployed?\n" if len(self.rovers) == 0 \
            else "\nWhere should the next rover be deployed?\n"
        while True:
            try:
                coordinates = input(prompt).split()
                x = int(coordinates[0])
                y = int(coordinates[1])
                d = coordinates[2].upper()
                if d not in ['N', 'S', 'E', 'W']:
                    MissionComms.print_warn(
                        f"Not a valid direction, options are N, S, E, and W")
                    continue
                if x > self.max_x or y > self.max_y:
                    MissionComms.print_warn(
                        f"Not a valid position, try and keep it in bounds")
                    continue
                rover = Rover("Rover", x, y, d)
                self.collision_avoidance(rover, deployment=True)
                break
            except (ValueError, IndexError):
                MissionComms.print_warn(
                    f"Expecting 2 numbers and a direction in the format: 0 0 N"
                )
                continue
            except RoverError:
                MissionComms.print_warn(
                    f"A rover is already deployed there, try again.")
                continue

        MissionComms.print_info(
            f"{rover.name} deployed at position:\t {rover.get_rover_position()}"
        )
        self.rovers.append(rover)
        self._drive_rover(rover)
Example #4
0
 def test_is_end_of_plateau(self, name, x, y, direction, expected):
     rover = Rover("Test", x, y, direction)
     print(self.mission_control.max_y)
     result = self.mission_control._is_end_of_plateau(rover)
     self.assertEquals(
         result, expected, f'Test end of plateau {name} failed.\n'
         f'Expected: {expected}\n'
         f'Got: {result}')
 def _action_command(self, command: str, rover: Rover) -> None:
     command = command.upper()
     if command == 'L':
         rover.rotate_left()
     elif command == 'R':
         rover.rotate_right()
     elif command == 'M':
         try:
             self.collision_avoidance(rover)
             rover.move_forward()
         except RoverError:
             raise CommandAbort("Aborting commands")
     else:
         MissionComms.print_warn(
             f"{command} is not a valid command, ignoring this command.")
 def setUp(self):
     self.rover = Rover("Test Rover", 0, 0, 'N')
class RoverTest(unittest.TestCase):
    def setUp(self):
        self.rover = Rover("Test Rover", 0, 0, 'N')

    @parameterized.expand([
        ('North', 'N', 'W'),
        ('West', 'W', 'S'),
        ('South', 'S', 'E'),
        ('East', 'E', 'N'),
    ])
    def test_rotate_left(self, name, test_input, expected):
        self.rover.direction = test_input
        self.rover.rotate_left()
        self.assertEquals(
            self.rover.direction, expected,
            f'Rotate Left for case "{name}" failed.\n'
            f'Expected: {expected}\n'
            f'Got: {test_input}')

    @parameterized.expand([
        ('North', 'N', 'E'),
        ('East', 'E', 'S'),
        ('South', 'S', 'W'),
        ('West', 'W', 'N'),
    ])
    def test_rotate_right(self, name, test_input, expected):
        self.rover.direction = test_input
        self.rover.rotate_right()
        self.assertEquals(
            self.rover.direction, expected,
            f'Rotate Right for case "{name}" failed.\n'
            f'Expected: {expected}\n'
            f'Got: {test_input}')

    def test_move_north(self):
        self.rover.move_north()
        self.assertEquals(
            self.rover.position_y, 1, f'Rover Move north failed.\n'
            f'Expected:1\n'
            f'Got: {self.rover.position_y}')

    def test_move_east(self):
        self.rover.move_east()
        self.assertEquals(
            self.rover.position_x, 1, f'Rover Move east failed.\n'
            f'Expected: 1\n'
            f'Got: {self.rover.position_x}')

    def test_move_south(self):
        self.rover.position_y = 1
        self.rover.move_south()
        self.assertEquals(
            self.rover.position_y, 0, f'Rover Move south failed.\n'
            f'Expected: 0\n'
            f'Got: {self.rover.position_y}')

    def test_move_west(self):
        self.rover.position_x = 1
        self.rover.move_west()
        self.assertEquals(
            self.rover.position_x, 0, f'Rover Move west failed.\n'
            f'Expected: 0\n'
            f'Got: {self.rover.position_x}')

    @parameterized.expand([
        ('N', 'mission.rover.Rover.move_north'),
        ('S', 'mission.rover.Rover.move_south'),
        ('E', 'mission.rover.Rover.move_east'),
        ('W', 'mission.rover.Rover.move_west'),
    ])
    def test_move_forward(self, test_input, target):
        with patch(target) as f:
            self.rover.direction = test_input
            self.rover.move_forward()
            f.assert_called()

    def test_get_rover_position(self):
        self.assertEquals(
            self.rover.get_rover_position(), "0 0 N",
            f'Get rover position failed.\n'
            f'Expected: 0 0 N\n'
            f'Got: {self.rover.get_rover_position()}')
Example #8
0
 def test_collision_avoidance(self, name, x, y, direction):
     with pytest.raises(RoverError):
         rover = Rover('Test', x, y, direction)
         self.mission_control.collision_avoidance(rover)
Example #9
0
 def test_manage_rover(self):
     rover = Rover("Test", 0, 0, 'N')
     self.mission_control._manage_rover(rover, 'MRML')
     self.assertEquals(rover.get_rover_position(), '1 1 N')