From e73914c85cbf5a3d2e32a36ce6af0fbb8e026a36 Mon Sep 17 00:00:00 2001 From: bishabosha Date: Sat, 9 Nov 2019 23:15:12 +0100 Subject: [PATCH 1/2] Fix #7529: Don't assume repl input is only 2 statements --- compiler/src/dotty/tools/repl/CollectTopLevelImports.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/repl/CollectTopLevelImports.scala b/compiler/src/dotty/tools/repl/CollectTopLevelImports.scala index 697c830b81a6..a872bbac022a 100644 --- a/compiler/src/dotty/tools/repl/CollectTopLevelImports.scala +++ b/compiler/src/dotty/tools/repl/CollectTopLevelImports.scala @@ -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 } } From e028fb660e2c6f1386ad03c11cea15ccdbb38282 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Mon, 11 Nov 2019 17:53:51 +0100 Subject: [PATCH 2/2] Add LoadTests repl test runner --- .../test/dotty/tools/repl/LoadTests.scala | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 compiler/test/dotty/tools/repl/LoadTests.scala diff --git a/compiler/test/dotty/tools/repl/LoadTests.scala b/compiler/test/dotty/tools/repl/LoadTests.scala new file mode 100644 index 000000000000..7686f3ed34b3 --- /dev/null +++ b/compiler/test/dotty/tools/repl/LoadTests.scala @@ -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 + } + +}