Ejemplo n.º 1
0
 def move(self) -> RoverPosition:
     moveMappingTable = {
         Orientation.NORTH:
         lambda: RoverPosition(self.currentPosition.coordinateInX, self.
                               currentPosition.coordinateInY + 1, self.
                               currentPosition.orientation),
         Orientation.SOUTH:
         lambda: RoverPosition(self.currentPosition.coordinateInX, self.
                               currentPosition.coordinateInY - 1, self.
                               currentPosition.orientation),
         Orientation.WEST:
         lambda: RoverPosition(self.currentPosition.coordinateInX - 1, self.
                               currentPosition.coordinateInY, self.
                               currentPosition.orientation),
         Orientation.EAST:
         lambda: RoverPosition(self.currentPosition.coordinateInX + 1, self.
                               currentPosition.coordinateInY, self.
                               currentPosition.orientation)
     }
     newRoverPosition = moveMappingTable.get(
         self.currentPosition.orientation,
         lambda: RoverPosition(0, 0, Orientation.SOUTH))()
     if not self.plateau.isPositionWithinPlateauArea(newRoverPosition):
         raise ValueError('rover cannot be driven out of plateau area')
     self.currentPosition = newRoverPosition
     return self.currentPosition
 def move(self):
     """
     Change X-Y coordinates of rover on move
     :return: None
     """
     # update x and y coordinates based on orientation
     # (N : y+1) ; (S : y-1)
     # (E : x+1) ; (W : x-1)
     moveMappingTable = {
         'N': lambda: RoverPosition(self.currentPosition.coordinateInX,
                                                  self.currentPosition.coordinateInY + 1,
                                                  self.currentPosition.orientation),
         'S': lambda: RoverPosition(self.currentPosition.coordinateInX,
                                                  self.currentPosition.coordinateInY - 1,
                                                  self.currentPosition.orientation),
         'W': lambda: RoverPosition(self.currentPosition.coordinateInX - 1,
                                                 self.currentPosition.coordinateInY,
                                                 self.currentPosition.orientation),
         'E': lambda: RoverPosition(self.currentPosition.coordinateInX + 1,
                                                 self.currentPosition.coordinateInY,
                                                 self.currentPosition.orientation)
     }
     newRoverPosition = moveMappingTable.get(self.currentPosition.orientation,
                                             lambda: RoverPosition(0, 0, 'S'))()
     if not self.plateau.isPositionWithinPlateauArea(newRoverPosition):
         raise ValueError('rover cannot be driven out of plateau area')
     self.currentPosition = newRoverPosition
     return self.currentPosition
Ejemplo n.º 3
0
 def test_position_instances_equal_operator(self):
     """
     Test position instances equality method
     """
     pos1 = RoverPosition(2, 5)
     self.assertTrue(pos1 == RoverPosition(2, 5))
     self.assertFalse(pos1 == RoverPosition(1, 2))
Ejemplo n.º 4
0
    def test_shouldParseAValidSetOfInstructions(self):
        # Given
        filePath = 'somePathToFile/file.txt'
        mockedFileContent = '5 5\n3 3 E\nMMR\n2 2 N\nRMLM'

        parser = Parser()

        plateau = Plateau(5, 5)
        movementCommands1 = [
            MovementCommand('M'),
            MovementCommand('M'),
            MovementCommand('R')
        ]
        movementCommands2 = [
            MovementCommand('R'),
            MovementCommand('M'),
            MovementCommand('L'),
            MovementCommand('M')
        ]
        roverInstruction1 = RoverInstruction(
            RoverPosition(3, 3, Orientation('E')), movementCommands1)
        roverInstruction2 = RoverInstruction(
            RoverPosition(2, 2, Orientation('N')), movementCommands2)

        expectedSetOfInstructions = SetOfInstructions(
            plateau, [roverInstruction1, roverInstruction2])

        # When
        with patch('builtins.open', mock_open(read_data=mockedFileContent)):
            result = parser.parseFile(filePath)

        # Then
        assert result.toString() == expectedSetOfInstructions.toString()
Ejemplo n.º 5
0
 def test_position_with_values_passed(self):
     """
     Test position instance with values passed as parameters
     """
     position = RoverPosition(3, 2)
     self.assertEqual(position.x, 3)
     self.assertEqual(position.y, 2)
Ejemplo n.º 6
0
 def test_position_with_default_values(self):
     """
     Test position instance with with default
     """
     position = RoverPosition()
     self.assertEqual(position.x, 0)
     self.assertEqual(position.y, 0)
Ejemplo n.º 7
0
 def turnRight(self) -> RoverPosition:
     rightOrientationMapping = {
         Orientation.NORTH: Orientation.EAST,
         Orientation.WEST: Orientation.NORTH,
         Orientation.SOUTH: Orientation.WEST,
         Orientation.EAST: Orientation.SOUTH
     }
     newOrientation = rightOrientationMapping.get(
         self.currentPosition.orientation, Orientation.NORTH)
     newPosition = RoverPosition(self.currentPosition.coordinateInX,
                                 self.currentPosition.coordinateInY,
                                 newOrientation)
     self.currentPosition = newPosition
     return newPosition
