File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
library/src-bootstrapped/scala/testing Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ package scala .testing
2+
3+ import scala .quoted ._
4+ import scala .tasty .Reflection
5+
6+ inline def typeChecks (inline code : String ): Boolean = $ { typeChecksImpl(code) }
7+
8+ private def typeChecksImpl (code : String )(implicit reflect : Reflection ): Expr [Boolean ] = {
9+ import reflect ._
10+ typing.typeChecks(code).toExpr
11+ }
12+
Original file line number Diff line number Diff line change 1+ import scala .testing ._
2+
3+ object Test {
4+
5+ trait Eq [T ]
6+ implicit val eq : Eq [Int ] = new Eq [Int ] {}
7+
8+ implicit class AnyOps [T ](x : T ) {
9+ def === (y : T )(implicit c : Eq [T ]) = x == y
10+ }
11+
12+ def main (args : Array [String ]): Unit = {
13+ assert(typeChecks(" 5 === 5" ))
14+ assert(! typeChecks(" 5.6 === 7.7" ))
15+
16+ val x : Int = 5
17+ assert(typeChecks(" x + 3" ))
18+ assert(! typeChecks(" y + 3" ))
19+ import scala .util .Left
20+ assert(typeChecks(" Left(3)" ))
21+ assert(! typeChecks(" Rigth(3)" ))
22+
23+ def f (x : Int ): Int = x * x
24+ assert(typeChecks(" f(3)" ))
25+ assert(! typeChecks(" g(3)" ))
26+
27+ type T
28+ assert(typeChecks(" def foo(x: T): T = x" ))
29+ assert(! typeChecks(" foo(???)" ))
30+ assert(! typeChecks(" def foo(x: S): S = x" ))
31+
32+ assert(! typeChecks(" def test(x: Int) =" ))
33+
34+ assert(typeChecks(
35+ """
36+ class EqString extends Eq[String]
37+ new EqString
38+ """
39+ ))
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments