Skip to content

Commit

Permalink
Merge pull request #952 from hcoles/feature/return_empty_map
Browse files Browse the repository at this point in the history
mutate map returns to emptyMap instead of null
  • Loading branch information
hcoles authored Oct 27, 2021
2 parents afc6fa1 + 89b0c2c commit dc0adab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Read all about it at http://pitest.org

## Releases

### 1.7.3 (unreleased)

* #952 Mutate map return to emptyMap instead of null

### 1.7.2

* #943 Change default mutators - replace negate conditional with remove conditional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ public boolean test(MutationDetails a) {
|| returns(method, mutatedInstruction, "java/util/Optional","empty")
|| returns(method, mutatedInstruction, "java/util/stream/Stream","empty")
|| returns(method, mutatedInstruction, "java/util/Collections","emptyList")
|| returns(method, mutatedInstruction, "java/util/Collections","emptyMap")
|| returns(method, mutatedInstruction, "java/util/Collections","emptySet")
|| returns(method, mutatedInstruction, "java/util/List","of")
|| returns(method, mutatedInstruction, "java/util/Set","of");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -143,6 +144,11 @@ public void filtersEquivalentListMutants() {
this.verifier.assertFiltersNMutationFromClass(1, AlreadyReturnsEmptyList.class);
}

@Test
public void filtersEquivalentMapMutants() {
this.verifier.assertFiltersNMutationFromClass(1, AlreadyReturnsEmptyMap.class);
}

@Test
public void filtersEquivalentListMutantsInTryCatch() {
this.verifier.assertFiltersNMutationFromClass(1, AlreadyReturnsEmptyListInTryCatch.class);
Expand Down Expand Up @@ -317,6 +323,11 @@ public List<Integer> a() {
}
}

class AlreadyReturnsEmptyMap {
public Map<Integer, Integer> a() {
return Collections.emptyMap();
}
}

class AlreadyReturnsEmptyListInTryCatch {
public List<Integer> a(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class AReturnMethodVisitor extends AbstractInsnMutator {
NON_NULL_MUTATIONS.put("java.util.Optional", returnEmptyOptional());
NON_NULL_MUTATIONS.put("java.util.stream.Stream", returnEmptyStream());
NON_NULL_MUTATIONS.put("java.util.List", returnEmptyList());
NON_NULL_MUTATIONS.put("java.util.Map", returnEmptyMap());
NON_NULL_MUTATIONS.put("java.util.Set", returnEmptySet());
NON_NULL_MUTATIONS.put("java.util.Collection", returnEmptyList());
}
Expand Down Expand Up @@ -204,6 +205,22 @@ public String describe(final int opCode, final MethodInfo methodInfo) {
};
}

private static ZeroOperandMutation returnEmptyMap() {
return new ZeroOperandMutation() {
@Override
public void apply(final int opCode, final MethodVisitor mv) {
mv.visitInsn(Opcodes.POP);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Collections", "emptyMap", "()Ljava/util/Map;", false);
mv.visitInsn(Opcodes.ARETURN);
}

@Override
public String describe(final int opCode, final MethodInfo methodInfo) {
return "replaced return value with Collections.emptyMap for " + methodInfo.getDescription();
}
};
}

private static ZeroOperandMutation returnEmptySet() {
return new ZeroOperandMutation() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -131,6 +133,12 @@ public void mutatesListToEmptyList() throws Exception {
createFirstMutant(AList.class), Collections.<String>emptyList());
}

@Test
public void mutatesMapToEmptyMap() throws Exception {
assertMutantCallableReturns(new AMap(),
createFirstMutant(AMap.class), Collections.emptyMap());
}

@Test
public void mutatesSetToEmptySet() throws Exception {
assertMutantCallableReturns(new ASet(),
Expand Down Expand Up @@ -235,6 +243,13 @@ public List<String> call() throws Exception {
}
}

private static class AMap implements Callable<Map<String, String>> {
@Override
public Map<String, String> call() throws Exception {
return new HashMap<>();
}
}

private static class ASet implements Callable<Set<String>> {
@Override
public Set<String> call() throws Exception {
Expand Down

0 comments on commit dc0adab

Please sign in to comment.