Skip to content

Commit

Permalink
Fix coverage reporting with for comprehensions and add some tests to …
Browse files Browse the repository at this point in the history
…prove it
  • Loading branch information
Vijay Ramesh committed Apr 13, 2023
1 parent e940957 commit f259dee
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,9 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
InstrumentedParts.notCovered(tree)

private def tryInstrument(tree: Select)(using Context): InstrumentedParts =
val sym = tree.symbol
val transformed = cpy.Select(tree)(transform(tree.qualifier), tree.name)
if canInstrumentParameterless(sym) then
// call to a parameterless method
val coverageCall = createInvokeCall(tree, tree.sourcePos)
InstrumentedParts.singleExpr(coverageCall, transformed)
else
InstrumentedParts.notCovered(transformed)
val coverageCall = createInvokeCall(tree, tree.sourcePos)
InstrumentedParts.singleExpr(coverageCall, transformed)

/** Generic tryInstrument */
private def tryInstrument(tree: Tree)(using Context): InstrumentedParts =
Expand Down
17 changes: 17 additions & 0 deletions tests/coverage/pos/Escaping.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ covtest.\n
Class
covtest.\n.\r\n\f
\r\n\f
69
73
3
\\
Ident
false
0
false
`\\\\`

2
Escaping.scala
covtest.\n
\r\n\f
Class
covtest.\n.\r\n\f
\r\n\f
40
48
3
Expand Down
10 changes: 10 additions & 0 deletions tests/coverage/pos/ForComprehension.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package covtest

def testForComprehension: Unit =
for {
a <- List(1)
b <- List(1)
if b > 1
c = a + b
} yield (a, b, c)

190 changes: 190 additions & 0 deletions tests/coverage/pos/ForComprehension.scoverage.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# Coverage data, format version: 3.0
# Statement data:
# - id
# - source path
# - package name
# - class name
# - class type (Class, Object or Trait)
# - full class name
# - method name
# - start offset
# - end offset
# - line number
# - symbol name
# - tree name
# - is branch
# - invocations count
# - is ignored
# - description (can be multi-line)
# ' ' sign
# ------------------------------------------
0
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
testForComprehension
52
138
3
flatMap
Apply
false
0
false
for {\n a <- List(1)\n b <- List(1)\n if b > 1\n c = a + b\n } yield (a, b, c)

1
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
testForComprehension
67
74
4
apply
Apply
false
0
false
List(1)

2
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
testForComprehension
67
71
4
List
Ident
false
0
false
List

3
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
79
138
5
map
Apply
false
0
false
b <- List(1)\n if b > 1\n c = a + b\n } yield (a, b, c)

4
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
79
118
5
map
Apply
false
0
false
b <- List(1)\n if b > 1\n c = a + b

5
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
79
104
5
withFilter
Apply
false
0
false
b <- List(1)\n if b > 1

6
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
84
91
5
apply
Apply
false
0
false
List(1)

7
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
84
88
5
List
Ident
false
0
false
List

8
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
$anonfun
79
110
5
apply
Apply
false
0
false
b <- List(1)\n if b > 1\n c

9
ForComprehension.scala
covtest
ForComprehension$package$
Object
covtest.ForComprehension$package$
testForComprehension
17
41
2
testForComprehension
DefDef
false
0
false
def testForComprehension

57 changes: 54 additions & 3 deletions tests/coverage/pos/Lift.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ SomeFunctions
Class
covtest.SomeFunctions
c
83
96
5
<init>
Select
false
0
false
SomeFunctions

4
Lift.scala
covtest
SomeFunctions
Class
covtest.SomeFunctions
c
75
80
5
Expand All @@ -86,7 +103,7 @@ false
false
def c

4
5
Lift.scala
covtest
SomeFunctions
Expand All @@ -103,7 +120,7 @@ false
false
c.f(g())

5
6
Lift.scala
covtest
SomeFunctions
Expand All @@ -120,7 +137,24 @@ false
false
c

6
7
Lift.scala
covtest
SomeFunctions
Class
covtest.SomeFunctions
test
113
116
7
f
Select
false
0
false
c.f

8
Lift.scala
covtest
SomeFunctions
Expand All @@ -137,7 +171,24 @@ false
false
g()

9
Lift.scala
covtest
SomeFunctions
Class
covtest.SomeFunctions
test
117
118
7
g
Select
false
0
false
g

10
Lift.scala
covtest
SomeFunctions
Expand Down
4 changes: 4 additions & 0 deletions tests/coverage/run/for_comprehension/test.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
1
2
41 changes: 41 additions & 0 deletions tests/coverage/run/for_comprehension/test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@main
def Test: Unit = {

def unreachableFunction(): Seq[Int] = {
for {
a <- List(1)
b <- List(2)
} yield {
println(a)
println(b)
a + b
}
}

def unreachableFunctionUnlessTrue(flag: Boolean): Option[Int] = {
if (flag) {
val foo: Seq[Int] = for {
a <- List(1)
b <- List(2)
} yield {
println(a)
println(b)
a + b
}
foo.headOption
} else {
None
}
}

for {
a <- List(1)
b <- List(2)
} yield {
println(a)
println(b)
(a, b)
}

unreachableFunctionUnlessTrue(false)
}
Loading

0 comments on commit f259dee

Please sign in to comment.