diff options
Diffstat (limited to 'test/test/TestREPL.scala')
-rw-r--r-- | test/test/TestREPL.scala | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/test/test/TestREPL.scala b/test/test/TestREPL.scala new file mode 100644 index 000000000..d01038c43 --- /dev/null +++ b/test/test/TestREPL.scala @@ -0,0 +1,47 @@ +package test + +import dotty.tools.dotc.repl._ +import dotty.tools.dotc.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 input(implicit ctx: Context) = new InteractiveReader { + val lines = script.lines + def readLine(prompt: String): String = { + val line = lines.next + if (line.startsWith(prompt) || line.startsWith(continuationPrompt)) { + output.println(line) + line.drop(prompt.length) + } + 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 != script) { + println("input differs from transcript:") + println(transcript) + assert(false) + } + } +}
\ No newline at end of file |