diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-11-01 18:34:29 +0100 |
---|---|---|
committer | Guillaume Martres <smarter@ubuntu.com> | 2016-11-22 01:35:07 +0100 |
commit | 6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f (patch) | |
tree | fe7729ddb03a84728687d5a3068f520b0bc1c297 /test/dotty/tools/dotc/repl/TestREPL.scala | |
parent | b3855424280a821601f126b6b4c6a731b72540ea (diff) | |
download | dotty-6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f.tar.gz dotty-6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f.tar.bz2 dotty-6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f.zip |
Move (most) unit tests to correct locations
Should still perhaps move `test/dotc/tests.scala` and the others in the
same directory to a better more cohesive location. Would like to delete
the worksheets as well - but maybe they hold sentimental value...
Diffstat (limited to 'test/dotty/tools/dotc/repl/TestREPL.scala')
-rw-r--r-- | test/dotty/tools/dotc/repl/TestREPL.scala | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test/dotty/tools/dotc/repl/TestREPL.scala b/test/dotty/tools/dotc/repl/TestREPL.scala new file mode 100644 index 000000000..7be8dc13e --- /dev/null +++ b/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) + } + } +} |