Skip to content

Case statements should be counted as branches #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions plugin/src/main/scala/scoverage/ScoveragePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,17 @@ class ScoverageInstrumentationComponent(
}) ++ cases.takeRight(1)
}

def transformCases(cases: List[CaseDef]): List[CaseDef] = {
def transformCases(
cases: List[CaseDef],
branch: Boolean = false
): List[CaseDef] = {
cases.map(c => {
treeCopy.CaseDef(
c,
c.pat,
process(c.guard),
process(c.body)
if (branch) instrument(process(c.body), c.body, branch = true)
else process(c.body)
)
})
}
Expand Down Expand Up @@ -392,7 +396,7 @@ class ScoverageInstrumentationComponent(
// note: do not transform last case as that is the default handling
d.rhs,
selector,
transformCases(cases.init) :+ cases.last
transformCases(cases.init, branch = true) :+ cases.last
)
)
case _ =>
Expand Down Expand Up @@ -697,7 +701,11 @@ class ScoverageInstrumentationComponent(
treeCopy.Match(tree, selector, transformCases(cases))
else
// .. but we will if it was a user match
treeCopy.Match(tree, process(selector), transformCases(cases))
treeCopy.Match(
tree,
process(selector),
transformCases(cases, branch = true)
)
}

// a synthetic object is a generated object, such as case class companion
Expand Down Expand Up @@ -791,7 +799,7 @@ class ScoverageInstrumentationComponent(
treeCopy.Try(
tree,
instrument(process(t), t, branch = true),
transformCases(cases),
transformCases(cases, branch = true),
if (f.isEmpty) f else instrument(process(f), f, branch = true)
)

Expand Down
15 changes: 9 additions & 6 deletions plugin/src/test/scala/scoverage/PluginCoverageTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
assert(!compiler.reporter.hasWarnings)

/** should have the following statements instrumented:
* the selector, clause 1
* the selector, clause/skip 1
*/
compiler.assertNMeasuredStatements(2)
compiler.assertNMeasuredStatements(3)
}
test("scoverage should instrument match guards") {
val compiler = ScoverageCompiler.default
Expand All @@ -98,7 +98,7 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
/** should have the following statements instrumented:
* the selector, guard 1, clause 1, guard 2, clause 2, clause 3
*/
compiler.assertNMeasuredStatements(6)
compiler.assertNMeasuredStatements(9)
}

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

test("scoverage should instrument conditional selectors in a match") {
Expand All @@ -134,7 +135,8 @@ class PluginCoverageTest extends FunSuite with MacroSupport {
// elsep block,
// elsep literal "2",
// case block "yes" literal
compiler.assertNMeasuredStatements(6)
// skip case block "yes" literal
compiler.assertNMeasuredStatements(7)
}

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

test("plugin should support yields") {
Expand Down