blob: f443330e0b198721232a67cd90534fd27ad5f5d8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import org.scalacheck._, Prop._, Gen._, Arbitrary._
import scala.reflect.runtime.universe._, Flag._, build.{Ident => _, _}
object TypecheckedProps extends QuasiquoteProperties("typechecked") {
def original(tree: Tree) = tree match {
case tt: TypeTree => Some(tt.original)
case _ => None
}
def originals(trees: List[Tree]) = trees.flatMap(original)
val int = ScalaDot(TypeName("Int"))
val intint = List(int, int)
property("tuple term") = test {
val q"(..$elements)" = typecheck(q"(1, 2)")
assert(elements ≈ List(q"1", q"2"))
}
property("tuple type") = test {
val tq"(..$els0)" = typecheckTyp(tq"Unit")
assert(els0.isEmpty)
val tq"(..$els1)" = typecheckTyp(tq"(Int, Int)")
assert(originals(els1) ≈ intint)
}
property("function type") = test {
val tq"(..$argtpes) => $restpe" = typecheckTyp(tq"(Int, Int) => Int")
assert(originals(argtpes) ≈ intint)
assert(original(restpe).get ≈ int)
}
property("for/for-yield") = test {
val enums = fq"x <- xs" :: fq"x1 = x + 1" :: fq"if x1 % 2 == 0" :: Nil
val body = q"x1"
val xs = q"val xs = List(1, 2, 3)"
val q"$_; for(..$enums0) yield $body0" = typecheck(q"$xs; for(..$enums) yield $body")
assert(enums0 ≈ enums)
assert(body0 ≈ body)
val q"$_; for(..$enums1) $body1" = typecheck(q"$xs; for(..$enums) $body")
assert(enums1 ≈ enums)
assert(body1 ≈ body)
}
property("for .filter instead of .withFilter") = test {
val enums = fq"foo <- new Foo" :: fq"if foo != null" :: Nil
val body = q"foo"
val q"$_; for(..$enums1) yield $body1" = typecheck(q"""
class Foo { def map(f: Any => Any) = this; def filter(cond: Any => Boolean) = this }
for(..$enums) yield $body
""")
assert(enums1 ≈ enums)
assert(body1 ≈ body)
}
}
|