Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 99ee665

Browse files
authored
Merge pull request #44 from kciesielski/handle-empty-diff
Handle empty diff
2 parents 174a816 + 01b4bba commit 99ee665

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

model/src/main/scala/com/softwaremill/clippy/StringDiff.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@ class StringDiff(expected: String, actual: String, color: String => String) {
1717
private def markDiff(source: String) = {
1818
val prefix = findCommonPrefix()
1919
val suffix = findCommonSuffix()
20-
val diff = color(source.substring(prefix.length, source.length - suffix.length))
21-
prefix + diff + suffix
20+
if (overlappingPrefixSuffix(source, prefix, suffix))
21+
source
22+
else {
23+
val diff = color(source.substring(prefix.length, source.length - suffix.length))
24+
prefix + diff + suffix
25+
}
2226
}
2327

28+
private def overlappingPrefixSuffix(source: String, prefix: String, suffix: String) =
29+
prefix.length + suffix.length >= source.length
30+
2431
def findCommonPrefix(expectedStr: String = expected, actualStr: String = actual): String = {
2532
val prefixChars = expectedStr.zip(actualStr).takeWhile(Function.tupled(_ == _)).map(_._1)
2633

2734
val lastSeparatorIndex = prefixChars.lastIndexWhere(isSeparator)
28-
val prefixStartIndex = if (lastSeparatorIndex == -1) 0 else lastSeparatorIndex + 1
35+
val prefixEndIndex = if (lastSeparatorIndex == -1) 0 else lastSeparatorIndex + 1
2936

30-
if (prefixChars.nonEmpty && prefixStartIndex < prefixChars.length)
31-
prefixChars.mkString.substring(0, prefixStartIndex)
37+
if (prefixChars.nonEmpty && prefixEndIndex < prefixChars.length)
38+
prefixChars.mkString.substring(0, prefixEndIndex)
3239
else {
3340
prefixChars.mkString
3441
}

model/src/test/scala/com/softwaremill/clippy/StringDiffTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class StringDiffTest extends FlatSpec with Matchers {
1212
("Super[String, String]", "Super[Option[String], String]", "expected Super[" + S + "String" + E + ", String] but was Super[" + S + "Option[String]" + E + ", String]"),
1313
("Cool[String, String]", "Super[Option[String], String]", "expected " + S + "Cool[String" + E + ", String] but was " + S + "Super[Option[String]" + E + ", String]"),
1414
("(String, String)", "Super[Option[String], String]", "expected " + S + "(String, String)" + E + " but was " + S + "Super[Option[String], String]" + E),
15-
("Map[Long, Double]", "Map[String, Double]", "expected Map[" + S + "Long" + E + ", Double] but was Map[" + S + "String" + E + ", Double]")
15+
("Map[Long, Double]", "Map[String, Double]", "expected Map[" + S + "Long" + E + ", Double] but was Map[" + S + "String" + E + ", Double]"),
16+
("(Int, Int, Float, Int, Char)", "(Int, Int, Int, Char)", "expected (Int, Int, " + S + "Float" + E + ", Int, Char) but was (Int, Int, Int, Char)")
1617
)
1718

1819
"StringDiff" should "diff" in {

0 commit comments

Comments
 (0)