Esempio n. 1
0
def test_set_statement_valid(default_test_case):
    int0 = prim.IntPrimitiveStatement(default_test_case, 5)
    int1 = prim.IntPrimitiveStatement(default_test_case, 5)
    default_test_case.add_statement(int0)
    default_test_case.add_statement(int1)
    assert default_test_case.set_statement(int1, 0) == int1.return_value
    assert default_test_case.get_statement(0) == int1
Esempio n. 2
0
def test__rollback_changes_nothing_to_rollback():
    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()

    tf.TestFactory._rollback_changes(test_case, cloned.size(), 3)
    assert cloned == test_case
def test_get_last_mutatable_statement_too_large(
        test_case_chromosome_with_test):
    chromosome, test_case = test_case_chromosome_with_test
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    result = MagicMock(ExecutionResult)
    result.has_test_exceptions.return_value = True
    result.get_first_position_of_thrown_exception.return_value = 4
    chromosome.set_last_execution_result(result)
    assert chromosome._get_last_mutatable_statement() == chromosome.size() - 1
Esempio n. 4
0
def test_get_last_mutatable_statement_too_large(default_test_case):
    default_test_case.add_statement(
        prim.IntPrimitiveStatement(default_test_case, 5))
    default_test_case.add_statement(
        prim.IntPrimitiveStatement(default_test_case, 5))
    result = MagicMock(ExecutionResult)
    result.has_test_exceptions.return_value = True
    result.get_first_position_of_thrown_exception.return_value = 4
    default_test_case.set_last_execution_result(result)
    assert (default_test_case._get_last_mutatable_statement() ==
            default_test_case.size() - 1)
Esempio n. 5
0
def test_constructor_replace_args(constructor_mock):
    test_case = dtc.DefaultTestCase()
    int0 = prim.IntPrimitiveStatement(test_case, 0)
    new_value = prim.IntPrimitiveStatement(test_case, 0)
    const = ps.ConstructorStatement(test_case, constructor_mock,
                                    {"a": int0.ret_val})
    test_case.add_statement(int0)
    test_case.add_statement(new_value)
    test_case.add_statement(const)
    const.replace(int0.ret_val, new_value.ret_val)
    assert const.args == {"a": new_value.ret_val}
Esempio n. 6
0
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
Esempio n. 7
0
def test_field_statement_eq_clone(field_mock):
    test_case1 = dtc.DefaultTestCase()
    test_case1.add_statement(prim.IntPrimitiveStatement(test_case1, 0))
    test_case2 = dtc.DefaultTestCase()
    test_case2.add_statement(prim.IntPrimitiveStatement(test_case2, 0))

    statement = fstmt.FieldStatement(test_case1, field_mock,
                                     test_case1.statements[0].ret_val)
    test_case1.add_statement(statement)
    clone = statement.clone(test_case2)
    test_case2.add_statement(clone)
    assert statement.__eq__(clone)
Esempio n. 8
0
def test_mutation_delete_not_empty():
    test_case = dtc.DefaultTestCase()
    int0 = prim.IntPrimitiveStatement(test_case, 5)
    int1 = prim.IntPrimitiveStatement(test_case, 5)
    test_case.add_statement(int0)
    test_case.add_statement(int1)
    with mock.patch("pynguin.utils.randomness.next_float") as float_mock:
        float_mock.side_effect = [0.0, 1.0]
        with mock.patch.object(test_case, "_delete_statement") as delete_mock:
            delete_mock.return_value = True
            assert test_case._mutation_delete()
            delete_mock.assert_has_calls([call(1)])
            assert delete_mock.call_count == 1
Esempio n. 9
0
def test_assignment_statement_clone():
    test_case = dtc.DefaultTestCase()
    int_prim = prim.IntPrimitiveStatement(test_case, 5)
    int_prim2 = prim.IntPrimitiveStatement(test_case, 10)
    # TODO(fk) the assignment statement from EvoSuite might not be fitting for our case?
    # Because currently we can only overwrite existing values?
    assignment_stmt = assign.AssignmentStatement(test_case, int_prim.ret_val,
                                                 int_prim2.ret_val)
    test_case.add_statement(int_prim)
    test_case.add_statement(int_prim2)
    test_case.add_statement(assignment_stmt)

    cloned = test_case.clone()
    assert isinstance(cloned.statements[2], assign.AssignmentStatement)
    assert cloned.statements[2] is not assignment_stmt
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
Esempio n. 11
0
def test_int_primitive_statement_delta(test_case_mock):
    config.INSTANCE.max_delta = 10
    statement = prim.IntPrimitiveStatement(test_case_mock, 1)
    with mock.patch("pynguin.utils.randomness.next_gaussian") as gauss_mock:
        gauss_mock.return_value = 0.5
        statement.delta()
    assert statement.value == 6
Esempio n. 12
0
def test_delete_statement(result):
    test_factory = MagicMock(tf.TestFactory)
    test_factory.delete_statement_gracefully.return_value = result
    test_case = dtc.DefaultTestCase(test_factory)
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    assert test_case._delete_statement(0) == result
    test_factory.delete_statement_gracefully.assert_called_with(test_case, 0)
