petertrr/kotlin-multiplatform-diff

Incorrect behavior of ChangeDelta

Closed this issue · 1 comments

Let's consider the following code:

val expectedData = listOf("Some line ")
val actualData = listOf("Some line")
val st = diff(expectedData, actualData).formatToString()
println("Status: $st")

And the result will be:
Status: [ChangeDelta, position 0, lines: [Some line[ ]]], so we can see, that the white space is absent - everything is ok.


However, if we add white space to the actualData, instead of expected:

val expectedData = listOf("Some line")
val actualData = listOf("Some line ")
val st = diff(expectedData, actualData).formatToString()
println("Status: $st")

The result will be
Status: [ChangeDelta, position 0, lines: [Some line]]
By this output, it's unclear what's going on, and it seems, that all data Some line is absent, instead of white space.

Also, maybe it will be convenient, if ChangeDelta will additionally print in which input there is the difference

E.g.
[ChangeDelta, position 0, lines: [["Absent data in actual input: ", Some line[ ]].....]
[ChangeDelta, position 0, lines: [["Extra data in actual input", Some line<>]....]

Or in some other manner

It seems that the problem was in not very clear API and our misuse of it. I.e., mergeOriginalRevised parameter has been set to false, and inline diff has been generated for newLine, while we were using oldLine. I can reproduce this problem like this:

fun test() {
        val generator = DiffRowGenerator.create()
            .showInlineDiffs(true)
            .mergeOriginalRevised(false)  // <- this is what I am talking about
            .inlineDiffByWord(false)
            .oldTag { _, f -> if (f) "[" else "]" }
            .newTag { _, f -> if (f) "<" else ">" }
            .build()
        val rows: List<DiffRow> = generator.generateDiffRows(
            listOf("Original line"),
            listOf("Original line ")
        )
        println(rows[0])
}

which prints

DiffRow(tag=CHANGE, oldLine=Original line, newLine=Original line< >)