Skip to content

Commit

Permalink
Make REPL tests capture all produced output
Browse files Browse the repository at this point in the history
It used to capture output only from 'println(String | Object)'
  • Loading branch information
allanrenucci committed May 9, 2018
1 parent 63d1bcc commit 579e833
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 10 additions & 28 deletions compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions compiler/test/dotty/tools/repl/ScriptedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 579e833

Please sign in to comment.