From 8a61ff432543a29234193cd1f7c14abd3f3d31a0 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 2 Nov 2016 11:08:28 +0100 Subject: Move compiler and compiler tests to compiler dir --- compiler/test/dotty/tools/dotc/repl/TestREPL.scala | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 compiler/test/dotty/tools/dotc/repl/TestREPL.scala (limited to 'compiler/test/dotty/tools/dotc/repl') diff --git a/compiler/test/dotty/tools/dotc/repl/TestREPL.scala b/compiler/test/dotty/tools/dotc/repl/TestREPL.scala new file mode 100644 index 000000000..2263e85a0 --- /dev/null +++ b/compiler/test/dotty/tools/dotc/repl/TestREPL.scala @@ -0,0 +1,66 @@ +package dotty.tools.dotc +package repl + +import core.Contexts.Context +import collection.mutable +import java.io.StringWriter + +/** A subclass of REPL used for testing. + * It takes a transcript of a REPL session in `script`. The transcript + * starts with the first input prompt `scala> ` and ends with `scala> :quit` and a newline. + * Invoking `process()` on the `TestREPL` runs all input lines and + * collects then interleaved with REPL output in a string writer `out`. + * Invoking `check()` checks that the collected output matches the original + * `script`. + */ +class TestREPL(script: String) extends REPL { + + private val out = new StringWriter() + + override lazy val config = new REPL.Config { + override val output = new NewLinePrintWriter(out) + + override def context(ctx: Context) = { + val fresh = ctx.fresh + fresh.setSetting(ctx.settings.color, "never") + fresh.setSetting( + ctx.settings.classpath, + "../library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar" + ) + fresh.initialize()(fresh) + fresh + } + + override def input(in: Interpreter)(implicit ctx: Context) = new InteractiveReader { + val lines = script.lines.buffered + def readLine(prompt: String): String = { + val line = lines.next + val buf = new StringBuilder + if (line.startsWith(prompt)) { + output.println(line) + buf append line.drop(prompt.length) + while (lines.hasNext && lines.head.startsWith(continuationPrompt)) { + val continued = lines.next + output.println(continued) + buf append System.lineSeparator() + buf append continued.drop(continuationPrompt.length) + } + buf.toString + } + else readLine(prompt) + } + val interactive = false + } + } + + def check() = { + out.close() + val printed = out.toString + val transcript = printed.drop(printed.indexOf(config.prompt)) + if (transcript.toString.lines.toList != script.lines.toList) { + println("input differs from transcript:") + println(transcript) + assert(false) + } + } +} -- cgit v1.2.3