Skip to content

Commit edb401e

Browse files
committed
Added generator method for test files
1 parent 6d8e627 commit edb401e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package dotty.tools.dotc.printing
22

3+
import java.io.PrintWriter
4+
35
import dotty.tools.io.JFile
4-
import org.junit.Test
56
import org.junit.Assert.fail
7+
import org.junit.Test
68

79
import scala.io.Source
810

911
/** 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*/
1114
class SyntaxHighlightingTests {
1215

1316
private def scripts(path: String): Array[JFile] = {
@@ -17,9 +20,9 @@ class SyntaxHighlightingTests {
1720
}
1821

1922
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")
2326
val actualOutput = SyntaxHighlighting(input).mkString
2427

2528
if (expectedOutput != actualOutput) {
@@ -28,8 +31,22 @@ class SyntaxHighlightingTests {
2831
println("Actual output:")
2932
println(actualOutput)
3033

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+
3138
fail(s"Error in file $f, expected output did not match actual")
3239
}
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 + "\nresult:\n" + actualOutput)
46+
close()
47+
}
48+
println(s"Test file for ${f.getName} has been generated")
49+
}
3350
}
3451

3552
@Test def syntaxHighlight = scripts("/printing").foreach(testFile)

0 commit comments

Comments
 (0)