Skip to content

Commit 7912002

Browse files
Test framework for the scripting engine implemented
1 parent 837a204 commit 7912002

File tree

7 files changed

+68
-9
lines changed

7 files changed

+68
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test = assert(2 + 2 == 4)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
World
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def Test(name: String) =
2+
assert(name == "World")

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By
5050
extension [A](state: State)
5151
def andThen(op: State => A): A = op(state)
5252

53-
def scripts(path: String): Array[JFile] = {
54-
val dir = new JFile(getClass.getResource(path).getPath)
55-
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
56-
dir.listFiles
57-
}
58-
5953
def testFile(f: JFile): Unit = {
6054
val prompt = "scala>"
6155

@@ -77,12 +71,11 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By
7771
case nonEmptyLine => nonEmptyLine :: Nil
7872
}
7973

80-
val expectedOutput =
81-
Using(Source.fromFile(f, StandardCharsets.UTF_8.name))(_.getLines().flatMap(filterEmpties).toList).get
74+
val expectedOutput = readLines(f).flatMap(filterEmpties)
8275
val actualOutput = {
8376
resetToInitial()
8477

85-
val lines = Using(Source.fromFile(f, StandardCharsets.UTF_8.name))(_.getLines.toList).get
78+
val lines = readLines(f)
8679
assert(lines.head.startsWith(prompt),
8780
s"""Each file has to start with the prompt: "$prompt"""")
8881
val inputRes = lines.filter(_.startsWith(prompt))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dotty
2+
package tools
3+
package scripting
4+
5+
import java.io.File
6+
7+
import org.junit.Test
8+
9+
import vulpix.TestConfiguration
10+
11+
12+
/** Runs all tests contained in `compiler/test-resources/repl/` */
13+
class ScriptingTests:
14+
extension (str: String) def dropExtension =
15+
str.reverse.dropWhile(_ != '.').drop(1).reverse
16+
17+
@Test def scriptingTests =
18+
val testFiles = scripts("/scripting")
19+
20+
val argss: Map[String, Array[String]] = (
21+
for
22+
argFile <- testFiles
23+
if argFile.getName.endsWith(".args")
24+
name = argFile.getName.dropExtension
25+
scriptArgs = readLines(argFile).toArray
26+
yield name -> scriptArgs).toMap
27+
28+
for
29+
scriptFile <- testFiles
30+
if scriptFile.getName.endsWith(".scala")
31+
name = scriptFile.getName.dropExtension
32+
scriptArgs = argss.getOrElse(name, Array.empty[String])
33+
do
34+
ScriptingDriver(
35+
compilerArgs = Array(
36+
"-classpath", TestConfiguration.basicClasspath,
37+
"-color:never",
38+
"-Yerased-terms",
39+
),
40+
scriptFile = scriptFile,
41+
scriptArgs = scriptArgs
42+
).compileAndRun()

compiler/test/dotty/tools/utils.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dotty.tools
2+
3+
import java.io.File
4+
import java.nio.charset.StandardCharsets.UTF_8
5+
6+
import scala.io.Source
7+
import scala.util.Using.resource
8+
9+
def scripts(path: String): Array[File] = {
10+
val dir = new File(getClass.getResource(path).getPath)
11+
assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir")
12+
dir.listFiles
13+
}
14+
15+
private def withFile[T](file: File)(action: Source => T): T =
16+
resource(Source.fromFile(file, UTF_8.name))(action)
17+
18+
def readLines(f: File): List[String] = withFile(f)(_.getLines.toList)
19+
def readFile(f: File): String = withFile(f)(_.mkString)

staging/test/scala/quoted/staging/repl/StagingScriptedReplTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scala.quoted.staging.repl
22

33
import dotty.BootstrappedOnlyTests
4+
import dotty.tools.scripts
45
import dotty.tools.repl.ReplTest
56
import dotty.tools.vulpix.TestConfiguration
67
import org.junit.Test

0 commit comments

Comments
 (0)