def is_primitive(self) -> bool: """Does this variable reference represent a primitive type. Returns: True if the variable is a primitive """ return type_utils.is_primitive_type(self._variable_type)
def _attempt_generation( self, test_case: tc.TestCase, parameter_type: Optional[Type], position: int, recursion_depth: int, allow_none: bool, exclude: Optional[vr.VariableReference] = None, ) -> Optional[vr.VariableReference]: # We only select a concrete type e.g. from a union, when we are forced to choose one. parameter_type = self._test_cluster.select_concrete_type( parameter_type) if not parameter_type: return None if allow_none and randomness.next_float( ) <= config.INSTANCE.none_probability: return self._create_none(test_case, parameter_type, position, recursion_depth) if is_primitive_type(parameter_type): return self._create_primitive( test_case, parameter_type, position, recursion_depth, ) if type_generators := self._test_cluster.get_generators_for( parameter_type): return self._attempt_generation_for_type(test_case, position, recursion_depth, allow_none, type_generators)
def _add_callable_dependencies( self, call: GenericCallableAccessibleObject, recursion_level: int ) -> None: """Add required dependencies. Args: call: The object whose parameter types should be added as dependencies. recursion_level: The current level of recursion of the search """ self._logger.debug("Find dependencies for %s", call) if recursion_level > config.INSTANCE.max_cluster_recursion: self._logger.debug("Reached recursion limit. No more dependencies added.") return for param_name, type_ in call.inferred_signature.parameters.items(): self._logger.debug("Resolving '%s' (%s)", param_name, type_) types = {type_} if is_union_type(type_): types = set(get_args(type_)) for elem in types: if is_primitive_type(elem): self._logger.debug("Not following primitive argument.") continue if inspect.isclass(elem): assert elem self._logger.debug("Adding dependency for class %s", elem) self._dependencies_to_solve.add( DependencyPair(elem, recursion_level) ) else: self._logger.debug("Found typing annotation %s, skipping", elem)
def add_generator(self, generator: GenericAccessibleObject) -> None: """Add the given accessible as a generator, if the type is known, not primitive and not NoneType.""" type_ = generator.generated_type() if ( type_ is None or type_utils.is_none_type(type_) or type_utils.is_primitive_type(type_) ): return if type_ in self._generators: self._generators[type_].add(generator) else: self._generators[type_] = {generator}
def _reuse_variable(self, test_case: tc.TestCase, parameter_type: Optional[Type], position: int) -> Optional[vr.VariableReference]: """Reuse an existing variable, if possible.""" objects = test_case.get_objects(parameter_type, position) probability = (config.INSTANCE.primitive_reuse_probability if is_primitive_type(parameter_type) else config.INSTANCE.object_reuse_probability) if objects and randomness.next_float() <= probability: var = randomness.choice(objects) self._logger.debug("Reusing variable %s for type %s", var, parameter_type) return var return None
def handle(self, statement: st.Statement) -> None: """Actually handle the given statement. Args: statement: the statement that is visited. """ value = self._exec_ctx.get_variable_value(self._variable) if is_primitive_type(type(value)): return self._trace.add_entry( statement.get_position(), self._variable, nte.NoneTraceEntry(self._variable, value is None), )
def _reuse_variable(self, test_case: tc.TestCase, parameter_type: Optional[Type], position: int) -> Optional[vr.VariableReference]: """Reuse an existing variable, if possible. Args: test_case: the test case to take the variable from parameter_type: the type of the variable that is needed position: the position to limit the search Returns: A matching existing variable, if existing """ objects = test_case.get_objects(parameter_type, position) probability = (config.INSTANCE.primitive_reuse_probability if is_primitive_type(parameter_type) else config.INSTANCE.object_reuse_probability) if objects and randomness.next_float() <= probability: var = randomness.choice(objects) self._logger.debug("Reusing variable %s for type %s", var, parameter_type) return var return None
def test_is_primitive_type(type_, result): assert is_primitive_type(type_) == result