Skip to content

Commit

Permalink
Fix: pit minimizer on generated assertion try/catch (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
lacinoire authored Jun 10, 2020
1 parent 5d39a2c commit 3d9be16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ private boolean check(List<AbstractPitResult> pitResultBeforeMinimization, List<

CtMethod<?> removeCloneAndInsert(final List<CtInvocation<?>> assertions, CtMethod<?> amplifiedTestToBeMinimized, int indexOfAssertion) {
final int index = amplifiedTestToBeMinimized.getBody().getStatements().indexOf(assertions.get(indexOfAssertion));
if (index == -1) {
// targeted assertion is not on first level of test to be minimized
// e.g. assertion generator wrapped everything in a try catch block
return amplifiedTestToBeMinimized;
}
amplifiedTestToBeMinimized.getBody().removeStatement(assertions.get(indexOfAssertion));
final CtMethod<?> clone = amplifiedTestToBeMinimized.clone();
amplifiedTestToBeMinimized.getBody().addStatement(index, assertions.get(indexOfAssertion).clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ public void test() {
assertEquals(4, minimize.getBody().getStatements().size());
}

@Test
public void testOnTryCatchAssertion() {
this.testMethod = Utils.findMethod(testClass, "test2_failAssert0");
final CtMethod<?> minimize = minimizer.minimize(testMethod);
}

@Test
public void testPrintCompileAndRunPit() {
/*
Test that the Minimizer is able to print, compile and run PIT
*/

final List<AbstractPitResult> abstractPitResults = minimizer.printCompileAndRunPit(testClass);
assertEquals(12, abstractPitResults.size());
assertEquals(13, abstractPitResults.size());
System.out.println(abstractPitResults);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public void compute(int j) {
i = i * j / 2;
}

public void throwException() throws IllegalArgumentException {
throw new IllegalArgumentException("Illegal Arg");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.stamp_project;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand All @@ -17,4 +18,16 @@ public void test1() {
app.compute(3);
assertEquals(60, app.getInt());
}

@Test
public void test2_failAssert0() throws Exception {
// AssertionGenerator generate try/catch block with fail statement
try {
App app = new App(4);
app.throwException();
Assert.fail("test2 should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
Assert.assertEquals("Illegal Arg", expected.getMessage());
}
}
}

0 comments on commit 3d9be16

Please sign in to comment.