@@ -13,11 +13,11 @@ class PluginCoverageTest
13
13
test(" scoverage should instrument default arguments with methods" ) {
14
14
val compiler = ScoverageCompiler .default
15
15
compiler.compileCodeSnippet( """ object DefaultArgumentsObject {
16
- | val defaultName = "world"
17
- | def makeGreeting(name: String = defaultName): String = {
18
- | s"Hello, $name"
19
- | }
20
- |} """ .stripMargin)
16
+ | val defaultName = "world"
17
+ | def makeGreeting(name: String = defaultName): String = {
18
+ | s"Hello, $name"
19
+ | }
20
+ |} """ .stripMargin)
21
21
assert(! compiler.reporter.hasErrors)
22
22
// we should have 2 statements - initialising the val and executing string sub in the def
23
23
compiler.assertNMeasuredStatements(2 )
@@ -43,14 +43,14 @@ class PluginCoverageTest
43
43
test(" scoverage should instrument final vals" ) {
44
44
val compiler = ScoverageCompiler .default
45
45
compiler.compileCodeSnippet( """ object FinalVals {
46
- | final val name = {
47
- | val name = "sammy"
48
- | if (System.currentTimeMillis() > 0) {
49
- | println(name)
50
- | }
51
- | }
52
- | println(name)
53
- |} """ .stripMargin)
46
+ | final val name = {
47
+ | val name = "sammy"
48
+ | if (System.currentTimeMillis() > 0) {
49
+ | println(name)
50
+ | }
51
+ | }
52
+ | println(name)
53
+ |} """ .stripMargin)
54
54
assert(! compiler.reporter.hasErrors)
55
55
// we should have 3 statements - initialising the val, executing println, and executing the parameter
56
56
compiler.assertNMeasuredStatements(8 )
@@ -59,10 +59,10 @@ class PluginCoverageTest
59
59
test(" scoverage should instrument selectors in match" ) {
60
60
val compiler = ScoverageCompiler .default
61
61
compiler.compileCodeSnippet( """ trait A {
62
- | def foo(a:String) = (if (a == "hello") 1 else 2) match {
63
- | case any => "yes"
64
- | }
65
- |} """ .stripMargin)
62
+ | def foo(a:String) = (if (a == "hello") 1 else 2) match {
63
+ | case any => "yes"
64
+ | }
65
+ |} """ .stripMargin)
66
66
assert(! compiler.reporter.hasErrors)
67
67
// should instrument the method call, the if clause, thenp, thenp literal, elsep, elsep literal, case block,
68
68
// case block literal
@@ -73,20 +73,53 @@ class PluginCoverageTest
73
73
test(" scoverage should instrument for-loops but not the generated default case" ) {
74
74
val compiler = ScoverageCompiler .default
75
75
compiler.compileCodeSnippet( """ trait A {
76
- | def print1(list: List[String]) = for (string: String <- list) println(string)
77
- |} """ .stripMargin)
76
+ | def print1(list: List[String]) = for (string: String <- list) println(string)
77
+ |} """ .stripMargin)
78
78
assert(! compiler.reporter.hasErrors)
79
79
assert(! compiler.reporter.hasWarnings)
80
80
// should have one statement for the withFilter invoke, one of the match selector,
81
81
// one of the case block, one for the case string RHS value, one for the foreach block.
82
82
compiler.assertNMeasuredStatements(5 )
83
83
}
84
84
85
+ test(" scoverage should correctly handle new with args (apply with list of args)" ) {
86
+ val compiler = ScoverageCompiler .default
87
+ compiler.compileCodeSnippet( """ object A {
88
+ | new String(new String(new String))
89
+ | } """ .stripMargin)
90
+ assert(! compiler.reporter.hasErrors)
91
+ assert(! compiler.reporter.hasWarnings)
92
+ // should have 3 statements, one for each of the nested strings
93
+ compiler.assertNMeasuredStatements(3 )
94
+ }
95
+
96
+ test(" scoverage should correctly handle no args new (apply, empty list of args)" ) {
97
+ val compiler = ScoverageCompiler .default
98
+ compiler.compileCodeSnippet( """ object A {
99
+ | new String
100
+ | } """ .stripMargin)
101
+ assert(! compiler.reporter.hasErrors)
102
+ assert(! compiler.reporter.hasWarnings)
103
+ // should have 1. the apply that wraps the select.
104
+ compiler.assertNMeasuredStatements(1 )
105
+ }
106
+
107
+ test(" scoverage should correctly handle new that invokes nested statements" ) {
108
+ val compiler = ScoverageCompiler .default
109
+ compiler.compileCodeSnippet( """ object A {
110
+ | new String(if (System.currentTimeMillis > 1) "yes" else "no")
111
+ | } """ .stripMargin)
112
+ assert(! compiler.reporter.hasErrors)
113
+ assert(! compiler.reporter.hasWarnings)
114
+ // should have 6 statements - the apply/new statement, two literals, the if cond, if elsep, if thenp
115
+ compiler.assertNMeasuredStatements(6 )
116
+ }
117
+
85
118
test(" scoverage should instrument val RHS" ) {
86
119
val compiler = ScoverageCompiler .default
87
120
compiler.compileCodeSnippet( """ object A {
88
- | val name = BigDecimal(50.0)
89
- |} """ .stripMargin)
121
+ | val name = BigDecimal(50.0)
122
+ |} """ .stripMargin)
90
123
assert(! compiler.reporter.hasErrors)
91
124
assert(! compiler.reporter.hasWarnings)
92
125
compiler.assertNMeasuredStatements(1 )
@@ -95,12 +128,12 @@ class PluginCoverageTest
95
128
test(" scoverage should instrument all case statements in an explicit match" ) {
96
129
val compiler = ScoverageCompiler .default
97
130
compiler.compileCodeSnippet( """ trait A {
98
- | def foo(name: Any) = name match {
99
- | case i : Int => 1
100
- | case b : Boolean => 2
101
- | case _ => 3
102
- | }
103
- |} """ .stripMargin)
131
+ | def foo(name: Any) = name match {
132
+ | case i : Int => 1
133
+ | case b : Boolean => 2
134
+ | case _ => 3
135
+ | }
136
+ |} """ .stripMargin)
104
137
assert(! compiler.reporter.hasErrors)
105
138
assert(! compiler.reporter.hasWarnings)
106
139
// should have one statement for each literal, one for each case block, and one for the selector.
@@ -110,12 +143,12 @@ class PluginCoverageTest
110
143
test(" scoverage should support yields" ) {
111
144
val compiler = ScoverageCompiler .default
112
145
compiler.compileCodeSnippet( """
113
- | object Yielder {
114
- | val holidays = for ( name <- Seq("sammy", "clint", "lee");
115
- | place <- Seq("london", "philly", "iowa") ) yield {
116
- | name + " has been to " + place
117
- | }
118
- | }""" .stripMargin)
146
+ | object Yielder {
147
+ | val holidays = for ( name <- Seq("sammy", "clint", "lee");
148
+ | place <- Seq("london", "philly", "iowa") ) yield {
149
+ | name + " has been to " + place
150
+ | }
151
+ | }""" .stripMargin)
119
152
assert(! compiler.reporter.hasErrors)
120
153
// 2 statements for the two applies in Seq, one for each literal which is 6, one for the flat map,
121
154
// one for the map, one for the yield op.
@@ -125,17 +158,17 @@ class PluginCoverageTest
125
158
test(" scoverage should not instrument local macro implementation" ) {
126
159
val compiler = ScoverageCompiler .default
127
160
compiler.compileCodeSnippet( """
128
- | object MyMacro {
129
- | import scala.language.experimental.macros
130
- | import scala.reflect.macros.Context
131
- | def test = macro testImpl
132
- | def testImpl(c: Context): c.Expr[Unit] = {
133
- | import c.universe._
134
- | reify {
135
- | println("macro test")
136
- | }
137
- | }
138
- |} """ .stripMargin)
161
+ | object MyMacro {
162
+ | import scala.language.experimental.macros
163
+ | import scala.reflect.macros.Context
164
+ | def test = macro testImpl
165
+ | def testImpl(c: Context): c.Expr[Unit] = {
166
+ | import c.universe._
167
+ | reify {
168
+ | println("macro test")
169
+ | }
170
+ | }
171
+ |} """ .stripMargin)
139
172
assert(! compiler.reporter.hasErrors)
140
173
compiler.assertNoCoverage()
141
174
}
@@ -153,9 +186,9 @@ class PluginCoverageTest
153
186
" scala-logging-slf4j_" + ScoverageCompiler .ShortScalaVersion ,
154
187
" 2.1.2" )
155
188
compiler.compileCodeSnippet( """ import com.typesafe.scalalogging.slf4j.StrictLogging
156
- |class MacroTest extends StrictLogging {
157
- | logger.info("will break")
158
- |} """ .stripMargin)
189
+ |class MacroTest extends StrictLogging {
190
+ | logger.info("will break")
191
+ |} """ .stripMargin)
159
192
assert(! compiler.reporter.hasErrors)
160
193
assert(! compiler.reporter.hasWarnings)
161
194
compiler.assertNoCoverage()
0 commit comments