Fix streamDiff tests (#796)
* fix: make streamDiff tests to pass * add missing lline
This commit is contained in:
parent
e8c76c7df7
commit
9ba2e76705
3 changed files with 53 additions and 12 deletions
|
|
@ -13,14 +13,15 @@ export async function* streamDiff(
|
|||
oldLines: string[],
|
||||
newLines: LineStream
|
||||
): AsyncGenerator<DiffLine> {
|
||||
oldLines = [...oldLines]; // be careful
|
||||
const mutatedOldLines = [...oldLines]; // be careful
|
||||
let seenIndentationMistake = false;
|
||||
|
||||
let newLineResult = await newLines.next();
|
||||
const postponedLines: string[] = [];
|
||||
while (oldLines.length > 0 && !newLineResult.done) {
|
||||
const [matchIndex, isPerfectMatch, newLine] = matchLine(
|
||||
newLineResult.value,
|
||||
oldLines,
|
||||
mutatedOldLines,
|
||||
seenIndentationMistake
|
||||
);
|
||||
if (!seenIndentationMistake && newLineResult.value !== newLine) {
|
||||
|
|
@ -29,20 +30,25 @@ export async function* streamDiff(
|
|||
|
||||
if (matchIndex < 0) {
|
||||
// Insert new line
|
||||
yield { type: "new", line: newLine };
|
||||
postponedLines.push(newLine);
|
||||
} else {
|
||||
// Insert all deleted lines before match
|
||||
for (let i = 0; i < matchIndex; i++) {
|
||||
yield { type: "old", line: oldLines.shift()! };
|
||||
yield { type: "old", line: mutatedOldLines.shift()! };
|
||||
}
|
||||
if (postponedLines.length > 0) {
|
||||
for (let i = 0; i <= postponedLines.length; i++) {
|
||||
yield { type: "new", line: postponedLines.shift()! };
|
||||
}
|
||||
}
|
||||
|
||||
if (isPerfectMatch) {
|
||||
// Same
|
||||
yield { type: "same", line: oldLines.shift()! };
|
||||
yield { type: "same", line: mutatedOldLines.shift()! };
|
||||
} else {
|
||||
// Delete old line and insert the new
|
||||
yield { type: "old", line: oldLines.shift()! };
|
||||
yield { type: "new", line: newLine };
|
||||
yield { type: "old", line: mutatedOldLines.shift()! };
|
||||
postponedLines.push(newLine);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -50,16 +56,22 @@ export async function* streamDiff(
|
|||
}
|
||||
|
||||
// Once at the edge, only one choice
|
||||
if (newLineResult.done === true && oldLines.length > 0) {
|
||||
if (newLineResult.done === true && mutatedOldLines.length > 0) {
|
||||
for (let oldLine of oldLines) {
|
||||
yield { type: "old", line: oldLine };
|
||||
}
|
||||
}
|
||||
|
||||
if (!newLineResult.done && oldLines.length === 0) {
|
||||
if (!newLineResult.done && mutatedOldLines.length === 0) {
|
||||
yield { type: "new", line: newLineResult.value };
|
||||
for await (const newLine of newLines) {
|
||||
yield { type: "new", line: newLine };
|
||||
}
|
||||
}
|
||||
|
||||
if (postponedLines.length > 0) {
|
||||
for await (const newLine of postponedLines) {
|
||||
yield { type: "new", line: newLine };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,8 +41,8 @@ export function matchLine(
|
|||
} else if (linesMatch(newLine, oldLines[i])) {
|
||||
// This is a way to fix indentation, but only for sufficiently long lines to avoid matching whitespace or short lines
|
||||
if (
|
||||
newLine.trimStart() === oldLines[i].trimStart() &&
|
||||
(permissiveAboutIndentation || newLine.trim().length > 8)
|
||||
newLine.trimStart() === oldLines[i].trimStart() // &&
|
||||
// (permissiveAboutIndentation) || newLine.trim().length > 8)
|
||||
) {
|
||||
return [i, true, oldLines[i]];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,18 @@
|
|||
import { streamDiff } from "../diff/streamDiff";
|
||||
|
||||
const oldCode = [
|
||||
`A
|
||||
B`,
|
||||
`A
|
||||
B
|
||||
C`,
|
||||
`A
|
||||
B
|
||||
C
|
||||
A
|
||||
B
|
||||
B
|
||||
A`,
|
||||
`function mergeSortAlgorithm() {
|
||||
// TODO: implement
|
||||
}`,
|
||||
|
|
@ -10,6 +22,19 @@ const oldCode = [
|
|||
];
|
||||
|
||||
const newCode = [
|
||||
`C
|
||||
D`,
|
||||
`D
|
||||
E
|
||||
C
|
||||
F
|
||||
C`,
|
||||
`C
|
||||
B
|
||||
A
|
||||
B
|
||||
A
|
||||
C`,
|
||||
`function mergeSortAlgorithm(arr: number[]): number[] {
|
||||
if (arr.length <= 1) {
|
||||
return arr;
|
||||
|
|
@ -113,7 +138,11 @@ describe("streamDiff", () => {
|
|||
// Check that there are no red lines immediately following green (they should always be above)
|
||||
for (let i = 1; i < diff.length; i++) {
|
||||
if (diff[i].type === "old" && diff[i - 1].type === "new") {
|
||||
throw new Error("Found red immediately after green line");
|
||||
throw new Error(
|
||||
`Found red '${diff[i].line}' immediately after green line '${
|
||||
diff[i - 1].line
|
||||
}`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue