-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move assert filtering to interceptor
- Loading branch information
Henry Coles
committed
Jun 9, 2022
1 parent
6424f24
commit 6697597
Showing
9 changed files
with
237 additions
and
248 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...ntry/src/main/java/org/pitest/mutationtest/build/intercept/javafeatures/AssertFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.FieldInsnNode; | ||
import org.objectweb.asm.tree.LabelNode; | ||
import org.pitest.bytecode.analysis.MethodTree; | ||
import org.pitest.mutationtest.build.intercept.Region; | ||
import org.pitest.mutationtest.build.intercept.RegionInterceptor; | ||
import org.pitest.sequence.Context; | ||
import org.pitest.sequence.Match; | ||
import org.pitest.sequence.QueryParams; | ||
import org.pitest.sequence.QueryStart; | ||
import org.pitest.sequence.SequenceMatcher; | ||
import org.pitest.sequence.Slot; | ||
import org.pitest.sequence.SlotWrite; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.pitest.bytecode.analysis.InstructionMatchers.anyInstruction; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.jumpsTo; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.notAnInstruction; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.IFNE; | ||
import static org.pitest.sequence.Result.result; | ||
|
||
|
||
public class AssertFilter extends RegionInterceptor { | ||
|
||
static final Slot<AbstractInsnNode> START = Slot.create(AbstractInsnNode.class); | ||
static final Slot<LabelNode> END = Slot.create(LabelNode.class); | ||
|
||
static final SequenceMatcher<AbstractInsnNode> ASSERT_GET = QueryStart | ||
.any(AbstractInsnNode.class) | ||
.then(getStatic("$assertionsDisabled").and(store(START.write()))) | ||
.then(IFNE.and(jumpsTo(END.write()))) | ||
.zeroOrMore(QueryStart.match(anyInstruction())) | ||
.compile(QueryParams.params(AbstractInsnNode.class) | ||
.withIgnores(notAnInstruction()) | ||
); | ||
|
||
private static Match<AbstractInsnNode> getStatic(String name) { | ||
return (c,n) -> { | ||
if (n instanceof FieldInsnNode) { | ||
return result(((FieldInsnNode) n).name.equals(name), c); | ||
} | ||
return result(false,c); | ||
}; | ||
} | ||
|
||
|
||
private static Match<AbstractInsnNode> store(SlotWrite<AbstractInsnNode> slot) { | ||
return (c,n) -> result(true, c.store(slot, n)); | ||
} | ||
|
||
protected List<Region> computeRegions(MethodTree method) { | ||
Context context = Context.start(); | ||
List<Region> regions = ASSERT_GET.contextMatches(method.instructions(), context).stream() | ||
.map(c -> new Region(c.retrieve(START.read()).get(), c.retrieve(END.read()).get())) | ||
.collect(Collectors.toList()); | ||
return regions; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...in/java/org/pitest/mutationtest/build/intercept/javafeatures/AssertionsFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
import org.pitest.mutationtest.build.InterceptorParameters; | ||
import org.pitest.mutationtest.build.MutationInterceptor; | ||
import org.pitest.mutationtest.build.MutationInterceptorFactory; | ||
import org.pitest.plugin.Feature; | ||
|
||
public class AssertionsFilterFactory implements MutationInterceptorFactory { | ||
|
||
@Override | ||
public String description() { | ||
return "Assertions filter"; | ||
} | ||
|
||
@Override | ||
public MutationInterceptor createInterceptor(InterceptorParameters params) { | ||
return new AssertFilter(); | ||
} | ||
|
||
@Override | ||
public Feature provides() { | ||
return Feature.named("FASSERT") | ||
.withOnByDefault(true) | ||
.withDescription("Filters mutations in compiler generated code for assertions"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...ry/src/test/java/org/pitest/mutationtest/build/intercept/javafeatures/AssertionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
import org.junit.Test; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.FieldInsnNode; | ||
import org.objectweb.asm.tree.JumpInsnNode; | ||
import org.pitest.mutationtest.engine.gregor.mutators.NullMutateEverything; | ||
import org.pitest.verifier.interceptors.InterceptorVerifier; | ||
import org.pitest.verifier.interceptors.VerifierStart; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import static org.pitest.bytecode.analysis.InstructionMatchers.isA; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.ATHROW; | ||
|
||
public class AssertionsTest { | ||
AssertionsFilterFactory underTest = new AssertionsFilterFactory(); | ||
InterceptorVerifier v = VerifierStart.forInterceptorFactory(underTest) | ||
.usingMutator(new NullMutateEverything()); | ||
|
||
@Test | ||
public void doesNotFilterCodeWithNoAsserts() { | ||
v.forClass(NoAsserts.class) | ||
.forAnyCode() | ||
.mutantsAreGenerated() | ||
.noMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void filtersAssertCode() { | ||
v.forClass(HasAssertStatement.class) | ||
.forMethod("foo") | ||
.forCodeMatching(assertInstructions()) | ||
.mutantsAreGenerated() | ||
.allMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void filtersAssertCodeWhenOtherStatementsPresent() { | ||
v.forClass(HasAssertStatementAndOtherStatements.class) | ||
.forMethod("foo") | ||
.forCodeMatching(sysoutFieldReference()) | ||
.mutantsAreGenerated() | ||
.noMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void leavesCodeNotInAssert() { | ||
v.forClass(HasAssertStatementAndOtherStatements.class) | ||
.forMethod("foo") | ||
.forCodeMatching(sysoutFieldReference()) | ||
.mutantsAreGenerated() | ||
.noMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void filtersMultipleAsserts() { | ||
v.forClass(MultipleAsserts.class) | ||
.forMethod("foo") | ||
.forCodeMatching(assertInstructions()) | ||
.mutantsAreGenerated() | ||
.allMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void leavesCodeNotInAssertWhenMultipleAsserts() { | ||
v.forClass(MultipleAsserts.class) | ||
.forMethod("foo") | ||
.forCodeMatching(assertInstructions()) | ||
.mutantsAreGenerated() | ||
.allMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
private Predicate<AbstractInsnNode> assertInstructions() { | ||
return isA(JumpInsnNode.class).or(ATHROW).asPredicate().or(fieldAccess()); | ||
} | ||
|
||
private Predicate<? super AbstractInsnNode> fieldAccess() { | ||
return isA(FieldInsnNode.class).asPredicate().and(sysoutFieldReference().negate()); | ||
} | ||
|
||
private Predicate<AbstractInsnNode> sysoutFieldReference() { | ||
return n -> n instanceof FieldInsnNode && ((FieldInsnNode) n).owner.equals("java/lang/System"); | ||
} | ||
|
||
static class HasAssertStatement { | ||
public void foo(final int i) { | ||
assert ((i + 20) > 10); | ||
} | ||
} | ||
|
||
static class HasAssertStatementAndOtherStatements { | ||
public int state; | ||
|
||
public void foo(final int i) { | ||
System.out.println("before"); | ||
assert ((i + 20) > 10); | ||
System.out.println("after"); | ||
} | ||
} | ||
|
||
static class NoAsserts { | ||
static boolean aField = true; | ||
public void foo(final int i) { | ||
if (aField) { | ||
throw new AssertionError(); | ||
} | ||
if (i > 10) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
} | ||
|
||
static class MultipleAsserts { | ||
public int state; | ||
|
||
public void foo(final int i) { | ||
assert (i != 11); | ||
System.out.println("before"); | ||
assert ((i + 20) > 10); | ||
System.out.println("middle"); | ||
assert (i != 10); | ||
System.out.println("after"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 0 additions & 87 deletions
87
pitest/src/main/java/org/pitest/mutationtest/engine/gregor/AvoidAssertsMethodAdapter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.