Skip to content

Commit 1652e78

Browse files
committed
#57 Fixed ambigious branch coveragae
1 parent 754541b commit 1652e78

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/main/scala/scoverage/coverage.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,21 @@ trait CoverageMetrics {
128128
def branchCoveragePercent = branchCoverage * 100
129129
def invokedBranches: Iterable[MeasuredStatement] = branches.filter(_.count > 0)
130130
def invokedBranchesCount = invokedBranches.size
131-
def branchCoverage: Double = if (branchCount == 0) 1 else invokedBranchesCount / branchCount.toDouble
131+
132+
/**
133+
* @see http://stackoverflow.com/questions/25184716/scoverage-ambiguous-measurement-from-branch-coverage
134+
*/
135+
def branchCoverage: Double = {
136+
// if there are zero branches, then we have a single line of execution.
137+
// in that case, if there is at least some coverage, we have covered the branch.
138+
// if there is no coverage then we have not covered the branch
139+
if (branchCount == 0) {
140+
if (statementCoverage > 0) 1
141+
else 0
142+
} else {
143+
invokedBranchesCount / branchCount.toDouble
144+
}
145+
}
132146
def branchCoverageFormatted: String = "%.2f".format(branchCoveragePercent)
133147
}
134148

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package scoverage
2+
3+
import org.scalatest.{FreeSpec, Matchers}
4+
5+
class CoverageMetricsTest extends FreeSpec with Matchers {
6+
7+
"no branches with at least one invoked statement should have 100% branch coverage" in {
8+
val metrics = new CoverageMetrics {
9+
override def statements: Iterable[MeasuredStatement] = Seq(MeasuredStatement(null,
10+
null,
11+
0,
12+
0,
13+
0,
14+
0,
15+
null,
16+
null,
17+
null,
18+
false,
19+
1))
20+
}
21+
metrics.branchCount shouldBe 0
22+
metrics.branchCoverage - 1 shouldBe < (0.0001)
23+
}
24+
25+
"no branches with no invoked statements should have 0% branch coverage" in {
26+
val metrics = new CoverageMetrics {
27+
override def statements: Iterable[MeasuredStatement] = Seq(MeasuredStatement(null,
28+
null,
29+
0,
30+
0,
31+
0,
32+
0,
33+
null,
34+
null,
35+
null,
36+
false,
37+
0))
38+
}
39+
metrics.branchCount shouldBe 0
40+
metrics.branchCoverage shouldBe 0
41+
}
42+
}

0 commit comments

Comments
 (0)