Skip to content

Commit 47391e6

Browse files
committed
Add LoadTests repl test runner
1 parent 986b1f2 commit 47391e6

File tree

1 file changed

+77
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)