Esempio n. 13
0
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
Esempio n. 14
0
def test_insert_random_statement_non_empty():
    test_case = dtc.DefaultTestCase()
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    test_cluster = MagicMock(TestCluster)
    test_cluster.num_accessible_objects_under_test.return_value = 1
    test_factory = tf.TestFactory(test_cluster)
    with mock.patch("pynguin.utils.randomness.next_float") as float_mock:
        float_mock.return_value = 0.0
        with mock.patch.object(test_factory, "insert_random_call") as ins_mock:
            ins_mock.return_value = True
            assert test_factory.insert_random_statement(
                test_case, test_case.size()) in range(test_case.size() + 1)
            assert ins_mock.call_args_list[0].args[1] in range(
                test_case.size() + 1)
Esempio n. 15
0
def create_stmt_from_constant(
        constant: ast.Constant,
        testcase: tc.TestCase) -> Optional[prim_stmt.PrimitiveStatement]:
    """Creates a statement from an ast.constant node.

    Args:
        constant: the ast.Constant statement
        testcase: the testcase containing the statement

    Returns:
        The corresponding statement.
    """
    if constant.value is None:
        return prim_stmt.NoneStatement(testcase, constant.value)

    val = constant.value
    if isinstance(val, bool):
        return prim_stmt.BooleanPrimitiveStatement(testcase, val)
    if isinstance(val, int):
        return prim_stmt.IntPrimitiveStatement(testcase, val)
    if isinstance(val, float):
        return prim_stmt.FloatPrimitiveStatement(testcase, val)
    if isinstance(val, str):
        return prim_stmt.StringPrimitiveStatement(testcase, val)
    if isinstance(val, bytes):
        return prim_stmt.BytesPrimitiveStatement(testcase, val)
    logger.info(
        "Could not find case for constant while handling assign statement.")
    return None
Esempio n. 16
0
def test_mutation_change_single_prim(default_test_case):
    int0 = prim.IntPrimitiveStatement(default_test_case, 5)
    int0.return_value.distance = 5
    default_test_case.add_statement(int0)
    with mock.patch("pynguin.utils.randomness.next_float") as float_mock:
        float_mock.side_effect = [0.0]
        assert default_test_case._mutation_change()
        assert int0.return_value.distance == 5
Esempio n. 17
0
def test_primitive_statement_replace_ignore(field_mock):
    test_case = dtc.DefaultTestCase()
    ref = prim.IntPrimitiveStatement(test_case, 5)
    statement = fstmt.FieldStatement(test_case, field_mock, ref.return_value)
    new = prim.FloatPrimitiveStatement(test_case, 0).return_value
    old = statement.source
    statement.replace(new, new)
    assert statement.source == old
def test_simple_execution():
    config.INSTANCE.module_name = "tests.fixtures.accessibles.accessible"
    tracer = ExecutionTracer()
    with install_import_hook(config.INSTANCE.module_name, tracer):
        test_case = dtc.DefaultTestCase()
        test_case.add_statement(prim_stmt.IntPrimitiveStatement(test_case, 5))
        executor = TestCaseExecutor(tracer)
        assert not executor.execute([test_case]).has_test_exceptions()
def test_constructor_replace_return_value(constructor_mock):
    test_case = dtc.DefaultTestCase()
    new_value = prim.IntPrimitiveStatement(test_case, 0)
    const = ps.ConstructorStatement(test_case, constructor_mock)
    test_case.add_statement(new_value)
    test_case.add_statement(const)
    const.replace(const.return_value, new_value.return_value)
    assert const.return_value == new_value.return_value
Esempio n. 20
0
def short_test_case(constructor_mock):
    test_case = dtc.DefaultTestCase()
    int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5)
    constructor_stmt = param_stmt.ConstructorStatement(test_case,
                                                       constructor_mock,
                                                       [int_stmt.return_value])
    test_case.add_statement(int_stmt)
    test_case.add_statement(constructor_stmt)
    return test_case
def test_mutation_change_single_prim(test_case_chromosome_with_test):
    chromosome, test_case = test_case_chromosome_with_test
    int0 = prim.IntPrimitiveStatement(test_case, 5)
    int0.return_value.distance = 5
    test_case.add_statement(int0)
    with mock.patch("pynguin.utils.randomness.next_float") as float_mock:
        float_mock.side_effect = [0.0]
        assert chromosome._mutation_change()
        assert int0.return_value.distance == 5
Esempio n. 22
0
def test_primitive_statement_replace(field_mock):
    test_case = dtc.DefaultTestCase()
    ref = prim.IntPrimitiveStatement(test_case, 5)
    test_case.add_statement(ref)
    statement = fstmt.FieldStatement(test_case, field_mock, ref.return_value)
    test_case.add_statement(statement)
    new = vri.VariableReferenceImpl(test_case, int)

    statement.replace(ref.return_value, new)
    assert statement.source == new
