Skip to content
Browse files

CoberturaReporter was missing @tailrec. Also added some tests for it.

  • Loading branch information...
1 parent b7a7216 commit 44d6e330d6d45df560a80e35ff02a86dad21c779 @mtkopone committed Oct 8, 2012
View
3 src/main/scala/reaktor/scct/report/CoberturaReporter.scala
@@ -2,6 +2,7 @@ package reaktor.scct.report
import reaktor.scct.CoveredBlock
import xml.{PrettyPrinter, NodeSeq}
+import annotation.tailrec
class CoberturaReporter(project: ProjectData, writer: HtmlReportWriter) {
val data = project.coverage
@@ -40,7 +41,7 @@ class CoberturaReporter(project: ProjectData, writer: HtmlReportWriter) {
line(1, 0, sourceLines, classData.blocks, NodeSeq.Empty)
}
- def line(num: Int, offset: Int, sourceLines: List[String], blocks: List[CoveredBlock], acc: NodeSeq): NodeSeq =
+ @tailrec final def line(num: Int, offset: Int, sourceLines: List[String], blocks: List[CoveredBlock], acc: NodeSeq): NodeSeq =
sourceLines match {
case Nil => acc
case sourceLine :: tail => {
View
71 src/test/scala/reaktor/scct/report/CoberturaReporterSpec.scala
@@ -0,0 +1,71 @@
+package reaktor.scct.report
+
+import org.specs2.mutable._
+import java.io.File
+import reaktor.scct.{IO, ClassTypes, Name, CoveredBlock}
+import xml.XML
+import org.specs2.specification.Scope
+
+class CoberturaReporterSpec extends Specification {
+
+ sequential
+
+ val tmpDir = new File(System.getProperty("java.io.tmpdir", "/tmp"))
+ val sourceFile = new File(tmpDir, "CoberturaReportSpec.scala")
+ val outputFile = new File(tmpDir, "cobertura.xml")
+
+ val name = Name(sourceFile.getName, ClassTypes.Class, "reaktor.scct.report", "CoberturaReportSpec", "scct")
+
+ "report output" in new CleanEnv {
+ IO.write(sourceFile, 1.to(4).map((ii:Int) => "0123456789").mkString("\n").getBytes("utf-8"))
+ val blocks = List(
+ new CoveredBlock(0, name, 0, false).increment,
+ new CoveredBlock(1, name, 11, false),
+ new CoveredBlock(1, name, 23, false).increment,
+ new CoveredBlock(2, name, 28, false).increment
+ )
+ val projectData = ProjectData("myProject", tmpDir, tmpDir, blocks)
+ val sut = new CoberturaReporter(projectData, new HtmlReportWriter(tmpDir))
+ sut.report
+ XML.loadFile(outputFile) must beEqualToIgnoringSpace(
+ <coverage line-rate="0.75">
+ <packages>
+ <package line-rate="0.75" name="reaktor.scct.report">
+ <classes>
+ <class line-rate="0.75" name="CoberturaReportSpec" filename="CoberturaReportSpec.scala">
+ <methods></methods>
+ <lines>
+ <line hits="1" number="1"></line>
+ <line hits="0" number="2"></line>
+ <line hits="2" number="3"></line>
+ </lines>
+ </class>
+ </classes>
+ </package>
+ </packages>
+ </coverage>
+ )
+ }
+
+ "tail recursive source line reading" in new CleanEnv {
+ IO.write(sourceFile, 1.to(4000).mkString("\n").getBytes("utf-8"))
+
+ val projectData = ProjectData("myProject", tmpDir, tmpDir, blocks(4000, name))
+ val sut = new CoberturaReporter(projectData, new HtmlReportWriter(tmpDir))
+ sut.report
+ outputFile must beAFile
+ }
+
+ trait CleanEnv extends Scope {
+ if (sourceFile.exists()) sourceFile.delete()
+ if (outputFile.exists()) outputFile.delete()
+ }
+
+ def blocks(ii: Int, name: Name) = {
+ 1.to(ii).map { ii:Int =>
+ val b = new CoveredBlock(ii, name, ii, false)
+ if (ii % 2 == 0) b.increment
+ b
+ }.toList
+ }
+}

0 comments on commit 44d6e33

Please sign in to comment.
Something went wrong with that request. Please try again.