diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/dotc/tests.scala | 2 | ||||
-rw-r--r-- | test/test/CompilerTest.scala | 17 | ||||
-rw-r--r-- | test/test/TestREPL.scala | 47 |
3 files changed, 65 insertions, 1 deletions
diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index d6bafe6be..48242f2d1 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -46,6 +46,7 @@ class tests extends CompilerTest { val negDir = testsDir + "neg/" val runDir = testsDir + "run/" val newDir = testsDir + "new/" + val replDir = testsDir + "repl/" val sourceDir = "./src/" val dottyDir = sourceDir + "dotty/" @@ -112,6 +113,7 @@ class tests extends CompilerTest { @Test def pos_859 = compileFile(posSpecialDir, "i859", scala2mode)(allowDeepSubtypes) @Test def new_all = compileFiles(newDir, twice) + @Test def repl_all = replFiles(replDir) @Test def neg_all = compileFiles(negDir, verbose = true, compileSubDirs = false) @Test def neg_typedIdents() = compileDir(negDir, "typedIdents") diff --git a/test/test/CompilerTest.scala b/test/test/CompilerTest.scala index ef2f719fc..1ca836133 100644 --- a/test/test/CompilerTest.scala +++ b/test/test/CompilerTest.scala @@ -5,12 +5,12 @@ import dotty.tools.dotc.{Main, Bench, Driver} import dotty.tools.dotc.reporting.Reporter import dotty.tools.dotc.util.SourcePosition import dotty.tools.dotc.config.CompilerCommand +import dotty.tools.io.PlainFile import scala.collection.mutable.ListBuffer import scala.reflect.io.{ Path, Directory, File => SFile, AbstractFile } import scala.tools.partest.nest.{ FileManager, NestUI } import scala.annotation.tailrec import java.io.{ RandomAccessFile, File => JFile } -import dotty.tools.io.PlainFile import org.junit.Test @@ -205,7 +205,22 @@ abstract class CompilerTest { } } + def replFile(prefix: String, fileName: String): Unit = { + val path = s"$prefix$fileName" + val f = new PlainFile(path) + val repl = new TestREPL(new String(f.toCharArray)) + repl.process(Array[String]()) + repl.check() + } + def replFiles(path: String): Unit = { + val dir = Directory(path) + val fileNames = dir.files.toArray.map(_.jfile.getName).filter(_ endsWith ".check") + for (name <- fileNames) { + log(s"testing $path$name") + replFile(path, name) + } + } // ========== HELPERS ============= 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 |