Skip to content

Commit

Permalink
Diff: refactor, move static methods to companion
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 18, 2025
1 parent 779870e commit e9e548e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions munit-diff/shared/src/main/scala/munit/diff/Diff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import munit.diff.console.Printers
import scala.collection.JavaConverters._

class Diff(val obtained: String, val expected: String) extends Serializable {
import Diff._

val obtainedClean: String = AnsiColors.filterAnsi(obtained)
val expectedClean: String = AnsiColors.filterAnsi(expected)
val obtainedLines: Seq[String] = splitIntoLines(obtainedClean)
val expectedLines: Seq[String] = splitIntoLines(expectedClean)
val unifiedDiff: String = createUnifiedDiff(obtainedLines, expectedLines)
def isEmpty: Boolean = unifiedDiff.isEmpty()
def isEmpty: Boolean = unifiedDiff.isEmpty

def createReport(
title: String,
Expand Down Expand Up @@ -45,6 +47,10 @@ class Diff(val obtained: String, val expected: String) extends Serializable {
sb.append(unifiedDiff)
}

}

object Diff {

private def asStripMargin(obtained: String): String =
if (!obtained.contains("\n")) Printers.print(obtained)
else {
Expand All @@ -69,19 +75,19 @@ class Diff(val obtained: String, val expected: String) extends Serializable {
if (diff.getDeltas.isEmpty) ""
else DiffUtils
.generateUnifiedDiff("obtained", "expected", original.asJava, diff, 1)
.asScala.iterator.drop(2).filterNot(_.startsWith("@@")).map(line =>
if (line.isEmpty()) line
else if (line.last == ' ') line + ""
else line
).map(line =>
if (line.startsWith("-")) AnsiColors.c(line, AnsiColors.LightRed)
else if (line.startsWith("+")) AnsiColors
.c(line, AnsiColors.LightGreen)
else line
.asScala.iterator.drop(2).filterNot(_.startsWith("@@"))
.map(line => if (line.lastOption.contains(' ')) line + "" else line)
.map(line =>
line.headOption match {
case Some('-') => AnsiColors.c(line, AnsiColors.LightRed)
case Some('+') => AnsiColors.c(line, AnsiColors.LightGreen)
case _ => line
}
).mkString("\n")
result
}

private def splitIntoLines(string: String): Seq[String] = string.trim()
.replace("\r\n", "\n").split("\n").toIndexedSeq

}

0 comments on commit e9e548e

Please sign in to comment.