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 authored and rholshausen committed Nov 24, 2022
1 parent 52d094e commit 52df115
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 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(path: ":core:model", configuration: 'default')
api project(path: ":core:support", configuration: 'default')
compile "org.apache.commons:commons-lang3:${project.commonsLang3Version}"
compile 'com.googlecode.java-diff-utils:diffutils:1.3.0'
compile 'io.github.java-diff-utils:java-diff-utils:4.12'
implementation 'xerces:xercesImpl:2.12.0'
implementation "org.slf4j:slf4j-api:${project.slf4jVersion}"
implementation 'org.atteo:evo-inflector:1.2.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package au.com.dius.pact.core.matchers

import au.com.dius.pact.core.support.json.JsonValue
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 ->
if (delta.original.position >= 1 && (diff.isEmpty() || expectedLines[delta.original.position - 1] != diff.last())) {
diff.add(expectedLines[delta.original.position - 1])
}

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

val pos = delta.original.position + delta.original.lines.size
if (pos < expectedLines.size) {
diff.add(expectedLines[pos])
when (delta) {
is ChangeDelta<*> -> {
if (delta.source.position >= 1 && (diff.isEmpty() ||
expectedLines[delta.source.position - 1] != diff.last())) {
diff.addAll(expectedLines.slice(line until delta.source.position))
}

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

line = delta.source.position + delta.source.lines.size
}
}
}
if (line < expectedLines.size) {
diff.addAll(expectedLines.listIterator(line).asSequence())
}
return diff
}

Expand Down

0 comments on commit 52df115

Please sign in to comment.