def processFile(self, file_path): """ Return an Output file with current Position :param str file_path: Folder path of Input File :return: Generates Output File with current Position of Rover. :raises InputReaderError: if input file instruction is not valid :raises Exception: if any error is generated """ rovers_current_poistion= [] reader = InputReader() # Read Input File and create a list of instruction to be executed for each Rover # in list of rovers set_of_instructions = reader.readFile(file_path) for instruction in set_of_instructions.roverInstructions: #create rover object for 1 rover rover = Rover(set_of_instructions.plateau, instruction.initialPosition) # run instruction commands for selected rover rover.processCommands(instruction.movementCommands) # Append final position of rover in output list rovers_current_poistion.append(rover.currentPosition.toString()) return rovers_current_poistion
def test_execute_instructions_raise_invalid_coordinate_exception(self): """ Test for Invalid Coordinate exception if rover is moved out of Plateau grid dimensions """ with self.assertRaises(InvalidCoordinateError): rover = Rover(self.plateau_dimensions, self.rover_initial_position, Rover.DIRECTIONS.get('E')) rover.execute_instructions('RMMM')
def test_rover_position(self): """ Test rover position after performing movements """ rover = Rover(self.plateau_dimensions, self.rover_initial_position, Rover.DIRECTIONS.get('E')) rover.execute_instructions("LMLM") self.assertEqual(rover._position.x, 1) self.assertEqual(rover._position.y, 2) self.assertEqual(rover.get_heading, 'W')
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 test_isInRange_returns_true_when_rover_inside_dimensions(self): # arrange rover = Rover(1, 3, "N") # act result = rover.isInRange((4, 4)) # assert self.assertEqual( result, True, )
def test_executeCommand_for_L_executes_spinLeft(self): # arrange rover = Rover(0, 0, "N") # act rover.executeCommand("L") # assert self.assertEqual( rover.orientation, "W", )
def test_spinRight_for_south_oriented_rover(self): # arrange rover = Rover(0, 0, "S") # act rover.spinRight() # assert self.assertEqual( rover.orientation, "W", )
def test_spinLeft_for_north_oriented_rover(self): # arrange rover = Rover(0, 0, "N") # act rover.spinLeft() # assert self.assertEqual( rover.orientation, "W", )
def test_isInRange_returns_false_when_rover_outside_dimensions(self): # arrange rover = Rover(1, 3, "N") # act result = rover.isInRange((1, 1)) # assert self.assertEqual( result, False, )
def test_executeCommand_for_R_executes_spinRight(self): # arrange rover = Rover(0, 0, "N") # act rover.executeCommand("R") # assert self.assertEqual( rover.orientation, "E", )
def test_executeCommand_for_M_executes_move(self): # arrange x = 2 y = 2 rover = Rover(x, y, "N") # act rover.executeCommand("M") # assert self.assertEqual( rover.yCoord, y + 1, )
def test_executeCommands_for_3_3_E_and_MMRMMRMRRM(self): # arrange rover = Rover(3, 3, "E") # act rover.executeCommands("MMRMMRMRRM") # assert with self.subTest(): self.assertEqual(rover.xCoord, 5) with self.subTest(): self.assertEqual(rover.yCoord, 1) with self.subTest(): self.assertEqual(rover.orientation, "E")
def test_move_for_north_oriented_rover(self): # arrange x = 0 y = 0 rover = Rover(x, y, "N") # act rover.move() # assert self.assertEqual( rover.yCoord, y + 1, )
def test_move_for_east_oriented_rover(self): # arrange x = 0 y = 0 rover = Rover(x, y, "E") # act rover.move() # assert self.assertEqual( rover.xCoord, x + 1, )
def test_move_for_south_oriented_rover(self): # arrange x = 2 y = 2 rover = Rover(x, y, "S") # act rover.move() # assert self.assertEqual( rover.yCoord, y - 1, )
def test_executeCommands_for_1_2_N_and_LMLMLMLMM(self): # arrange rover = Rover(1, 2, "N") # act rover.executeCommands("LMLMLMLMM") # assert with self.subTest(): self.assertEqual(rover.xCoord, 1) with self.subTest(): self.assertEqual(rover.yCoord, 3) with self.subTest(): self.assertEqual(rover.orientation, "N")
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')
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)
def test_get_heading(self): """ Test the rover heading direction """ rover = Rover(self.plateau_dimensions, self.rover_initial_position, Rover.DIRECTIONS.get('E')) self.assertEqual(rover.get_heading, 'E')
def test_get_heading_raise_exception(self): """ Test for exception for invalid rover heading direction from the the instructions """ rover = Rover(self.plateau_dimensions, self.rover_initial_position, 8) with self.assertRaises(InvalidDirectionError): rover.get_heading
def test_deploy_multiple_rovers(plateau): rover_a = Rover(name='RoverA', initial_position=(1, 2, 'N')) plateau.deploy_rover(rover_a) rover_a.execute_instructions('LMLMLMLMM') assert rover_a.current_position == 'RoverA:1 3 N' rover_b = Rover(name='RoverB', initial_position=(3, 3, 'E')) plateau.deploy_rover(rover_b) rover_b.execute_instructions('MMRMMRMRRM') assert rover_b.current_position == 'RoverB:5 1 E' assert len(plateau.deployed_rovers()) == 2
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 test_should_stop_before_collision(plateau): rover_a = Rover(name='RoverA', initial_position=(1, 2, 'N')) plateau.deploy_rover(rover_a) rover_a.execute_instructions('LMLMLMLMM') # 1 3 N assert rover_a.current_position == 'RoverA:1 3 N' rover_b = Rover(name='RoverB', initial_position=(3, 3, 'E')) plateau.deploy_rover(rover_b) executed_instructions = rover_b.execute_instructions('LLMMMRMLM') assert rover_b.current_position == 'RoverB:2 3 W' assert executed_instructions == 'LLM' assert len(plateau.deployed_rovers()) == 2
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))
def test_cannot_add_rover_at_taken_position(plateau): rover_a = Rover(name='RoverA', initial_position=(1, 2, 'N')) plateau.deploy_rover(rover_a) rover_b = Rover(name='RoverB', initial_position=(1, 2, 'E')) plateau.deploy_rover(rover_b) assert len(plateau.deployed_rovers()) == 1
def rover(plateau): rover = Rover(name='RoverX', initial_position=(3, 5, 'N')) plateau.deploy_rover(rover) return rover
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'))