Skip to content

Commit

Permalink
add test on applyRandom()
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Dec 7, 2016
1 parent 91d5208 commit 49119b0
Showing 1 changed file with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,72 @@
public class TestMethodCallAdderTest {

@Test
public void testMethodCallAdd() throws Exception {
public void testMethodCallAddAll() throws Exception {

/*
Test that we duplicate method call in a test for each used method in the test.
Test that we reuse method call in a test for each used method in the test.
3 method are called in the original test, we produce 3 test methods.
*/

Launcher launcher = Utils.buildSpoon();
CtClass<Object> testClass = launcher.getFactory().Class().get("mutation.ClassUnderTestTest");

TestMethodCallAdder methodCallAdder = new TestMethodCallAdder();
AmplifierHelper.reset();
methodCallAdder.reset(null, null);

final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
List<CtMethod> amplifiedMethods = methodCallAdder.apply(originalMethod);

assertEquals(3, amplifiedMethods.size());

for (int i = 0 ; i < amplifiedMethods.size() ; i++) {
for (int i = 0; i < amplifiedMethods.size(); i++) {
CtMethod amplifiedMethod = amplifiedMethods.get(i);
assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
CtStatement expectedStatement = originalMethod.getBody().getStatements().get(i+1);//+1 to skip the construction statement.
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(i+1));
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(i+2));
CtStatement expectedStatement = originalMethod.getBody().getStatements().get(i + 1);//+1 to skip the construction statement.
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(i + 1));
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(i + 2));
}
}

@Test
public void testMethodCallAddRandom() throws Exception {

/*
Test that we duplicate method call in a test for each used method in the test.
Here, we test the applyRandom feature that will build one method randomly by reusing an existing call.
*/

AmplifierHelper.setSeedRandom(23L);
Launcher launcher = Utils.buildSpoon();
CtClass<Object> testClass = launcher.getFactory().Class().get("mutation.ClassUnderTestTest");

TestMethodCallAdder methodCallAdder = new TestMethodCallAdder();
methodCallAdder.reset(null, null);

final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
CtMethod amplifiedMethod = methodCallAdder.applyRandom(originalMethod);

assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
CtStatement expectedStatement = originalMethod.getBody().getStatements().get(2);
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(2));
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(3));


amplifiedMethod = methodCallAdder.applyRandom(originalMethod);
assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
expectedStatement = originalMethod.getBody().getStatements().get(3);
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(3));
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(4));

amplifiedMethod = methodCallAdder.applyRandom(originalMethod);
assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
expectedStatement = originalMethod.getBody().getStatements().get(1);
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(1));
assertEquals(expectedStatement, amplifiedMethod.getBody().getStatements().get(2));

// stack random amplification
CtMethod stackedAmplifiedMethod = methodCallAdder.applyRandom(amplifiedMethod);
assertEquals(amplifiedMethod.getBody().getStatements().size() + 1, stackedAmplifiedMethod.getBody().getStatements().size());
assertEquals(originalMethod.getBody().getStatements().size() + 2, stackedAmplifiedMethod.getBody().getStatements().size());
}
}

0 comments on commit 49119b0

Please sign in to comment.