use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testIDNamingWithSameMethodGoals.
@Test
public void testIDNamingWithSameMethodGoals() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// method goals
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>()V");
TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "getPublicID()Ljava/lang/String;");
TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "setPublicID(Ljava/lang/String;)LFooClass;");
// output goals
TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "getPublicID()", Type.getType("Ljava.lang.String;"), REF_NONNULL));
TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "setPublicID(Ljava/lang/String;)", Type.getType("LFooClass;"), REF_NONNULL));
DefaultTestCase test1 = new DefaultTestCase();
// any statement to fool hashcode function
test1.addStatement(new IntPrimitiveStatement(test1, 1));
test1.addCoveredGoal(goal1);
test1.addCoveredGoal(goal2);
test1.addCoveredGoal(goal4);
DefaultTestCase test2 = new DefaultTestCase();
// any statement to fool hashcode function
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal3);
test2.addCoveredGoal(goal5);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
// TODO: testGetPublicID ?
assertEquals("Generated test name differs from expected", "testGetPublicIDTakingNoArguments", nameTest1);
// TODO: testSetPublicID ?
assertEquals("Generated test name differs from expected", "testSetPublicIDTakingString", nameTest2);
}
use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testConstructors.
@Test
public void testConstructors() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// method goal
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
// exception goal
TestFitnessFunction goal2 = new ExceptionCoverageTestFitness("FooClass", "<init>(LFooClass;)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
DefaultTestCase test1 = new DefaultTestCase();
test1.addCoveredGoal(goal1);
DefaultTestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal2);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
assertEquals("Generated test name differs from expected", "testCreatesFooClass", nameTest1);
assertEquals("Generated test name differs from expected", "testFailsToCreateFooClassThrowsArrayIndexOutOfBoundsException", nameTest2);
}
use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testOverloadedMethods.
@Test
public void testOverloadedMethods() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// Method goals
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values([I)[B");
// Output goals for method values
TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([I)", Type.getType("[B"), ARRAY_NONEMPTY));
DefaultTestCase test1 = new DefaultTestCase();
test1.addCoveredGoal(goal1);
test1.addCoveredGoal(goal2);
test1.addCoveredGoal(goal4);
DefaultTestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal3);
test2.addCoveredGoal(goal5);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
assertEquals("Generated test name differs from expected", "testValuesTakingByteArray", nameTest1);
assertEquals("Generated test name differs from expected", "testValuesTakingIntArray", nameTest2);
}
use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method getUniqueNonMethodGoals.
/**
* There is nothing unique about the test, so we have to add goals until we find a unique name
*/
private Set<TestFitnessFunction> getUniqueNonMethodGoals(TestCase test, Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
Set<TestFitnessFunction> allGoals = new LinkedHashSet<>();
for (Set<TestFitnessFunction> goals : testToGoals.values()) {
allGoals.addAll(goals);
}
Set<TestFitnessFunction> goals = new LinkedHashSet<>();
for (TestFitnessFunction goal : filterSupportedGoals(test.getCoveredGoals())) {
if (goal instanceof MethodCoverageTestFitness)
continue;
if (goal instanceof MethodNoExceptionCoverageTestFitness)
continue;
if (!allGoals.contains(goal)) {
goals.add(goal);
}
}
return goals;
}
use of org.evosuite.coverage.method.MethodCoverageTestFitness in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testMethodWithInputGoals.
@Test
public void testMethodWithInputGoals() {
TestCase test1 = new DefaultTestCase();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString");
InputCoverageGoal inputGoal1 = new InputCoverageGoal("FooClass", "toString", 0, objectType(), REF_NULL);
InputCoverageTestFitness goal2 = new InputCoverageTestFitness(inputGoal1);
test1.addCoveredGoal(goal1);
test1.addCoveredGoal(goal2);
TestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 0));
test2.addCoveredGoal(goal1);
InputCoverageGoal inputGoal2 = new InputCoverageGoal("FooClass", "toString", 0, objectType(), REF_NONNULL);
InputCoverageTestFitness goal3 = new InputCoverageTestFitness(inputGoal2);
test2.addCoveredGoal(goal3);
List<TestCase> tests = new ArrayList<>();
tests.add(test1);
tests.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
assertEquals("testToStringWithNull", naming.getName(test1));
assertEquals("testToStringWithNonNull", naming.getName(test2));
}
Aggregations