From e617c1f7ce34436e301bd6a3a28abf314f0b1e0c Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Mon, 1 Nov 2021 13:41:18 +0000 Subject: [PATCH] support simple mutator exclusion for #954 --- README.md | 3 +- .../build/MutationDiscoveryTest.java | 1 - .../gregor/config/GregorEngineFactory.java | 2 +- .../engine/gregor/config/Mutator.java | 17 ++++++- .../config/GregorEngineFactoryTest.java | 48 +++++++++++++++++++ .../engine/gregor/config/MutatorTest.java | 12 +++++ 6 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactoryTest.java diff --git a/README.md b/README.md index b4f5cd433..aae9ac965 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ Read all about it at http://pitest.org ### 1.7.3 (unreleased) -* #952 Mutate map return to emptyMap instead of null +* #952 Mutate map return to `emptyMap` instead of null +* #954 Allow mutators to be excluded ### 1.7.2 diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationDiscoveryTest.java b/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationDiscoveryTest.java index 063458e5b..ba8f314ad 100755 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationDiscoveryTest.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationDiscoveryTest.java @@ -1,6 +1,5 @@ package org.pitest.mutationtest.build; -import com.example.HasPrivateStreamMethodUsedOnlyInSingleFlatMap; import org.junit.Before; import org.junit.Test; import org.pitest.classinfo.ClassByteArraySource; diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactory.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactory.java index 4680ae699..40a4d8b46 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactory.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactory.java @@ -34,7 +34,7 @@ public MutationEngine createEngine(EngineArguments args) { createMutatorListFromArrayOrUseDefaults(args.mutators())); } - public MutationEngine createEngineWithMutators( + MutationEngine createEngineWithMutators( final Collection excludedMethods, final Collection mutators) { diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java index 5a8d95509..1535e51b2 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java @@ -123,8 +123,23 @@ public static Collection byName(final String name) { public static Collection fromStrings( final Collection names) { + List exclusions = names.stream() + .filter(s -> s.startsWith("-")) + .map(s -> s.substring(1)) + .collect(Collectors.toList()); + + List inclusions = names.stream() + .filter(s -> !s.startsWith("-")) + .collect(Collectors.toList()); + final Set unique = new TreeSet<>(compareId()); - FCollection.flatMapTo(names, fromString(MUTATORS), unique); + FCollection.flatMapTo(inclusions, fromString(MUTATORS), unique); + + final Set excluded = new TreeSet<>(compareId()); + FCollection.flatMapTo(exclusions, fromString(MUTATORS), excluded); + + unique.removeAll(excluded); + return unique; } diff --git a/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactoryTest.java b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactoryTest.java new file mode 100644 index 000000000..c7bec0b2b --- /dev/null +++ b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/GregorEngineFactoryTest.java @@ -0,0 +1,48 @@ +package org.pitest.mutationtest.engine.gregor.config; + +import org.junit.Test; +import org.pitest.mutationtest.EngineArguments; +import org.pitest.mutationtest.engine.MutationEngine; + +import java.util.List; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class GregorEngineFactoryTest { + + GregorEngineFactory underTest = new GregorEngineFactory(); + + @Test + public void parsesSingleMutatorString() { + MutationEngine actual = parseMutators(asList("NULL_RETURNS")); + + assertThat(actual.getMutatorNames()).contains("NULL_RETURNS"); + } + + @Test + public void parsesGroups() { + MutationEngine actual = parseMutators(asList("STRONGER")); + + assertThat(actual.getMutatorNames()).contains("NULL_RETURNS", "REMOVE_CONDITIONALS_EQUAL_ELSE"); + } + + @Test + public void parsesExclusions() { + MutationEngine excluded = parseMutators(asList("STRONGER", "-REMOVE_CONDITIONALS_EQUAL_ELSE")); + + assertThat(excluded.getMutatorNames()).doesNotContain("REMOVE_CONDITIONALS_EQUAL_ELSE"); + assertThat(excluded.getMutatorNames()).isNotEmpty(); + } + + @Test + public void excludesGroups() { + MutationEngine excluded = parseMutators(asList("ALL", "-ALL")); + assertThat(excluded.getMutatorNames()).isEmpty(); + } + + private MutationEngine parseMutators(List mutators) { + return underTest.createEngine(EngineArguments.arguments().withMutators(mutators)); + } + +} \ No newline at end of file diff --git a/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/MutatorTest.java b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/MutatorTest.java index b3a9ebb20..98ba852e1 100644 --- a/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/MutatorTest.java +++ b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/config/MutatorTest.java @@ -22,6 +22,7 @@ import java.util.List; import org.junit.Test; +import org.pitest.mutationtest.engine.MutationEngine; import org.pitest.mutationtest.engine.gregor.MethodMutatorFactory; import org.pitest.mutationtest.engine.gregor.mutators.experimental.ArgumentPropagationMutator; import org.pitest.mutationtest.engine.gregor.mutators.InvertNegsMutator; @@ -290,6 +291,17 @@ public void overlappingGroupsDoNotCauseDuplicates() { .hasSameSizeAs(parseStrings("DEFAULTS", "INCREMENTS")); } + @Test + public void parsesExclusions() { + assertThat(parseStrings("DEFAULTS", "-INCREMENTS")) + .noneMatch(m -> m.getName().equals("INCREMENTS")); + } + + @Test + public void excludesGroups() { + assertThat(parseStrings("DEFAULTS", "STRONGER", "-ALL")).isEmpty(); + } + private Collection parseStrings(final String... s) { return Mutator.fromStrings(asList(s)); }