Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(632): Make the theshold code more testable and allow limit of 0 surviving mutants #633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
*/
package org.pitest.mutationtest.commandline;

import java.util.HashMap;

import org.pitest.coverage.CoverageSummary;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.ReportOptions;
import org.pitest.mutationtest.statistics.MutationStatistics;
import org.pitest.mutationtest.tooling.AnalysisResult;
import org.pitest.mutationtest.tooling.CombinedStatistics;
import org.pitest.mutationtest.tooling.EntryPoint;
import org.pitest.util.Unchecked;

import java.util.HashMap;

/**
* Entry point for command line interface
*/
Expand All @@ -35,6 +33,7 @@ public static void main(final String[] args) {
final PluginServices plugins = PluginServices.makeForContextLoader();
final OptionsParser parser = new OptionsParser(new PluginFilter(plugins));
final ParseResult pr = parser.parse(args);
final ThresholdValidator thresholdValidator = new ThresholdValidator();

if (!pr.isOk()) {
parser.printHelp();
Expand All @@ -44,42 +43,16 @@ public static void main(final String[] args) {

final CombinedStatistics stats = runReport(data, plugins);

throwErrorIfScoreBelowCoverageThreshold(stats.getCoverageSummary(),
thresholdValidator.throwErrorIfScoreBelowCoverageThreshold(stats.getCoverageSummary(),
data.getCoverageThreshold());
throwErrorIfScoreBelowMutationThreshold(stats.getMutationStatistics(),
thresholdValidator.throwErrorIfScoreBelowMutationThreshold(stats.getMutationStatistics(),
data.getMutationThreshold());
throwErrorIfMoreThanMaxSuvivingMutants(stats.getMutationStatistics(), data.getMaximumAllowedSurvivors());
thresholdValidator.throwErrorIfMoreThanMaxSurvivingMutants(stats.getMutationStatistics(),
data.getMaximumAllowedSurvivors());
}

}

private static void throwErrorIfScoreBelowCoverageThreshold(
CoverageSummary stats, int threshold) {
if ((threshold != 0) && (stats.getCoverage() < threshold)) {
throw new RuntimeException("Line coverage of " + stats.getCoverage()
+ " is below threshold of " + threshold);
}
}

private static void throwErrorIfScoreBelowMutationThreshold(
final MutationStatistics stats, final int threshold) {
if ((threshold != 0) && (stats.getPercentageDetected() < threshold)) {
throw new RuntimeException("Mutation score of "
+ stats.getPercentageDetected() + " is below threshold of "
+ threshold);
}
}

private static void throwErrorIfMoreThanMaxSuvivingMutants(
final MutationStatistics stats, final long threshold) {
if ((threshold > 0)
&& (stats.getTotalSurvivingMutations() > threshold)) {
throw new RuntimeException("Had "
+ stats.getTotalSurvivingMutations() + " surviving mutants, but only "
+ threshold + " survivors allowed");
}
}

private static CombinedStatistics runReport(ReportOptions data,
PluginServices plugins) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.pitest.mutationtest.commandline;

import org.pitest.coverage.CoverageSummary;
import org.pitest.mutationtest.statistics.MutationStatistics;

public class ThresholdValidator {

public void throwErrorIfScoreBelowCoverageThreshold(CoverageSummary stats, int threshold) {
if ((threshold != 0) && (stats.getCoverage() < threshold)) {
throw new RuntimeException("Line coverage of " + stats.getCoverage()
+ " is below threshold of " + threshold);
}
}

public void throwErrorIfScoreBelowMutationThreshold(
final MutationStatistics stats, final int threshold) {
if ((threshold != 0) && (stats.getPercentageDetected() < threshold)) {
throw new RuntimeException("Mutation score of "
+ stats.getPercentageDetected() + " is below threshold of "
+ threshold);
}
}

public void throwErrorIfMoreThanMaxSurvivingMutants(
final MutationStatistics stats, final long threshold) {
if ((threshold >= 0)
&& (stats.getTotalSurvivingMutations() > threshold)) {
throw new RuntimeException("Had "
+ stats.getTotalSurvivingMutations() + " surviving mutants, but only "
+ threshold + " survivors allowed");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package org.pitest.mutationtest.commandline;

import org.junit.Test;
import org.pitest.coverage.CoverageSummary;
import org.pitest.mutationtest.statistics.MutationStatistics;
import org.pitest.mutationtest.statistics.Score;

import java.util.Collections;

import static org.junit.Assert.fail;

public class ThresholdValidatorTest {

private final ThresholdValidator testee = new ThresholdValidator();

@Test
public void shouldThrowErrorIfScoreBelowCoverageThreshold() throws Exception {
final CoverageSummary coverageSummary = new CoverageSummary(1, 0);

try {
this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 1);
fail();
} catch (RuntimeException e) {
}
}

@Test
public void shouldNotThrowErrorIfScoreBelowCoverageThresholdZero() throws Exception {
final CoverageSummary coverageSummary = new CoverageSummary(1, 0);

try {
this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 0);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfScoreAboveCoverage() throws Exception {
final CoverageSummary coverageSummary = new CoverageSummary(2, 1);

try {
this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 49);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfScoreEqualToCoverage() throws Exception {
final CoverageSummary coverageSummary = new CoverageSummary(2, 1);

try {
this.testee.throwErrorIfScoreBelowCoverageThreshold(coverageSummary, 50);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldThrowErrorIfScoreBelowMutationThreshold() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0);

try {
this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 1);
fail();
} catch (RuntimeException e) {
}
}

@Test
public void shouldNotThrowErrorIfScoreBelowMutationThresholdZero() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0);

try {
this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 0);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfScoreAboveMutationThreshold() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 4, 1, 0);

try {
this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 24);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfScoreEqualToMutationThreshold() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 4, 1, 0);

try {
this.testee.throwErrorIfScoreBelowMutationThreshold(mutationStatistics, 25);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldThrowErrorIfMoreThanMaxSurvivingMutantsThresholdZero() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0);

try {
this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 0);
fail();
} catch (RuntimeException e) {
}
}

@Test
public void shouldNotThrowErrorIfEqualToMaxSurvivingMutantsThresholdZero() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0);

try {
this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfLessThanMaxSurvivingMutantsThresholdOne() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 1, 0);

try {
this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldNotThrowErrorIfEqualToMaxSurvivingMutantsThresholdOne() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 2, 1, 0);

try {
this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1);
} catch (RuntimeException e) {
fail();
}
}

@Test
public void shouldThrowErrorIfMoreThanMaxSurvivingMutantsThresholdOne() throws Exception {
final Iterable<Score> scores = Collections.emptyList();
final MutationStatistics mutationStatistics = new MutationStatistics(scores, 1, 0, 0);

try {
this.testee.throwErrorIfMoreThanMaxSurvivingMutants(mutationStatistics, 1);
} catch (RuntimeException e) {
fail();
}
}

}