Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diff output format in TestComparator #668

Merged
merged 3 commits into from
Dec 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.cqfn.diktat.test.framework.processing

import com.github.difflib.DiffUtils
import com.github.difflib.patch.ChangeDelta
import com.github.difflib.text.DiffRowGenerator
import org.slf4j.LoggerFactory

import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
import java.util.ArrayList
import java.util.StringJoiner
import java.util.stream.Collectors

/**
Expand All @@ -17,6 +18,13 @@ import java.util.stream.Collectors
class FileComparator {
private val expectedResultFile: File
private val actualResultList: List<String?>
private val diffGenerator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(false)
.oldTag { start -> if (start) "[" else "]" }
.newTag { start -> if (start) "<" else ">" }
.build()

constructor(expectedResultFile: File, actualResultList: List<String?>) {
this.expectedResultFile = expectedResultFile
Expand All @@ -43,15 +51,22 @@ class FileComparator {
if (patch.deltas.isEmpty()) {
return true
}
val deltasJoiner = StringJoiner(System.lineSeparator())
patch
.deltas
.map { it.toString() }
.forEach { delta -> deltasJoiner.add(delta) }
val joinedDeltas = patch.deltas.joinToString(System.lineSeparator()) { delta ->
when (delta) {
is ChangeDelta -> diffGenerator
.generateDiffRows(delta.source.lines, delta.target.lines)
.joinToString(System.lineSeparator()) { it.oldLine }
.let { "[ChangeDelta, position ${delta.source.position}, lines: [$it]]" }
else -> delta.toString()
}
}

log.error("""Expected result from <${expectedResultFile.name}> and actual formatted are different.
| See difference below:
| Expected vs Actual ${System.lineSeparator()}$deltasJoiner""".trimMargin())
log.error("""
|Expected result from <${expectedResultFile.name}> and actual formatted are different.
|See difference below (for ChangeDelta [text] indicates removed text, <text> - inserted):
|$joinedDeltas
""".trimMargin()
)
} catch (e: RuntimeException) {
log.error("Not able to prepare diffs between <${expectedResultFile.name}> and <$actualResultList>", e)
}
Expand Down