private static void verifyBuildSuitableForMutationTesting(final CoverageData coverage) { if (!coverage.allTestsGreen()) { LOG.severe("Tests failing without mutation: " + StringUtil.newLine() + coverage.getFailingTestDescriptions().stream().map(test -> test.toString()) .collect(Collectors.joining(StringUtil.newLine()))); throw new PitHelpError(Help.FAILING_TESTS, coverage.getCountFailedTests()); } }
@Override public void apply(final CoverageResult cr) { if (cr.isGreenTest() || !coverageOptions.getPitConfig().skipFailingTests()) { coverage.calculateClassCoverage(cr); } if (DefaultCoverageGenerator.this.showProgress) { System.out.printf("%s", this.spinner[this.i % this.spinner.length]); } this.i++; }
private BiFunction<Integer, ClassName, Integer> numberCoveredLines() { return (a, clazz) -> a + getNumberOfCoveredLines(clazz); }
private int coveredLines() { return FCollection.fold(numberCoveredLines(), 0, allClasses()); }
private int numberOfLines() { return FCollection.fold(numberLines(), 0, this.code.getClassInfo(allClasses())); }
@Test public void shouldReportAGreenSuiteWhenNoTestHasFailed() { this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest", 42, 1)); assertTrue(this.testee.allTestsGreen()); }
@Test public void shouldProvideListOfClassesForSourceFile() { final ClassInfo fooClass = ClassInfoMother.make(this.foo, "foo.java"); final ClassInfo barClass = ClassInfoMother.make(this.bar, "bar.java"); final Collection<ClassInfo> classes = Arrays.asList(fooClass, barClass); when(this.code.getCode()).thenReturn(classes); this.testee = new CoverageData(this.code, this.lm); assertEquals(Arrays.asList(barClass), this.testee.getClassesForFile("bar.java", "")); }
@Override public CoverageData calculateCoverage() { try { final long t0 = System.currentTimeMillis(); this.timings.registerStart(Timings.Stage.SCAN_CLASS_PATH); final Collection<ClassInfo> tests = this.code.getTests(); this.timings.registerEnd(Timings.Stage.SCAN_CLASS_PATH); final CoverageData coverage = new CoverageData(this.code, new LineMapper( this.code)); this.timings.registerStart(Timings.Stage.COVERAGE); gatherCoverageData(tests, coverage); this.timings.registerEnd(Timings.Stage.COVERAGE); final long time = (System.currentTimeMillis() - t0) / 1000; LOG.info("Calculated coverage in " + time + " seconds."); verifyBuildSuitableForMutationTesting(coverage); this.exporter.recordCoverage(coverage.createCoverage()); return coverage; } catch (final PitHelpError phe) { throw phe; } catch (final Exception e) { throw Unchecked.translateCheckedException(e); } }
@Test public void shouldReportNumberOfCoveredLinesWhenSomeCovered() { final BlockLocationBuilder block = aBlockLocation().withLocation( aLocation().withClass(this.foo)); when(this.lm.mapLines(any(ClassName.class))).thenReturn( makeCoverageMapForBlock(block, 101, 300)); final CoverageResultBuilder cr = aCoverageResult().withVisitedBlocks( block.build(1)); this.testee.calculateClassCoverage(cr.build()); assertEquals(2, this.testee.getNumberOfCoveredLines(Collections .singletonList(this.foo))); }
@Test public void shouldProvideCoverageListWhenCoverageRecorded() { final BlockLocationBuilder block = aBlockLocation().withLocation( aLocation().withClass(this.foo)); final CoverageResultBuilder cr = aCoverageResult().withVisitedBlocks( block.build(1)); this.testee.calculateClassCoverage(cr.build()); this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest", 0, 1)); final BlockCoverage actual = this.testee.createCoverage().get(0); assertEquals(block.build(), actual.getBlock()); assertThat(actual.getTests()).contains("FooTest.fooTest"); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(this.lm.mapLines(any(ClassName.class))).thenReturn( new HashMap<BlockLocation, Set<Integer>>()); when(this.code.findTestee(any())).thenReturn(Optional.empty()); this.testee = new CoverageData(this.code, this.lm); }
public void calculateClassCoverage(final CoverageResult cr) { checkForFailedTest(cr); final TestInfo ti = this.createTestInfo(cr.getTestUnitDescription(), cr.getExecutionTime(), cr.getNumberOfCoveredBlocks()); for (final BlockLocation each : cr.getCoverage()) { addTestsToBlockMap(ti, each); } }
@Test public void shouldReturnUniqueTestsForClassWhenSomeTestsCoverClass() { this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest", 0, 1)); this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest", 0, 2)); this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest2", 0, 2)); assertEquals(Arrays.asList("fooTest", "fooTest2"), FCollection.map( this.testee.getTestsForClass(this.foo), testInfoToString())); }
@Test public void shouldReturnNonZeroCoverageIdWhenTestsCoverClass() { final ClassName foo = ClassName.fromString("Foo"); final ClassInfo ci = ClassInfoMother.make(foo); when(this.code.getClassInfo(any(Collection.class))).thenReturn( Collections.singletonList(ci)); final BlockLocationBuilder block = aBlockLocation().withLocation( aLocation().withClass(foo)); final HashMap<BlockLocation, Set<Integer>> map = makeCoverageMapForBlock(block, 42); when(this.lm.mapLines(any(ClassName.class))).thenReturn(map); this.testee.calculateClassCoverage(aCoverageResult().withVisitedBlocks( block.build(1)).build()); assertThat(this.testee.getCoverageIdForClass(foo).longValue()) .isNotEqualTo(0); }
@Test public void shouldIncludeAllCoveredLinesInCoverageSummary() { final BlockLocationBuilder block = aBlockLocation(); when(this.code.getCodeUnderTestNames()).thenReturn( Collections.singleton(block.build().getLocation().getClassName())); when(this.lm.mapLines(any(ClassName.class))).thenReturn( makeCoverageMapForBlock(block, 1, 2, 3, 4)); final CoverageResultBuilder cr = aCoverageResult().withVisitedBlocks( block.build(1)); this.testee.calculateClassCoverage(cr.build()); final CoverageSummary actual = this.testee.createSummary(); assertEquals(4, actual.getNumberOfCoveredLines()); }
@Test public void shouldStoreExecutionTimesOfTests() { final int line = 1; final int time = 42; final BlockLocationBuilder block = aBlockLocation().withLocation( aLocation().withClass(this.foo)); when(this.lm.mapLines(any(ClassName.class))).thenReturn( makeCoverageMapForBlock(block, line)); final CoverageResultBuilder cr = aCoverageResult().withVisitedBlocks( block.build(1)).withExecutionTime(time); this.testee.calculateClassCoverage(cr.build()); assertEquals(Arrays.asList(42), FCollection.map( this.testee.getTestsForClassLine(new ClassLine(this.foo, line)), testInfoToExecutionTime())); }
@Test public void shouldProvideEmptyBlockCoverageListWhenNoCoverage() { assertEquals(Collections.emptyList(), this.testee.createCoverage()); }
private Set<Integer> getLinesForBlock(BlockLocation bl) { Set<Integer> lines = this.blocksToLines.get(bl); if (lines == null) { calculateLinesForBlocks(bl.getLocation().getClassName()); lines = this.blocksToLines.get(bl); if (lines == null) { lines = Collections.emptySet(); } } return lines; }
@Test public void shouldNotReportAGreenSuiteWhenATestHasFailed() { this.testee.calculateClassCoverage(makeCoverageResult("foo", new Description("fooTest"), 42, 1, false)); assertFalse(this.testee.allTestsGreen()); }
@Test public void shouldMatchPackageWhenFindingSources() { final ClassName foo1 = ClassName.fromString("a.b.c.foo"); final ClassName foo2 = ClassName.fromString("d.e.f.foo"); final ClassInfo foo1Class = ClassInfoMother.make(foo1, "foo.java"); final ClassInfo foo2Class = ClassInfoMother.make(foo2, "foo.java"); final Collection<ClassInfo> classes = Arrays.asList(foo1Class, foo2Class); when(this.code.getCode()).thenReturn(classes); this.testee = new CoverageData(this.code, this.lm); assertEquals(Arrays.asList(foo1Class), this.testee.getClassesForFile("foo.java", "a.b.c")); }