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

fix(assert): fix swapping of multiline str diff #3685

Merged
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions assert/_diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,21 @@ export function diffstr(A: string, B: string) {
}

// Compute word-diff
const aLines = added.length < removed.length ? added : removed;
const bLines = aLines === removed ? added : removed;
const hasMoreRemovedLines = added.length < removed.length;
const aLines = hasMoreRemovedLines ? added : removed;
const bLines = hasMoreRemovedLines ? removed : added;
for (const a of aLines) {
let tokens = [] as Array<DiffResult<string>>,
b: undefined | DiffResult<string>;
// Search another diff line with at least one common token
while (bLines.length) {
b = bLines.shift();
tokens = diff(
const tokenized = [
tokenize(a.value, { wordDiff: true }),
tokenize(b?.value ?? "", { wordDiff: true }),
);
] as string[][];
if (hasMoreRemovedLines) tokenized.reverse();
tokens = diff(tokenized[0], tokenized[1]);
if (
tokens.some(({ type, value }) =>
type === DiffType.common && value.trim().length
Expand Down
29 changes: 29 additions & 0 deletions assert/_diff_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,32 @@ Deno.test({
]);
},
});

Deno.test({
name: "multiline diff with more removed lines",
fn() {
const diffResult = diffstr("a\na", "e");
assertEquals(diffResult, [
{
type: DiffType.removed,
value: "a\\n\n",
},
{
type: DiffType.removed,
value: "a\n",
details: [
{ type: DiffType.removed, value: "a" },
{ type: DiffType.common, value: "\n" },
],
},
{
type: DiffType.added,
value: "e\n",
details: [
{ type: DiffType.added, value: "e" },
{ type: DiffType.common, value: "\n" },
],
},
]);
},
});