-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow unit tests which assure that some code does not type-check.
This consists of a ShouldNotTypecheck macro which does the actual work, and a modification of the build process to delete the output from these tests after compiling (because they should be recompiled every time). (cherry picked from commit 1fe9863)
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
slick-testkit/src/main/scala/com/typesafe/slick/testkit/util/ShouldNotTypecheck.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.typesafe.slick.testkit.util | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.{Context, TypecheckException} | ||
import scala.util.control.NonFatal | ||
import java.util.regex.Pattern | ||
|
||
/** | ||
* A macro that ensures that a code snippet does not typecheck. | ||
*/ | ||
object ShouldNotTypecheck { | ||
def apply(code: String): Unit = macro applyImplNoExp | ||
def apply(code: String, expected: String): Unit = macro applyImpl | ||
|
||
def applyImplNoExp(ctx: Context)(code: ctx.Expr[String]) = applyImpl(ctx)(code, null) | ||
|
||
def applyImpl(ctx: Context)(code: ctx.Expr[String], expected: ctx.Expr[String]): ctx.Expr[Unit] = { | ||
import ctx.universe._ | ||
|
||
val Expr(Literal(Constant(codeStr: String))) = code | ||
val (expPat, expMsg) = expected match { | ||
case null => (null, "Expected some error.") | ||
case Expr(Literal(Constant(s: String))) => | ||
(Pattern.compile(s, Pattern.CASE_INSENSITIVE), "Expected error matching: "+s) | ||
} | ||
|
||
try ctx.typeCheck(ctx.parse("{ "+codeStr+" }")) catch { case e: TypecheckException => | ||
val msg = e.getMessage | ||
if((expected ne null) && !(expPat.matcher(msg)).matches) | ||
ctx.abort(ctx.enclosingPosition, "Type-checking failed in an unexpected way.\n"+ | ||
expMsg+"\nActual error: "+msg) | ||
else return reify(()) | ||
} | ||
|
||
ctx.abort(ctx.enclosingPosition, "Type-checking succeeded unexpectedly.\n"+expMsg) | ||
} | ||
} |