Skip to content

Commit 87ff005

Browse files
committed
Print colored diff for scripted tests
1 parent 1705fcf commit 87ff005

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

compiler/src/dotty/tools/dotc/util/DiffUtil.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ object DiffUtil {
5959
(fnd, exp, totalChange.toDouble / (expected.length + found.length))
6060
}
6161

62+
def mkColoredLinesDiff(expected: Seq[String], actual: Seq[String]): String = {
63+
val expectedSize = DiffUtil.EOF.length max expected.map(_.length).max
64+
actual.padTo(expected.length, "").zip(expected.padTo(actual.length, "")).map { case (act, exp) =>
65+
DiffUtil.mkColoredLineDiff(exp, act, expectedSize)
66+
}.mkString("\n")
67+
}
68+
6269
def mkColoredLineDiff(expected: String, actual: String, expectedSize: Int): String = {
6370
lazy val diff = {
6471
val tokens = splitTokens(expected, Nil).toArray

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package dotty.tools
22
package repl
33

4-
import java.io.{ File => JFile }
4+
import java.io.{File => JFile}
55

66
import org.junit.Assert._
77
import org.junit.Test
88

99
import scala.collection.mutable.ArrayBuffer
1010
import scala.io.Source
11-
1211
import dotc.reporting.MessageRendering
12+
import dotty.tools.dotc.util.DiffUtil
1313

1414
/** Runs all tests contained in `compiler/test-resources/repl/` */
1515
class ScriptedTests extends ReplTest with MessageRendering {
@@ -56,11 +56,11 @@ class ScriptedTests extends ReplTest with MessageRendering {
5656
def filterEmpties(line: String): List[String] =
5757
line.replaceAll("""(?m)\s+$""", "") match {
5858
case "" => Nil
59-
case nonEmptyLine => nonEmptyLine :: Nil
59+
case nonEmptyLine => nonEmptyLine.split("\n").toList
6060
}
6161

6262
val expectedOutput =
63-
Source.fromFile(f).getLines.flatMap(filterEmpties).mkString("\n")
63+
Source.fromFile(f).getLines.flatMap(filterEmpties).toList
6464
val actualOutput = {
6565
resetToInitial()
6666
init()
@@ -71,15 +71,11 @@ class ScriptedTests extends ReplTest with MessageRendering {
7171
buf.append(out)
7272
nstate
7373
}
74-
buf.flatMap(filterEmpties).mkString("\n")
74+
buf.flatMap(filterEmpties).toList
7575
}
7676

7777
if (expectedOutput != actualOutput) {
78-
println("expected =========>")
79-
println(expectedOutput)
80-
println("actual ===========>")
81-
println(actualOutput)
82-
78+
println(DiffUtil.mkColoredLinesDiff(expectedOutput, actualOutput))
8379
fail(s"Error in file $f, expected output did not match actual")
8480
}
8581
}

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
535535

536536
if (outputLines.length != checkLines.length || !linesMatch) {
537537
// Print diff to files and summary:
538-
val expectedSize = DiffUtil.EOF.length max checkLines.map(_.length).max
539-
val diff = outputLines.padTo(checkLines.length, "").zip(checkLines.padTo(outputLines.length, "")).map { case (act, exp) =>
540-
DiffUtil.mkColoredLineDiff(exp, act, expectedSize)
541-
}.mkString("\n")
538+
val diff = DiffUtil.mkColoredLinesDiff(checkLines, outputLines)
542539

543540
val msg =
544541
s"""|Output from '$sourceTitle' did not match check file.

0 commit comments

Comments
 (0)