def test_should_skip_parameter(param_name, result): def inner_func(normal: str, *args, **kwargs): pass inf_sig = MagicMock(InferredSignature, signature=inspect.signature(inner_func)) assert should_skip_parameter(inf_sig, param_name) == result
def _get_reuse_parameters(test_case: tc.TestCase, inf_signature: InferredSignature, position: int) -> List[vr.VariableReference]: """Find specified parameters from existing objects.""" found = [] for parameter_name, parameter_type in inf_signature.parameters.items(): if should_skip_parameter(inf_signature, parameter_name): continue assert parameter_type found.append(test_case.get_random_object(parameter_type, position)) return found
def _discard_accessible_with_missing_type_hints( accessible_object: GenericCallableAccessibleObject, ) -> bool: """Should we discard accessible objects that are not fully type hinted? :param accessible_object: the object to check """ if config.INSTANCE.guess_unknown_types: return False inf_sig = accessible_object.inferred_signature return any([ not should_skip_parameter(inf_sig, param) and type_ is None for param, type_ in inf_sig.parameters.items() ])
def _discard_accessible_with_missing_type_hints( accessible_object: GenericCallableAccessibleObject, ) -> bool: """Should we discard accessible objects that are not fully type hinted? Args: accessible_object: the object to check Returns: Whether or not the accessible should be discarded """ if config.configuration.guess_unknown_types: return False inf_sig = accessible_object.inferred_signature return any(not should_skip_parameter(inf_sig, param) and type_ is None for param, type_ in inf_sig.parameters.items())
def satisfy_parameters( self, test_case: tc.TestCase, signature: InferredSignature, callee: Optional[vr.VariableReference] = None, position: int = -1, recursion_depth: int = 0, allow_none: bool = True, can_reuse_existing_variables: bool = True, ) -> List[vr.VariableReference]: """Satisfy a list of parameters by reusing or creating variables. :param test_case: The test case :param signature: The inferred signature of the method :param callee: The callee of the method :param position: The current position in the test case :param recursion_depth: The recursion depth :param allow_none: Whether or not a variable can be a None value :param can_reuse_existing_variables: Whether or not existing variables shall be reused. :return: A list of variable references for the parameters """ if position < 0: position = test_case.size() parameters: List[vr.VariableReference] = [] self._logger.debug( "Trying to satisfy %d parameters at position %d", len(signature.parameters), position, ) for parameter_name, parameter_type in signature.parameters.items(): self._logger.debug("Current parameter type: %s", parameter_type) previous_length = test_case.size() if should_skip_parameter(signature, parameter_name): # TODO Implement generation for positional parameters of variable length # TODO Implement generation for keyword parameters of variable length self._logger.info("Skip parameter %s", parameter_name) continue if can_reuse_existing_variables: self._logger.debug("Can re-use variables") var = self._create_or_reuse_variable( test_case, parameter_type, position, recursion_depth, allow_none, callee, ) else: self._logger.debug( "Cannot re-use variables: attempt to creating new one") var = self._create_variable( test_case, parameter_type, position, recursion_depth, allow_none, callee, ) if not var: raise ConstructionFailedException( f"Failed to create variable for type {parameter_type} " f"at position {position}", ) parameters.append(var) current_length = test_case.size() position += current_length - previous_length self._logger.debug("Satisfied %d parameters", len(parameters)) return parameters