def test_shouldConvertToString(self):
     # Given
     plateau = Plateau(3, 3)
     exptectedPlateauAsString = '3 3'
     # When
     result = plateau.toString()
     assert exptectedPlateauAsString == result
Esempio n. 2
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()
Esempio n. 3
0
    def readPlateauInput(self, plateau_dimension):
        """
        Calls Validate Plateau dimension fnction
        :param str plateau_dimension: First Line of Input File
        :return: Return object of type Plateau containing info regarding
         dimension of plateau
        :rtype:  Plateau
        :raises InputReaderError: if input file instruction is not valid

        """
        input_string_as_list = self.validatePlateauInput(plateau_dimension)
        return Plateau(int(input_string_as_list[0]),
                       int(input_string_as_list[1]))
Esempio n. 4
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')
Esempio n. 5
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)
Esempio n. 6
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))
Esempio n. 7
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))
Esempio 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())
Esempio n. 9
0
 def parsePlateauInput(self, inputString: str) -> Plateau:
     if not re.match(self.validPlateauInput, inputString):
         raise ParsingError("Invalid plateau dimensions")
     inputStringAsList = inputString.split()
     return Plateau(int(inputStringAsList[0]), int(inputStringAsList[1]))
Esempio n. 10
0
 def setUp(self):
     self.plateau_dimensions = Plateau(8, 4)
     self.rover_initial_position = RoverPosition(2, 1)
Esempio n. 11
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'))
Esempio n. 12
0
 def __init__(self, plateau: Plateau, initialPosition: RoverPosition):
     if not plateau.isPositionWithinPlateauArea(initialPosition):
         raise ValueError('rover initial position out of plateau area')
     self.plateau: Plateau = plateau
     self.currentPosition: RoverPosition = initialPosition