def test_place(self): ''' This function will be used to test if the 'place' function works in Robot's method ''' self.robot.place_robot(Coordinate(0, 0), Direction("east")) assert self.robot.coordinate == Coordinate(0, 0) assert self.robot.direction == Direction("east")
def test_place_move(self): ''' Will be used to test the place functionality and the move functionality ''' self.robot.place_robot(Coordinate(1, 2), Direction("east")) self.robot.move_robot() self.robot.move_robot() # at this point the new coordinate should be 3,2 assert self.robot.coordinate == Coordinate(3, 2) assert self.robot.direction == Direction("east")
def test_place_move_turn(self): ''' This test will placing the robot, moving it, and turning it ''' self.robot.place_robot(Coordinate(1, 2), Direction("east")) self.robot.move_robot() self.robot.move_robot() self.robot.turn_robot(-1) # left is -1 and right it 1 self.robot.move_robot() # at this point the robot should be at 3,3,NORTH assert self.robot.coordinate == Coordinate(3, 3) assert self.robot.direction == Direction("north")
def propagate(self): while self.removal_stack: (tile_index, cell_index) = self.removal_stack.pop() for direction in Direction: neighbor_index = self.get_neighbor_index(cell_index, direction) try: neighbor_cell = self.grid[neighbor_index] except IndexError: continue if neighbor_cell.collapsed: continue for adj in self.adjacencies.get_compatible( tile_index, direction): opposite_direction = Direction.opposite(direction) enabler_counts = neighbor_cell.adjacency_tile_counts[adj] if enabler_counts[opposite_direction.value] == 1: if neighbor_cell.possible[adj]: neighbor_cell.remove_possibility(adj) if not any(neighbor_cell.possible): raise RuntimeError( "Hit a contradiction and cannot continue.") heapq.heappush( self.entropy_heap, (neighbor_cell.entropy(), neighbor_index)) self.removal_stack.append((adj, neighbor_index)) neighbor_cell.adjacency_tile_counts[adj][ opposite_direction.value] -= 1
def test_invalid_int_coordinates(self): ''' This function will test invalid coordinates as in ('alpha', 2) -> alpha is not int so should throw an error ''' with pytest.raises(InvalidIntError): self.robot.place_robot(Coordinate('alpha', 0), Direction("east"))
def test_place_command(self): ''' Will be used to test the place command ''' cmd = Place(["1", "2", "east"]) cmd.execute(self.robot) assert self.robot.coordinate == Coordinate(1,2) assert self.robot.direction == Direction("east")
def test_right_turns(self): ''' Place the robot on the table and test by turning all the way right ''' self.robot.place_robot(Coordinate(1, 1), Direction("south")) self.robot.turn_robot(1) # left is -1 and right it 1 assert self.robot.direction == Direction("west") self.robot.turn_robot(1) # left is -1 and right it 1 assert self.robot.direction == Direction("north") self.robot.turn_robot(1) # left is -1 and right it 1 assert self.robot.direction == Direction("east") self.robot.turn_robot(1) # left is -1 and right it 1 assert self.robot.direction == Direction("south")
def test_left_turns(self): ''' Place the robot and then test by turning all the way left to see if each direction works ''' self.robot.place_robot(Coordinate(1, 1), Direction("south")) self.robot.turn_robot(-1) # left is -1 and right it 1 assert self.robot.direction == Direction("east") self.robot.turn_robot(-1) # left is -1 and right it 1 assert self.robot.direction == Direction("north") #should be north self.robot.turn_robot(-1) # left is -1 and right it 1 assert self.robot.direction == Direction("west") #should be west self.robot.turn_robot(-1) # left is -1 and right it 1 assert self.robot.direction == Direction("south") #should be south
def execute(self, robot): ''' actually perform the 'place' command ''' (x, y, direction) = self._args if not self._coordinate: self._coordinate = Coordinate(x, y) if not self._direction: self._direction = Direction(direction) robot.place_robot(self._coordinate, self._direction) #call the robot function -->
def args(self, argument_values): Input.args.fset(self, argument_values) # for place commands there should be 3 args if not argument_values or len(argument_values) != 3: raise InvalidPlaceCommandError() (x, y, direction) = argument_values try: self._coordinate = Coordinate(x, y) self._direction = Direction(direction) except InvalidDirectionError as e: return
def test_move_command(self): ''' This function will test the move command to see if the coordinate updates as expected ''' cmd = Place(["1", "2", "east"]) cmd.execute(self.robot) # now issue the move command cmd = Move() cmd.execute(self.robot) # at this point the coordinate should be 2,2 assert self.robot.coordinate == Coordinate(2,2) assert self.robot.direction == Direction("east")
def test_left(self): ''' This function will be used test the "left" command. And see if the direction updates ''' cmd = Place(["1", "2", "east"]) cmd.execute(self.robot) # now issue the left cmd cmd = Left() cmd.execute(self.robot) # at this point the values should be 1,2,north assert self.robot.coordinate == Coordinate(1,2) assert self.robot.direction == Direction("north")
def test_thorough_move_robot(self): ''' This test will be a thorough test, with a few different components to see if they all work well together ''' cmd = self.parse_input.parse("place 1,2,east") cmd.execute(self.robot) ## check the parse assert cmd.value == "place" assert cmd.args == ["1", "2", "east"] ## check the robot assert self.robot.coordinate == Coordinate(1,2) assert self.robot.direction == Direction("east")
def test_off_table_error(self): ''' This function aims to test the OffTableError and checks if it gets raised ''' with pytest.raises(OffTableError): self.robot.place_robot(Coordinate(5, 5), Direction("east"))