add bunch of tests for relative positions
This commit is contained in:
parent
4af04d6a29
commit
6f074a873d
@ -3,17 +3,9 @@ import * as Y from '../src/internals'
|
|||||||
import * as t from 'lib0/testing.js'
|
import * as t from 'lib0/testing.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {t.TestCase} tc
|
* @param {Y.YText} ytext
|
||||||
*/
|
*/
|
||||||
export const testRelativePosition = tc => {
|
const checkRelativePositions = ytext => {
|
||||||
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
|
// test if all positions are encoded and restored correctly
|
||||||
for (let i = 0; i < ytext.length; i++) {
|
for (let i = 0; i < ytext.length; i++) {
|
||||||
// for all types of associations..
|
// for all types of associations..
|
||||||
@ -21,9 +13,92 @@ export const testRelativePosition = tc => {
|
|||||||
const rpos = Y.createRelativePositionFromTypeIndex(ytext, i, assoc)
|
const rpos = Y.createRelativePositionFromTypeIndex(ytext, i, assoc)
|
||||||
const encodedRpos = Y.encodeRelativePosition(rpos)
|
const encodedRpos = Y.encodeRelativePosition(rpos)
|
||||||
const decodedRpos = Y.decodeRelativePosition(encodedRpos)
|
const decodedRpos = Y.decodeRelativePosition(encodedRpos)
|
||||||
const absPos = /** @type {Y.AbsolutePosition} */ (Y.createAbsolutePositionFromRelativePosition(decodedRpos, ydoc))
|
const absPos = /** @type {Y.AbsolutePosition} */ (Y.createAbsolutePositionFromRelativePosition(decodedRpos, /** @type {Y.Doc} */ (ytext.doc)))
|
||||||
t.assert(absPos.index === i)
|
t.assert(absPos.index === i)
|
||||||
t.assert(absPos.assoc === assoc)
|
t.assert(absPos.assoc === assoc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase1 = 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')
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase2 = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
ytext.insert(0, 'abc')
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase3 = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
ytext.insert(0, 'abc')
|
||||||
|
ytext.insert(0, '1')
|
||||||
|
ytext.insert(0, 'xyz')
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase4 = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
ytext.insert(0, '1')
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase5 = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
ytext.insert(0, '2')
|
||||||
|
ytext.insert(0, '1')
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionCase6 = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
checkRelativePositions(ytext)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {t.TestCase} tc
|
||||||
|
*/
|
||||||
|
export const testRelativePositionAssociationDifference = tc => {
|
||||||
|
const ydoc = new Y.Doc()
|
||||||
|
const ytext = ydoc.getText()
|
||||||
|
ytext.insert(0, '2')
|
||||||
|
ytext.insert(0, '1')
|
||||||
|
const rposRight = Y.createRelativePositionFromTypeIndex(ytext, 1, 0)
|
||||||
|
const rposLeft = Y.createRelativePositionFromTypeIndex(ytext, 1, -1)
|
||||||
|
ytext.insert(1, 'x')
|
||||||
|
const posRight = Y.createAbsolutePositionFromRelativePosition(rposRight, ydoc)
|
||||||
|
const posLeft = Y.createAbsolutePositionFromRelativePosition(rposLeft, ydoc)
|
||||||
|
t.assert(posRight != null && posRight.index === 2)
|
||||||
|
t.assert(posLeft != null && posLeft.index === 1)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user