Skip to content

Commit 022c2b1

Browse files
committed
Enrich test case
Run a typical dotty compiler scenario with implicit contexts.
1 parent 3b66be1 commit 022c2b1

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/run/implicitFuns.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,46 @@ object Test {
5151
val c = xx("hh", 22)
5252

5353
val c1: Int = c
54+
55+
Contextual.main(args)
56+
}
57+
}
58+
59+
object Contextual {
60+
61+
class Key[+V]
62+
63+
class Context(bindings: Map[Key[Any], Any]) {
64+
def binding[V](key: Key[V]): Option[V] =
65+
bindings.get(key).asInstanceOf[Option[V]]
66+
def withBinding[V](key: Key[V], value: V): Context =
67+
new Context(bindings + ((key, value)))
68+
}
69+
70+
val rootContext = new Context(Map())
71+
72+
val Source = new Key[String]
73+
val Options = new Key[List[String]]
74+
75+
type Ctx[T] = implicit Context => T
76+
77+
def ctx: Ctx[Context] = implicitly[Context]
78+
79+
def compile(s: String): Ctx[Boolean] =
80+
runOn(new java.io.File(s))(ctx.withBinding(Source, s)) >= 0
81+
82+
def runOn(f: java.io.File): Ctx[Int] = {
83+
val options = List("-verbose", "-explaintypes")
84+
process(f)(ctx.withBinding(Options, options))
85+
}
86+
87+
def process(f: java.io.File): Ctx[Int] =
88+
ctx.binding(Source).get.length - ctx.binding(Options).get.length
89+
90+
def main(args: Array[String]) = {
91+
implicit val context: Context = rootContext
92+
assert(compile("abc"))
93+
assert(compile("ab"))
94+
assert(!compile("a"))
5495
}
5596
}

0 commit comments

Comments
 (0)