Skip to content

Commit 559b94f

Browse files
Merge pull request #5788 from dotty-staging/test-reflect
Add a more complex macro to test more APIs
2 parents fa0b169 + 5166393 commit 559b94f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object scalatest {
5+
6+
inline def assert(condition: => Boolean): Unit = ~assertImpl('(condition), '(""))
7+
8+
def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = {
9+
import refl._
10+
import util._
11+
import quoted.Toolbox.Default._
12+
13+
def isImplicitMethodType(tp: Type): Boolean =
14+
Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty
15+
16+
cond.unseal.underlyingArgument match {
17+
case Term.Apply(sel @ Term.Select(lhs, op), rhs :: Nil) =>
18+
let(lhs) { left =>
19+
let(rhs) { right =>
20+
let(Term.Apply(Term.Select.copy(sel)(left, op), right :: Nil)) { result =>
21+
val l = left.seal[Any]
22+
val r = right.seal[Any]
23+
val b = result.seal[Boolean]
24+
val code = '{ scala.Predef.assert(~b) }
25+
code.unseal
26+
}
27+
}
28+
}.seal[Unit]
29+
case Term.Apply(f @ Term.Apply(Term.IsSelect(sel @ Term.Select(Term.Apply(qual, lhs :: Nil), op)), rhs :: Nil), implicits)
30+
if isImplicitMethodType(f.tpe) =>
31+
let(lhs) { left =>
32+
let(rhs) { right =>
33+
let(Term.Apply(Term.Apply(Term.Select.copy(sel)(Term.Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
34+
val l = left.seal[Any]
35+
val r = right.seal[Any]
36+
val b = result.seal[Boolean]
37+
val code = '{ scala.Predef.assert(~b) }
38+
code.unseal
39+
}
40+
}
41+
}.seal[Unit]
42+
}
43+
}
44+
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
object Test {
2+
import scalatest._
3+
4+
case class Box[T](v: T) {
5+
def >(that: Box[T]): Boolean = this == that
6+
}
7+
8+
trait EqInt
9+
implicit val eq: EqInt = new EqInt {}
10+
11+
implicit class AnyOps[T](x: T) {
12+
def === (y: T)(implicit c: EqInt) = x == y
13+
}
14+
15+
def main(args: Array[String]): Unit = {
16+
val a = Box(Some(10))
17+
val five: Float = 5.0f
18+
val six: Double = 6.0
19+
val ten: Int = 10
20+
assert(a.v === Some(10))
21+
assert(five < six)
22+
assert(five > 4)
23+
assert(ten > 5)
24+
assert(six < 7)
25+
assert(six > 5L)
26+
assert(Box(6) > Box(6))
27+
assert(Box("h") > Box("h"))
28+
}
29+
}

0 commit comments

Comments
 (0)