Skip to content

Commit 579e833

Browse files
committed
Make REPL tests capture all produced output
It used to capture output only from 'println(String | Object)'
1 parent 63d1bcc commit 579e833

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ case class Completions(cursor: Int,
8181

8282
/** Main REPL instance, orchestrating input, compilation and presentation */
8383
class ReplDriver(settings: Array[String],
84-
protected val out: PrintStream = Console.out,
85-
protected val classLoader: Option[ClassLoader] = None) extends Driver {
84+
out: PrintStream = Console.out,
85+
classLoader: Option[ClassLoader] = None) extends Driver {
8686

8787
/** Overridden to `false` in order to not have to give sources on the
8888
* commandline

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,20 @@ import org.junit.Assert.fail
1111
import dotc.reporting.diagnostic.MessageContainer
1212
import results.Result
1313

14-
sealed class NullPrintStream extends ByteArrayOutputStream {
15-
override def write(b: Int) = ()
16-
override def write(b: Array[Byte], off: Int, len: Int) = ()
17-
override def writeTo(out: OutputStream) = ()
18-
}
19-
20-
sealed class StoringPrintStream extends PrintStream(new NullPrintStream) {
21-
private[this] var sb = new StringBuilder
22-
23-
override def println(obj: Object) =
24-
println(obj.toString)
25-
26-
override def println(str: String) = {
27-
sb.append(str)
28-
sb += '\n'
29-
}
3014

31-
def flushStored(): String = {
32-
val str = sb.toString
33-
sb = new StringBuilder
34-
str
35-
}
36-
}
37-
38-
class ReplTest extends ReplDriver(
15+
class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
3916
Array("-classpath", List(Jars.dottyLib, Jars.dottyInterfaces).mkString(":"), "-color:never"),
40-
new StoringPrintStream
17+
new PrintStream(out)
4118
) with MessageRendering {
4219

43-
/** Get the stored output from `out`, resetting the `StoringPrintStream` */
44-
def storedOutput(): String =
45-
stripColor(out.asInstanceOf[StoringPrintStream].flushStored())
20+
def this() = this(new ByteArrayOutputStream)
21+
22+
/** Get the stored output from `out`, resetting the buffer */
23+
def storedOutput(): String = {
24+
val output = stripColor(out.toString)
25+
out.reset()
26+
output
27+
}
4628

4729
protected implicit def toParsed(expr: String)(implicit state: State): Parsed = {
4830
implicit val ctx = state.run.runContext

compiler/test/dotty/tools/repl/ScriptedTests.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class ScriptedTests extends ReplTest with MessageRendering {
2222

2323
private def testFile(f: JFile): Unit = {
2424
val prompt = "scala>"
25-
val lines = Source.fromFile(f).getLines.buffered
25+
val lines = Source.fromFile(f).getLines().buffered
26+
27+
assert(lines.head.startsWith(prompt),
28+
s"""Each file has to start with the prompt: "$prompt"""")
2629

2730
def extractInputs(prompt: String): List[String] = {
2831
val input = lines.next()
2932

30-
assert(input.startsWith(prompt),
31-
s"""Each file has to start with the prompt: "$prompt"""")
32-
3333
if (!input.startsWith(prompt)) extractInputs(prompt)
3434
else if (lines.hasNext) {
3535
// read lines and strip trailing whitespace:
@@ -60,7 +60,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
6060
}
6161

6262
val expectedOutput =
63-
Source.fromFile(f).getLines.flatMap(filterEmpties).mkString("\n")
63+
Source.fromFile(f).getLines().flatMap(filterEmpties).mkString("\n")
6464
val actualOutput = {
6565
resetToInitial()
6666
init()

0 commit comments

Comments
 (0)