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()
def test_shouldRaiseExceptionWhenPlateauDimensionsAreNotExactlyTwo(self): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = 'hjhkjj\n3 3 E\nMMRMMRMRRM' parser = Parser() # Then with pytest.raises(ParsingError, match='Invalid plateau dimensions'): with patch('builtins.open', mock_open(read_data=mockedFileContent)): parser.parseFile(filePath)
def test_shouldRaiseExceptionWhenAnMovementCommandIsNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 3 E\nMMRXMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' parser = Parser() # Then with pytest.raises(ValueError, match="'X' is not a valid MovementCommand"): with patch('builtins.open', mock_open(read_data=mockedFileContent)): parser.parseFile(filePath)
def test_shouldRaiseExceptionWhenInitialPositionIsNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 s E\nMMRMMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' parser = Parser() # Then with pytest.raises(ParsingError, match='Invalid rover initial position'): with patch('builtins.open', mock_open(read_data=mockedFileContent)): parser.parseFile(filePath)
def test_shouldCalculateFinalPositionForTwoRovers(capsys): parser = Parser() # Given inputFilePath = 'spec/testFiles/inputFile.txt' inputFileController = InputFileController() # When inputFileController.processFile(inputFilePath, parser) printedOutput = capsys.readouterr().out # Then expectedRoversFinalPositions = '1 3 N' + '\n' + '5 1 E' + '\n' assert expectedRoversFinalPositions == printedOutput
def processFile(self, filePath: str, parser: Parser): try: setOfInstructions = parser.parseFile(filePath) for instruction in setOfInstructions.roverInstructions: rover = Rover(setOfInstructions.plateau, instruction.initialPosition) rover.processCommands(instruction.movementCommands) print(rover.currentPosition.toString()) except ParsingError as error: print(error) except ValueError as error: print(error)
def main(): parser = Parser() inputFileController = InputFileController() inputFile = 'inputFile.txt' inputFileController.processFile(inputFile, parser)
class Test_InputFileController: parser = Parser() def test_shouldCalculateFinalPositionForOneRover(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 3 E\nMMRMMRMRRM' # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController = InputFileController() inputFileController.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out lastPrintedLine = printedOutput.replace('\n', '') expectedFinalPosition = "5 1 E" assert lastPrintedLine == expectedFinalPosition def test_shouldCalculateFinalPositionForThreeeRovers(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 3 E\nMMRMMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' inputFileController2 = InputFileController() # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController2.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out expectedFinalPosition = "5 1 E\n3 4 N\n1 2 N\n" assert printedOutput == expectedFinalPosition def test_shouldPrintAnErrorWhenPlateauDimensionsAreNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = 's 5\n3 3 E\nMMRMMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' inputFileController = InputFileController() # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out errorMessage = "Invalid plateau dimensions\n" assert printedOutput == errorMessage def test_shouldPrintAnErrorWhenRoverInitialPositionIsNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 s E\nMMRMMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' inputFileController = InputFileController() # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out errorMessage = "Invalid rover initial position\n" assert printedOutput == errorMessage def test_shouldPrintAnErrorWhenRoverInitialOrientationIsNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 3 X\nMMRMMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' inputFileController = InputFileController() # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out errorMessage = "Invalid rover initial position\n" assert printedOutput == errorMessage def test_shouldPrintAnErrorWhenAnMovementCommandIsNotValid(self, capsys): # Given filePath = 'somePathToFile/file.txt' mockedFileContent = '5 5\n3 3 E\nMMRXMRMRRM\n2 2 N\nMRMLM\n1 1 N\nM' inputFileController = InputFileController() # When with patch('builtins.open', mock_open(read_data=mockedFileContent)): inputFileController.processFile(filePath, self.parser) # Then printedOutput = capsys.readouterr().out errorMessage = "'X' is not a valid MovementCommand\n" assert printedOutput == errorMessage