From 3d9be16af3a2d810b9c97ed62cf54705f351a337 Mon Sep 17 00:00:00 2001 From: Caro Date: Wed, 10 Jun 2020 14:31:41 +0200 Subject: [PATCH] Fix: pit minimizer on generated assertion try/catch (#951) --- .../prettifier/minimization/PitMutantMinimizer.java | 5 +++++ .../minimization/PitMutantMinimizerTest.java | 8 +++++++- .../sample/src/main/java/eu/stamp_project/App.java | 3 +++ .../src/test/java/eu/stamp_project/AppTest.java | 13 +++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizer.java b/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizer.java index d7d8735ce..419228459 100644 --- a/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizer.java +++ b/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizer.java @@ -244,6 +244,11 @@ private boolean check(List pitResultBeforeMinimization, List< CtMethod removeCloneAndInsert(final List> 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()); diff --git a/dspot-prettifier/src/test/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizerTest.java b/dspot-prettifier/src/test/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizerTest.java index 10a15f152..74da246df 100644 --- a/dspot-prettifier/src/test/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizerTest.java +++ b/dspot-prettifier/src/test/java/eu/stamp_project/prettifier/minimization/PitMutantMinimizerTest.java @@ -55,6 +55,12 @@ 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() { /* @@ -62,7 +68,7 @@ public void testPrintCompileAndRunPit() { */ final List abstractPitResults = minimizer.printCompileAndRunPit(testClass); - assertEquals(12, abstractPitResults.size()); + assertEquals(13, abstractPitResults.size()); System.out.println(abstractPitResults); } diff --git a/dspot-prettifier/src/test/resources/sample/src/main/java/eu/stamp_project/App.java b/dspot-prettifier/src/test/resources/sample/src/main/java/eu/stamp_project/App.java index fa7745c19..080758a1c 100644 --- a/dspot-prettifier/src/test/resources/sample/src/main/java/eu/stamp_project/App.java +++ b/dspot-prettifier/src/test/resources/sample/src/main/java/eu/stamp_project/App.java @@ -24,4 +24,7 @@ public void compute(int j) { i = i * j / 2; } + public void throwException() throws IllegalArgumentException { + throw new IllegalArgumentException("Illegal Arg"); + } } \ No newline at end of file diff --git a/dspot-prettifier/src/test/resources/sample/src/test/java/eu/stamp_project/AppTest.java b/dspot-prettifier/src/test/resources/sample/src/test/java/eu/stamp_project/AppTest.java index 9353e0ee2..0d2ad9494 100644 --- a/dspot-prettifier/src/test/resources/sample/src/test/java/eu/stamp_project/AppTest.java +++ b/dspot-prettifier/src/test/resources/sample/src/test/java/eu/stamp_project/AppTest.java @@ -1,5 +1,6 @@ package eu.stamp_project; +import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -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()); + } + } } \ No newline at end of file