From 32b734b24d43919224dec978117760219f34229a Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Fri, 8 Jan 2021 23:03:44 +0100 Subject: [PATCH] add tests --- tests/relativePositions.tests.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/relativePositions.tests.js diff --git a/tests/relativePositions.tests.js b/tests/relativePositions.tests.js new file mode 100644 index 00000000..55f27ab1 --- /dev/null +++ b/tests/relativePositions.tests.js @@ -0,0 +1,29 @@ + +import * as Y from '../src/internals' +import * as t from 'lib0/testing.js' + +/** + * @param {t.TestCase} tc + */ +export const testRelativePosition = tc => { + const ydoc = new Y.Doc() + const ytext = ydoc.getText() + ytext.insert(0, '1') + ytext.insert(0, 'abc') + ytext.insert(0, 'z') + ytext.insert(0, 'y') + ytext.insert(0, 'x') + + // test if all positions are encoded and restored correctly + for (let i = 0; i < ytext.length; i++) { + // for all types of associations.. + for (let assoc = -1; assoc < 2; assoc++) { + const rpos = Y.createRelativePositionFromTypeIndex(ytext, i, assoc) + const encodedRpos = Y.encodeRelativePosition(rpos) + const decodedRpos = Y.decodeRelativePosition(encodedRpos) + const absPos = /** @type {Y.AbsolutePosition} */ (Y.createAbsolutePositionFromRelativePosition(decodedRpos, ydoc)) + t.assert(absPos.index === i) + t.assert(absPos.assoc === assoc) + } + } +}