From bfd72c29dc64125ee4df41c97b7d4c9e8b952629 Mon Sep 17 00:00:00 2001 From: Tom Grigg Date: Sun, 7 Feb 2021 22:44:57 -0800 Subject: [PATCH 1/2] Pass command line options to `ReplTest` constructor (with default) This permits constructing tests that depend on particular command line options such as `-Xprint:typer` --- compiler/test/dotty/tools/repl/ReplTest.scala | 20 +++++++------------ .../repl/StagingScriptedReplTests.scala | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/compiler/test/dotty/tools/repl/ReplTest.scala b/compiler/test/dotty/tools/repl/ReplTest.scala index aa884dad8712..841f8492a7b3 100644 --- a/compiler/test/dotty/tools/repl/ReplTest.scala +++ b/compiler/test/dotty/tools/repl/ReplTest.scala @@ -16,19 +16,8 @@ import dotty.tools.dotc.reporting.MessageRendering import org.junit.{After, Before} import org.junit.Assert._ - -class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new ByteArrayOutputStream) extends ReplDriver( - Array( - "-classpath", - if (withStaging) - TestConfiguration.withStagingClasspath - else - TestConfiguration.basicClasspath, - "-color:never", - "-Yerased-terms", - ), - new PrintStream(out, true, StandardCharsets.UTF_8.name) -) with MessageRendering { +class ReplTest(options: Array[String] = ReplTest.defaultOptions, out: ByteArrayOutputStream = new ByteArrayOutputStream) +extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.name)) with MessageRendering { /** Get the stored output from `out`, resetting the buffer */ def storedOutput(): String = { val output = stripColor(out.toString(StandardCharsets.UTF_8.name)) @@ -103,3 +92,8 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By end if } } + +object ReplTest: + val commonOptions = Array("-color:never", "-Yerased-terms") + val defaultOptions = commonOptions ++ Array("-classpath", TestConfiguration.basicClasspath) + lazy val withStagingOptions = commonOptions ++ Array("-classpath", TestConfiguration.withStagingClasspath) diff --git a/staging/test/scala/quoted/staging/repl/StagingScriptedReplTests.scala b/staging/test/scala/quoted/staging/repl/StagingScriptedReplTests.scala index c7abbec41f09..01519f3737d2 100644 --- a/staging/test/scala/quoted/staging/repl/StagingScriptedReplTests.scala +++ b/staging/test/scala/quoted/staging/repl/StagingScriptedReplTests.scala @@ -8,7 +8,7 @@ import org.junit.Test import org.junit.experimental.categories.Category /** Runs all tests contained in `staging/test-resources/repl-staging` */ -class StagingScriptedReplTests extends ReplTest(withStaging = true) { +class StagingScriptedReplTests extends ReplTest(ReplTest.withStagingOptions) { @Category(Array(classOf[BootstrappedOnlyTests])) @Test def replStagingTests = scripts("/repl-staging").foreach(testFile) From de930cf3eee917fe6cc3cc6ed1d28bba357d415d Mon Sep 17 00:00:00 2001 From: Tom Grigg Date: Sun, 7 Feb 2021 22:46:35 -0800 Subject: [PATCH 2/2] Override `line` and `column` in NoSourcePosition Otherwise, usage causes an assertion error. The use of the value -1 is in agreement with the documentation in interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java The REPL driver depends on these methods for ordering its output, and some diagnostics are reported (intentionally) using NoSoucePosition. --- .../tools/dotc/util/SourcePosition.scala | 2 ++ .../dotty/tools/repl/ReplCompilerTests.scala | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala index 160631bc41b0..9bc21fdf408f 100644 --- a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala +++ b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala @@ -86,6 +86,8 @@ extends SrcPos, interfaces.SourcePosition, Showable { /** A sentinel for a non-existing source position */ @sharable object NoSourcePosition extends SourcePosition(NoSource, NoSpan, null) { + override def line: Int = -1 + override def column: Int = -1 override def toString: String = "?" override def withOuter(outer: SourcePosition): SourcePosition = outer } diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 7192f8de7e4f..bc06727f2ea4 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -233,3 +233,31 @@ object ReplCompilerTests { } } + +class ReplXPrintTyperTests extends ReplTest(ReplTest.defaultOptions :+ "-Xprint:typer") { + @Test def i9111 = fromInitialState { implicit state => + run("""|enum E { + | case A + |}""".stripMargin) + assert(storedOutput().trim().endsWith("// defined class E")) + } + + @Test def i10883 = fromInitialState { implicit state => + run("val a = 42") + assert(storedOutput().trim().endsWith("val a: Int = 42")) + } +} + +class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose") { + @Test def i9111 = fromInitialState { implicit state => + run("""|enum E { + | case A + |}""".stripMargin) + assert(storedOutput().trim().endsWith("// defined class E")) + } + + @Test def i10883 = fromInitialState { implicit state => + run("val a = 42") + assert(storedOutput().trim().endsWith("val a: Int = 42")) + } +}