Skip to content

Fix diff utils for coloring #8989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/util/DiffUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ object DiffUtil {
}

private def needlemanWunsch(x: Array[String], y: Array[String], builder: mutable.ArrayBuilder[Patch]): Unit = {
def similarity(a: String, b: String) = if (a == b) 2 else -1
def similarity(a: String, b: String) = if (a == b) 3 else -1
val d = 1
val score = Array.tabulate(x.length + 1, y.length + 1) { (i, j) =>
if (i == 0) d * j
Expand Down
57 changes: 57 additions & 0 deletions compiler/test/dotty/tools/dotc/util/DiffUtilTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dotty.tools.dotc.util

import org.junit.Assert._
import org.junit.Test

class DiffUtilTests {

def testExpected(found: String, expected: String, foundColoring: String, expectedColoring: String): Unit = {
def humanAscii(str: String): String =
str
.replace(Console.RESET, ">")
.replace(Console.BOLD, "")
.replace(Console.RED, "<R|")
.replace(Console.GREEN, "<G|")
// merging two aligning colors
.replace("><R|", "")
.replace("><G|", "")

val diff = DiffUtil.mkColoredTypeDiff(found, expected)
val fnd = humanAscii(diff._1)
val exp = humanAscii(diff._2)

if (fnd != foundColoring) fail(s"expected(found):\n$foundColoring but was: \n$fnd")
if (exp != expectedColoring) fail(s"expected(expected): \n$expectedColoring but was: \n$exp")
}

@Test
def simpleString(): Unit = {
testExpected("Foo", "Bar", "<R|Foo>", "<G|Bar>")
testExpected("Bar", "Foo", "<R|Bar>", "<G|Foo>")
}

@Test
def tuple(): Unit = {
testExpected("(Foo, Bar)", "(Bar, Foo)", "(<R|Foo>, <R|Bar>)", "(<G|Bar>, <G|Foo>)")
testExpected("(Int, Bar, Float)", "Bar", "<R|(Int, >Bar<R|, Float)>", "Bar")
}

@Test
def tupleSeq(): Unit = {
testExpected("(Foo, Seq[Bar])", "Seq[Bar]", "<R|(Foo, >Seq[Bar]<R|)>", "Seq[Bar]")
testExpected("Seq[Bar]", "(Foo, Seq[Bar])", "Seq[Bar]", "<G|(Foo, >Seq[Bar]<G|)>")
}

@Test
def seqTuple(): Unit = {
testExpected("Seq[(Foo, Bar)]", "Seq[Bar]", "Seq[<R|(Foo, >Bar<R|)>]", "Seq[Bar]")
testExpected("Seq[Bar]", "Seq[(Foo, Bar)]", "Seq[Bar]", "Seq[<G|(Foo, >Bar<G|)>]")
}

@Test
def seqSeq(): Unit = {
testExpected("Seq[Seq[Seq[Foo]]]", "Seq[List[Seq[(Bar, Foo)]]]", "Seq[<R|Seq>[Seq[Foo]]]", "Seq[<G|List>[Seq[<G|(Bar, >Foo<G|)>]]]")
testExpected("Seq[List[Seq[(Bar, Foo)]]]", "Seq[Seq[Seq[Foo]]]", "Seq[<R|List>[Seq[<R|(Bar, >Foo<R|)>]]]", "Seq[<G|Seq>[Seq[Foo]]]")
}

}