diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index 115940d52e5e..f279d44b7f8a 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -81,8 +81,8 @@ case class Completions(cursor: Int, /** Main REPL instance, orchestrating input, compilation and presentation */ class ReplDriver(settings: Array[String], - protected val out: PrintStream = Console.out, - protected val classLoader: Option[ClassLoader] = None) extends Driver { + out: PrintStream = Console.out, + classLoader: Option[ClassLoader] = None) extends Driver { /** Overridden to `false` in order to not have to give sources on the * commandline @@ -197,8 +197,8 @@ class ReplDriver(settings: Array[String], private def extractImports(trees: List[untpd.Tree]): List[untpd.Import] = trees.collect { case imp: untpd.Import => imp } - private def interpret(res: ParseResult)(implicit state: State): State = - res match { + private def interpret(res: ParseResult)(implicit state: State): State = { + val newState = res match { case parsed: Parsed if parsed.trees.nonEmpty => compile(parsed) .withHistory(parsed.sourceCode :: state.history) @@ -216,6 +216,9 @@ class ReplDriver(settings: Array[String], case _ => // new line, empty tree state } + out.println() + newState + } /** Compile `parsed` trees and evolve `state` in accordance */ protected[this] final def compile(parsed: Parsed)(implicit state: State): State = { diff --git a/compiler/test-resources/repl/i1369 b/compiler/test-resources/repl/i1369 new file mode 100644 index 000000000000..ced0af6aea36 --- /dev/null +++ b/compiler/test-resources/repl/i1369 @@ -0,0 +1,4 @@ +scala> print("foo") +foo +scala> "Hello" +val res0: String = Hello diff --git a/compiler/test/dotty/tools/repl/ReplTest.scala b/compiler/test/dotty/tools/repl/ReplTest.scala index 6d3679f9c4b7..3c5a3df416fb 100644 --- a/compiler/test/dotty/tools/repl/ReplTest.scala +++ b/compiler/test/dotty/tools/repl/ReplTest.scala @@ -11,38 +11,20 @@ import org.junit.Assert.fail import dotc.reporting.diagnostic.MessageContainer import results.Result -sealed class NullPrintStream extends ByteArrayOutputStream { - override def write(b: Int) = () - override def write(b: Array[Byte], off: Int, len: Int) = () - override def writeTo(out: OutputStream) = () -} - -sealed class StoringPrintStream extends PrintStream(new NullPrintStream) { - private[this] var sb = new StringBuilder - - override def println(obj: Object) = - println(obj.toString) - - override def println(str: String) = { - sb.append(str) - sb += '\n' - } - def flushStored(): String = { - val str = sb.toString - sb = new StringBuilder - str - } -} - -class ReplTest extends ReplDriver( +class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver( Array("-classpath", List(Jars.dottyLib, Jars.dottyInterfaces).mkString(":"), "-color:never"), - new StoringPrintStream + new PrintStream(out) ) with MessageRendering { - /** Get the stored output from `out`, resetting the `StoringPrintStream` */ - def storedOutput(): String = - stripColor(out.asInstanceOf[StoringPrintStream].flushStored()) + def this() = this(new ByteArrayOutputStream) + + /** Get the stored output from `out`, resetting the buffer */ + def storedOutput(): String = { + val output = stripColor(out.toString) + out.reset() + output + } protected implicit def toParsed(expr: String)(implicit state: State): Parsed = { implicit val ctx = state.run.runContext diff --git a/compiler/test/dotty/tools/repl/ScriptedTests.scala b/compiler/test/dotty/tools/repl/ScriptedTests.scala index 3b8262aa0666..1e89a9de2104 100644 --- a/compiler/test/dotty/tools/repl/ScriptedTests.scala +++ b/compiler/test/dotty/tools/repl/ScriptedTests.scala @@ -22,14 +22,14 @@ class ScriptedTests extends ReplTest with MessageRendering { private def testFile(f: JFile): Unit = { val prompt = "scala>" - val lines = Source.fromFile(f).getLines.buffered + val lines = Source.fromFile(f).getLines().buffered + + assert(lines.head.startsWith(prompt), + s"""Each file has to start with the prompt: "$prompt"""") def extractInputs(prompt: String): List[String] = { val input = lines.next() - assert(input.startsWith(prompt), - s"""Each file has to start with the prompt: "$prompt"""") - if (!input.startsWith(prompt)) extractInputs(prompt) else if (lines.hasNext) { // read lines and strip trailing whitespace: @@ -60,7 +60,7 @@ class ScriptedTests extends ReplTest with MessageRendering { } val expectedOutput = - Source.fromFile(f).getLines.flatMap(filterEmpties).mkString("\n") + Source.fromFile(f).getLines().flatMap(filterEmpties).mkString("\n") val actualOutput = { resetToInitial() init()