Skip to content

Commit 31aff29

Browse files
committed
DRY SemanticdbTests
1 parent 6a47cf6 commit 31aff29

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

compiler/test/dotty/tools/dotc/semanticdb/SemanticdbTests.scala

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ class SemanticdbTests with
4040
val target = generateSemanticdb()
4141
val errors = mutable.ArrayBuffer.empty[Path]
4242
val metacSb: StringBuilder = StringBuilder(5000)
43+
def collectErrorOrUpdate(expectPath: Path, obtained: String) =
44+
if updateExpectFiles
45+
Files.write(expectPath, obtained.getBytes(StandardCharsets.UTF_8))
46+
println("updated: " + expectPath)
47+
else
48+
val expected = new String(Files.readAllBytes(expectPath), StandardCharsets.UTF_8)
49+
val expectName = expectPath.getFileName
50+
val relExpect = rootSrc.relativize(expectPath)
51+
collectFailingDiff(expected, obtained, s"a/$relExpect", s"b/$relExpect") {
52+
Files.write(expectPath.resolveSibling("" + expectName + ".out"), obtained.getBytes(StandardCharsets.UTF_8))
53+
errors += expectPath
54+
}
4355
for source <- inputFiles().sorted do
4456
val filename = source.getFileName.toString
4557
val relpath = expectSrc.relativize(source)
@@ -52,38 +64,16 @@ class SemanticdbTests with
5264
val doc = Tools.loadTextDocument(source, relpath, semanticdbPath)
5365
Tools.metac(doc, rootSrc.relativize(source))(given metacSb)
5466
val obtained = trimTrailingWhitespace(SemanticdbTests.printTextDocument(doc))
55-
if updateExpectFiles
56-
Files.write(expectPath, obtained.getBytes(StandardCharsets.UTF_8))
57-
println("updated: " + expectPath)
58-
else
59-
val expected = new String(Files.readAllBytes(expectPath), StandardCharsets.UTF_8)
60-
val expectName = expectPath.getFileName
61-
val relExpect = rootSrc.relativize(expectPath)
62-
collectFailingDiff(expected, obtained, s"a/$relExpect", s"b/$relExpect") {
63-
Files.write(expectPath.resolveSibling("" + expectName + ".out"), obtained.getBytes(StandardCharsets.UTF_8))
64-
errors += expectPath
65-
}
66-
if updateExpectFiles then
67-
Files.write(metacExpectFile, metacSb.toString.getBytes)
68-
println("updated: " + metacExpectFile)
69-
else
70-
val expected = new String(Files.readAllBytes(metacExpectFile), StandardCharsets.UTF_8)
71-
val expectName = metacExpectFile.getFileName
72-
val relExpect = rootSrc.relativize(metacExpectFile)
73-
val obtained = metacSb.toString
74-
collectFailingDiff(expected, obtained, s"a/$relExpect", s"b/$relExpect") {
75-
Files.write(metacExpectFile.resolveSibling("" + expectName + ".out"), obtained.getBytes(StandardCharsets.UTF_8))
76-
errors += metacExpectFile
77-
}
78-
errors.foreach { expect =>
67+
collectErrorOrUpdate(expectPath, obtained)
68+
collectErrorOrUpdate(metacExpectFile, metacSb.toString)
69+
for expect <- errors do
7970
def red(msg: String) = Console.RED + msg + Console.RESET
8071
def blue(msg: String) = Console.BLUE + msg + Console.RESET
8172
println(s"""[${red("error")}] check file ${blue(expect.toString)} does not match generated.
8273
|If you meant to make a change, replace the expect file by:
8374
| mv ${expect.resolveSibling("" + expect.getFileName + ".out")} $expect
8475
|Or else update all expect files with
8576
| sbt 'dotty-compiler-bootstrapped/test:runMain dotty.tools.dotc.semanticdb.updateExpect'""".stripMargin)
86-
}
8777
Files.walk(target).sorted(Comparator.reverseOrder).forEach(Files.delete)
8878
if errors.nonEmpty
8979
fail(s"${errors.size} errors in expect test.")

0 commit comments

Comments
 (0)