| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { diffLines, diffStats } from "./diff.ts"; |
| 5 | |
| 6 | test("a line diff keeps what stayed and marks what changed", () => { |
| 7 | const lines = diffLines("# Title\n\nold line\nkept\n", "# Title\n\nnew line\nkept\nadded\n"); |
| 8 | assert.deepEqual(lines, [ |
| 9 | { op: "same", text: "# Title" }, |
| 10 | { op: "same", text: "" }, |
| 11 | { op: "del", text: "old line" }, |
| 12 | { op: "add", text: "new line" }, |
| 13 | { op: "same", text: "kept" }, |
| 14 | { op: "add", text: "added" }, |
| 15 | ]); |
| 16 | assert.deepEqual(diffStats(lines), { added: 2, removed: 1 }); |
| 17 | }); |
| 18 | |
| 19 | test("from nothing, everything is added", () => { |
| 20 | assert.deepEqual(diffLines("", "a\nb"), [ |
| 21 | { op: "add", text: "a" }, |
| 22 | { op: "add", text: "b" }, |
| 23 | ]); |
| 24 | }); |