continue-custom/core/diff/myers.test.ts
Owen W. Taylor 698633815f reapplyWithMyersDiff(): don't skip trailing blank lines
myersDiff inherits the jsdiff behavior for trailing \n, so would
consider "foo\n" => "foo" a changed single line, rather than a change
from "foo", "" to "foo" - this would result in a trailing blank line
in the highlighted region silently vanishing from the buffer.

Adding \n to the inputs so that lines are \n terminated rather than
separated fixes this.

To make myersDiff logically consistent, pass ignoreNewlineAtEof: true
to jsdiff. (This is actually not needed for this bug fix.)
2025-02-25 15:36:40 -05:00

44 lines
1 KiB
TypeScript

import { dedent } from "../util";
import { myersDiff } from "./myers";
describe("Test myers diff function", () => {
test("should ...", () => {
const linesA = dedent`
A
B
C
D
E
`;
const linesB = dedent`
A
B
C'
D'
E
`;
const diffLines = myersDiff(linesA, linesB);
expect(diffLines).toEqual([
{ type: "same", line: "A" },
{ type: "same", line: "B" },
{ type: "old", line: "C" },
{ type: "old", line: "D" },
{ type: "new", line: "C'" },
{ type: "new", line: "D'" },
{ type: "same", line: "E" },
]);
});
test("should ignore newline differences at end", () => {
const linesA = "A\nB\nC\n";
const linesB = "A\nB\nC";
const diffLines = myersDiff(linesA, linesB);
expect(diffLines).toEqual([
{ type: "same", line: "A" },
{ type: "same", line: "B" },
{ type: "same", line: "C" },
]);
});
});