Ejemplo n.º 8
0
    def test_shouldParseAValidSetOfInstructions(self):
        # Custom Inputs
        filePath = 'somePathToFile/file.txt'
        mockedFileContent = '5 5\n3 3 E\nMMR\n2 2 N\nRMLM'

        plateau = Plateau(5, 5)
        movement_commands1 = ['M' 'M' 'R']
        movement_commands2 = ['R', 'M', 'L', 'M']
        rover_instruction1 = RoverInstruction(RoverPosition(3, 3, 'E'),
                                              movement_commands1)
        rover_instruction2 = RoverInstruction(RoverPosition(2, 2, 'N'),
                                              movement_commands2)

        expectedSetOfInstructions = SetOfInstructions(
            plateau, [rover_instruction1, rover_instruction2])

        # mocking is done for  assertion statements
        with patch('builtins.open', mock_open(read_data=mockedFileContent)):
            result = self.obj.readFile(filePath)

        # result
        self.assertEqual(result.toString(),
                         expectedSetOfInstructions.toString())
Ejemplo n.º 9
0
    def test_rover_instance(self):
        """
        Test rover instance
        """
        plateau_grid = Plateau(7, 7)
        position = RoverPosition(0, 0)

        rover = Rover(plateau_grid, position, Rover.DIRECTIONS['W'])
        self.assertEqual(position, rover._position)
        self.assertEqual(plateau_grid, rover._plateau)

        rover.set_position(3, 3, Rover.DIRECTIONS.get('E'))
        self.assertEqual(rover._position.x, 3)
        self.assertEqual(rover._position.y, 3)
        self.assertEqual(rover.get_heading, 'E')
Ejemplo n.º 10
0
 def test_RoverCanMoveToPosition(self):
     """
     test to check X- Y coordinates and Orientation changes when rover is given command to move
     :return:
     """
     # Given
     initialPosition = RoverPosition(2, 2, 'N')
     plateau = Plateau(5, 5)
     movementCommands = ['M','R','M','L','M']
     rover = Rover(plateau, initialPosition)
     # When
     rover.processCommands(movementCommands)
     # Then
     expectedFinalPosition = '3 4 N'
     self.assertEqual(rover.currentPosition.toString() ,expectedFinalPosition)
Ejemplo n.º 11
0
    def test_CannotCreateRoverIfInitialPositionOutOfPlateauArea(self):
        """
        test to check validation error is raised when initial X- Y coordinates of rover is
        outside plateau dimesnion
        :return:
                """

        # Given
        plateau = Plateau(5, 5)
        initialPosition = RoverPosition(6, 5, 'N')
        # Then
        self.assertRaises(ValueError, Rover,plateau, initialPosition)
        expected_error = 'rover initial position out of plateau area'
        try:
            # check that correct error message is reported when plateau dimension is not correct
            Rover(plateau, initialPosition)
        except Exception as error:

            self.assertTrue(expected_error in str(error))
Ejemplo n.º 12
0
    def test_CannotMoveRoverOutOfPlateau(self):
        """
        test to check validation error is raised when current X- Y coordinates of rover is
        outside plateau dimesnion
        :return:
                """
        # Given
        initialPosition = RoverPosition(2, 2, 'N')
        plateau = Plateau(3, 3)
        movementCommands = ['M', 'M', 'M']
        rover = Rover(plateau, initialPosition)
        # Then
        self.assertRaises(ValueError, rover.processCommands,  movementCommands)
        expected_error = 'rover cannot be driven out of plateau area'
        try:
            # check that correct error message is reported when plateau dimension is not correct
            rover.processCommands(movementCommands)
        except Exception as error:

            self.assertTrue(expected_error in str(error))
 def turnRight(self):
     """
     Change Orientation of rover by 90 degree clockwise
     when instruction is right
     :return: None
     """
     rightOrientationMapping = {
         'N': 'E',
         'W': 'N',
         'S': 'W',
         'E': 'S'
     }
     #if found in dict rightOrientationMapping else 'N'
     newOrientation = rightOrientationMapping.get(self.currentPosition.orientation, 'N')
     #update rover position
     newPosition = RoverPosition(self.currentPosition.coordinateInX,
                                 self.currentPosition.coordinateInY,
                                 newOrientation)
     #assign new position to rover object
     self.currentPosition = newPosition
     return newPosition
Ejemplo n.º 14
0
 def setUp(self):
     self.plateau_dimensions = Plateau(8, 4)
     self.rover_initial_position = RoverPosition(2, 1)
Ejemplo n.º 15
0
 def test_cannot_create_Rover_IfInitial_position_outOf_plateauArea(self):
     plateau = Plateau(5, 5)
     initial_position = RoverPosition(6, 5)
     with self.assertRaises(InvalidCoordinateError):
         Rover(plateau, initial_position, Rover.DIRECTIONS.get('N'))