Skip to content

Commit a6dc5c0

Browse files
authored
Case statements should be counted as branches (#493)
* Case statements should be counted as branches * Run scalafmt
1 parent 3f95dfc commit a6dc5c0

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

plugin/src/main/scala/scoverage/ScoveragePlugin.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,17 @@ class ScoverageInstrumentationComponent(
213213
}) ++ cases.takeRight(1)
214214
}
215215

216-
def transformCases(cases: List[CaseDef]): List[CaseDef] = {
216+
def transformCases(
217+
cases: List[CaseDef],
218+
branch: Boolean = false
219+
): List[CaseDef] = {
217220
cases.map(c => {
218221
treeCopy.CaseDef(
219222
c,
220223
c.pat,
221224
process(c.guard),
222-
process(c.body)
225+
if (branch) instrument(process(c.body), c.body, branch = true)
226+
else process(c.body)
223227
)
224228
})
225229
}
@@ -392,7 +396,7 @@ class ScoverageInstrumentationComponent(
392396
// note: do not transform last case as that is the default handling
393397
d.rhs,
394398
selector,
395-
transformCases(cases.init) :+ cases.last
399+
transformCases(cases.init, branch = true) :+ cases.last
396400
)
397401
)
398402
case _ =>
@@ -697,7 +701,11 @@ class ScoverageInstrumentationComponent(
697701
treeCopy.Match(tree, selector, transformCases(cases))
698702
else
699703
// .. but we will if it was a user match
700-
treeCopy.Match(tree, process(selector), transformCases(cases))
704+
treeCopy.Match(
705+
tree,
706+
process(selector),
707+
transformCases(cases, branch = true)
708+
)
701709
}
702710

703711
// a synthetic object is a generated object, such as case class companion
@@ -791,7 +799,7 @@ class ScoverageInstrumentationComponent(
791799
treeCopy.Try(
792800
tree,
793801
instrument(process(t), t, branch = true),
794-
transformCases(cases),
802+
transformCases(cases, branch = true),
795803
if (f.isEmpty) f else instrument(process(f), f, branch = true)
796804
)
797805

plugin/src/test/scala/scoverage/PluginCoverageTest.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
7979
assert(!compiler.reporter.hasWarnings)
8080

8181
/** should have the following statements instrumented:
82-
* the selector, clause 1
82+
* the selector, clause/skip 1
8383
*/
84-
compiler.assertNMeasuredStatements(2)
84+
compiler.assertNMeasuredStatements(3)
8585
}
8686
test("scoverage should instrument match guards") {
8787
val compiler = ScoverageCompiler.default
@@ -98,7 +98,7 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
9898
/** should have the following statements instrumented:
9999
* the selector, guard 1, clause 1, guard 2, clause 2, clause 3
100100
*/
101-
compiler.assertNMeasuredStatements(6)
101+
compiler.assertNMeasuredStatements(9)
102102
}
103103

104104
test("scoverage should instrument non basic selector") {
@@ -114,7 +114,8 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
114114
// the someValue method entry
115115
// the selector call
116116
// case block "yes" literal
117-
compiler.assertNMeasuredStatements(3)
117+
// skip case block
118+
compiler.assertNMeasuredStatements(4)
118119
}
119120

120121
test("scoverage should instrument conditional selectors in a match") {
@@ -134,7 +135,8 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
134135
// elsep block,
135136
// elsep literal "2",
136137
// case block "yes" literal
137-
compiler.assertNMeasuredStatements(6)
138+
// skip case block "yes" literal
139+
compiler.assertNMeasuredStatements(7)
138140
}
139141

140142
// https://github.com/scoverage/sbt-scoverage/issues/16
@@ -261,8 +263,9 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
261263
assert(!compiler.reporter.hasErrors)
262264
assert(!compiler.reporter.hasWarnings)
263265
// should have one statement for each case body
266+
// and one statement for each case skipped
264267
// selector is a constant so would be ignored.
265-
compiler.assertNMeasuredStatements(3)
268+
compiler.assertNMeasuredStatements(6)
266269
}
267270

268271
test("plugin should support yields") {

0 commit comments

Comments
 (0)