def test_simple_execution():
    config.configuration.module_name = "tests.fixtures.accessibles.accessible"
    tracer = ExecutionTracer()
    with install_import_hook(config.configuration.module_name, tracer):
        module = importlib.import_module(config.configuration.module_name)
        importlib.reload(module)
        test_case = dtc.DefaultTestCase()
        test_case.add_statement(prim_stmt.IntPrimitiveStatement(test_case, 5))
        executor = TestCaseExecutor(tracer)
        assert not executor.execute(test_case).has_test_exceptions()
def simple_test_case(constructor_mock):
    test_case = dtc.DefaultTestCase()
    int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5)
    constructor_stmt = param_stmt.ConstructorStatement(test_case,
                                                       constructor_mock,
                                                       [int_stmt.return_value])
    constructor_stmt.add_assertion(
        pas.PrimitiveAssertion(constructor_stmt.return_value, 3))
    test_case.add_statement(int_stmt)
    test_case.add_statement(constructor_stmt)
    return test_case
Esempio n. 25
0
def test_change_call_method(constructor_mock, method_mock):
    test_case = dtc.DefaultTestCase()
    test_case.add_statement(par_stmt.ConstructorStatement(test_case, constructor_mock))
    test_case.add_statement(prim.IntPrimitiveStatement(test_case, 3))
    to_replace = prim.NoneStatement(test_case, float)
    test_case.add_statement(to_replace)
    test_cluster = MagicMock(TestCluster)
    test_factory = tf.TestFactory(test_cluster)
    test_factory.change_call(test_case, to_replace, method_mock)
    assert test_case.statements[2].accessible_object() == method_mock
    assert test_case.statements[2].return_value is to_replace.return_value
def test_constructor_replace_kwargs(constructor_mock):
    test_case = dtc.DefaultTestCase()
    int0 = prim.IntPrimitiveStatement(test_case, 0)
    int1 = prim.IntPrimitiveStatement(test_case, 0)
    new_value = prim.IntPrimitiveStatement(test_case, 0)
    const = ps.ConstructorStatement(
        test_case,
        constructor_mock,
        kwargs={
            "test": int0.return_value,
            "test2": int1.return_value
        },
    )
    test_case.add_statement(int0)
    test_case.add_statement(int1)
    test_case.add_statement(new_value)
    test_case.add_statement(const)
    const.replace(int0.return_value, new_value.return_value)
    assert const.kwargs == {
        "test": new_value.return_value,
        "test2": int1.return_value
    }
def test_illegal_call(method_mock):
    config.INSTANCE.module_name = "tests.fixtures.accessibles.accessible"
    test_case = dtc.DefaultTestCase()
    int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5)
    method_stmt = param_stmt.MethodStatement(test_case, method_mock,
                                             int_stmt.return_value)
    test_case.add_statement(int_stmt)
    test_case.add_statement(method_stmt)
    tracer = ExecutionTracer()
    with install_import_hook(config.INSTANCE.module_name, tracer):
        executor = TestCaseExecutor(tracer)
        result = executor.execute([test_case])
        assert result.has_test_exceptions()
def test_mutate_no_chop(test_case_chromosome_with_test):
    chromosome, test_case = test_case_chromosome_with_test
    for i in range(50):
        test_case.add_statement(prim.IntPrimitiveStatement(test_case, 5))
    chromosome.set_changed(False)
    config.INSTANCE.test_insert_probability = 0.0
    config.INSTANCE.test_change_probability = 0.0
    config.INSTANCE.test_delete_probability = 0.0
    with mock.patch.object(chromosome,
                           "_get_last_mutatable_statement") as mut_mock:
        mut_mock.return_value = None
        chromosome.mutate()
        assert len(test_case.statements) == 50
        assert not chromosome.has_changed()
Esempio n. 29
0
def test_mutate_no_chop(default_test_case):
    for i in range(50):
        default_test_case.add_statement(
            prim.IntPrimitiveStatement(default_test_case, 5))
    default_test_case.set_changed(False)
    config.INSTANCE.test_insert_probability = 0.0
    config.INSTANCE.test_change_probability = 0.0
    config.INSTANCE.test_delete_probability = 0.0
    with mock.patch.object(default_test_case,
                           "_get_last_mutatable_statement") as mut_mock:
        mut_mock.return_value = None
        default_test_case.mutate()
        assert len(default_test_case.statements) == 50
        assert not default_test_case.has_changed()
def test_illegal_call(method_mock):
    config.configuration.module_name = "tests.fixtures.accessibles.accessible"
    test_case = dtc.DefaultTestCase()
    int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5)
    method_stmt = param_stmt.MethodStatement(test_case, method_mock,
                                             int_stmt.ret_val)
    test_case.add_statement(int_stmt)
    test_case.add_statement(method_stmt)
    tracer = ExecutionTracer()
    with install_import_hook(config.configuration.module_name, tracer):
        module = importlib.import_module(config.configuration.module_name)
        importlib.reload(module)
        executor = TestCaseExecutor(tracer)
        result = executor.execute(test_case)
        assert result.has_test_exceptions()