Skip to content

Commit bc87529

Browse files
Merge pull request #7530 from bishabosha/issue/7529
Don't assume repl input is only 2 statements
2 parents 3cb93f3 + e028fb6 commit bc87529

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

compiler/src/dotty/tools/repl/CollectTopLevelImports.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CollectTopLevelImports extends Phase {
2020

2121
def run(implicit ctx: Context): Unit = {
2222
def topLevelImports(tree: Tree) = {
23-
val PackageDef(_, _ :: TypeDef(_, rhs: Template) :: Nil) = tree
23+
val PackageDef(_, _ :: TypeDef(_, rhs: Template) :: _) = tree
2424
rhs.body.collect { case tree: Import => tree }
2525
}
2626

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dotty.tools.repl
2+
3+
import java.nio.file.{ Path, Files }
4+
5+
import org.junit.{ Test, BeforeClass, AfterClass }
6+
import org.junit.Assert.assertEquals
7+
8+
class LoadTests extends ReplTest {
9+
import LoadTests._
10+
11+
@Test def helloworld = loadTest(
12+
file = """def helloWorld = "Hello, World!"""",
13+
defs = """|def helloWorld: String
14+
|
15+
|
16+
|""".stripMargin,
17+
runCode = "helloWorld",
18+
output = """|val res0: String = Hello, World!
19+
|
20+
|""".stripMargin
21+
)
22+
23+
@Test def maindef = loadTest(
24+
file = """@main def helloWorld = println("Hello, World!")""",
25+
defs = """|def helloWorld: Unit
26+
|
27+
|
28+
|""".stripMargin,
29+
runCode = "helloWorld",
30+
output = """|Hello, World!
31+
|
32+
|""".stripMargin
33+
)
34+
35+
@Test def maindefs = loadTest(
36+
file = """|@main def helloWorld = println("Hello, World!")
37+
|@main def helloTo(name: String) = println(s"Hello, $name!")""".stripMargin,
38+
defs = """|def helloTo(name: String): Unit
39+
|def helloWorld: Unit
40+
|
41+
|
42+
|""".stripMargin,
43+
runCode = """helloWorld; helloTo("Scala")""",
44+
output = """|Hello, World!
45+
|Hello, Scala!
46+
|
47+
|""".stripMargin
48+
)
49+
50+
def loadTest(file: String, defs: String, runCode: String, output: String) =
51+
eval(s":load ${writeFile(file)}").andThen { implicit s =>
52+
assertEquals(defs, storedOutput())
53+
run(runCode)
54+
assertEquals(output, storedOutput())
55+
}
56+
57+
private def eval(code: String): State =
58+
fromInitialState { implicit s => run(code) }
59+
60+
}
61+
62+
object LoadTests {
63+
64+
private var dir: Path = null
65+
66+
@BeforeClass def setupDir: Unit =
67+
dir = Files.createTempDirectory("repl_load_src")
68+
69+
@AfterClass def removeDir: Unit =
70+
Files.walk(dir).filter(!Files.isDirectory(_)).forEach(Files.delete)
71+
Files.delete(dir)
72+
dir = null
73+
74+
def writeFile(contents: String): Path = {
75+
val file = Files.createTempFile(dir, "repl_test", ".scala")
76+
Files.write(file, contents.getBytes)
77+
file
78+
}
79+
80+
}

0 commit comments

Comments
 (0)