Skip to content

Commit ab9a607

Browse files
nicolasstuckibiboudis
authored andcommitted
Add infrastructure to interpret run test
1 parent 8443112 commit ab9a607

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

tests/pos/HelloWorld.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

tests/run-with-compiler-custom-args/tasty-interpreter/Test.scala

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import java.io.{ByteArrayOutputStream, PrintStream}
1+
import java.io.{ByteArrayOutputStream, File, PrintStream}
22

3+
import dotty.tools.dotc.core.Contexts
4+
import dotty.tools.dotc.reporting.Reporter
5+
import dotty.tools.dotc.reporting.diagnostic.MessageContainer
36
import dotty.tools.dotc.util.DiffUtil
7+
import dotty.tools.io.Path
48

59
import scala.io.Source
610
import scala.tasty.file._
@@ -9,12 +13,8 @@ import scala.tasty.Reflection
913

1014
object Test {
1115
def main(args: Array[String]): Unit = {
12-
val ps = new ByteArrayOutputStream()
13-
try scala.Console.withOut(ps) {
14-
ConsumeTasty("", List("IntepretedMain", "InterpretedBar"), new TastyInterpreter)
15-
} catch {
16-
case e: Throwable => throw new Exception(ps.toString, e)
17-
}
16+
17+
val actualOutput = interpret("")("IntepretedMain", "InterpretedBar")
1818
val expectedOutput =
1919
"""42
2020
|
@@ -54,12 +54,45 @@ object Test {
5454
|68
5555
|""".stripMargin
5656

57-
val actualOutput = ps.toString
58-
5957
assert(expectedOutput == actualOutput,
6058
"\n>>>>>>>>>>>>>>>>>>\n" +
6159
DiffUtil.mkColoredCodeDiff(actualOutput, expectedOutput, true) +
6260
"<<<<<<<<<<<<<<<<<<"
6361
)
62+
63+
// compileAndInterpret("HelloWorld")
64+
// compileAndInterpret("i3518")
65+
// compileAndInterpret("withIndex")
66+
}
67+
68+
def compileAndInterpret(testName: String) = {
69+
val reproter = new Reporter {
70+
def doReport(m: MessageContainer)(implicit ctx: Contexts.Context): Unit = println(m)
71+
}
72+
val out = java.nio.file.Paths.get("out/interpreted")
73+
if (!java.nio.file.Files.exists(out))
74+
java.nio.file.Files.createDirectory(out)
75+
dotty.tools.dotc.Main.process(Array("-classpath", System.getProperty("java.class.path"), "-d", out.toString, "tests/run/" + testName + ".scala"), reproter)
76+
77+
val actualOutput = interpret(out.toString)("Test")
78+
79+
val checkFile = java.nio.file.Paths.get("tests/run/" + testName + ".check")
80+
val expectedOutput = Source.fromFile(checkFile.toFile).getLines().mkString("\n")
81+
82+
assert(expectedOutput == actualOutput,
83+
"\n>>>>>>>>>>>>>>>>>>\n" +
84+
DiffUtil.mkColoredCodeDiff(actualOutput, expectedOutput, true) +
85+
"<<<<<<<<<<<<<<<<<<"
86+
)
87+
}
88+
89+
def interpret(classpath: String*)(interpretedClasses: String*): String = {
90+
val ps = new ByteArrayOutputStream()
91+
try scala.Console.withOut(ps) {
92+
ConsumeTasty(classpath.mkString(java.io.File.pathSeparatorChar.toString), interpretedClasses.toList, new TastyInterpreter)
93+
} catch {
94+
case e: Throwable => throw new Exception(ps.toString, e)
95+
}
96+
ps.toString
6497
}
6598
}

tests/run/HelloWorld.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = println("hello world")
3+
}

0 commit comments

Comments
 (0)