Skip to content

Commit 39087d4

Browse files
committed
Add run tests
1 parent 426d1a3 commit 39087d4

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ lazy val root = (project in file(".")).
88
scalaVersion := dottyVersion,
99

1010
libraryDependencies ++= Seq(
11+
"ch.epfl.lamp" % "dotty_0.6" % "0.6.0-RC1" % "test->runtime",
1112
"com.novocode" % "junit-interface" % "0.11" % "test"
1213
)
1314
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// QUICK FIX
2+
// TODO remove this file in 0.7
3+
// Copy of fix in https://github.com/lampepfl/dotty/pull/3871
4+
5+
package scala.quoted
6+
package util
7+
8+
import dotty.tools.dotc.Driver
9+
import dotty.tools.dotc.core.Contexts.Context
10+
import dotty.tools.dotc.core.StdNames._
11+
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
12+
import dotty.tools.repl.AbstractFileClassLoader
13+
14+
import scala.quoted.Expr
15+
import java.io.ByteArrayOutputStream
16+
import java.io.PrintStream
17+
import java.net.URLClassLoader
18+
import java.nio.charset.StandardCharsets
19+
20+
class QuoteDriver extends Driver {
21+
22+
def run[T](expr: Expr[T], settings: Runners.RunSettings): T = {
23+
val ctx: Context = initCtx.fresh
24+
ctx.settings.optimise.update(settings.optimise)(ctx)
25+
26+
val outDir: AbstractFile = settings.outDir match {
27+
case Some(out) =>
28+
val dir = Directory(out)
29+
dir.createDirectory()
30+
new PlainDirectory(Directory(out))
31+
case None =>
32+
new VirtualDirectory("(memory)", None)
33+
}
34+
35+
val driver = new dotty.tools.dotc.quoted.ExprCompiler(outDir)
36+
driver.newRun(ctx).compileExpr(expr)
37+
38+
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)
39+
40+
val clazz = classLoader.loadClass(driver.outputClassName.toString)
41+
val method = clazz.getMethod("apply")
42+
val instance = clazz.newInstance()
43+
44+
method.invoke(instance).asInstanceOf[T]
45+
}
46+
47+
def show(expr: Expr[_]): String = {
48+
val ctx: Context = initCtx.fresh
49+
ctx.settings.color.update("never")(ctx) // TODO support colored show
50+
val baos = new ByteArrayOutputStream
51+
var ps: PrintStream = null
52+
try {
53+
ps = new PrintStream(baos, true, "utf-8")
54+
55+
new dotty.tools.dotc.quoted.ExprDecompiler(ps).newRun(ctx).compileExpr(expr)
56+
57+
new String(baos.toByteArray, StandardCharsets.UTF_8)
58+
}
59+
finally if (ps != null) ps.close()
60+
}
61+
62+
override def initCtx: Context = {
63+
val ictx = super.initCtx.fresh
64+
var classpath = System.getProperty("java.class.path")
65+
this.getClass.getClassLoader match {
66+
case cl: URLClassLoader =>
67+
classpath = cl.getURLs.map(_.getFile()).mkString("", ":", if (classpath == "") "" else ":" + classpath)
68+
case _ =>
69+
}
70+
ictx.settings.classpath.update(classpath)(ictx)
71+
ictx
72+
}
73+
74+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// QUICK FIX
2+
// TODO remove this file in 0.7
3+
// Copy of fix in https://github.com/lampepfl/dotty/pull/3871
4+
5+
package scala.quoted
6+
package util
7+
8+
import dotty.tools.dotc.ast.Trees.Literal
9+
import dotty.tools.dotc.core.Constants.Constant
10+
import dotty.tools.dotc.printing.RefinedPrinter
11+
12+
import scala.quoted.Expr
13+
import scala.quoted.Liftable.ConstantExpr
14+
import scala.runtime.quoted._
15+
16+
/** Default runners for quoted expressions */
17+
object Runners {
18+
19+
implicit def runner[T]: Runner[T] = new Runner[T] {
20+
21+
def run(expr: Expr[T]): T = Runners.run(expr, RunSettings())
22+
23+
def show(expr: Expr[T]): String = expr match {
24+
case expr: ConstantExpr[T] =>
25+
val ctx = new QuoteDriver().initCtx
26+
ctx.settings.color.update("never")(ctx)
27+
val printer = new RefinedPrinter(ctx)
28+
printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
29+
case _ => new QuoteDriver().show(expr)
30+
}
31+
}
32+
33+
def run[T](expr: Expr[T], settings: RunSettings): T = expr match {
34+
case expr: ConstantExpr[T] => expr.value
35+
case _ => new QuoteDriver().run(expr, settings)
36+
}
37+
38+
case class RunSettings(
39+
/** Enable optimisation when compiling the quoted code */
40+
optimise: Boolean = false,
41+
/** Output directory for the copiled quote. If set to None the output will be in memory */
42+
outDir: Option[String] = None
43+
)
44+
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package scala.quoted
2-
package util.liftables
2+
package util
3+
package liftables
34

45
import org.junit.Test
56
import org.junit.Assert._
67

7-
import dotty.tools.dotc.quoted.Runners._
8+
import util.Runners._
89

910
class TuplesTest {
1011

1112
@Test def testLifing: Unit = {
13+
// FixClasspath()
1214
import Tuples._
13-
val t1: Expr[Tuple1[Int]] = Tuple1(5)
15+
val t1: Expr[Tuple1[Int]] = Tuple1(1)
1416
val t2: Expr[(Int, Int)] = (1, 2)
1517
val t3: Expr[(Int, Int, Int)] = (1, 2, 3)
1618
val t4: Expr[(Int, Int, Int, Int)] = (1, 2, 3, 4)
1719

18-
// FIXME
19-
// t1.run
20-
21-
20+
assertEquals(Tuple1(1), t1.run)
21+
assertEquals((1, 2), t2.run)
22+
assertEquals((1, 2, 3), t3.run)
23+
assertEquals((1, 2, 3, 4), t4.run)
2224
}
2325

2426
}

0 commit comments

Comments
 (0)