File tree Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments