Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SI-7805 REPL -i startup #2917

Merged
merged 1 commit into from
Sep 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/partest-extras/scala/tools/partest/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,14 +22,32 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion src/repl/scala/tools/nsc/interpreter/ILoop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,9 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
globalFuture = future {
intp.initializeSynchronous()
loopPostInit()
loadFiles(settings)
!intp.reporter.hasErrors
}
loadFiles(settings)
printWelcome()

try loop()
Expand Down
11 changes: 6 additions & 5 deletions test/files/run/repl-trim-stack-trace.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import scala.tools.partest.SessionTest
import scala.tools.partest.{ SessionTest, Welcoming }

// SI-7740
object Test extends SessionTest {
object Test extends SessionTest with Welcoming {
def session =
"""Type in expressions to have them evaluated.
"""Welcome to Scala
Type in expressions to have them evaluated.
Type :help for more information.

scala> def f = throw new Exception("Uh-oh")
Expand Down Expand Up @@ -35,10 +36,10 @@ scala> """

// normalize the "elided" lines because the frame count depends on test context
lazy val elided = """(\s+\.{3} )\d+( elided)""".r
def normalize(line: String) = line match {
override def normalize(line: String) = line match {
case welcome(w) => w
case elided(ellipsis, suffix) => s"$ellipsis???$suffix"
case s => s
}
override def eval() = super.eval() map normalize
override def expected = super.expected map normalize
}
11 changes: 11 additions & 0 deletions test/files/run/t7805-repl-i.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Loading t7805-repl-i.script...
import util._

Welcome to Scala
Type in expressions to have them evaluated.
Type :help for more information.

scala> Console println Try(8)
Success(8)

scala>
42 changes: 42 additions & 0 deletions test/files/run/t7805-repl-i.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import scala.tools.partest.{ ReplTest, Welcoming }
import scala.tools.nsc.{ GenericRunnerSettings, Settings }
import scala.tools.nsc.settings.MutableSettings

object Test extends ReplTest with HangingRepl with Welcoming {
def script = testPath changeExtension "script"
override def transformSettings(s: Settings) = s match {
case m: MutableSettings =>
val t = new GenericRunnerSettings(s.errorFn)
m copyInto t
t processArgumentString s"-i $script"
t
case _ => s
}
def code = "Console println Try(8)"
}

object Resulting {
import scala.concurrent._
import scala.concurrent.duration._
implicit class AwaitResult[A](val f: Future[A]) extends AnyVal {
def resultWithin(d: Duration): A = Await.result(f, d)
}
}

/** Test that hangs the REPL.
* Usually that is the "before" case.
*/
trait HangingRepl extends ReplTest {
import scala.language.postfixOps
import scala.util._
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits._
import Resulting._
def timeout = 15 seconds
def hanging[A](a: =>A): A = future(a) resultWithin timeout
override def show() = Try(hanging(super.show())) recover {
case e => e.printStackTrace()
}
}
1 change: 1 addition & 0 deletions test/files/run/t7805-repl-i.script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import util._