element-web/test/editor/diff-test.ts

138 lines
5.5 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
2021-06-29 12:11:58 +00:00
import { diffDeletion, diffAtCaret } from "../../src/editor/diff";
2022-12-12 11:24:14 +00:00
describe("editor/diff", function () {
describe("diffDeletion", function () {
describe("with a single character removed", function () {
it("at start of string", function () {
const diff = diffDeletion("hello", "ello");
expect(diff.at).toBe(0);
expect(diff.removed).toBe("h");
});
2022-12-12 11:24:14 +00:00
it("in middle of string", function () {
const diff = diffDeletion("hello", "hllo");
expect(diff.at).toBe(1);
expect(diff.removed).toBe("e");
});
2022-12-12 11:24:14 +00:00
it("in middle of string with duplicate character", function () {
const diff = diffDeletion("hello", "helo");
expect(diff.at).toBe(3);
expect(diff.removed).toBe("l");
});
2022-12-12 11:24:14 +00:00
it("at end of string", function () {
const diff = diffDeletion("hello", "hell");
expect(diff.at).toBe(4);
expect(diff.removed).toBe("o");
});
});
2022-12-12 11:24:14 +00:00
describe("with a multiple removed", function () {
it("at start of string", function () {
const diff = diffDeletion("hello", "llo");
expect(diff.at).toBe(0);
expect(diff.removed).toBe("he");
});
2022-12-12 11:24:14 +00:00
it("removing whole string", function () {
const diff = diffDeletion("hello", "");
expect(diff.at).toBe(0);
expect(diff.removed).toBe("hello");
});
2022-12-12 11:24:14 +00:00
it("in middle of string", function () {
const diff = diffDeletion("hello", "hlo");
expect(diff.at).toBe(1);
expect(diff.removed).toBe("el");
});
2022-12-12 11:24:14 +00:00
it("in middle of string with duplicate character", function () {
const diff = diffDeletion("hello", "heo");
expect(diff.at).toBe(2);
expect(diff.removed).toBe("ll");
});
2022-12-12 11:24:14 +00:00
it("at end of string", function () {
const diff = diffDeletion("hello", "hel");
expect(diff.at).toBe(3);
expect(diff.removed).toBe("lo");
});
});
});
2022-12-12 11:24:14 +00:00
describe("diffAtCaret", function () {
it("insert at start", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("world", "hello world", 6);
expect(diff.at).toBe(0);
expect(diff.added).toBe("hello ");
expect(diff.removed).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("insert at end", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "hello world", 11);
expect(diff.at).toBe(5);
expect(diff.added).toBe(" world");
expect(diff.removed).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("insert in middle", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello world", "hello cruel world", 12);
expect(diff.at).toBe(6);
expect(diff.added).toBe("cruel ");
expect(diff.removed).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("replace at start", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("morning, world!", "afternoon, world!", 9);
expect(diff.at).toBe(0);
expect(diff.removed).toBe("morning");
expect(diff.added).toBe("afternoon");
});
2022-12-12 11:24:14 +00:00
it("replace at end", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("morning, world!", "morning, mars?", 14);
expect(diff.at).toBe(9);
expect(diff.removed).toBe("world!");
expect(diff.added).toBe("mars?");
});
2022-12-12 11:24:14 +00:00
it("replace in middle", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("morning, blue planet", "morning, red planet", 12);
expect(diff.at).toBe(9);
expect(diff.removed).toBe("blue");
expect(diff.added).toBe("red");
});
2022-12-12 11:24:14 +00:00
it("remove at start of string", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "ello", 0);
expect(diff.at).toBe(0);
expect(diff.removed).toBe("h");
expect(diff.added).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("removing whole string", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "", 0);
expect(diff.at).toBe(0);
expect(diff.removed).toBe("hello");
expect(diff.added).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("remove in middle of string", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "hllo", 1);
expect(diff.at).toBe(1);
expect(diff.removed).toBe("e");
expect(diff.added).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("forwards remove in middle of string", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "hell", 4);
expect(diff.at).toBe(4);
expect(diff.removed).toBe("o");
expect(diff.added).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("forwards remove in middle of string with duplicate character", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "helo", 3);
expect(diff.at).toBe(3);
expect(diff.removed).toBe("l");
expect(diff.added).toBeFalsy();
});
2022-12-12 11:24:14 +00:00
it("remove at end of string", function () {
2019-07-24 15:26:18 +00:00
const diff = diffAtCaret("hello", "hell", 4);
expect(diff.at).toBe(4);
expect(diff.removed).toBe("o");
expect(diff.added).toBeFalsy();
});
});
});