|
| 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