@@ -8,10 +8,13 @@ object DiffUtil {
88 private final val ANSI_DEFAULT = " \u001B [0m"
99 private final val ANSI_RED = " \u001B [31m"
1010 private final val ANSI_GREEN = " \u001B [32m"
11+ private final val ANSI_EOF = " \u001B [2m"
1112
1213 private final val DELETION_COLOR = ANSI_RED
1314 private final val ADDITION_COLOR = ANSI_GREEN
1415
16+ val EOF = new String (" EOF" ) // Unique string up to reference
17+
1518 @ tailrec private def splitTokens (str : String , acc : List [String ] = Nil ): List [String ] = {
1619 if (str == " " ) {
1720 acc.reverse
@@ -58,22 +61,38 @@ object DiffUtil {
5861 (fnd, exp, totalChange.toDouble / (expected.length + found.length))
5962 }
6063
61- def mkColoredLineDiff (expected : String , actual : String ): String = {
62- val tokens = splitTokens(expected, Nil ).toArray
63- val lastTokens = splitTokens(actual, Nil ).toArray
64-
65- val diff = hirschberg(lastTokens, tokens)
64+ def mkColoredLineDiff (expected : String , actual : String , expectedSize : Int ): String = {
65+ lazy val diff = {
66+ val tokens = splitTokens(expected, Nil ).toArray
67+ val lastTokens = splitTokens(actual, Nil ).toArray
68+ hirschberg(lastTokens, tokens)
69+ }
6670
67- " |SOF\n " + diff.collect {
68- case Unmodified (str) =>
69- " |" + str
70- case Inserted (str) =>
71- ADDITION_COLOR + " e |" + str + ANSI_DEFAULT
72- case Modified (old, str) =>
73- DELETION_COLOR + " a |" + old + " \n e |" + ADDITION_COLOR + str + ANSI_DEFAULT
74- case Deleted (str) =>
75- DELETION_COLOR + " \n a |" + str + ANSI_DEFAULT
76- }.mkString + " \n |EOF"
71+ val expectedDiff =
72+ if (expected eq EOF ) ANSI_EOF + expected + ANSI_DEFAULT
73+ else diff.collect {
74+ case Unmodified (str) => str
75+ case Inserted (str) =>
76+ ADDITION_COLOR + str + ANSI_DEFAULT
77+ case Modified (_, str) =>
78+ ADDITION_COLOR + str + ANSI_DEFAULT
79+ case Deleted (_) => " "
80+ }.mkString
81+
82+ val actualDiff =
83+ if (actual eq EOF ) ANSI_EOF + actual + ANSI_DEFAULT
84+ else diff.collect {
85+ case Unmodified (str) => str
86+ case Inserted (_) => " "
87+ case Modified (str, _) =>
88+ DELETION_COLOR + str + ANSI_DEFAULT
89+ case Deleted (str) =>
90+ DELETION_COLOR + str + ANSI_DEFAULT
91+ }.mkString
92+
93+ val pad = " " * 0 .max(expectedSize - expected.length)
94+
95+ expectedDiff + pad + " | " + actualDiff
7796 }
7897
7998 def mkColoredCodeDiff (code : String , lastCode : String , printDiffDel : Boolean ): String = {
0 commit comments