Skip to content

Commit 3874e45

Browse files
committed
Implemented xml reader
1 parent cce2890 commit 3874e45

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/coverage.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ case class Statement(source: String,
9999
def isInvoked = count > 0
100100
}
101101

102+
sealed trait ClassType
102103
object ClassType {
103104
case object Object extends ClassType
104105
case object Class extends ClassType
105106
case object Trait extends ClassType
107+
def fromString(str: String): ClassType = {
108+
str.toLowerCase match {
109+
case "object" => Object
110+
case "trait" => Trait
111+
case _ => Class
112+
}
113+
}
106114
}
107115

108-
sealed trait ClassType
109116

110117

111118

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package scoverage.report
2+
3+
import java.io.File
4+
5+
import scoverage._
6+
7+
import scala.xml.XML
8+
9+
/**
10+
* Reads in an Scoverage XML report and converts to a Coverage instance.
11+
*/
12+
object ScoverageXmlReader {
13+
14+
def read(file: File): Coverage = {
15+
val xml = XML.loadFile(file)
16+
17+
var id = 0
18+
val coverage = Coverage()
19+
(xml \ "statement") foreach { node => {
20+
21+
val source = node \ "@source"
22+
val pkg = node \ "@package"
23+
val classname = node \ "@class"
24+
val classType = node \ "@class-type"
25+
val topLevelClass = node \ "@top-level-class"
26+
val method = node \ "@method"
27+
val start = node \ "@start"
28+
val end = node \ "@end"
29+
val line = node \ "@line"
30+
val branch = node \ "@branch"
31+
val count = node \ "@invocation-count"
32+
val symbolName = node \ "@symbol"
33+
val treeName = node \ "@tree"
34+
35+
val location = Location(pkg.text,
36+
classname.text,
37+
topLevelClass.text,
38+
ClassType fromString classType.text,
39+
method.text,
40+
source.text)
41+
42+
id = id + 1
43+
44+
coverage add Statement(
45+
source.text,
46+
location,
47+
id,
48+
start.text.toInt,
49+
end.text.toInt,
50+
line.text.toInt,
51+
"", // not interested in debug info when re-creating
52+
symbolName.text,
53+
treeName.text,
54+
branch.text.toBoolean,
55+
count.text.toInt
56+
)
57+
}
58+
}
59+
coverage
60+
}
61+
}

scalac-scoverage-plugin/src/main/scala/scoverage/report/ScoverageXmlWriter.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ class ScoverageXmlWriter(sourceDir: File, outputDir: File, debug: Boolean) {
3535
case true =>
3636
<statement package={stmt.location.packageName}
3737
class={stmt.location.className}
38-
topLevelClass={stmt.location.topLevelClass}
38+
class-type={stmt.location.classType.toString}
39+
top-level-class={stmt.location.topLevelClass}
40+
source={stmt.source}
3941
method={stmt.location.method}
4042
start={stmt.start.toString}
43+
end={stmt.end.toString}
4144
line={stmt.line.toString}
4245
symbol={Serializer.escape(stmt.symbolName)}
4346
tree={Serializer.escape(stmt.treeName)}
@@ -48,9 +51,12 @@ class ScoverageXmlWriter(sourceDir: File, outputDir: File, debug: Boolean) {
4851
case false =>
4952
<statement package={stmt.location.packageName}
5053
class={stmt.location.className}
51-
topLevelClass={stmt.location.topLevelClass}
54+
class-type={stmt.location.classType.toString}
55+
top-level-class={stmt.location.topLevelClass}
56+
source={stmt.source}
5257
method={stmt.location.method}
5358
start={stmt.start.toString}
59+
end={stmt.end.toString}
5460
line={stmt.line.toString}
5561
branch={stmt.branch.toString}
5662
invocation-count={stmt.count.toString}/>

0 commit comments

Comments
 (0)