Skip to content

Commit

Permalink
Added the ability to run a silent startup script
Browse files Browse the repository at this point in the history
  • Loading branch information
John Johnson II committed May 29, 2022
1 parent a9beb82 commit 962aa3f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/main/scala/com/potenciasoftware/rebel/BaseRepl.scala
Expand Up @@ -69,12 +69,19 @@ class BaseRepl {
field
}

/**
* Override to provide a script to run on startup.
*
* This can be a multiline string. No output will be shown from this script.
*/
protected def startupScript: String = ""

/** Read the current text of the REPL prompt. */
def prompt: String = repl.prompt

/** Change the current text of the REPL prompt. */
def prompt_=(newValue: String): Unit = {
promptField.foreach { field =>
promptField foreach { field =>
field.set(repl, newValue)
}
}
Expand All @@ -91,6 +98,9 @@ class BaseRepl {
intp.beQuietDuring {
for (param <- boundValues)
param.bindTo(intp)

if (startupScript.nonEmpty)
intp.interpret(startupScript)
}
}
}
Expand Down
34 changes: 29 additions & 5 deletions src/test/scala/com/potenciasoftware/rebel/BaseReplTest.scala
@@ -1,5 +1,6 @@
package com.potenciasoftware.rebel

import org.scalatest.Tag
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand All @@ -17,9 +18,8 @@ class BaseReplTest extends AnyFlatSpec with Matchers {

"BaseRepl" should "behave like the normal ILoop by default" in {
replTest[BaseReplTest]("basic", "42 + 42")
.out.asBlock shouldBe """
|scala> val res0: Int = 84
|
.out.compressed.asBlock shouldBe
"""scala> val res0: Int = 84
|scala> """.stripMargin
}

Expand Down Expand Up @@ -89,16 +89,40 @@ class BaseReplTest extends AnyFlatSpec with Matchers {
}

it should "use the full classpath" in {
replTest[BaseReplTest]("fullClasspath",
replTest[BaseReplTest]("basic",
"import com.potenciasoftware.rebel.BaseReplTest.TestValue",
"val value = TestValue(42)",
"println(value)"
).all.printAll
).out.compressed.take(3).asBlock shouldBe
"""scala> import com.potenciasoftware.rebel.BaseReplTest.TestValue
|scala> val value = TestValue(42)val value: com.potenciasoftware.rebel.BaseReplTest.TestValue = TestValue(42)
|scala> println(value)TestValue(42)""".stripMargin
}

def startupScript() = new TestRepl {
override protected def startupScript: String =
"""
|object printAnswer {
|
| private val answer = 42
|
| def apply(): Unit =
| println(s"The answer is: $answer")
|}""".stripMargin
}

it should "allow a silent script to run at startup" in {
replTest[BaseReplTest]("startupScript",
"printAnswer()").out.take(2).asBlock shouldBe
"""
|scala> printAnswer()The answer is: 42""".stripMargin
}
}

object BaseReplTest {

object Focus extends Tag("Focus")

class TestRepl extends BaseRepl {
// Normally we don't need the banner as part of our test outupt
override protected val banner: String = ""
Expand Down
3 changes: 3 additions & 0 deletions src/test/scala/com/potenciasoftware/rebel/TestUtils.scala
Expand Up @@ -107,6 +107,9 @@ object TestUtils {

def printAll: Unit = s.foreach(println)

def compressed: Seq[String] =
s.filterNot(_.isEmpty)

def withLineNumbers: Seq[String] = {
val padTo = (s.size - 1).toString.size
for {
Expand Down

0 comments on commit 962aa3f

Please sign in to comment.