Skip to content

Commit

Permalink
Replace diff-utils with the latest actively maintained version 4.12
Browse files Browse the repository at this point in the history
  • Loading branch information
bfugas committed Aug 5, 2022
1 parent 8c31b4f commit 1883253
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/matchers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
api project(':core:model')
api project(':core:support')
implementation "org.apache.commons:commons-lang3:${project.commonsLang3Version}"
implementation 'com.googlecode.java-diff-utils:diffutils:1.3.0'
implementation 'io.github.java-diff-utils:java-diff-utils:4.12'
implementation 'xerces:xercesImpl:2.12.1'
implementation "org.slf4j:slf4j-api:${project.slf4jVersion}"
implementation 'org.atteo:evo-inflector:1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package au.com.dius.pact.core.matchers

import au.com.dius.pact.core.support.json.JsonValue
import difflib.ChangeDelta
import com.github.difflib.DiffUtils
import com.github.difflib.patch.ChangeDelta

private const val NEW_LINE = '\n'

fun generateDiff(expectedBodyString: String, actualBodyString: String): List<String> {
val expectedLines = expectedBodyString.split(NEW_LINE)
val actualLines = actualBodyString.split(NEW_LINE)
val patch = difflib.DiffUtils.diff(expectedLines, actualLines)
val patch = DiffUtils.diff(expectedLines, actualLines)

val diff = mutableListOf<String>()
var line = 0
patch.deltas.forEach { delta ->
when (delta) {
is ChangeDelta<*> -> {
if (delta.original.position >= 1 && (diff.isEmpty() ||
expectedLines[delta.original.position - 1] != diff.last())) {
diff.addAll(expectedLines.slice(line until delta.original.position))
if (delta.source.position >= 1 && (diff.isEmpty() ||
expectedLines[delta.source.position - 1] != diff.last())) {
diff.addAll(expectedLines.slice(line until delta.source.position))
}

delta.original.lines.forEach {
delta.source.lines.forEach {
diff.add("-$it")
}
delta.revised.lines.forEach {
delta.target.lines.forEach {
diff.add("+$it")
}

line = delta.original.position + delta.original.lines.size
line = delta.source.position + delta.source.lines.size
}
}
}
Expand Down

0 comments on commit 1883253

Please sign in to comment.