-
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.
filter null stream mutations called only by flatmap
- Loading branch information
Henry Coles
committed
Aug 11, 2021
1 parent
0910d03
commit da553a6
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
...y/src/main/java/org/pitest/mutationtest/build/intercept/equivalent/NullFlatMapFilter.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,53 @@ | ||
package org.pitest.mutationtest.build.intercept.equivalent; | ||
|
||
import org.objectweb.asm.Opcodes; | ||
import org.pitest.bytecode.analysis.ClassTree; | ||
import org.pitest.bytecode.analysis.MethodTree; | ||
import org.pitest.classinfo.ClassName; | ||
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.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class NullFlatMapFilter implements MutationInterceptor { | ||
private ClassTree currentClass; | ||
|
||
@Override | ||
public InterceptorType type() { | ||
return InterceptorType.FILTER; | ||
} | ||
|
||
@Override | ||
public void begin(ClassTree clazz) { | ||
currentClass = clazz; | ||
} | ||
|
||
@Override | ||
public Collection<MutationDetails> intercept(Collection<MutationDetails> mutations, Mutater unused) { | ||
return mutations.stream() | ||
.filter(m -> !this.mutatesStreamEmpty(m)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean mutatesStreamEmpty(MutationDetails mutationDetails) { | ||
MethodTree mutated = currentClass.method(mutationDetails.getId().getLocation()).get(); | ||
if (!mutated.isPrivate() || !mutated.returns(ClassName.fromClass(Stream.class))) { | ||
return false; | ||
} | ||
|
||
if (mutated.instruction(mutationDetails.getInstructionIndex()).getOpcode() != Opcodes.ARETURN) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public void end() { | ||
currentClass = null; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ain/java/org/pitest/mutationtest/build/intercept/equivalent/NullFlatMapFilterFactory.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,24 @@ | ||
package org.pitest.mutationtest.build.intercept.equivalent; | ||
|
||
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 NullFlatMapFilterFactory implements MutationInterceptorFactory { | ||
|
||
@Override | ||
public MutationInterceptor createInterceptor(InterceptorParameters params) { | ||
return new NullFlatMapFilter(); | ||
} | ||
|
||
@Override | ||
public Feature provides() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String description() { | ||
return null; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...try/src/test/java/org/pitest/mutationtest/build/intercept/equivalent/NullFlatmapTest.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,97 @@ | ||
package org.pitest.mutationtest.build.intercept.equivalent; | ||
|
||
import org.junit.Test; | ||
import org.pitest.mutationtest.build.InterceptorType; | ||
import org.pitest.mutationtest.build.MutationInterceptor; | ||
import org.pitest.mutationtest.build.intercept.javafeatures.FilterTester; | ||
import org.pitest.mutationtest.engine.gregor.mutators.NullReturnValsMutator; | ||
import org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class NullFlatmapTest { | ||
|
||
MutationInterceptor testee = new NullFlatMapFilterFactory().createInterceptor(null); | ||
|
||
FilterTester verifier = new FilterTester("", this.testee, NullReturnValsMutator.NULL_RETURN_VALUES, | ||
VoidMethodCallMutator.VOID_METHOD_CALL_MUTATOR); | ||
|
||
@Test | ||
public void declaresTypeAsFilter() { | ||
assertThat(this.testee.type()).isEqualTo(InterceptorType.FILTER); | ||
} | ||
|
||
@Test | ||
public void filtersNullReturnMutantWhenMethodUsedOnlyInFlatMap() { | ||
verifier.assertFiltersNMutationFromClass(111, HasPrivateStreamMethodUsedOnlyInSingleFlatMap.class); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterNullReturnsInPublicStreamMethods() { | ||
verifier.assertFiltersNMutationFromClass(0, HasPublicStreamMethodUsedOnlyInSingleFlatMap.class); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterOtherMutantsInNullReturnsStreamMethods() { | ||
verifier.assertFiltersNMutationFromClass(1, HasPrivateStreamMethodUsedOnlyInSingleFlatMapThatHasOtherMutableCode.class); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterNullReturnsWhenOriginalNotEmptyStream() { | ||
verifier.assertFiltersNMutationFromClass(0, HasPrivateStreamMethodThatDoesNotReturnEmpty.class); | ||
} | ||
|
||
} | ||
|
||
class HasPrivateStreamMethodUsedOnlyInSingleFlatMap { | ||
public Stream<String> makesCall(List<String> l) { | ||
return l.stream() | ||
.flatMap(this::aStream); | ||
|
||
} | ||
|
||
private Stream<String> aStream(String l) { | ||
return Stream.empty(); | ||
} | ||
} | ||
|
||
class HasPrivateStreamMethodUsedOnlyInSingleFlatMapThatHasOtherMutableCode { | ||
public Stream<String> makesCall(List<String> l) { | ||
return l.stream() | ||
.flatMap(this::aStream); | ||
|
||
} | ||
|
||
private Stream<String> aStream(String l) { | ||
System.out.println("Keep mutating me"); | ||
return Stream.empty(); | ||
} | ||
} | ||
|
||
class HasPrivateStreamMethodThatDoesNotReturnEmpty { | ||
public Stream<String> makesCall(List<String> l) { | ||
return l.stream() | ||
.flatMap(this::aStream); | ||
|
||
} | ||
|
||
private Stream<String> aStream(String l) { | ||
return Stream.of(""); | ||
} | ||
} | ||
|
||
|
||
class HasPublicStreamMethodUsedOnlyInSingleFlatMap { | ||
public Stream<String> makesCall(List<String> l) { | ||
return l.stream() | ||
.flatMap(this::aStream); | ||
|
||
} | ||
|
||
public Stream<String> aStream(String l) { | ||
return Stream.empty(); | ||
} | ||
} |