Skip to content

Commit

Permalink
Merge pull request #1159 from hcoles/feature/report_limited_coverage
Browse files Browse the repository at this point in the history
Limit line coverage to only mutated classes
  • Loading branch information
hcoles authored Feb 27, 2023
2 parents ba064c1 + e01dcef commit 5945e95
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
*/
package org.pitest.mutationtest.statistics;

import org.pitest.classinfo.ClassName;

import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.Set;

public final class MutationStatistics {
private final Iterable<Score> scores;
Expand All @@ -26,13 +29,20 @@ public final class MutationStatistics {
private final long totalDetected;
private final long totalWithCoverage;

public MutationStatistics(Iterable<Score> scores, long totalMutations,
long totalDetected, long totalWithCoverage, long numberOfTestsRun) {
private final Set<ClassName> mutatedClasses;

public MutationStatistics(Iterable<Score> scores,
long totalMutations,
long totalDetected,
long totalWithCoverage,
long numberOfTestsRun,
Set<ClassName> mutatedClasses) {
this.scores = scores;
this.totalMutations = totalMutations;
this.totalDetected = totalDetected;
this.numberOfTestsRun = numberOfTestsRun;
this.totalWithCoverage = totalWithCoverage;
this.mutatedClasses = mutatedClasses;
}

public Iterable<Score> getScores() {
Expand All @@ -59,6 +69,10 @@ public long getTotalSurvivingMutations() {
return getTotalMutations() - getTotalDetectedMutations();
}

public Set<ClassName> mutatedClasses() {
return mutatedClasses;
}

public long getPercentageDetected() {
if (getTotalMutations() == 0) {
return 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void runEnd() {
}

private void processMetaData(final ClassMutationResults value) {
this.mutatorScores.registerClass(value.getMutatedClass());
this.mutatorScores.registerResults(value.getMutations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import org.pitest.classinfo.ClassName;
import org.pitest.functional.FCollection;
import org.pitest.mutationtest.MutationResult;

class MutationStatisticsPrecursor {
private final Map<String, ScorePrecursor> mutatorTotalMap = new HashMap<>();
private final Set<ClassName> mutatedClasses = new HashSet<>();
private long numberOfTestsRun = 0;

public void registerResults(final Collection<MutationResult> results) {
results.forEach(register());
}

public void registerClass(ClassName mutatedClass) {
mutatedClasses.add(mutatedClass);
}

public Set<ClassName> mutatedClasses() {
return mutatedClasses;
}

private Consumer<MutationResult> register() {
return mr -> {
MutationStatisticsPrecursor.this.numberOfTestsRun = MutationStatisticsPrecursor.this.numberOfTestsRun
Expand All @@ -39,7 +51,7 @@ public MutationStatistics toStatistics() {
.fold(addDetectedTotals(), 0L, scores);
final long totalWithCoverage = FCollection.fold(addCoveredTotals(), 0L, scores);
return new MutationStatistics(scores, totalMutations, totalDetected, totalWithCoverage,
this.numberOfTestsRun);
this.numberOfTestsRun, mutatedClasses());
}

Iterable<Score> getScores() {
Expand All @@ -58,4 +70,6 @@ private static BiFunction<Long, Score, Long> addDetectedTotals() {
private static BiFunction<Long, Score, Long> addCoveredTotals() {
return (a, b) -> a + b.getTotalWithCoverage();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;

public class MutationCoverage {

Expand Down Expand Up @@ -143,7 +145,7 @@ public CombinedStatistics runReport() throws IOException {
}

private CombinedStatistics emptyStatistics() {
MutationStatistics mutationStatistics = new MutationStatistics(emptyList(),0,0,0,0);
MutationStatistics mutationStatistics = new MutationStatistics(emptyList(),0,0,0,0, emptySet());
return new CombinedStatistics(mutationStatistics, new CoverageSummary(0,0), Collections.emptyList());
}

Expand Down Expand Up @@ -185,8 +187,9 @@ private CombinedStatistics runAnalysis(Runtime runtime, long t0, EngineArguments

LOG.info("Completed in " + timeSpan(t0));

CombinedStatistics combined = new CombinedStatistics(stats.getStatistics(),
createSummary(modifiedCoverage), issues);
MutationStatistics mutationStats = stats.getStatistics();
CombinedStatistics combined = new CombinedStatistics(mutationStats,
createSummary(modifiedCoverage, mutationStats.mutatedClasses()), issues);

printStats(combined);

Expand All @@ -199,12 +202,17 @@ private ReportCoverage transformCoverage(ReportCoverage coverageData) {
return strategies.coverageTransformer().transform(coverageData);
}

private CoverageSummary createSummary(ReportCoverage modifiedCoverage) {
int numberOfCodeLines = this.code.getCodeUnderTestNames().stream()
.map(c -> modifiedCoverage.getCodeLinesForClass(c).getNumberOfCodeLines())
private CoverageSummary createSummary(ReportCoverage modifiedCoverage, Set<ClassName> mutatedClasses) {
List<ClassName> examinedClasses = this.code.getCodeUnderTestNames().stream()
.filter(mutatedClasses::contains)
.collect(Collectors.toList());

int numberOfCodeLines = examinedClasses.stream()
.map(c -> modifiedCoverage.getCodeLinesForClass(c))
.map(c -> c.getNumberOfCodeLines())
.reduce(0, Integer::sum);

int coveredLines = this.code.getCodeUnderTestNames().stream()
int coveredLines = examinedClasses.stream()
.mapToInt(c -> modifiedCoverage.getCoveredLines(c).size())
.sum();

Expand Down Expand Up @@ -324,7 +332,7 @@ private void printStats(CombinedStatistics combinedStatistics) {

final CoverageSummary coverage = combinedStatistics.getCoverageSummary();
if (coverage != null) {
ps.println(String.format(">> Line Coverage: %d/%d (%d%%)", coverage.getNumberOfCoveredLines(),
ps.println(String.format(">> Line Coverage (for mutated classes only): %d/%d (%d%%)", coverage.getNumberOfCoveredLines(),
coverage.getNumberOfLines(), coverage.getCoverage()));
}

Expand Down
6 changes: 3 additions & 3 deletions pitest-maven/src/test/java/org/pitest/maven/PitMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void testEmptyFeatureIsIgnored() throws Exception {
private void setupCoverage(long mutationScore, int lines, int linesCovered)
throws MojoExecutionException {
Iterable<Score> scores = Collections.<Score>emptyList();
final MutationStatistics stats = new MutationStatistics(scores, 100, mutationScore, 100, 0);
final MutationStatistics stats = new MutationStatistics(scores, 100, mutationScore, 100, 0, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(lines, linesCovered);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
Expand All @@ -408,7 +408,7 @@ private void setupCoverage(long mutationScore, int lines, int linesCovered)
private void setupTestStrength(long totalMutations, long mutationDetected, long mutationsWithCoverage)
throws MojoExecutionException {
Iterable<Score> scores = Collections.<Score>emptyList();
final MutationStatistics stats = new MutationStatistics(scores, totalMutations, mutationDetected, mutationsWithCoverage, 0);
final MutationStatistics stats = new MutationStatistics(scores, totalMutations, mutationDetected, mutationsWithCoverage, 0, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(0, 0);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
Expand All @@ -421,7 +421,7 @@ private void setupSuvivingMutants(long survivors)
throws MojoExecutionException {
Iterable<Score> scores = Collections.<Score>emptyList();
int detected = 100;
final MutationStatistics stats = new MutationStatistics(scores, detected + survivors, detected, detected + survivors, 0);
final MutationStatistics stats = new MutationStatistics(scores, detected + survivors, detected, detected + survivors, 0, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(0, 0);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
Expand Down

0 comments on commit 5945e95

Please sign in to comment.