-
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.
Merge pull request #728 from hcoles/enum_constructors
adds filter for enum constructors for #556
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...main/java/org/pitest/mutationtest/build/intercept/javafeatures/EnumConstructorFilter.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,49 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
import org.pitest.bytecode.analysis.ClassTree; | ||
import org.pitest.mutationtest.build.InterceptorType; | ||
import org.pitest.mutationtest.build.MutationInterceptor; | ||
import org.pitest.mutationtest.engine.Mutater; | ||
import org.pitest.mutationtest.engine.MutationDetails; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Filters out mutations in Enum constructors, these are called only once | ||
* per instance so are effectively static initializers. | ||
*/ | ||
public class EnumConstructorFilter implements MutationInterceptor { | ||
|
||
private boolean isEnum; | ||
|
||
@Override | ||
public InterceptorType type() { | ||
return InterceptorType.FILTER; | ||
} | ||
|
||
@Override | ||
public void begin(ClassTree clazz) { | ||
this.isEnum = clazz.rawNode().superName.equals("java/lang/Enum"); | ||
} | ||
|
||
@Override | ||
public Collection<MutationDetails> intercept( | ||
Collection<MutationDetails> mutations, Mutater m) { | ||
return mutations.stream() | ||
.filter(isInEnumConstructor().negate()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Predicate<MutationDetails> isInEnumConstructor() { | ||
return m -> isEnum && m.getMethod().name().equals("<init>"); | ||
} | ||
|
||
|
||
@Override | ||
public void end() { | ||
} | ||
|
||
} | ||
|
28 changes: 28 additions & 0 deletions
28
...va/org/pitest/mutationtest/build/intercept/javafeatures/EnumConstructorFilterFactory.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,28 @@ | ||
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 EnumConstructorFilterFactory implements MutationInterceptorFactory { | ||
|
||
@Override | ||
public String description() { | ||
return "Enum constructor filter"; | ||
} | ||
|
||
@Override | ||
public MutationInterceptor createInterceptor(InterceptorParameters params) { | ||
return new EnumConstructorFilter(); | ||
} | ||
|
||
@Override | ||
public Feature provides() { | ||
return Feature.named("FENUM") | ||
.withOnByDefault(true) | ||
.withDescription("Filters mutations in enum constructors"); | ||
} | ||
|
||
} | ||
|
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
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
13 changes: 13 additions & 0 deletions
13
pitest-entry/src/test/java/org/pitest/mutationtest/build/intercept/javafeatures/AnEnum.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,13 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
public enum AnEnum { | ||
AN_INSTANCE("hello"); | ||
|
||
AnEnum(String s) { | ||
System.out.println(s); | ||
} | ||
|
||
public void aMethod() { | ||
System.out.println("dont mutate me"); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...c/test/java/org/pitest/mutationtest/build/intercept/javafeatures/EnumConstructorTest.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,39 @@ | ||
package org.pitest.mutationtest.build.intercept.javafeatures; | ||
|
||
import org.junit.Test; | ||
import org.pitest.mutationtest.engine.MutationDetails; | ||
import java.util.function.Predicate; | ||
|
||
import static org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator.VOID_METHOD_CALL_MUTATOR; | ||
|
||
public class EnumConstructorTest { | ||
|
||
EnumConstructorFilter testee = new EnumConstructorFilter(); | ||
|
||
FilterTester verifier = new FilterTester("unused", this.testee, VOID_METHOD_CALL_MUTATOR); | ||
|
||
@Test | ||
public void filtersMutantsFromEnumConstructor() { | ||
this.verifier.assertFiltersMutationsMatching(inMethodNamed("<init>"), AnEnum.class); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterMutantsInCustomEnumMethods() { | ||
this.verifier.assertFiltersNoMutationsMatching(inMethodNamed("aMethod"), AnEnum.class); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterMutantsInNonEnumConstructors() { | ||
this.verifier.assertFiltersNoMutationsMatching(inMethodNamed("<init>"), AClass.class); | ||
} | ||
|
||
private Predicate<MutationDetails> inMethodNamed(String name) { | ||
return m -> m.getMethod().name().equals(name); | ||
} | ||
} | ||
|
||
class AClass { | ||
AClass(String s) { | ||
System.out.println(s); | ||
} | ||
} |
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