summaryrefslogtreecommitdiff
path: root/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala
diff options
context:
space:
mode:
authorDen Shabalin <den.shabalin@gmail.com>2013-07-08 20:21:33 +0200
committerEugene Burmako <xeno.by@gmail.com>2013-07-08 21:20:28 +0200
commit7553b0afa9e4a071c7ea1cd51effd57030b66be6 (patch)
tree0bdbf723ecf823e07c942c922d73a9ad768363c2 /test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala
parent17719231c615c06eb83f4109f53bfdd948a6fdb1 (diff)
downloadscala-7553b0afa9e4a071c7ea1cd51effd57030b66be6.tar.gz
scala-7553b0afa9e4a071c7ea1cd51effd57030b66be6.tar.bz2
scala-7553b0afa9e4a071c7ea1cd51effd57030b66be6.zip
tests for quasiquotes
Introduces an extensive ScalaCheck-based test suite for recently implemented quasiquotes. Provides tools for syntactic tree comparison and verifying compilation error messages.
Diffstat (limited to 'test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala')
-rw-r--r--test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala b/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala
new file mode 100644
index 0000000000..5e87aa57cc
--- /dev/null
+++ b/test/files/scalacheck/quasiquotes/QuasiquoteProperties.scala
@@ -0,0 +1,89 @@
+import scala.reflect.runtime.universe._
+import scala.tools.reflect.ToolBox
+import scala.tools.reflect.ToolBoxError
+import scala.reflect.macros.TypecheckException
+
+import org.scalacheck._
+import Prop._
+import Gen._
+import Arbitrary._
+
+class QuasiquoteProperties(name: String) extends Properties(name) with ArbitraryTreesAndNames with Helpers
+
+trait Helpers {
+ /** Runs a code block and returns proof confirmation
+ * if no exception has been thrown while executing code
+ * block. This is useful for simple one-off tests.
+ */
+ def test[T](block: => T)=
+ Prop { (params) =>
+ block
+ Result(Prop.Proof)
+ }
+
+ implicit class TestSimilarTree(tree1: Tree) {
+ def ≈(tree2: Tree) = tree1.equalsStructure(tree2)
+ }
+
+ implicit class TestSimilarListTree(lst: List[Tree]) {
+ def ≈(other: List[Tree]) = (lst.length == other.length) && lst.zip(other).forall { case (t1, t2) => t1 ≈ t2 }
+ }
+
+ implicit class TestSimilarListListTree(lst: List[List[Tree]]) {
+ def ≈(other: List[List[Tree]]) = (lst.length == other.length) && lst.zip(other).forall { case (l1, l2) => l1 ≈ l2 }
+ }
+
+ implicit class TestSimilarName(name: Name) {
+ def ≈(other: Name) = name == other
+ }
+
+ implicit class TestSimilarMods(mods: Modifiers) {
+ def ≈(other: Modifiers) = (mods.flags == other.flags) && (mods.privateWithin ≈ other.privateWithin) && (mods.annotations ≈ other.annotations)
+ }
+
+ def assertThrows[T <: AnyRef](f: => Any)(implicit manifest: Manifest[T]): Unit = {
+ val clazz = manifest.erasure.asInstanceOf[Class[T]]
+ val thrown =
+ try {
+ f
+ false
+ } catch {
+ case u: Throwable =>
+ if (!clazz.isAssignableFrom(u.getClass))
+ assert(false, s"wrong exception: $u")
+ true
+ }
+ if(!thrown)
+ assert(false, "exception wasn't thrown")
+ }
+
+ def fails(msg: String, block: String) = {
+ def result(ok: Boolean, description: String = "") = {
+ val status = if (ok) Prop.Proof else Prop.False
+ val labels = if (description != "") Set(description) else Set.empty[String]
+ Prop { new Prop.Result(status, Nil, Set.empty, labels) }
+ }
+ try {
+ val tb = rootMirror.mkToolBox()
+ val tree = tb.parse(s"""
+ object Wrapper extends Helpers {
+ import scala.reflect.runtime.universe._
+ $block
+ }
+ """)
+ tb.compile(tree)
+ result(false, "given code doesn't fail to typecheck")
+ } catch {
+ case ToolBoxError(emsg, _) =>
+ if (!emsg.contains(msg))
+ result(false, s"error message '${emsg}' is not the same as expected '$msg'")
+ else
+ result(true)
+ }
+ }
+
+ def annot(name: String): Tree = annot(TypeName(name), Nil)
+ def annot(name: TypeName): Tree = annot(name, Nil)
+ def annot(name: String, args: List[Tree]): Tree = annot(TypeName(name), args)
+ def annot(name: TypeName, args: List[Tree]): Tree = q"new $name(..$args)"
+} \ No newline at end of file