Skip to content

Commit 59fcde7

Browse files
Merge pull request #8989 from rethab/fix-diff-utils-for-coloring
Fix diff utils for coloring
2 parents 76b42dd + 5c75d47 commit 59fcde7

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ object DiffUtil {
191191
}
192192

193193
private def needlemanWunsch(x: Array[String], y: Array[String], builder: mutable.ArrayBuilder[Patch]): Unit = {
194-
def similarity(a: String, b: String) = if (a == b) 2 else -1
194+
def similarity(a: String, b: String) = if (a == b) 3 else -1
195195
val d = 1
196196
val score = Array.tabulate(x.length + 1, y.length + 1) { (i, j) =>
197197
if (i == 0) d * j
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package dotty.tools.dotc.util
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
6+
class DiffUtilTests {
7+
8+
def testExpected(found: String, expected: String, foundColoring: String, expectedColoring: String): Unit = {
9+
def humanAscii(str: String): String =
10+
str
11+
.replace(Console.RESET, ">")
12+
.replace(Console.BOLD, "")
13+
.replace(Console.RED, "<R|")
14+
.replace(Console.GREEN, "<G|")
15+
// merging two aligning colors
16+
.replace("><R|", "")
17+
.replace("><G|", "")
18+
19+
val diff = DiffUtil.mkColoredTypeDiff(found, expected)
20+
val fnd = humanAscii(diff._1)
21+
val exp = humanAscii(diff._2)
22+
23+
if (fnd != foundColoring) fail(s"expected(found):\n$foundColoring but was: \n$fnd")
24+
if (exp != expectedColoring) fail(s"expected(expected): \n$expectedColoring but was: \n$exp")
25+
}
26+
27+
@Test
28+
def simpleString(): Unit = {
29+
testExpected("Foo", "Bar", "<R|Foo>", "<G|Bar>")
30+
testExpected("Bar", "Foo", "<R|Bar>", "<G|Foo>")
31+
}
32+
33+
@Test
34+
def tuple(): Unit = {
35+
testExpected("(Foo, Bar)", "(Bar, Foo)", "(<R|Foo>, <R|Bar>)", "(<G|Bar>, <G|Foo>)")
36+
testExpected("(Int, Bar, Float)", "Bar", "<R|(Int, >Bar<R|, Float)>", "Bar")
37+
}
38+
39+
@Test
40+
def tupleSeq(): Unit = {
41+
testExpected("(Foo, Seq[Bar])", "Seq[Bar]", "<R|(Foo, >Seq[Bar]<R|)>", "Seq[Bar]")
42+
testExpected("Seq[Bar]", "(Foo, Seq[Bar])", "Seq[Bar]", "<G|(Foo, >Seq[Bar]<G|)>")
43+
}
44+
45+
@Test
46+
def seqTuple(): Unit = {
47+
testExpected("Seq[(Foo, Bar)]", "Seq[Bar]", "Seq[<R|(Foo, >Bar<R|)>]", "Seq[Bar]")
48+
testExpected("Seq[Bar]", "Seq[(Foo, Bar)]", "Seq[Bar]", "Seq[<G|(Foo, >Bar<G|)>]")
49+
}
50+
51+
@Test
52+
def seqSeq(): Unit = {
53+
testExpected("Seq[Seq[Seq[Foo]]]", "Seq[List[Seq[(Bar, Foo)]]]", "Seq[<R|Seq>[Seq[Foo]]]", "Seq[<G|List>[Seq[<G|(Bar, >Foo<G|)>]]]")
54+
testExpected("Seq[List[Seq[(Bar, Foo)]]]", "Seq[Seq[Seq[Foo]]]", "Seq[<R|List>[Seq[<R|(Bar, >Foo<R|)>]]]", "Seq[<G|Seq>[Seq[Foo]]]")
55+
}
56+
57+
}

0 commit comments

Comments
 (0)