Add test util to check neg macro tests

This commit is contained in:
jvican 2017-05-24 10:21:52 +02:00
parent 88b2e84559
commit 03daae49e2
No known key found for this signature in database
GPG Key ID: 42DAFA0F112E8050
1 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,56 @@
/*
* This file has been copy-pasted from spores.
* https://github.com/scalacenter/spores/blob/master/core/src/test/scala/scala/spores/TestUtil.scala
*/
package sbt.std
import scala.reflect._
object TestUtil {
import scala.language.postfixOps
import tools.reflect.{ ToolBox, ToolBoxError }
def intercept[T <: Throwable: ClassTag](test: => Any): T = {
try {
test
throw new Exception(s"Expected exception ${classTag[T]}")
} catch {
case t: Throwable =>
if (classTag[T].runtimeClass != t.getClass) throw t
else t.asInstanceOf[T]
}
}
def eval(code: String, compileOptions: String = ""): Any = {
val tb = mkToolbox(compileOptions)
tb.eval(tb.parse(code))
}
def mkToolbox(compileOptions: String = ""): ToolBox[_ <: scala.reflect.api.Universe] = {
val m = scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
m.mkToolBox(options = compileOptions)
}
lazy val toolboxClasspath: String = {
val resource = getClass.getClassLoader.getResource("toolbox.classpath")
val classpathFile = scala.io.Source.fromFile(resource.toURI)
val completeSporesCoreClasspath = classpathFile.getLines.mkString
completeSporesCoreClasspath
}
def expectError(errorSnippet: String,
compileOptions: String = "-Xmacro-settings:debug-spores",
baseCompileOptions: String = s"-cp $toolboxClasspath")(code: String) {
val errorMessage = intercept[ToolBoxError] {
eval(code, s"$compileOptions $baseCompileOptions")
}.getMessage
val userMessage =
s"""
|FOUND: $errorMessage
|EXPECTED: $errorSnippet
""".stripMargin
assert(errorMessage.contains(errorSnippet), userMessage)
}
}