Skip to content

Commit

Permalink
Merge pull request #1373 from hcoles/feature/report_test_count
Browse files Browse the repository at this point in the history
report test count
  • Loading branch information
hcoles authored Jan 10, 2025
2 parents efec2f3 + 8394517 commit 0201d6f
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private ReportCoverage calculateCoverage(final CodeSource codeSource) throws Rep
Collection<BlockLocation> coverageData = this.blockCoverageLoader.loadData().stream()
.map(BlockCoverage::getBlock)
.collect(Collectors.toList());
CoverageData cd = new CoverageData(codeSource, new LineMapper(codeSource));
CoverageData cd = new CoverageData(codeSource, new LineMapper(codeSource), 0);
cd.loadBlockDataOnly(coverageData);

return transformCoverage(cd);
Expand Down
10 changes: 8 additions & 2 deletions pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public class CoverageData implements CoverageDatabase {

private final Map<BlockLocation, Set<TestInfo>> blockCoverage = new LinkedHashMap<>();
private final LegacyClassCoverage legacyClassCoverage;

private final CodeSource code;
private final int testCount;

private final List<Description> failingTestDescriptions = new ArrayList<>();

public CoverageData(final CodeSource code, final LineMap lm) {
public CoverageData(CodeSource code, LineMap lm, int testCount) {
this.code = code;
this.legacyClassCoverage = new LegacyClassCoverage(code, lm);
this.testCount = testCount;
}

public void calculateClassCoverage(final CoverageResult cr) {
Expand All @@ -76,6 +77,11 @@ public Collection<TestInfo> getTestsForBlockLocation(BlockLocation location) {
return this.blockCoverage.getOrDefault(location, Collections.emptySet());
}

@Override
public int testCount() {
return testCount;
}

public boolean allTestsGreen() {
return this.failingTestDescriptions.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public interface CoverageDatabase extends ReportCoverage {

int testCount();

Collection<TestInfo> getTestsForClass(ClassName clazz);

Collection<TestInfo> getTestsForBlockLocation(BlockLocation location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
*/
public final class CoverageSummary {

private final int numberOfTests;
private final int numberOfLines;
private final int numberOfCoveredLines;

public CoverageSummary(final int numberOfLines, final int numberOfCoveredLines) {
public CoverageSummary(int numberOfLines, int numberOfCoveredLines, int numberOfTests) {
this.numberOfLines = numberOfLines;
this.numberOfCoveredLines = numberOfCoveredLines;
this.numberOfTests = numberOfTests;
}

public int getNumberOfTests() {
return this.numberOfTests;
}

public int getNumberOfLines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public Set<ClassLine> getCoveredLines(ClassName clazz) {
return Collections.emptySet();
}

@Override
public int testCount() {
return 0;
}

@Override
public Collection<TestInfo> getTestsForClass(ClassName clazz) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public CoverageData calculateCoverage(Predicate<ClassName> testFilter) {
this.timings.registerEnd(Timings.Stage.SCAN_CLASS_PATH);

final CoverageData coverage = new CoverageData(this.code, new LineMapper(
this.code));
this.code), tests.size());

this.timings.registerStart(Timings.Stage.COVERAGE);
if (tests.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public CombinedStatistics runReport() throws IOException {

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

private CombinedStatistics runAnalysis(Runtime runtime, long t0, EngineArguments args, MutationEngine engine, List<BuildMessage> issues, List<MutationDetails> unfilteredMutants) {
Expand Down Expand Up @@ -191,7 +191,7 @@ private CombinedStatistics runAnalysis(Runtime runtime, long t0, EngineArguments

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

printStats(combined);

Expand All @@ -204,7 +204,7 @@ private ReportCoverage transformCoverage(ReportCoverage coverageData) {
return strategies.coverageTransformer().transform(coverageData);
}

private CoverageSummary createSummary(ReportCoverage modifiedCoverage, Set<ClassName> mutatedClasses) {
private CoverageSummary createSummary(int numberOfTests, ReportCoverage modifiedCoverage, Set<ClassName> mutatedClasses) {
List<ClassName> examinedClasses = this.code.getCodeUnderTestNames().stream()
.filter(mutatedClasses::contains)
.collect(Collectors.toList());
Expand All @@ -218,7 +218,7 @@ private CoverageSummary createSummary(ReportCoverage modifiedCoverage, Set<Class
.mapToInt(c -> modifiedCoverage.getCoveredLines(c).size())
.sum();

return new CoverageSummary(numberOfCodeLines, coveredLines);
return new CoverageSummary(numberOfCodeLines, coveredLines, numberOfTests);
}

private Predicate<MutationInterceptor> allInterceptors() {
Expand Down Expand Up @@ -318,6 +318,7 @@ private void printStats(CombinedStatistics combinedStatistics) {
if (coverage != null) {
ps.println(String.format(">> Line Coverage (for mutated classes only): %d/%d (%d%%)", coverage.getNumberOfCoveredLines(),
coverage.getNumberOfLines(), coverage.getCoverage()));
ps.println(String.format(">> %d tests examined", coverage.getNumberOfTests()));
}

stats.report(ps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void setUp() {
when(this.lm.mapLines(any(ClassName.class))).thenReturn(
new HashMap<>());
when(this.code.findTestee(any())).thenReturn(Optional.empty());
this.testee = new CoverageData(this.code, this.lm);
this.testee = new CoverageData(this.code, this.lm, 42);
}

@Test
Expand Down Expand Up @@ -195,7 +195,7 @@ public void shouldProvideListOfClassesForSourceFile() {
ClassTree barClass = treeFor(com.example.a.b.c.Bar.class);
when(this.code.codeTrees()).thenReturn(Stream.of(fooClass, barClass));

this.testee = new CoverageData(this.code, this.lm);
this.testee = new CoverageData(this.code, this.lm, 0);

assertThat(this.testee.getClassesForFile("Bar.java", "com.example.a.b.c"))
.containsExactly(new ClassLines(barClass.name(), Collections.emptySet()));
Expand All @@ -209,13 +209,18 @@ public void shouldMatchPackageWhenFindingSources() {

when(this.code.codeTrees()).thenReturn(classes.stream());

this.testee = new CoverageData(this.code, this.lm);
this.testee = new CoverageData(this.code, this.lm, 0);

assertThat(this.testee.getClassesForFile("Foo.java", "com.example.a.b.c"))
.containsExactly(new ClassLines(foo1Class.name(), Collections.emptySet()));

}

@Test
public void reportsTestCount() {
assertThat(testee.testCount()).isEqualTo(42);
}

private CoverageResult makeCoverageResult(final String clazz,
final String testName, final int time, final int block) {
return makeCoverageResult(clazz, new Description(testName), time, block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ public class CoverageSummaryTest {

@Test
public void shouldCorrectlyCalculateLineCoverageWhenNoLinesPresent() {
assertEquals(100, new CoverageSummary(0, 0).getCoverage());
assertEquals(100, new CoverageSummary(0, 0, 0).getCoverage());
}

@Test
public void shouldCorrectlyCalculateLineCoverageWhenNoLinesCovered() {
assertEquals(0, new CoverageSummary(100, 0).getCoverage());
assertEquals(0, new CoverageSummary(100, 0, 0).getCoverage());
}

@Test
public void shouldCorrectlyCalculateLineCoverageWhenAllLinesCovered() {
assertEquals(100, new CoverageSummary(100, 100).getCoverage());
assertEquals(100, new CoverageSummary(100, 100, 0).getCoverage());
}

@Test
public void shouldCorrectlyCalculateLineCoverageWhenPartiallyCovered() {
assertEquals(50, new CoverageSummary(100, 50).getCoverage());
assertEquals(50, new CoverageSummary(100, 50, 0).getCoverage());
}

}
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 @@ -415,7 +415,7 @@ 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, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(lines, linesCovered);
CoverageSummary sum = new CoverageSummary(lines, linesCovered, 0);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
this.executionStrategy.execute(any(File.class),
Expand All @@ -427,7 +427,7 @@ private void setupTestStrength(long totalMutations, long mutationDetected, long
throws MojoExecutionException {
Iterable<Score> scores = Collections.<Score>emptyList();
final MutationStatistics stats = new MutationStatistics(scores, totalMutations, mutationDetected, mutationsWithCoverage, 0, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(0, 0);
CoverageSummary sum = new CoverageSummary(0, 0, 0);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
this.executionStrategy.execute(any(File.class),
Expand All @@ -440,7 +440,7 @@ private void setupSuvivingMutants(long survivors)
Iterable<Score> scores = Collections.<Score>emptyList();
int detected = 100;
final MutationStatistics stats = new MutationStatistics(scores, detected + survivors, detected, detected + survivors, 0, Collections.emptySet());
CoverageSummary sum = new CoverageSummary(0, 0);
CoverageSummary sum = new CoverageSummary(0, 0, 0);
final CombinedStatistics cs = new CombinedStatistics(stats, sum, Collections.emptyList());
when(
this.executionStrategy.execute(any(File.class),
Expand Down

0 comments on commit 0201d6f

Please sign in to comment.