def test_constructor_mutate_parameter_choose_none(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, [float0.return_value]) test_case.add_statement(float0) test_case.add_statement(const) assert const._mutate_parameter(0) assert isinstance( test_case.get_statement(const.args[0].get_statement_position()), prim.NoneStatement, )
def test_method_get_variable_replace(method_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 0.0) float1 = prim.FloatPrimitiveStatement(test_case, 5.0) float2 = prim.FloatPrimitiveStatement(test_case, 10.0) float3 = prim.FloatPrimitiveStatement(test_case, 10.0) meth = ps.MethodStatement( test_case, method_mock, float2.return_value, args=[float0.return_value], kwargs={"test": float1.return_value}, ) test_case.add_statement(float0) test_case.add_statement(float1) test_case.add_statement(float2) test_case.add_statement(float3) test_case.add_statement(meth) meth.replace(float2.return_value, float3.return_value) meth.replace(float1.return_value, float3.return_value) assert meth.callee == float3.return_value
def test_select_random_variable_for_call_one(constructor_mock, function_mock): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.NoneStatement(test_case, MagicMock)) test_case.add_statement(prim.FloatPrimitiveStatement(test_case, 5.0)) function_mock.inferred_signature.update_return_type(None) test_case.add_statement(par_stmt.FunctionStatement(test_case, function_mock)) const = par_stmt.ConstructorStatement(test_case, constructor_mock) test_case.add_statement(const) assert ( tf.TestFactory._select_random_variable_for_call(test_case, test_case.size()) == const.return_value )
def test_change_random_call_no_calls(function_mock): test_case = dtc.DefaultTestCase() float_prim = prim.FloatPrimitiveStatement(test_case, 5.0) float_function1 = par_stmt.FunctionStatement(test_case, function_mock, {"z": float_prim.ret_val}) test_case.add_statement(float_prim) test_case.add_statement(float_function1) test_cluster = MagicMock(TestCluster) test_cluster.get_generators_for.return_value = {function_mock} test_factory = tf.TestFactory(test_cluster) assert not test_factory.change_random_call(test_case, float_function1)
def test_get_reuse_parameters(): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) float1 = prim.FloatPrimitiveStatement(test_case, 5.0) test_case.add_statement(float0) test_case.add_statement(float1) sign_mock = MagicMock(inspect.Signature) params = {"test0": float, "test1": float} inf_sig = MagicMock(InferredSignature, parameters=params, signature=sign_mock) with mock.patch("pynguin.utils.randomness.next_float") as float_mock: float_mock.return_value = 0.0 with mock.patch("pynguin.testcase.testfactory.is_optional_parameter" ) as optional_mock: optional_mock.side_effect = [False, True] assert tf.TestFactory._get_reuse_parameters(test_case, inf_sig, 1) == { "test0": float0.ret_val }
def test__rollback_changes_mid(): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5)) test_case.add_statement(prim.IntPrimitiveStatement(test_case, 10)) test_case.add_statement(prim.IntPrimitiveStatement(test_case, 15)) cloned = test_case.clone() test_case.add_statement(prim.FloatPrimitiveStatement(test_case, 7.5), 1) assert cloned != test_case tf.TestFactory._rollback_changes(test_case, cloned.size(), 1) assert cloned == test_case
def test_constructor_mutate_parameter_get_objects(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, [float0.return_value]) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(const, "_test_case") as tc: tc.get_objects.return_value = [float0.return_value] assert const._mutate_parameter(0) tc.get_objects.assert_called_with(float0.return_value.variable_type, const.get_position())
def test_method_get_variable_references(method_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 0.0) float1 = prim.FloatPrimitiveStatement(test_case, 5.0) float2 = prim.FloatPrimitiveStatement(test_case, 10.0) meth = ps.MethodStatement( test_case, method_mock, float2.ret_val, args=[float0.ret_val], kwargs={"test": float1.ret_val}, ) test_case.add_statement(float0) test_case.add_statement(float1) test_case.add_statement(float2) test_case.add_statement(meth) assert meth.get_variable_references() == { float0.ret_val, float1.ret_val, float2.ret_val, meth.ret_val, }
def test_change_call_unknown(): test_case = dtc.DefaultTestCase() test_case.add_statement(prim.FloatPrimitiveStatement(test_case, 3.5)) to_replace = prim.NoneStatement(test_case, float) test_case.add_statement(to_replace) test_cluster = MagicMock(TestCluster) test_factory = tf.TestFactory(test_cluster) acc = MagicMock(gao.GenericAccessibleObject) acc.is_method.return_value = False acc.is_constructor.return_value = False acc.is_function.return_value = False with pytest.raises(AssertionError): test_factory.change_call(test_case, to_replace, acc)
def test_constructor_mutate_parameter_not_included(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, [float0.return_value]) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(test_case, "get_objects") as get_objs: get_objs.return_value = [] assert const._mutate_parameter(0) get_objs.assert_called_with(float0.return_value.variable_type, 1) assert isinstance( test_case.get_statement(const.args[0].get_statement_position()), prim.NoneStatement, )
def simple_test_case(function_mock) -> dtc.DefaultTestCase: test_case = dtc.DefaultTestCase() int_prim = prim.IntPrimitiveStatement(test_case, 5) int_prim2 = prim.IntPrimitiveStatement(test_case, 5) float_prim = prim.FloatPrimitiveStatement(test_case, 5.5) func = ps.FunctionStatement(test_case, function_mock, [float_prim.return_value]) string_prim = prim.StringPrimitiveStatement(test_case, "Test") string_prim.return_value.variable_type = type(None) test_case.add_statement(int_prim) test_case.add_statement(int_prim2) test_case.add_statement(float_prim) test_case.add_statement(func) test_case.add_statement(string_prim) return test_case
def test_constructor_mutate_parameter_choose_none(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, {"a": float0.ret_val}) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch("pynguin.testcase.testfactory.is_optional_parameter" ) as optional_mock: optional_mock.return_value = False assert const._mutate_parameter("a", MagicMock(parameters={"a": float})) assert isinstance( test_case.get_statement(const.args["a"].get_statement_position()), prim.NoneStatement, )
def simple_test_case(function_mock) -> dtc.DefaultTestCase: test_case = dtc.DefaultTestCase() int_prim = prim.IntPrimitiveStatement(test_case, 5) int_prim2 = prim.IntPrimitiveStatement(test_case, 5) float_prim = prim.FloatPrimitiveStatement(test_case, 5.5) func = ps.FunctionStatement(test_case, function_mock, {"z": float_prim.ret_val}) func.add_assertion(pas.PrimitiveAssertion(func.ret_val, 3.1415)) string_prim = prim.StringPrimitiveStatement(test_case, "Test") string_prim.ret_val.variable_type = type(None) test_case.add_statement(int_prim) test_case.add_statement(int_prim2) test_case.add_statement(float_prim) test_case.add_statement(func) test_case.add_statement(string_prim) return test_case
def test_constructor_mutate_parameter_get_objects(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, {"a": float0.ret_val}) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(const, "_test_case") as tc: tc.get_objects.return_value = [float0.ret_val] with mock.patch("pynguin.testcase.testfactory.is_optional_parameter" ) as optional_mock: optional_mock.return_value = False assert const._mutate_parameter("a", MagicMock(parameters={"a": float})) tc.get_objects.assert_called_with(float0.ret_val.variable_type, const.get_position())
def test_constructor_mutate_parameter_add_copy(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, [float0.return_value]) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(const, "_param_count_of_type") as param_count_of_type: with mock.patch("pynguin.utils.randomness.choice") as choice_mock: choice_mock.side_effect = lambda coll: coll[0] param_count_of_type.return_value = 5 assert const._mutate_parameter(0) param_count_of_type.assert_called_with( float0.return_value.variable_type) assert const.args[0] != float0.return_value
def test_method_mutate_special_parameters_none_found(method_mock, constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const0 = ps.ConstructorStatement(test_case, constructor_mock, [float0.return_value]) int0 = prim.IntPrimitiveStatement(test_case, 5) meth = ps.MethodStatement(test_case, method_mock, const0.return_value) test_case.add_statement(float0) test_case.add_statement(const0) test_case.add_statement(int0) test_case.add_statement(meth) with mock.patch.object(meth, "_test_case") as tc: tc.get_objects.return_value = [const0.return_value] assert not meth._mutate_special_parameters(1.0) tc.get_objects.assert_called_with(const0.return_value.variable_type, meth.get_position())
def _create_primitive( test_case: tc.TestCase, parameter_type: Type, position: int, recursion_depth: int, ) -> vr.VariableReference: if parameter_type == int: statement: prim.PrimitiveStatement = prim.IntPrimitiveStatement( test_case) elif parameter_type == float: statement = prim.FloatPrimitiveStatement(test_case) elif parameter_type == bool: statement = prim.BooleanPrimitiveStatement(test_case) else: statement = prim.StringPrimitiveStatement(test_case) ret = test_case.add_statement(statement, position) ret.distance = recursion_depth return ret
def test_change_random_call_success(function_mock, method_mock, constructor_mock): test_case = dtc.DefaultTestCase() float_prim = prim.FloatPrimitiveStatement(test_case, 5.0) int0 = prim.IntPrimitiveStatement(test_case, 2) float_function1 = par_stmt.FunctionStatement(test_case, function_mock, [float_prim.return_value]) const = par_stmt.ConstructorStatement(test_case, constructor_mock) test_case.add_statement(float_prim) test_case.add_statement(int0) test_case.add_statement(const) test_case.add_statement(float_function1) test_cluster = MagicMock(TestCluster) test_cluster.get_generators_for.return_value = {function_mock, method_mock} test_factory = tf.TestFactory(test_cluster) with mock.patch.object(test_factory, "change_call") as change_mock: assert test_factory.change_random_call(test_case, float_function1) change_mock.assert_called_with(test_case, float_function1, method_mock)
def test_constructor_mutate_parameter_not_included(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, {"a": float0.ret_val}) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(test_case, "get_objects") as get_objs: get_objs.return_value = [] with mock.patch("pynguin.testcase.testfactory.is_optional_parameter" ) as optional_mock: optional_mock.return_value = False assert const._mutate_parameter("a", MagicMock(parameters={"a": float})) get_objs.assert_called_with(float0.ret_val.variable_type, 1) assert isinstance( test_case.get_statement( const.args["a"].get_statement_position()), prim.NoneStatement, )
def test_constructor_mutate_parameter_add_copy(constructor_mock): test_case = dtc.DefaultTestCase() float0 = prim.FloatPrimitiveStatement(test_case, 5.0) const = ps.ConstructorStatement(test_case, constructor_mock, {"a": float0.ret_val}) test_case.add_statement(float0) test_case.add_statement(const) with mock.patch.object(const, "_param_count_of_type") as param_count_of_type: with mock.patch("pynguin.utils.randomness.choice") as choice_mock: choice_mock.side_effect = lambda coll: coll[0] param_count_of_type.return_value = 5 with mock.patch( "pynguin.testcase.testfactory.is_optional_parameter" ) as optional_mock: optional_mock.return_value = False assert const._mutate_parameter( "a", MagicMock(parameters={"a": float})) param_count_of_type.assert_called_with( float0.ret_val.variable_type) assert const.args["a"] != float0.ret_val
def create_stmt_from_unaryop( unaryop: ast.UnaryOp, testcase: tc.TestCase) -> Optional[prim_stmt.PrimitiveStatement]: """Creates a statement from an ast.unaryop node. Args: unaryop: the ast.UnaryOp statement testcase: the testcase containing the statement Returns: The corresponding statement. """ val = unaryop.operand.value # type: ignore if isinstance(val, bool): return prim_stmt.BooleanPrimitiveStatement(testcase, not val) if isinstance(val, float): return prim_stmt.FloatPrimitiveStatement(testcase, (-1) * val) if isinstance(val, int): return prim_stmt.IntPrimitiveStatement(testcase, (-1) * val) logger.info( "Could not find case for unary operator while handling assign statement." ) return None
def test_float_primitive_statement_randomize_value(test_case_mock): statement = prim.FloatPrimitiveStatement(test_case_mock) statement.randomize_value() assert isinstance(statement.value, float)
def test_primitive_statement_replace_ignore(test_case_mock): statement = prim.IntPrimitiveStatement(test_case_mock, 0) new = prim.FloatPrimitiveStatement(test_case_mock, 0).return_value old = statement.return_value statement.replace(new, new) assert statement.return_value == old