Skip to content

Don't assume repl input is only 2 statements #7530

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

Merged
merged 2 commits into from
Nov 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/CollectTopLevelImports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CollectTopLevelImports extends Phase {

def run(implicit ctx: Context): Unit = {
def topLevelImports(tree: Tree) = {
val PackageDef(_, _ :: TypeDef(_, rhs: Template) :: Nil) = tree
val PackageDef(_, _ :: TypeDef(_, rhs: Template) :: _) = tree
rhs.body.collect { case tree: Import => tree }
}

Expand Down
80 changes: 80 additions & 0 deletions compiler/test/dotty/tools/repl/LoadTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package dotty.tools.repl

import java.nio.file.{ Path, Files }

import org.junit.{ Test, BeforeClass, AfterClass }
import org.junit.Assert.assertEquals

class LoadTests extends ReplTest {
import LoadTests._

@Test def helloworld = loadTest(
file = """def helloWorld = "Hello, World!"""",
defs = """|def helloWorld: String
|
|
|""".stripMargin,
runCode = "helloWorld",
output = """|val res0: String = Hello, World!
|
|""".stripMargin
)

@Test def maindef = loadTest(
file = """@main def helloWorld = println("Hello, World!")""",
defs = """|def helloWorld: Unit
|
|
|""".stripMargin,
runCode = "helloWorld",
output = """|Hello, World!
|
|""".stripMargin
)

@Test def maindefs = loadTest(
file = """|@main def helloWorld = println("Hello, World!")
|@main def helloTo(name: String) = println(s"Hello, $name!")""".stripMargin,
defs = """|def helloTo(name: String): Unit
|def helloWorld: Unit
|
|
|""".stripMargin,
runCode = """helloWorld; helloTo("Scala")""",
output = """|Hello, World!
|Hello, Scala!
|
|""".stripMargin
)

def loadTest(file: String, defs: String, runCode: String, output: String) =
eval(s":load ${writeFile(file)}").andThen { implicit s =>
assertEquals(defs, storedOutput())
run(runCode)
assertEquals(output, storedOutput())
}

private def eval(code: String): State =
fromInitialState { implicit s => run(code) }

}

object LoadTests {

private var dir: Path = null

@BeforeClass def setupDir: Unit =
dir = Files.createTempDirectory("repl_load_src")

@AfterClass def removeDir: Unit =
Files.walk(dir).filter(!Files.isDirectory(_)).forEach(Files.delete)
Files.delete(dir)
dir = null

def writeFile(contents: String): Path = {
val file = Files.createTempFile(dir, "repl_test", ".scala")
Files.write(file, contents.getBytes)
file
}

}