summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-04-05 01:48:31 +0000
committerPaul Phillips <paulp@improving.org>2011-04-05 01:48:31 +0000
commite50fbcc3b32d4d65deb98a06c644894d3561c81c (patch)
tree182009f58959df2f676a73c9bb33fc18bbfcead2 /src/compiler/scala/tools/nsc/interpreter/ILoop.scala
parente06244cb55797d9928a52a22a548d547555be733 (diff)
downloadscala-e50fbcc3b32d4d65deb98a06c644894d3561c81c.tar.gz
scala-e50fbcc3b32d4d65deb98a06c644894d3561c81c.tar.bz2
scala-e50fbcc3b32d4d65deb98a06c644894d3561c81c.zip
Enhancing the repl-testing code by turning it i...
Enhancing the repl-testing code by turning it into a transcript producing machine. "Here's some code." "Here's a transcript!" "Good day to you, sir!" "No, good day to YOU!" These changes are awesome. Look at the checkfile diffs for god's sake, they'll make you weep with joy. No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/ILoop.scala')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/ILoop.scala44
1 files changed, 40 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
index 30c2c86d88..ccec5c0e22 100644
--- a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
@@ -772,10 +772,45 @@ class ILoop(in0: Option[BufferedReader], protected val out: PrintWriter)
object ILoop {
implicit def loopToInterpreter(repl: ILoop): IMain = repl.intp
+ // Designed primarily for use by test code: take a String with a
+ // bunch of code, and prints out a transcript of what it would look
+ // like if you'd just typed it into the repl.
+ def runForTranscript(code: String, settings: Settings): String = {
+ import java.io.{ BufferedReader, StringReader, OutputStreamWriter }
+
+ stringFromStream { ostream =>
+ Console.withOut(ostream) {
+ val output = new PrintWriter(new OutputStreamWriter(ostream), true) {
+ override def write(str: String) = {
+ // completely skip continuation lines
+ if (str forall (ch => ch.isWhitespace || ch == '|')) ()
+ // print a newline on empty scala prompts
+ else if ((str contains '\n') && (str.trim == "scala> ")) super.write("\n")
+ else super.write(str)
+ }
+ }
+ val input = new BufferedReader(new StringReader(code)) {
+ override def readLine(): String = {
+ val s = super.readLine()
+ // helping out by printing the line being interpreted.
+ if (s != null)
+ output.println(s)
+ s
+ }
+ }
+ val repl = new ILoop(input, output)
+ if (settings.classpath.isDefault)
+ settings.classpath.value = sys.props("java.class.path")
+
+ repl process settings
+ }
+ }
+ }
+
/** Creates an interpreter loop with default settings and feeds
* the given code to it as input.
*/
- def run(code: String): String = {
+ def run(code: String, sets: Settings = new Settings): String = {
import java.io.{ BufferedReader, StringReader, OutputStreamWriter }
stringFromStream { ostream =>
@@ -783,10 +818,11 @@ object ILoop {
val input = new BufferedReader(new StringReader(code))
val output = new PrintWriter(new OutputStreamWriter(ostream), true)
val repl = new ILoop(input, output)
- val settings = new Settings
- settings.classpath.value = sys.props("java.class.path")
- repl process settings
+ if (sets.classpath.isDefault)
+ sets.classpath.value = sys.props("java.class.path")
+
+ repl process sets
}
}
}