1
1
package dotty .tools .dotc .printing
2
2
3
+ import java .io .PrintWriter
4
+
3
5
import dotty .tools .io .JFile
4
- import org .junit .Test
5
6
import org .junit .Assert .fail
7
+ import org .junit .Test
6
8
7
9
import scala .io .Source
8
10
9
11
/** Runs all tests contained in `compiler/test-resources/printing/`
10
- * To check test cases, you can use "cat" or "less -r" from bash*/
12
+ * To check test cases, you can use "cat" or "less -r" from bash
13
+ * To generate test files you can call the generateTestFile method*/
11
14
class SyntaxHighlightingTests {
12
15
13
16
private def scripts (path : String ): Array [JFile ] = {
@@ -17,9 +20,9 @@ class SyntaxHighlightingTests {
17
20
}
18
21
19
22
private def testFile (f : JFile ): Unit = {
20
- val linesIt = Source .fromFile(f).getLines()
21
- val input = linesIt .takeWhile(_ != " result:" ).mkString(" \n " )
22
- val expectedOutput = linesIt .mkString(" \n " )
23
+ val lines = Source .fromFile(f).getLines()
24
+ val input = lines .takeWhile(_ != " result:" ).mkString(" \n " )
25
+ val expectedOutput = lines .mkString(" \n " )
23
26
val actualOutput = SyntaxHighlighting (input).mkString
24
27
25
28
if (expectedOutput != actualOutput) {
@@ -28,8 +31,22 @@ class SyntaxHighlightingTests {
28
31
println(" Actual output:" )
29
32
println(actualOutput)
30
33
34
+ // Call generateTestFile when you want to update a test file
35
+ // or generate a test from a scala source file
36
+ // if (f.getName == "nameOfFileToConvertToATest") generateTestFile()
37
+
31
38
fail(s " Error in file $f, expected output did not match actual " )
32
39
}
40
+
41
+ /** Writes `input` and `actualOutput` to the current test file*/
42
+ def generateTestFile (): Unit = {
43
+ val path = " compiler/test-resources/printing/" + f.getName
44
+ new PrintWriter (path) {
45
+ write(input + " \n result:\n " + actualOutput)
46
+ close()
47
+ }
48
+ println(s " Test file for ${f.getName} has been generated " )
49
+ }
33
50
}
34
51
35
52
@ Test def syntaxHighlight = scripts(" /printing" ).foreach(testFile)
0 commit comments