From 7fe03485ac991b7e2140c265a3c8e9c098213628 Mon Sep 17 00:00:00 2001 From: Peter Trifaniov Date: Sat, 18 Dec 2021 23:34:52 +0300 Subject: [PATCH] feat: Refactor DiffRowGenerator, code cleanup * Get rid of builder in favor of constructor * Code cleanup, docs cleanup BREAKING: API of DiffRowGenerator has changed, constructor should be used instead of builder --- .../io/github/petertrr/diffutils/DiffUtils.kt | 4 +- .../diffutils/algorithm/myers/MyersDiff.kt | 27 +- .../github/petertrr/diffutils/patch/Chunk.kt | 11 +- .../github/petertrr/diffutils/patch/Patch.kt | 2 +- .../diffutils/text/DiffRowGenerator.kt | 343 ++++------------ .../petertrr/diffutils/DiffUtilsTest.kt | 4 +- .../diffutils/text/DiffRowGeneratorTest.kt | 381 +++++++++--------- 7 files changed, 297 insertions(+), 475 deletions(-) diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt index 699301a..076b2e7 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/DiffUtils.kt @@ -39,7 +39,7 @@ import io.github.petertrr.diffutils.patch.PatchFailedException * @return The patch describing the difference between the original and revised sequences. */ public fun diff(original: List, revised: List, progress: DiffAlgorithmListener?): Patch { - return diff(original, revised, MyersDiff(), progress) + return diff(original, revised, MyersDiff(), progress) } public fun diff(original: List, revised: List): Patch { @@ -168,7 +168,7 @@ private fun compressLines(lines: List, delimiter: String): List * @return the revised text * @throws PatchFailedException if can't apply patch */ -@kotlin.Throws(PatchFailedException::class) +@Throws(PatchFailedException::class) public fun patch(original: List, patch: Patch): List { return patch.applyTo(original) } diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt index 69e3fdf..2e24327 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/algorithm/myers/MyersDiff.kt @@ -45,7 +45,7 @@ internal class MyersDiff(private val equalizer: (T, T) -> Boolean = { t1, t2 * @param orig The original sequence. * @param rev The revised sequence. * @return A minimum [PathNode] across the differences graph. - * @throws DifferentiationFailedException if a diff path could not be found. + * @throws IllegalStateException if a diff path could not be found. */ private fun buildPath(orig: List, rev: List, progress: DiffAlgorithmListener?): PathNode? { // these are local constants @@ -74,13 +74,13 @@ internal class MyersDiff(private val equalizer: (T, T) -> Boolean = { t1, t2 } diagonal[kminus] = null // no longer used var j = i - k - var node = PathNode(i, j, false, false, prev) + var node = PathNode(i, j, snake = false, bootstrap = false, prev = prev) while (i < N && j < M && equalizer.invoke(orig[i], rev[j])) { i++ j++ } if (i != node.i) { - node = PathNode(i, j, true, false, node) + node = PathNode(i, j, snake = true, bootstrap = false, prev = node) } diagonal[kmiddle] = node if (i >= N && j >= M) { @@ -94,14 +94,13 @@ internal class MyersDiff(private val equalizer: (T, T) -> Boolean = { t1, t2 } /** - * Constructs a [Patch] from a difference path. + * Constructs a patch from a difference path. * * @param actualPath The path. * @param orig The original sequence. * @param rev The revised sequence. - * @return A [Patch] script corresponding to the path. - * @throws DifferentiationFailedException if a [Patch] could not be built from the given - * path. + * @return A list of [Change]s corresponding to the path. + * @throws IllegalStateException if a patch could not be built from the given path. */ private fun buildRevision(actualPath: PathNode?, orig: List, rev: List): List { var path: PathNode? = actualPath @@ -114,14 +113,14 @@ internal class MyersDiff(private val equalizer: (T, T) -> Boolean = { t1, t2 val i: Int = path.i val j: Int = path.j path = path.prev - val ianchor: Int = path!!.i - val janchor: Int = path.j - if (ianchor == i && janchor != j) { - changes.add(Change(DeltaType.INSERT, ianchor, i, janchor, j)) - } else if (ianchor != i && janchor == j) { - changes.add(Change(DeltaType.DELETE, ianchor, i, janchor, j)) + val iAnchor: Int = path!!.i + val jAnchor: Int = path.j + if (iAnchor == i && jAnchor != j) { + changes.add(Change(DeltaType.INSERT, iAnchor, i, jAnchor, j)) + } else if (iAnchor != i && jAnchor == j) { + changes.add(Change(DeltaType.DELETE, iAnchor, i, jAnchor, j)) } else { - changes.add(Change(DeltaType.CHANGE, ianchor, i, janchor, j)) + changes.add(Change(DeltaType.CHANGE, iAnchor, i, jAnchor, j)) } if (path.snake) { path = path.prev diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Chunk.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Chunk.kt index 6330593..e7451d9 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Chunk.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Chunk.kt @@ -21,12 +21,11 @@ package io.github.petertrr.diffutils.patch /** * Holds the information about the part of text involved in the diff process * - * Text is represented as `Object[]` because the diff engine is capable of handling more - * than plain ascci. In fact, arrays or lists of any type that implements - * [hashCode()][java.lang.Object.hashCode] and [equals()][java.lang.Object.equals] - * correctly can be subject to differencing using this library. + * Text is represented as generic class `T` because the diff engine is capable of handling more + * than plain ASCII. In fact, arrays or lists of any type that implements + * `hashCode()` and `equals()` correctly can be subject to differencing using this library. * @param T The type of the compared elements in the 'lines'. -) */ + */ public data class Chunk( /** * the start position of chunk in the text @@ -45,7 +44,7 @@ public data class Chunk( * Verifies that this chunk's saved text matches the corresponding text in the given sequence. * * @param target the sequence to verify against. - * @throws com.github.difflib.patch.PatchFailedException + * @throws PatchFailedException */ @Throws(PatchFailedException::class) public fun verify(target: List) { diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt index c69e77d..5f90a4b 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/patch/Patch.kt @@ -115,7 +115,7 @@ public class Patch(estimatedPatchSize: Int = 10) { } if (includeEquals && startOriginal < original.size) { patch.addDelta( - EqualDelta( + EqualDelta( buildChunk(startOriginal, original.size, original), buildChunk(startRevised, revised.size, revised) ) diff --git a/src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt b/src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt index 0c489dc..1cec3d3 100644 --- a/src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt +++ b/src/commonMain/kotlin/io/github/petertrr/diffutils/text/DiffRowGenerator.kt @@ -34,31 +34,93 @@ import kotlin.math.min * the way of generating. For example, show inline diffs on not, ignoring white * spaces or/and blank lines and so on. All parameters for generating are * optional. If you do not specify them, the class will use the default values. - * - * These values are: showInlineDiffs = false; ignoreWhiteSpaces = true; mergeOriginalRevised = false; - * reportLinesUnchanged = false; replaceOriginalLinefeedInChangesWithSpaces = false; - * - * For instantiating the DiffRowGenerator you should use the its builder. Like - * in example - * ```kotlin - * val generator = DiffRowGenerator.create().showInlineDiffs(true) - * .ignoreWhiteSpaces(true).columnWidth(100).build() - * ``` */ -public class DiffRowGenerator private constructor(builder: Builder) { - private val columnWidth: Int - private var equalizer: ((String, String) -> Boolean)? = null // nullable; if null, the default equalizer will be used - private val ignoreWhiteSpaces: Boolean// = true - private val inlineDiffSplitter: (String) -> List // non-nullable per original library - private val mergeOriginalRevised: Boolean// = false - private val newTag: (DiffRow.Tag, Boolean) -> String // non-nullable, essential for algorithm - private val oldTag: (DiffRow.Tag, Boolean) -> String // non-nullable, essential for algorithm - private val reportLinesUnchanged: Boolean// = false - private val lineNormalizer: (String) -> String // non-nullable per original library - private val processDiffs: ((String) -> String)? // nullable by design - private val showInlineDiffs: Boolean// = false - private val replaceOriginalLinefeedInChangesWithSpaces: Boolean// = false +public class DiffRowGenerator( + /** + * Set the column width of generated lines of original and revised + * texts. Making it < 0 doesn't make any sense. + */ + public val columnWidth: Int = 80, + + /** + * Ignore white spaces in generating diff rows or not. + */ + public val ignoreWhiteSpaces: Boolean = false, + + /** + * Provide an equalizer for diff processing. + */ + public var equalizer: ((String, String) -> Boolean) = if (ignoreWhiteSpaces) IGNORE_WHITESPACE_EQUALIZER else DEFAULT_EQUALIZER, + + /** + * Per default each character is separately processed. Setting this parameter to `true` + * introduces processing by word, which does not deliver in word + * changes. Therefore, the whole word will be tagged as changed: + * + * ``` + *
+     * false:    (aBa : aba) --  changed: a(B)a : a(b)a
+     * true:     (aBa : aba) --  changed: (aBa) : (aba)
+     * 
* + * ``` + */ + inlineDiffByWord: Boolean = false, + + /** + * To provide some customized splitting a splitter can be provided. Here + * someone could think about sentence splitter, comma splitter or stuff + * like that. + */ + public val inlineDiffSplitter: (String) -> List = if (inlineDiffByWord) SPLITTER_BY_WORD else SPLITTER_BY_CHARACTER, + /** + * Merge the complete result within the original text. This makes sense + * for one line display. + */ + public val mergeOriginalRevised: Boolean = false, + + /** + * Generator for New-Text-Tags. + */ + public val newTag: (DiffRow.Tag, Boolean) -> String = { _: DiffRow.Tag, f: Boolean -> if (f) "" else "" }, + + /** + * Generator for Old-Text-Tags. + */ + public val oldTag: (DiffRow.Tag, Boolean) -> String = { _: DiffRow.Tag, f: Boolean -> if (f) "" else "" }, + + /** + * Give the original old and new text lines to DiffRow without any + * additional processing and without any tags to highlight the change. + */ + public val reportLinesUnchanged: Boolean = false, + + /** + * By default DiffRowGenerator preprocesses lines for HTML output. Tabs + * and special HTML characters like "<" are replaced with its encoded + * value. To change this you can provide a customized line normalizer + * here. + */ + public val lineNormalizer: (String) -> String = LINE_NORMALIZER_FOR_HTML, + + /** + * Optional processor for diffed text parts. Here e.g. white characters could be + * replaced by something visible. + */ + public val processDiffs: ((String) -> String)? = null, + + /** + * Show inline diffs in generating diff rows or not. Default: false. + */ + public val showInlineDiffs: Boolean = false, + + /** + * Sometimes it happens that a change contains multiple lines. If there + * is no correspondence in old and new. To keep the merged line more + * readable the linefeeds could be replaced by spaces. + */ + public val replaceOriginalLinefeedInChangesWithSpaces: Boolean = false, +) { /** * Get the DiffRows describing the difference between original and revised * texts. Useful for displaying side-by-side diff. @@ -315,216 +377,6 @@ public class DiffRowGenerator private constructor(builder: Builder) { } } - /** - * This class used for building the DiffRowGenerator. - */ - public class Builder internal constructor() { - internal var showInlineDiffs = false - internal var ignoreWhiteSpaces = false - internal var oldTag = { _: DiffRow.Tag, f: Boolean -> if (f) "" else "" } - internal var newTag = { _: DiffRow.Tag, f: Boolean -> if (f) "" else "" } - internal var columnWidth = 0 - internal var mergeOriginalRevised = false - internal var reportLinesUnchanged = false - internal var inlineDiffSplitter = SPLITTER_BY_CHARACTER - internal var lineNormalizer = LINE_NORMALIZER_FOR_HTML - internal var processDiffs: ((String) -> String)? = null - internal var equalizer: ((String, String) -> Boolean)? = null - internal var replaceOriginalLinefeedInChangesWithSpaces = false - - /** - * Show inline diffs in generating diff rows or not. - * - * @param val the value to set. Default: false. - * @return builder with configured showInlineDiff parameter - */ - public fun showInlineDiffs(`val`: Boolean): Builder { - showInlineDiffs = `val` - return this - } - - /** - * Ignore white spaces in generating diff rows or not. - * - * @param val the value to set. Default: true. - * @return builder with configured ignoreWhiteSpaces parameter - */ - public fun ignoreWhiteSpaces(`val`: Boolean): Builder { - ignoreWhiteSpaces = `val` - return this - } - - /** - * Give the original old and new text lines to DiffRow without any - * additional processing and without any tags to highlight the change. - * - * @param val the value to set. Default: false. - * @return builder with configured reportLinesUnWrapped parameter - */ - public fun reportLinesUnchanged(`val`: Boolean): Builder { - reportLinesUnchanged = `val` - return this - } - - /** - * Generator for Old-Text-Tags. - * - * @param generator the tag generator - * @return builder with configured ignoreBlankLines parameter - */ - public fun oldTag(generator: (DiffRow.Tag, Boolean) -> String): Builder { - oldTag = generator - return this - } - - /** - * Generator for Old-Text-Tags. - * - * @param generator the tag generator - * @return builder with configured ignoreBlankLines parameter - */ - public fun oldTag(generator: (Boolean) -> String): Builder { - oldTag = { _: DiffRow.Tag, f: Boolean -> generator.invoke(f) } - return this - } - - /** - * Generator for New-Text-Tags. - * - * @param generator - * @return - */ - public fun newTag(generator: (DiffRow.Tag, Boolean) -> String): Builder { - newTag = generator - return this - } - - /** - * Generator for New-Text-Tags. - * - * @param generator - * @return - */ - public fun newTag(generator: (Boolean) -> String): Builder { - newTag = { _: DiffRow.Tag, f: Boolean -> generator.invoke(f) } - return this - } - - /** - * Processor for diffed text parts. Here e.g. white characters could be - * replaced by something visible. - * - * @param processDiffs - * @return - */ - public fun processDiffs(processDiffs: (String) -> String): Builder { - this.processDiffs = processDiffs - return this - } - - /** - * Set the column width of generated lines of original and revised - * texts. - * - * @param width the width to set. Making it < 0 doesn't make any sense. - * Default 80. @return builder with config of column width - */ - public fun columnWidth(width: Int): Builder { - if (width >= 0) { - columnWidth = width - } - return this - } - - /** - * Build the DiffRowGenerator. If some parameters is not set, the - * default values are used. - * - * @return the customized DiffRowGenerator - */ - public fun build(): DiffRowGenerator { - return DiffRowGenerator(this) - } - - /** - * Merge the complete result within the original text. This makes sense - * for one line display. - * - * @param mergeOriginalRevised - * @return - */ - public fun mergeOriginalRevised(mergeOriginalRevised: Boolean): Builder { - this.mergeOriginalRevised = mergeOriginalRevised - return this - } - - /** - * Per default each character is separatly processed. This variant - * introduces processing by word, which does not deliver in word - * changes. Therefore the whole word will be tagged as changed: - * - *
-         * false:    (aBa : aba) --  changed: a(B)a : a(b)a
-         * true:     (aBa : aba) --  changed: (aBa) : (aba)
-        
* - */ - public fun inlineDiffByWord(inlineDiffByWord: Boolean): Builder { - inlineDiffSplitter = if (inlineDiffByWord) SPLITTER_BY_WORD else SPLITTER_BY_CHARACTER - return this - } - - /** - * To provide some customized splitting a splitter can be provided. Here - * someone could think about sentence splitter, comma splitter or stuff - * like that. - * - * @param inlineDiffSplitter - * @return - */ - public fun inlineDiffBySplitter(inlineDiffSplitter: (String) -> List): Builder { - this.inlineDiffSplitter = inlineDiffSplitter - return this - } - - /** - * By default DiffRowGenerator preprocesses lines for HTML output. Tabs - * and special HTML characters like "<" are replaced with its encoded - * value. To change this you can provide a customized line normalizer - * here. - * - * @param lineNormalizer - * @return - */ - public fun lineNormalizer(lineNormalizer: (String) -> String): Builder { - this.lineNormalizer = lineNormalizer - return this - } - - /** - * Provide an equalizer for diff processing. - * - * @param equalizer equalizer for diff processing. - * @return builder with configured equalizer parameter - */ - public fun equalizer(equalizer: (String, String) -> Boolean): Builder { - this.equalizer = equalizer - return this - } - - /** - * Sometimes it happens that a change contains multiple lines. If there - * is no correspondence in old and new. To keep the merged line more - * readable the linefeeds could be replaced by spaces. - * - * @param replace - * @return - */ - public fun replaceOriginalLinefeedInChangesWithSpaces(replace: Boolean): Builder { - replaceOriginalLinefeedInChangesWithSpaces = replace - return this - } - } - public companion object { internal val DEFAULT_EQUALIZER: (Any?, Any?) -> Boolean = { o1: Any?, o2: Any? -> o1 == o2 } internal val IGNORE_WHITESPACE_EQUALIZER: (String, String) -> Boolean = { original: String, revised: String -> @@ -557,10 +409,6 @@ public class DiffRowGenerator private constructor(builder: Builder) { } internal val WHITESPACE_PATTERN = Regex("\\s+") - public fun create(): Builder { - return Builder() - } - private fun adjustWhitespace(raw: String): String { return WHITESPACE_PATTERN.replace(raw.trim(), " ") } @@ -637,26 +485,5 @@ public class DiffRowGenerator private constructor(builder: Builder) { } return sequence } - - - } - - init { - showInlineDiffs = builder.showInlineDiffs - ignoreWhiteSpaces = builder.ignoreWhiteSpaces - oldTag = builder.oldTag - newTag = builder.newTag - columnWidth = builder.columnWidth - mergeOriginalRevised = builder.mergeOriginalRevised - inlineDiffSplitter = builder.inlineDiffSplitter - equalizer = if (builder.equalizer != null) { - builder.equalizer - } else { - if (ignoreWhiteSpaces) IGNORE_WHITESPACE_EQUALIZER else DEFAULT_EQUALIZER - } - reportLinesUnchanged = builder.reportLinesUnchanged - lineNormalizer = builder.lineNormalizer - processDiffs = builder.processDiffs - replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces } } diff --git a/src/commonTest/kotlin/io/github/petertrr/diffutils/DiffUtilsTest.kt b/src/commonTest/kotlin/io/github/petertrr/diffutils/DiffUtilsTest.kt index cb773be..5b3350d 100644 --- a/src/commonTest/kotlin/io/github/petertrr/diffutils/DiffUtilsTest.kt +++ b/src/commonTest/kotlin/io/github/petertrr/diffutils/DiffUtilsTest.kt @@ -18,8 +18,6 @@ */ package io.github.petertrr.diffutils -import io.github.petertrr.diffutils.diff -import io.github.petertrr.diffutils.diffInline import io.github.petertrr.diffutils.patch.ChangeDelta import io.github.petertrr.diffutils.patch.Chunk import io.github.petertrr.diffutils.patch.DeleteDelta @@ -172,7 +170,7 @@ class DiffUtilsTest { assertEquals(Chunk(0, listOf("hhh")), delta.target) delta = patch.deltas[1] assertTrue(delta is InsertDelta) - assertEquals(Chunk(1, emptyList()), delta.source) + assertEquals(Chunk(1, emptyList()), delta.source) assertEquals(Chunk(1, listOf("jjj", "kkk")), delta.target) } diff --git a/src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt b/src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt index b9d5aec..7eccaa5 100644 --- a/src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt +++ b/src/commonTest/kotlin/io/github/petertrr/diffutils/text/DiffRowGeneratorTest.kt @@ -27,9 +27,9 @@ class DiffRowGeneratorTest { fun testGenerator_Default() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator = DiffRowGenerator.create() - .columnWidth(Int.MAX_VALUE) // do not wrap - .build() + val generator = DiffRowGenerator( + columnWidth = Int.MAX_VALUE // do not wrap + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -40,8 +40,7 @@ class DiffRowGeneratorTest { */ @Test fun testNormalize_List() { - val generator: DiffRowGenerator = DiffRowGenerator.create() - .build() + val generator = DiffRowGenerator() assertEquals(listOf(" test"), generator.normalizeLines(listOf("\ttest"))) } @@ -49,9 +48,9 @@ class DiffRowGeneratorTest { fun testGenerator_Default2() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator: DiffRowGenerator = DiffRowGenerator.create() - .columnWidth(0) // do not wrap - .build() + val generator = DiffRowGenerator( + columnWidth = 0 // do not wrap + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -61,10 +60,10 @@ class DiffRowGeneratorTest { fun testGenerator_InlineDiff() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator: DiffRowGenerator = DiffRowGenerator.create() - .showInlineDiffs(true) - .columnWidth(Int.MAX_VALUE) // do not wrap - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + columnWidth = Int.MAX_VALUE // do not wrap + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -75,10 +74,10 @@ class DiffRowGeneratorTest { fun testGenerator_IgnoreWhitespaces() { val first = "anything \n \nother\nmore lines" val second = "anything\n\nother\nsome more lines" - val generator: DiffRowGenerator = DiffRowGenerator.create() - .ignoreWhiteSpaces(true) - .columnWidth(Int.MAX_VALUE) // do not wrap - .build() + val generator = DiffRowGenerator( + ignoreWhiteSpaces = true, + columnWidth = Int.MAX_VALUE // do not wrap + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(4, rows.size) @@ -92,9 +91,9 @@ class DiffRowGeneratorTest { fun testGeneratorWithWordWrap() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator = DiffRowGenerator.create() - .columnWidth(5) - .build() + val generator = DiffRowGenerator( + columnWidth = 5 + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -107,10 +106,10 @@ class DiffRowGeneratorTest { fun testGeneratorWithMerge() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -121,10 +120,10 @@ class DiffRowGeneratorTest { @Test fun testGeneratorWithMerge2() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + ) val rows: List = generator.generateDiffRows(listOf("Test"), listOf("ester")) print(rows) assertEquals(1, rows.size) @@ -135,10 +134,10 @@ class DiffRowGeneratorTest { fun testGeneratorWithMerge3() { val first = "test\nanything \n \nother" val second = "anything\n\nother\ntest\ntest2" - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + ) val rows = generator.generateDiffRows(first.lines(), second.lines()) println(rows) assertEquals(6, rows.size) @@ -152,11 +151,11 @@ class DiffRowGeneratorTest { @Test fun testGeneratorWithMergeByWord4() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .inlineDiffByWord(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + inlineDiffByWord = true, + ) val rows: List = generator.generateDiffRows(listOf("Test"), listOf("ester")) print(rows) assertEquals(1, rows.size) @@ -165,12 +164,12 @@ class DiffRowGeneratorTest { @Test fun testGeneratorWithMergeByWord5() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .inlineDiffByWord(true) - .columnWidth(80) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + inlineDiffByWord = true, + columnWidth = 80 + ) val rows: List = generator.generateDiffRows(listOf("Test feature"), listOf("ester feature best")) print(rows) assertEquals(1, rows.size) @@ -205,13 +204,13 @@ class DiffRowGeneratorTest { @Test fun testGeneratorExample1() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows: List = generator.generateDiffRows( listOf("This is a test senctence."), listOf("This is a test for diffutils.") @@ -223,12 +222,12 @@ class DiffRowGeneratorTest { @Test fun testGeneratorExample2() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows: List = generator.generateDiffRows( listOf("This is a test senctence.", "This is the second line.", "And here is the finish."), listOf("This is a test for diffutils.", "This is the second line.") @@ -247,10 +246,10 @@ class DiffRowGeneratorTest { fun testGeneratorUnchanged() { val first = "anything \n \nother" val second = "anything\n\nother" - val generator = DiffRowGenerator.create() - .columnWidth(5) - .reportLinesUnchanged(true) - .build() + val generator = DiffRowGenerator( + columnWidth = 5, + reportLinesUnchanged = true, + ) val rows: List = generator.generateDiffRows(first.lines(), second.lines()) print(rows) assertEquals(3, rows.size) @@ -261,13 +260,13 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue14() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .inlineDiffBySplitter { line -> DiffRowGenerator.splitStringPreserveDelimiter(line, Regex(",")) } - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + inlineDiffSplitter = { line -> DiffRowGenerator.splitStringPreserveDelimiter(line, Regex(",")) }, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows: List = generator.generateDiffRows( listOf("J. G. Feldstein, Chair"), listOf("T. P. Pastor, Chair") @@ -279,13 +278,13 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue15() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) //show the ~ ~ and ** ** symbols on each difference - .inlineDiffByWord(true) //show the ~ ~ and ** ** around each different word instead of each letter - //.reportLinesUnchanged(true) //experiment - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, //show the ~ ~ and ** ** symbols on each difference + inlineDiffByWord = true, //show the ~ ~ and ** ** around each different word instead of each letter + //reportLinesUnchanged = true) //experiment + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val listOne: List = """ TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, NULLABLE, ACTIONS_C17005, ID, NUMBER, 22, 19, N, @@ -321,12 +320,12 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue22() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val aa = "This is a test senctence." val bb = "This is a test for diffutils.\nThis is the second line." val rows: List = generator.generateDiffRows( @@ -349,12 +348,12 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue22_2() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val aa = "This is a test for diffutils.\nThis is the second line." val bb = "This is a test senctence." val rows: List = generator.generateDiffRows( @@ -372,12 +371,12 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue22_3() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val aa = "This is a test senctence." val bb = "This is a test for diffutils.\nThis is the second line.\nAnd one more." val rows: List = generator.generateDiffRows( @@ -396,17 +395,17 @@ class DiffRowGeneratorTest { @Test fun testGeneratorIssue41DefaultNormalizer() { - val generator = DiffRowGenerator.create() - .build() + val generator = DiffRowGenerator( + ) val rows: List = generator.generateDiffRows(listOf("<"), listOf("<")) assertEquals(DiffRow(DiffRow.Tag.EQUAL, "<", "<"), rows.single()) } @Test fun testGeneratorIssue41UserNormalizer() { - val generator = DiffRowGenerator.create() - .lineNormalizer { str -> str.replace("\t", " ") } - .build() + val generator = DiffRowGenerator( + lineNormalizer = { str -> str.replace("\t", " ") } + ) var rows: List = generator.generateDiffRows(listOf("<"), listOf("<")) assertEquals(DiffRow(DiffRow.Tag.EQUAL, "<", "<"), rows.single()) rows = generator.generateDiffRows(listOf("\t<"), listOf("<")) @@ -415,26 +414,26 @@ class DiffRowGeneratorTest { @Test fun testGenerationIssue44reportLinesUnchangedProblem() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .reportLinesUnchanged(true) - .oldTag { _ -> "~~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + reportLinesUnchanged = true, + oldTag = { _, _ -> "~~" }, + newTag = { _, _ -> "**" }, + ) val rows: List = generator.generateDiffRows(listOf("
To do
"), listOf("
Done
")) assertEquals(DiffRow(DiffRow.Tag.CHANGE, "
~~T~~o~~ do~~
", "
**D**o**ne**
"), rows.single()) } @Test fun testIgnoreWhitespaceIssue66() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .ignoreWhiteSpaces(true) - .mergeOriginalRevised(true) - .oldTag { _ -> "~" } //introduce markdown style for strikethrough - .newTag { _ -> "**" } //introduce markdown style for bold - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + ignoreWhiteSpaces = true, + mergeOriginalRevised = true, + oldTag = { _, _ -> "~" }, //introduce markdown style for strikethrough, + newTag = { _, _ -> "**" } //introduce markdown style for bold, + ) //compute the differences for two test texts. //CHECKSTYLE:OFF @@ -448,14 +447,14 @@ class DiffRowGeneratorTest { @Test fun testIgnoreWhitespaceIssue66_2() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .ignoreWhiteSpaces(true) - .mergeOriginalRevised(true) - .oldTag { _ -> "~" } //introduce markdown style for strikethrough - .newTag { _ -> "**" } //introduce markdown style for bold - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + ignoreWhiteSpaces = true, + mergeOriginalRevised = true, + oldTag = { _, _ -> "~" }, //introduce markdown style for strikethrough, + newTag = { _, _ -> "**" } //introduce markdown style for bold, + ) //compute the differences for two test texts. val rows: List = generator.generateDiffRows( @@ -467,14 +466,14 @@ class DiffRowGeneratorTest { @Test fun testIgnoreWhitespaceIssue64() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .ignoreWhiteSpaces(true) - .mergeOriginalRevised(true) - .oldTag { _ -> "~" } //introduce markdown style for strikethrough - .newTag { _ -> "**" } //introduce markdown style for bold - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + ignoreWhiteSpaces = true, + mergeOriginalRevised = true, + oldTag = { _, _ -> "~" }, //introduce markdown style for strikethrough, + newTag = { _, _ -> "**" } //introduce markdown style for bold, + ) //compute the differences for two test texts. val rows: List = generator.generateDiffRows( @@ -496,14 +495,14 @@ class DiffRowGeneratorTest { @Test fun testReplaceDiffsIssue63() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .mergeOriginalRevised(true) - .oldTag { _ -> "~" } //introduce markdown style for strikethrough - .newTag { _ -> "**" } //introduce markdown style for bold - .processDiffs { str -> str.replace(" ", "/") } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + mergeOriginalRevised = true, + oldTag = { _, _ -> "~" }, //introduce markdown style for strikethrough, + newTag = { _, _ -> "**" }, //introduce markdown style for bold, + processDiffs = { str -> str.replace(" ", "/") }, + ) //compute the differences for two test texts. val rows: List = generator.generateDiffRows( @@ -515,15 +514,15 @@ class DiffRowGeneratorTest { @Test fun testProblemTooManyDiffRowsIssue65() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .reportLinesUnchanged(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .mergeOriginalRevised(true) - .inlineDiffByWord(false) - .replaceOriginalLinefeedInChangesWithSpaces(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + reportLinesUnchanged = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + mergeOriginalRevised = true, + inlineDiffByWord = false, + replaceOriginalLinefeedInChangesWithSpaces = true + ) val diffRows: List = generator.generateDiffRows( listOf("Ich möchte nicht mit einem Bot sprechen.", "Ich soll das schon wieder wiederholen?"), listOf("Ich möchte nicht mehr mit dir sprechen. Leite mich weiter.", "Kannst du mich zum Kundendienst weiterleiten?") @@ -534,14 +533,14 @@ class DiffRowGeneratorTest { @Test fun testProblemTooManyDiffRowsIssue65_NoMerge() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .reportLinesUnchanged(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .mergeOriginalRevised(false) - .inlineDiffByWord(false) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + reportLinesUnchanged = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + mergeOriginalRevised = false, + inlineDiffByWord = false, + ) val diffRows: List = generator.generateDiffRows( listOf("Ich möchte nicht mit einem Bot sprechen.", "Ich soll das schon wieder wiederholen?"), listOf("Ich möchte nicht mehr mit dir sprechen. Leite mich weiter.", "Kannst du mich zum Kundendienst weiterleiten?") @@ -552,14 +551,14 @@ class DiffRowGeneratorTest { @Test fun testProblemTooManyDiffRowsIssue65_DiffByWord() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .reportLinesUnchanged(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .mergeOriginalRevised(true) - .inlineDiffByWord(true) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + reportLinesUnchanged = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + mergeOriginalRevised = true, + inlineDiffByWord = true, + ) val diffRows: List = generator.generateDiffRows( listOf("Ich möchte nicht mit einem Bot sprechen.", "Ich soll das schon wieder wiederholen?"), listOf("Ich möchte nicht mehr mit dir sprechen. Leite mich weiter.", "Kannst du mich zum Kundendienst weiterleiten?") @@ -570,14 +569,14 @@ class DiffRowGeneratorTest { @Test fun testProblemTooManyDiffRowsIssue65_NoInlineDiff() { - val generator = DiffRowGenerator.create() - .showInlineDiffs(false) - .reportLinesUnchanged(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .mergeOriginalRevised(true) - .inlineDiffByWord(false) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = false, + reportLinesUnchanged = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + mergeOriginalRevised = true, + inlineDiffByWord = false, + ) val diffRows: List = generator.generateDiffRows( listOf("Ich möchte nicht mit einem Bot sprechen.", "Ich soll das schon wieder wiederholen?"), listOf("Ich möchte nicht mehr mit dir sprechen. Leite mich weiter.", "Kannst du mich zum Kundendienst weiterleiten?") @@ -598,11 +597,11 @@ Bengal tiger panther but singapura but bombay munchkin for cougar.""".split("\n" Russian blue leopard. Lion. Tabby scottish folded for russian blue, so savannah yettie? lynx. Tomcat singapura, cheetah. Bengal tiger panther but singapura but bombay munchkin for cougar. And more.""".split("\n").toTypedArray() ) - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .ignoreWhiteSpaces(true) - .columnWidth(100) - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + ignoreWhiteSpaces = true, + columnWidth = 100, + ) val deltas = generator.generateDiffRows(original, revised) println(deltas) } @@ -628,13 +627,13 @@ Bengal tiger panther but singapura but bombay munchkin for cougar. And more.""". 2020-04-04T17:00:00.000Z,S,HHD_MAY20,Close,,,,,,,,,,,,,,,,,,,,,,,, 2020-04-04T17:00:00.000Z,S,FHK_C23.5_MAY20,Close,,,,,,,,,,,,,,,,,,,,,,,, """.trimIndent() - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .mergeOriginalRevised(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + mergeOriginalRevised = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows: List = generator.generateDiffRows( listOf(*original.split("\n").toTypedArray()), listOf(*revised.split("\n").toTypedArray()) @@ -648,12 +647,12 @@ Bengal tiger panther but singapura but bombay munchkin for cougar. And more.""". fun testCorrectChangeIssue114() { val original: List = listOf("A", "B", "C", "D", "E") val revised: List = listOf("a", "C", "", "E") - val generator = DiffRowGenerator.create() - .showInlineDiffs(false) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = false, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows = generator.generateDiffRows(original, revised) for (diff in rows) { println(diff) @@ -668,12 +667,12 @@ Bengal tiger panther but singapura but bombay munchkin for cougar. And more.""". fun testCorrectChangeIssue114_2() { val original: List = listOf("A", "B", "C", "D", "E") val revised: List = listOf("a", "C", "", "E") - val generator = DiffRowGenerator.create() - .showInlineDiffs(true) - .inlineDiffByWord(true) - .oldTag { _ -> "~" } - .newTag { _ -> "**" } - .build() + val generator = DiffRowGenerator( + showInlineDiffs = true, + inlineDiffByWord = true, + oldTag = { _, _ -> "~" }, + newTag = { _, _ -> "**" }, + ) val rows = generator.generateDiffRows(original, revised) for (diff in rows) { println(diff)