@Test public void shouldExportSuppliedCoverage() { final LocationBuilder loc = aLocation().withMethod("method"); final BlockLocationBuilder block = aBlockLocation().withBlock(42); final Collection<BlockCoverage> coverage = Arrays.asList( new BlockCoverage(block.withLocation( loc.withClass(ClassName.fromString("Foo"))).build(), Arrays.asList( "Test1", "Test2")), new BlockCoverage(block.withLocation( loc.withClass(ClassName.fromString("Bar"))).build(), Arrays.asList( "Test3", "Test4"))); this.testee.recordCoverage(coverage); final String actual = this.out.toString(); assertThat(actual).contains( "<block classname='Foo' method='method()I' number='42'>"); assertThat(actual).contains( "<block classname='Bar' method='method()I' number='42'>"); assertThat(actual).contains( "<tests>\n<test name='Test1'/>\n<test name='Test2'/>\n</tests>"); assertThat(actual).contains( "<tests>\n<test name='Test3'/>\n<test name='Test4'/>\n</tests>"); }
@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 shouldEscapeSpecialCharsInTestName() { final LocationBuilder loc = aLocation().withMethod("method"); final BlockLocationBuilder block = aBlockLocation().withBlock(42); final Collection<BlockCoverage> coverage = Arrays.asList( new BlockCoverage( block.withLocation(loc.withClass(ClassName.fromString("Foo"))).build(), Collections.singletonList("ParameterizedTest[case='Not so simple quotes']")), new BlockCoverage( block.withLocation(loc.withClass(ClassName.fromString("Foo"))).build(), Collections.singletonList("ParameterizedTest[case=\0 Null-Byte]")) ); testee.recordCoverage(coverage); final String actual = this.out.toString(); assertThat(actual).contains( "<tests>\n<test name='ParameterizedTest[case='Not so simple quotes']'/>\n</tests>"); assertThat(actual).contains( "<tests>\n<test name='ParameterizedTest[case=\\0 Null-Byte]'/>\n</tests>"); }
@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 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 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"); }