diff options
Diffstat (limited to 'src/partest-extras')
-rw-r--r-- | src/partest-extras/scala/tools/partest/ReplTest.scala | 50 | ||||
-rw-r--r-- | src/partest-extras/scala/tools/partest/ScriptTest.scala | 21 |
2 files changed, 63 insertions, 8 deletions
diff --git a/src/partest-extras/scala/tools/partest/ReplTest.scala b/src/partest-extras/scala/tools/partest/ReplTest.scala index 7cc2dd39a9..a728e8bdef 100644 --- a/src/partest-extras/scala/tools/partest/ReplTest.scala +++ b/src/partest-extras/scala/tools/partest/ReplTest.scala @@ -9,8 +9,8 @@ import scala.tools.nsc.Settings import scala.tools.nsc.interpreter.ILoop import java.lang.reflect.{ Method => JMethod, Field => JField } -/** A trait for testing repl code. It drops the first line - * of output because the real repl prints a version number. +/** A class for testing repl code. + * It filters the line of output that mentions a version number. */ abstract class ReplTest extends DirectTest { // override to transform Settings object immediately before the finish @@ -22,22 +22,56 @@ abstract class ReplTest extends DirectTest { s.Xnojline.value = true transformSettings(s) } + def welcoming: Boolean = false + lazy val welcome = "(Welcome to Scala) version .*".r + def normalize(s: String) = s match { + case welcome(w) => w + case s => s + } + def unwelcoming(s: String) = s match { + case welcome(w) => false + case _ => true + } def eval() = { val s = settings log("eval(): settings = " + s) - ILoop.runForTranscript(code, s).lines drop 1 + //ILoop.runForTranscript(code, s).lines drop 1 // not always first line + val lines = ILoop.runForTranscript(code, s).lines + if (welcoming) lines map normalize + else lines filter unwelcoming } def show() = eval() foreach println } +/** Retain and normalize the welcome message. */ +trait Welcoming { this: ReplTest => + override def welcoming = true +} + +/** Run a REPL test from a session transcript. + * The `session` should be a triple-quoted String starting + * with the `Type in expressions` message and ending + * after the final `prompt`, including the last space. + */ abstract class SessionTest extends ReplTest { + /** Session transcript, as a triple-quoted, multiline, marginalized string. */ def session: String - override final def code = expected filter (_.startsWith(prompt)) map (_.drop(prompt.length)) mkString "\n" - def expected = session.stripMargin.lines.toList + + /** Expected output, as an iterator. */ + def expected = session.stripMargin.lines + + /** Code is the command list culled from the session (or the expected session output). + * Would be nicer if code were lazy lines. + */ + override final def code = expected filter (_ startsWith prompt) map (_ drop prompt.length) mkString "\n" + final def prompt = "scala> " + + /** Default test is to compare expected and actual output and emit the diff on a failed comparison. */ override def show() = { - val out = eval().toList - if (out.size != expected.size) Console println s"Expected ${expected.size} lines, got ${out.size}" - if (out != expected) Console print nest.FileManager.compareContents(expected, out, "expected", "actual") + val evaled = eval().toList + val wanted = expected.toList + if (evaled.size != wanted.size) Console println s"Expected ${wanted.size} lines, got ${evaled.size}" + if (evaled != wanted) Console print nest.FileManager.compareContents(wanted, evaled, "expected", "actual") } } diff --git a/src/partest-extras/scala/tools/partest/ScriptTest.scala b/src/partest-extras/scala/tools/partest/ScriptTest.scala new file mode 100644 index 0000000000..24a4121b54 --- /dev/null +++ b/src/partest-extras/scala/tools/partest/ScriptTest.scala @@ -0,0 +1,21 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2013 LAMP/EPFL + */ + +package scala.tools.partest + +import scala.reflect.internal.util.ScalaClassLoader + +/** A `ScriptTest` is a `DirectTest` for which the code + * is the contents of a script file. + */ +abstract class ScriptTest extends DirectTest { + def testmain = "TestMain" + override def extraSettings = s"-usejavacp -Xscript $testmain" + def scriptPath = testPath changeExtension "script" + def code = scriptPath.toFile.slurp + def show() = { + compile() + ScalaClassLoader(getClass.getClassLoader).run(testmain, Seq.empty[String]) + } +} |