|
| 1 | +import { AtomicOperation, StringOperation } from "./operation"; |
| 2 | + |
| 3 | +describe("String transformation functions", () => { |
| 4 | + |
| 5 | + const s = "abcde"; |
| 6 | + |
| 7 | + const apply = (o: AtomicOperation, s: string): string => { |
| 8 | + const strOp = o as StringOperation; |
| 9 | + const start = Math.min(s.length, strOp.start); |
| 10 | + const end = strOp.newValue == "" ? strOp.end : start; |
| 11 | + return s.substring(0, start) + strOp.newValue + s.substring(end); |
| 12 | + }; |
| 13 | + |
| 14 | + describe("insert insert", () => { |
| 15 | + test("non overlap", () => { |
| 16 | + const o1 = new StringOperation(1, 2, "1"); |
| 17 | + const o2 = new StringOperation(2, 3, "2"); |
| 18 | + expect(apply(o1, s)).toBe("a1bcde"); |
| 19 | + expect(apply(o2, s)).toBe("ab2cde"); |
| 20 | + |
| 21 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 22 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 23 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("a1b2cde"); |
| 24 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("a1b2cde"); |
| 25 | + }); |
| 26 | + |
| 27 | + test("same location", () => { |
| 28 | + const o1 = new StringOperation(1, 2, "2"); |
| 29 | + const o2 = new StringOperation(1, 2, "1"); |
| 30 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 31 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 32 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("a12bcde"); |
| 33 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("a12bcde"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("overlap", () => { |
| 37 | + const o1 = new StringOperation(1, 3, "11"); |
| 38 | + const o2 = new StringOperation(2, 4, "22"); |
| 39 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 40 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 41 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("a11b22cde"); |
| 42 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("a11b22cde"); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("insert delete", () => { |
| 47 | + test("non overlap", () => { |
| 48 | + const o1 = new StringOperation(1, 2, "1"); |
| 49 | + const o2 = new StringOperation(2, 3, ""); |
| 50 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 51 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 52 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("a1bde"); |
| 53 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("a1bde"); |
| 54 | + }); |
| 55 | + |
| 56 | + test("same location", () => { |
| 57 | + const o1 = new StringOperation(1, 2, "1"); |
| 58 | + const o2 = new StringOperation(0, 1, ""); |
| 59 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 60 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 61 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("1bcde"); |
| 62 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("1bcde"); |
| 63 | + }); |
| 64 | + |
| 65 | + test("overlap insert before delete", () => { |
| 66 | + const o1 = new StringOperation(1, 3, "12"); |
| 67 | + const o2 = new StringOperation(0, 1, ""); |
| 68 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 69 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 70 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("12bcde"); |
| 71 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("12bcde"); |
| 72 | + }); |
| 73 | + |
| 74 | + test("overlap delete before insert", () => { |
| 75 | + const o1 = new StringOperation(2, 3, "1"); |
| 76 | + const o2 = new StringOperation(0, 3, ""); |
| 77 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 78 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 79 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("1de"); |
| 80 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("1de"); |
| 81 | + }); |
| 82 | + |
| 83 | + test("overlap less insert than delete", () => { |
| 84 | + const o1 = new StringOperation(0, 1, "1"); |
| 85 | + const o2 = new StringOperation(0, 5, ""); |
| 86 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 87 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 88 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("1"); |
| 89 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("1"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("overlap more insert than delete", () => { |
| 93 | + const o1 = new StringOperation(0, 5, "11111"); |
| 94 | + const o2 = new StringOperation(0, 1, ""); |
| 95 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 96 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 97 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("11111bcde"); |
| 98 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("11111bcde"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("delete delete", () => { |
| 103 | + test("non overlap", () => { |
| 104 | + const o1 = new StringOperation(1, 2, ""); |
| 105 | + const o2 = new StringOperation(2, 3, ""); |
| 106 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 107 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 108 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("ade"); |
| 109 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("ade"); |
| 110 | + }); |
| 111 | + |
| 112 | + test("same operations", () => { |
| 113 | + const o1 = new StringOperation(1, 2, ""); |
| 114 | + const o2 = new StringOperation(1, 2, ""); |
| 115 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 116 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 117 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("acde"); |
| 118 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("acde"); |
| 119 | + }); |
| 120 | + |
| 121 | + test("overlap", () => { |
| 122 | + const o1 = new StringOperation(1, 3, ""); |
| 123 | + const o2 = new StringOperation(2, 3, ""); |
| 124 | + const [o1_t1, o2_t1] = o1.transformAgainst(o2); |
| 125 | + const [o1_t2, o2_t2] = o2.transformAgainst(o1); |
| 126 | + expect(apply(o1_t1, apply(o2_t1, s))).toBe("ade"); |
| 127 | + expect(apply(o2_t2, apply(o1_t2, s))).toBe("ade"); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments