diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-11-02 11:08:28 +0100 |
---|---|---|
committer | Guillaume Martres <smarter@ubuntu.com> | 2016-11-22 01:35:07 +0100 |
commit | 8a61ff432543a29234193cd1f7c14abd3f3d31a0 (patch) | |
tree | a8147561d307af862c295cfc8100d271063bb0dd /compiler/test/dotty/tools/dotc/repl/TestREPL.scala | |
parent | 6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f (diff) | |
download | dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.gz dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.bz2 dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.zip |
Move compiler and compiler tests to compiler dir
Diffstat (limited to 'compiler/test/dotty/tools/dotc/repl/TestREPL.scala')
-rw-r--r-- | compiler/test/dotty/tools/dotc/repl/TestREPL.scala | 66 |
1 files changed, 66 insertions, 0 deletions
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) + } + } +} |