Skip to content

Commit d94cbc4

Browse files
committed
#23 Added synthetic checks
1 parent 81e4db3 commit d94cbc4

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

src/main/scala/scoverage/plugin.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,18 @@ class ScoverageInstrumentationComponent(val global: Global)
278278
}
279279
}
280280

281+
private def isSynthetic(t: Tree): Boolean = Option(t.symbol).fold(false)(_.isSynthetic)
282+
private def isNonSynthetic(t: Tree): Boolean = !isSynthetic(t)
283+
private def containsNonSynthetic(t: Tree): Boolean = isNonSynthetic(t) || t.children.exists(containsNonSynthetic)
284+
281285
def allConstArgs(args: List[Tree]) = args.forall(arg => arg.isInstanceOf[Literal] || arg.isInstanceOf[Ident])
282286

283287
def process(tree: Tree): Tree = {
284288
tree match {
285289

290+
// ignore synthetic trees that contain non-syths. Probably macros. Either way breaks due to range validation
291+
case t if isSynthetic(t) && containsNonSynthetic(t) => super.transform(t)
292+
286293
/**
287294
* Object creation from new.
288295
* Ignoring creation calls to anon functions
@@ -349,6 +356,8 @@ class ScoverageInstrumentationComponent(val global: Global)
349356
c
350357
}
351358

359+
//case d : DefDef => d
360+
352361
// this will catch methods defined as macros, eg def test = macro testImpl
353362
// it will not catch macro implemenations
354363
case d: DefDef if d.symbol != null && d.symbol.annotations.size > 0

src/test/scala/scoverage/PluginASTSupportTest.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,23 @@ class PluginASTSupportTest
7575
| }
7676
| }
7777
|} """.stripMargin)
78+
assert(!reporter.hasErrors)
7879
assert(!reporter.hasWarnings)
7980
}
8081

81-
// test("scoverage supports skinny #23") {
82-
// addToClassPath("org.joda", "joda-convert", "1.3.1")
83-
// addToClassPath("joda-time", "joda-time", "2.3")
84-
// addToClassPath("org.scalikejdbc", "scalikejdbc_2.10", "1.7.5")
85-
// addToClassPath("org.scalikejdbc", "scalikejdbc-interpolation_2.10", "1.7.5")
86-
// addToClassPath("org.scalikejdbc", "scalikejdbc-interpolation-core_2.10", "1.7.5")
87-
// addToClassPath("org.scalikejdbc", "scalikejdbc-interpolation-macro_2.10", "1.7.5")
88-
// addToClassPath("org.scalikejdbc", "scalikejdbc-config_2.10", "1.7.5")
89-
// addToClassPath("org.skinny-framework", "skinny-common_2.10", "1.0.8")
90-
// addToClassPath("org.skinny-framework", "skinny-framework_2.10", "1.0.8")
91-
// addToClassPath("org.skinny-framework", "skinny-orm_2.10", "1.0.8")
92-
// addToClassPath("org.slf4j", "slf4j-api", "1.7.7")
93-
// compileSourceResources(getClass.getResource("/scoverage/skinny/Member.scala"))
94-
// assert(!reporter.hasErrors)
95-
// }
82+
// https://github.com/skinny-framework/skinny-framework/issues/97
83+
test("macro range positions should not break plugin") {
84+
addToClassPath("org.slf4j", "slf4j-api", "1.7.7")
85+
addToClassPath("com.typesafe.scala-logging", "scala-logging-api_" + shortScalaVersion, "2.1.2")
86+
addToClassPath("com.typesafe.scala-logging", "scala-logging-slf4j_" + shortScalaVersion, "2.1.2")
87+
compileCodeSnippet( """import com.typesafe.scalalogging.slf4j.StrictLogging
88+
|
89+
|object MacroTest extends StrictLogging {
90+
| println("Hello")
91+
| logger.info("will break")
92+
|} """.stripMargin)
93+
assert(!reporter.hasErrors)
94+
assert(!reporter.hasWarnings)
95+
}
9696
}
9797

src/test/scala/scoverage/PluginCoverageTest.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class PluginCoverageTest
7171
assertNMeasuredStatements(7)
7272
}
7373

74-
test("scoverage component should not instrument any macro code") {
74+
test("scoverage should not instrument local macro implementation") {
7575
compileCodeSnippet( """
7676
| object MyMacro {
7777
| import scala.language.experimental.macros
@@ -85,6 +85,21 @@ class PluginCoverageTest
8585
| }
8686
|} """.stripMargin)
8787
assert(!reporter.hasErrors)
88-
assertNMeasuredStatements(0)
88+
assertNoCoverage
89+
}
90+
91+
// https://github.com/skinny-framework/skinny-framework/issues/97
92+
test("scoverage should not instrument expanded macro code") {
93+
addToClassPath("org.slf4j", "slf4j-api", "1.7.7")
94+
addToClassPath("com.typesafe.scala-logging", "scala-logging-api_" + shortScalaVersion, "2.1.2")
95+
addToClassPath("com.typesafe.scala-logging", "scala-logging-slf4j_" + shortScalaVersion, "2.1.2")
96+
compileCodeSnippet( """import com.typesafe.scalalogging.slf4j.StrictLogging
97+
|
98+
|object MacroTest extends StrictLogging {
99+
| logger.info("will break")
100+
|} """.stripMargin)
101+
assert(!reporter.hasErrors)
102+
assert(!reporter.hasWarnings)
103+
assertNoCoverage
89104
}
90105
}

src/test/scala/scoverage/PluginRunner.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ trait PluginSupport {
6262
if (file.exists) {
6363
println(s"Located ivy jar [$file]")
6464
file
65-
} else throw new FileNotFoundException(s"Could not locate [$jarPath]. Tests require SBT 0.13+")
65+
} else throw new FileNotFoundException(s"Could not locate [$jarPath].")
6666
}
6767

6868
def sbtCompileDir: File = {
@@ -71,6 +71,8 @@ trait PluginSupport {
7171
else throw new FileNotFoundException(s"Could not locate SBT compile directory for plugin files [$dir]")
7272
}
7373

74+
def assertNoCoverage() = assert(!compiler.testStore.sources.mkString(" ").contains(s"scoverage.Invoker.invoked"))
75+
7476
def assertNMeasuredStatements(n: Int): Unit = {
7577
for ( k <- 1 to n ) {
7678
assert(compiler.testStore.sources.mkString(" ").contains(s"scoverage.Invoker.invoked($k,"),

0 commit comments

Comments
 (0)