Skip to content

Commit 25435cd

Browse files
committed
Add a more complex macro to test more APIs
1 parent c48864a commit 25435cd

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
case _ =>
44+
'{ scala.Predef.assert(~cond) }
45+
}
46+
}
47+
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
assert(a.v === Some(10))
18+
assert(10 > 5)
19+
assert(4.0 < 5)
20+
assert(6.0 > 5L)
21+
assert(Box(6) > Box(6))
22+
assert(Box("h") > Box("h"))
23+
}
24+
}

0 commit comments

Comments
 (0)