From 419c8001679d167e9165c07d3432a612a03ca838 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 24 Jul 2019 17:26:18 +0200 Subject: [PATCH] add tests for diffAtCaret --- src/editor/diff.js | 7 ++++ test/editor/diff-test.js | 76 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/editor/diff.js b/src/editor/diff.js index 3b7b2aa79d..b6839b1a88 100644 --- a/src/editor/diff.js +++ b/src/editor/diff.js @@ -52,6 +52,13 @@ export function diffDeletion(oldStr, newStr) { return {at: firstDiffIdx, removed: oldStr.substr(firstDiffIdx, amount)}; } +/** + * Calculates which string was added and removed around the caret position + * @param {String} oldValue the previous value + * @param {String} newValue the new value + * @param {Number} caretPosition the position of the caret after `newValue` was applied. + * @return {object} + */ export function diffAtCaret(oldValue, newValue, caretPosition) { const diffLen = newValue.length - oldValue.length; const caretPositionBeforeInput = caretPosition - diffLen; diff --git a/test/editor/diff-test.js b/test/editor/diff-test.js index 257c35c22f..966e4db6cc 100644 --- a/test/editor/diff-test.js +++ b/test/editor/diff-test.js @@ -15,7 +15,7 @@ limitations under the License. */ import expect from 'expect'; -import {diffDeletion} from "../../src/editor/diff"; +import {diffDeletion, diffAtCaret} from "../../src/editor/diff"; describe('editor/diff', function() { describe('diffDeletion', function() { @@ -45,4 +45,78 @@ describe('editor/diff', function() { expect(diff.removed).toBe("o"); }); }); + describe('diffAtCaret', function() { + it('insert at start', function() { + const diff = diffAtCaret("world", "hello world", 6); + expect(diff.at).toBe(0); + expect(diff.added).toBe("hello "); + expect(diff.removed).toBeFalsy(); + }); + it('insert at end', function() { + const diff = diffAtCaret("hello", "hello world", 11); + expect(diff.at).toBe(5); + expect(diff.added).toBe(" world"); + expect(diff.removed).toBeFalsy(); + }); + it('insert in middle', function() { + const diff = diffAtCaret("hello world", "hello cruel world", 12); + expect(diff.at).toBe(6); + expect(diff.added).toBe("cruel "); + expect(diff.removed).toBeFalsy(); + }); + it('replace at start', function() { + const diff = diffAtCaret("morning, world!", "afternoon, world!", 9); + expect(diff.at).toBe(0); + expect(diff.removed).toBe("morning"); + expect(diff.added).toBe("afternoon"); + }); + it('replace at end', function() { + const diff = diffAtCaret("morning, world!", "morning, mars?", 14); + expect(diff.at).toBe(9); + expect(diff.removed).toBe("world!"); + expect(diff.added).toBe("mars?"); + }); + it('replace in middle', function() { + 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"); + }); + it('remove at start of string', function() { + const diff = diffAtCaret("hello", "ello", 0); + expect(diff.at).toBe(0); + expect(diff.removed).toBe("h"); + expect(diff.added).toBeFalsy(); + }); + it('removing whole string', function() { + const diff = diffAtCaret("hello", "", 0); + expect(diff.at).toBe(0); + expect(diff.removed).toBe("hello"); + expect(diff.added).toBeFalsy(); + }); + it('remove in middle of string', function() { + const diff = diffAtCaret("hello", "hllo", 1); + expect(diff.at).toBe(1); + expect(diff.removed).toBe("e"); + expect(diff.added).toBeFalsy(); + }); + it('forwards remove in middle of string', function() { + const diff = diffAtCaret("hello", "hell", 4); + expect(diff.at).toBe(4); + expect(diff.removed).toBe("o"); + expect(diff.added).toBeFalsy(); + }); + it('forwards remove in middle of string with duplicate character', function() { + const diff = diffAtCaret("hello", "helo", 3); + expect(diff.at).toBe(3); + expect(diff.removed).toBe("l"); + expect(diff.added).toBeFalsy(); + }); + it('remove at end of string', function() { + const diff = diffAtCaret("hello", "hell", 4); + expect(diff.at).toBe(4); + expect(diff.removed).toBe("o"); + expect(diff.added).toBeFalsy(); + }); + }); });