fix: handle non-line diff modes and add tests for +/- notation
This commit is contained in:
parent
7a3982e43d
commit
d7a9578b93
@ -60,6 +60,7 @@ class Diff extends Operation {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Show +/- notation",
|
"name": "Show +/- notation",
|
||||||
|
"hint": "Works only in Line diff mode.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"value": false
|
"value": false
|
||||||
}
|
}
|
||||||
@ -92,6 +93,34 @@ class Diff extends Operation {
|
|||||||
throw new OperationError("Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?");
|
throw new OperationError("Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// +/- notation only works properly with Line diff mode
|
||||||
|
// For other modes, it falls back to HTML format and this option is ignored (see hint).
|
||||||
|
const useNotation = showNotation && diffBy === "Line";
|
||||||
|
|
||||||
|
if (useNotation) {
|
||||||
|
const patch = jsdiff.createTwoFilesPatch("original", "modified", samples[0], samples[1], "", "", {
|
||||||
|
context: Infinity,
|
||||||
|
ignoreWhitespace: ignoreWhitespace,
|
||||||
|
});
|
||||||
|
|
||||||
|
const lines = patch.split("\n");
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const line = lines[i];
|
||||||
|
if (line.startsWith("@@")) {
|
||||||
|
if (showSubtraction) output += line + "\n";
|
||||||
|
} else if (line.startsWith("+") && !line.startsWith("+++")) {
|
||||||
|
if (showAdded) output += line + "\n";
|
||||||
|
} else if (line.startsWith("-") && !line.startsWith("---")) {
|
||||||
|
if (showRemoved) output += line + "\n";
|
||||||
|
} else if (line.startsWith("===") || line.startsWith("---") || line.startsWith("+++") || line.startsWith("\\")) {
|
||||||
|
continue;
|
||||||
|
} else if (line.length > 0 && !showSubtraction) {
|
||||||
|
output += line.substring(1) + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
switch (diffBy) {
|
switch (diffBy) {
|
||||||
case "Character":
|
case "Character":
|
||||||
diff = jsdiff.diffChars(samples[0], samples[1]);
|
diff = jsdiff.diffChars(samples[0], samples[1]);
|
||||||
@ -124,23 +153,12 @@ class Diff extends Operation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < diff.length; i++) {
|
for (let i = 0; i < diff.length; i++) {
|
||||||
if (showNotation) {
|
if (diff[i].added) {
|
||||||
const escapedValue = Utils.escapeHtml(diff[i].value);
|
if (showAdded) output += "<ins>" + Utils.escapeHtml(diff[i].value) + "</ins>";
|
||||||
if (diff[i].added) {
|
} else if (diff[i].removed) {
|
||||||
if (showAdded) output += "+ " + escapedValue;
|
if (showRemoved) output += "<del>" + Utils.escapeHtml(diff[i].value) + "</del>";
|
||||||
} else if (diff[i].removed) {
|
} else if (!showSubtraction) {
|
||||||
if (showRemoved) output += "- " + escapedValue;
|
output += Utils.escapeHtml(diff[i].value);
|
||||||
} else if (!showSubtraction) {
|
|
||||||
output += " " + escapedValue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (diff[i].added) {
|
|
||||||
if (showAdded) output += "<ins>" + Utils.escapeHtml(diff[i].value) + "</ins>";
|
|
||||||
} else if (diff[i].removed) {
|
|
||||||
if (showRemoved) output += "<del>" + Utils.escapeHtml(diff[i].value) + "</del>";
|
|
||||||
} else if (!showSubtraction) {
|
|
||||||
output += Utils.escapeHtml(diff[i].value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
313
tests/operations/tests/Diff.mjs
Normal file
313
tests/operations/tests/Diff.mjs
Normal file
@ -0,0 +1,313 @@
|
|||||||
|
/**
|
||||||
|
* @author mikecat
|
||||||
|
* @copyright Crown Copyright 2023
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation - true (Line mode)",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2modified\nline3",
|
||||||
|
"expectedOutput": "line1\n-line2\n+line2modified\nline3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation - false (Line mode)",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2modified\nline3",
|
||||||
|
"expectedOutput": "line1\n<del>line2\n</del><ins>line2modified\n</ins>line3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation - with showAdded false (Line mode)",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2modified\nline3",
|
||||||
|
"expectedOutput": "line1\n-line2\nline3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation - with showRemoved false (Line mode)",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2modified\nline3",
|
||||||
|
"expectedOutput": "line1\n+line2modified\nline3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation - with showSubtraction true (Line mode)",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2modified\nline3",
|
||||||
|
"expectedOutput": "@@ -1,3 +1,3 @@\n-line2\n+line2modified",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation ignored for Character mode",
|
||||||
|
"input": "abc\ndef",
|
||||||
|
"expectedOutput": "<del>abc</del><ins>def</ins>",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Character",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation ignored for Word mode",
|
||||||
|
"input": "hello world\nhello cruel world",
|
||||||
|
"expectedOutput": "hello <ins>cruel </ins>world",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Word",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Show +/- notation ignored for Sentence mode",
|
||||||
|
"input": "Hello world.\nHello there.",
|
||||||
|
"expectedOutput": "<del>Hello world.</del><ins>Hello there.</ins>",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Sentence",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Character mode basic",
|
||||||
|
"input": "abc\nabc",
|
||||||
|
"expectedOutput": "abc",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Character",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Character mode with changes",
|
||||||
|
"input": "abc\nabcdef",
|
||||||
|
"expectedOutput": "abc<ins>def</ins>",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Character",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Word mode basic",
|
||||||
|
"input": "hello world\nhello world",
|
||||||
|
"expectedOutput": "hello world",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Word",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Word mode with changes",
|
||||||
|
"input": "hello world\nhello cruel world",
|
||||||
|
"expectedOutput": "hello <ins>cruel </ins>world",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Word",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Line mode basic",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline2\nline3",
|
||||||
|
"expectedOutput": "line1\nline2\nline3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Line mode with changes",
|
||||||
|
"input": "line1\nline2\nline3\n\nline1\nline4\nline3",
|
||||||
|
"expectedOutput": "line1\n<del>line2\n</del><ins>line4\n</ins>line3",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n\\n",
|
||||||
|
"Line",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Sentence mode basic",
|
||||||
|
"input": "Hello world.\nHello world.",
|
||||||
|
"expectedOutput": "Hello world.",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Sentence",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Diff: Sentence mode with changes",
|
||||||
|
"input": "Hello world.\nHello there.",
|
||||||
|
"expectedOutput": "<del>Hello world.</del><ins>Hello there.</ins>",
|
||||||
|
"recipeConfig": [
|
||||||
|
{
|
||||||
|
"op": "Diff",
|
||||||
|
"args": [
|
||||||
|
"\\n",
|
||||||
|
"Sentence",
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user