added all y-map tests

This commit is contained in:
Kevin Jahns 2019-03-11 12:31:37 +01:00
parent 3a7411f9e8
commit da9836fe59
3 changed files with 127 additions and 132 deletions

View File

@ -353,7 +353,7 @@ export const compare = users => {
export const applyRandomTests = (tc, mods, iterations) => { export const applyRandomTests = (tc, mods, iterations) => {
const gen = tc.prng const gen = tc.prng
const result = init({ users: 5, prng: gen }) const result = init(tc, { users: 5 })
const { testConnector, users } = result const { testConnector, users } = result
for (var i = 0; i < iterations; i++) { for (var i = 0; i < iterations; i++) {
if (prng.int31(gen, 0, 100) <= 2) { if (prng.int31(gen, 0, 100) <= 2) {

View File

@ -224,8 +224,8 @@ export const testIteratingArrayContainingTypes = tc => {
let _uniqueNumber = 0 let _uniqueNumber = 0
const getUniqueNumber = () => _uniqueNumber++ const getUniqueNumber = () => _uniqueNumber++
let arrayTransactions = [ const arrayTransactions = [
function insert (t, user, gen) { function insert (tc, user, gen) {
const yarray = user.define('array', Y.Array) const yarray = user.define('array', Y.Array)
var uniqueNumber = getUniqueNumber() var uniqueNumber = getUniqueNumber()
var content = [] var content = []
@ -236,14 +236,14 @@ let arrayTransactions = [
var pos = prng.int31(gen, 0, yarray.length) var pos = prng.int31(gen, 0, yarray.length)
yarray.insert(pos, content) yarray.insert(pos, content)
}, },
function insertTypeArray (t, user, gen) { function insertTypeArray (tc, user, gen) {
const yarray = user.define('array', Y.Array) const yarray = user.define('array', Y.Array)
var pos = prng.int31(gen, 0, yarray.length) var pos = prng.int31(gen, 0, yarray.length)
yarray.insert(pos, [Y.Array]) yarray.insert(pos, [Y.Array])
var array2 = yarray.get(pos) var array2 = yarray.get(pos)
array2.insert(0, [1, 2, 3, 4]) array2.insert(0, [1, 2, 3, 4])
}, },
function insertTypeMap (t, user, gen) { function insertTypeMap (tc, user, gen) {
const yarray = user.define('array', Y.Array) const yarray = user.define('array', Y.Array)
var pos = prng.int31(gen, 0, yarray.length) var pos = prng.int31(gen, 0, yarray.length)
yarray.insert(pos, [Y.Map]) yarray.insert(pos, [Y.Map])
@ -252,7 +252,7 @@ let arrayTransactions = [
map.set('someprop', 43) map.set('someprop', 43)
map.set('someprop', 44) map.set('someprop', 44)
}, },
function _delete (t, user, gen) { function _delete (tc, user, gen) {
const yarray = user.define('array', Y.Array) const yarray = user.define('array', Y.Array)
var length = yarray.length var length = yarray.length
if (length > 0) { if (length > 0) {

View File

@ -1,6 +1,7 @@
import { init, compare } from './testHelper.js' import { init, compare, applyRandomTests } from './testHelper.js'
import * as Y from '../src/index.js' import * as Y from '../src/index.js'
import * as t from 'lib0/testing.js' import * as t from 'lib0/testing.js'
import * as prng from 'lib0/prng.js'
export const testBasicMapTests = tc => { export const testBasicMapTests = tc => {
const { testConnector, users, map0, map1, map2 } = init(tc, { users: 3 }) const { testConnector, users, map0, map1, map2 } = init(tc, { users: 3 })
@ -67,52 +68,38 @@ export const testYmapSetsYmap = tc => {
export const testYmapSetsYarray = tc => { export const testYmapSetsYarray = tc => {
const { users, map0 } = init(tc, { users: 2 }) const { users, map0 } = init(tc, { users: 2 })
const array = map0.set('Array', new Y.Array()) const array = map0.set('Array', new Y.Array())
t.assert(array === map0.get('Array'))
array.insert(0, [1, 2, 3]) array.insert(0, [1, 2, 3])
t.compare(map0.toJSON(), { Array: [1, 2, 3] }) t.compare(map0.toJSON(), { Array: [1, 2, 3] })
compare(users) compare(users)
} }
/* export const testGetAndSetOfMapPropertySyncs = tc => {
const { testConnector, users, map0 } = init(tc, { users: 2 })
test('Map can set custom types (Array)', async function map4 (t) {
let { users, map0 } = await initArrays(t, { users: 2 })
var array = map0.set('Array', new Y.Array())
array.insert(0, [1, 2, 3])
array = map0.get('Array')
t.compare(array.toArray(), [1, 2, 3])
await compareUsers(t, users)
})
test('Basic get&set of Map property (converge via update)', async function map5 (t) {
let { testConnector, users, map0 } = await initArrays(t, { users: 2 })
map0.set('stuff', 'stuffy') map0.set('stuff', 'stuffy')
t.compare(map0.get('stuff'), 'stuffy') t.compare(map0.get('stuff'), 'stuffy')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.define('map', Y.Map)
t.compare(u.get('stuff'), 'stuffy') t.compare(u.get('stuff'), 'stuffy')
} }
await compareUsers(t, users) compare(users)
}) }
test('Basic get&set of Map property (handle conflict)', async function map6 (t) { export const testGetAndSetOfMapPropertyWithConflict = tc => {
let { testConnector, users, map0, map1 } = await initArrays(t, { users: 3 }) const { testConnector, users, map0, map1 } = init(tc, { users: 3 })
map0.set('stuff', 'c0') map0.set('stuff', 'c0')
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
testConnector.flushAllMessages() testConnector.flushAllMessages()
for (let user of users) { for (let user of users) {
var u = user.define('map', Y.Map) var u = user.define('map', Y.Map)
t.compare(u.get('stuff'), 'c0') t.compare(u.get('stuff'), 'c0')
} }
await compareUsers(t, users) compare(users)
}) }
test('Basic get&set&delete of Map property (handle conflict)', async function map7 (t) { export const testGetAndSetAndDeleteOfMapProperty = tc => {
let { testConnector, users, map0, map1 } = await initArrays(t, { users: 3 }) const { testConnector, users, map0, map1 } = init(tc, { users: 3 })
map0.set('stuff', 'c0') map0.set('stuff', 'c0')
map0.delete('stuff') map0.delete('stuff')
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
@ -121,11 +108,11 @@ test('Basic get&set&delete of Map property (handle conflict)', async function ma
var u = user.define('map', Y.Map) var u = user.define('map', Y.Map)
t.assert(u.get('stuff') === undefined) t.assert(u.get('stuff') === undefined)
} }
await compareUsers(t, users) compare(users)
}) }
test('Basic get&set of Map property (handle three conflicts)', async function map8 (t) { export const testGetAndSetOfMapPropertyWithThreeConflicts = tc => {
let { testConnector, users, map0, map1, map2 } = await initArrays(t, { users: 3 }) const { testConnector, users, map0, map1, map2 } = init(tc, { users: 3 })
map0.set('stuff', 'c0') map0.set('stuff', 'c0')
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
map1.set('stuff', 'c2') map1.set('stuff', 'c2')
@ -135,11 +122,11 @@ test('Basic get&set of Map property (handle three conflicts)', async function ma
var u = user.define('map', Y.Map) var u = user.define('map', Y.Map)
t.compare(u.get('stuff'), 'c0') t.compare(u.get('stuff'), 'c0')
} }
await compareUsers(t, users) compare(users)
}) }
test('Basic get&set&delete of Map property (handle three conflicts)', async function map9 (t) { export const testGetAndSetAndDeleteOfMapPropertyWithThreeConflicts = tc => {
let { testConnector, users, map0, map1, map2, map3 } = await initArrays(t, { users: 4 }) const { testConnector, users, map0, map1, map2, map3 } = init(tc, { users: 4 })
map0.set('stuff', 'c0') map0.set('stuff', 'c0')
map1.set('stuff', 'c1') map1.set('stuff', 'c1')
map1.set('stuff', 'c2') map1.set('stuff', 'c2')
@ -155,12 +142,11 @@ test('Basic get&set&delete of Map property (handle three conflicts)', async func
var u = user.define('map', Y.Map) var u = user.define('map', Y.Map)
t.assert(u.get('stuff') === undefined) t.assert(u.get('stuff') === undefined)
} }
await compareUsers(t, users) compare(users)
}) }
/* TODO reimplement observePath export const testObservepathProperties = tc => {
test('observePath properties', async function map10 (t) { const { users, map0, map1, map2, testConnector } = init(tc, { users: 3 })
let { users, map0, map1, map2 } = await initArrays(t, { users: 3 })
let map let map
map0.observePath(['map'], map => { map0.observePath(['map'], map => {
if (map != null) { if (map != null) {
@ -171,14 +157,14 @@ test('observePath properties', async function map10 (t) {
testConnector.flushAllMessages() testConnector.flushAllMessages()
map = map2.get('map') map = map2.get('map')
t.compare(map.get('yay'), 4) t.compare(map.get('yay'), 4)
await compareUsers(t, users) compare(users)
}) }
test('observe deep properties', async function map11 (t) { export const testObserveDeepProperties = tc => {
let { testConnector, users, map1, map2, map3 } = await initArrays(t, { users: 4 }) const { testConnector, users, map1, map2, map3 } = init(tc, { users: 4 })
var _map1 = map1.set('map', new Y.Map()) const _map1 = map1.set('map', new Y.Map())
var calls = 0 let calls = 0
var dmapid let dmapid
map1.observeDeep(events => { map1.observeDeep(events => {
events.forEach(event => { events.forEach(event => {
calls++ calls++
@ -189,26 +175,26 @@ test('observe deep properties', async function map11 (t) {
}) })
}) })
testConnector.flushAllMessages() testConnector.flushAllMessages()
var _map3 = map3.get('map') const _map3 = map3.get('map')
_map3.set('deepmap', new Y.Map()) _map3.set('deepmap', new Y.Map())
testConnector.flushAllMessages() testConnector.flushAllMessages()
var _map2 = map2.get('map') const _map2 = map2.get('map')
_map2.set('deepmap', new Y.Map()) _map2.set('deepmap', new Y.Map())
testConnector.flushAllMessages() testConnector.flushAllMessages()
var dmap1 = _map1.get('deepmap') const dmap1 = _map1.get('deepmap')
var dmap2 = _map2.get('deepmap') const dmap2 = _map2.get('deepmap')
var dmap3 = _map3.get('deepmap') const dmap3 = _map3.get('deepmap')
t.assert(calls > 0) t.assert(calls > 0)
t.assert(dmap1._id.equals(dmap2._id)) t.assert(dmap1._id.equals(dmap2._id))
t.assert(dmap1._id.equals(dmap3._id)) t.assert(dmap1._id.equals(dmap3._id))
t.assert(dmap1._id.equals(dmapid)) t.assert(dmap1._id.equals(dmapid))
await compareUsers(t, users) compare(users)
}) }
test('observes using observeDeep', async function map12 (t) { export const testObserversUsingObservedeep = tc => {
let { users, map0 } = await initArrays(t, { users: 2 }) const { users, map0 } = init(tc, { users: 2 })
var pathes = [] const pathes = []
var calls = 0 let calls = 0
map0.observeDeep(events => { map0.observeDeep(events => {
events.forEach(event => { events.forEach(event => {
pathes.push(event.path) pathes.push(event.path)
@ -220,20 +206,19 @@ test('observes using observeDeep', async function map12 (t) {
map0.get('map').get('array').insert(0, ['content']) map0.get('map').get('array').insert(0, ['content'])
t.assert(calls === 3) t.assert(calls === 3)
t.compare(pathes, [[], ['map'], ['map', 'array']]) t.compare(pathes, [[], ['map'], ['map', 'array']])
await compareUsers(t, users) compare(users)
}) }
TODO: Test events in Y.Map // TODO: Test events in Y.Map
function compareEvent (t, is, should) { const compareEvent = (t, is, should) => {
for (var key in should) { for (var key in should) {
t.assert(should[key] === is[key]) t.assert(should[key] === is[key])
} }
} }
test('throws add & update & delete events (with type and primitive content)', async function map13 (t) { export const testThrowsAddAndUpdateAndDeleteEvents = tc => {
let { users, map0 } = await initArrays(t, { users: 2 }) const { users, map0 } = init(tc, { users: 2 })
var event let event
await flushAll(t, users)
map0.observe(e => { map0.observe(e => {
event = e // just put it on event, should be thrown synchronously anyway event = e // just put it on event, should be thrown synchronously anyway
}) })
@ -251,10 +236,10 @@ test('throws add & update & delete events (with type and primitive content)', as
name: 'stuff', name: 'stuff',
oldValue: 4 oldValue: 4
}) })
var replacedArray = map0.get('stuff') let replacedArray = map0.get('stuff')
// update, oldValue is in opContents // update, oldValue is in opContents
map0.set('stuff', 5) map0.set('stuff', 5)
var array = event.oldValue let array = event.oldValue
t.compare(array._model, replacedArray._model) t.compare(array._model, replacedArray._model)
// delete // delete
map0.delete('stuff') map0.delete('stuff')
@ -264,45 +249,41 @@ test('throws add & update & delete events (with type and primitive content)', as
object: map0, object: map0,
oldValue: 5 oldValue: 5
}) })
await compareUsers(t, users) compare(users)
}) }
*/
/* reimplement event.value somehow (probably with ss vector) export const testYmapEventHasCorrectValueWhenSettingAPrimitive = tc => {
test('event has correct value when setting a primitive on a YMap (same user)', async function map14 (t) { const { users, map0 } = init(tc, { users: 3 })
let { users, map0 } = await initArrays(t, { users: 3 }) let event
var event
await flushAll(t, users)
map0.observe(e => { map0.observe(e => {
event = e event = e
}) })
map0.set('stuff', 2) map0.set('stuff', 2)
t.compare(event.value, event.target.get(event.name)) t.compare(event.value, event.target.get(event.name))
await compareUsers(t, users) compare(users)
}) }
test('event has correct value when setting a primitive on a YMap (received from another user)', async function map15 (t) { export const testYmapEventHasCorrectValueWhenSettingAPrimitiveFromOtherUser = tc => {
let { users, map0, map1 } = await initArrays(t, { users: 3 }) const { users, map0, map1, testConnector } = init(tc, { users: 3 })
var event let event
await flushAll(t, users)
map0.observe(e => { map0.observe(e => {
event = e event = e
}) })
map1.set('stuff', 2) map1.set('stuff', 2)
await flushAll(t, users) testConnector.flushAllMessages()
t.compare(event.value, event.target.get(event.name)) t.compare(event.value, event.target.get(event.name))
await compareUsers(t, users) compare(users)
}) }
var mapTransactions = [ const mapTransactions = [
function set (t, user, prng) { function set (tc, user, gen) {
let key = random.oneOf(prng, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
var value = random.utf16String(prng) var value = prng.utf16String(gen)
user.define('map', Y.Map).set(key, value) user.define('map', Y.Map).set(key, value)
}, },
function setType (t, user, prng) { function setType (tc, user, gen) {
let key = random.oneOf(prng, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
var type = random.oneOf(prng, [new Y.Array(), new Y.Map()]) var type = prng.oneOf(gen, [new Y.Array(), new Y.Map()])
user.define('map', Y.Map).set(key, type) user.define('map', Y.Map).set(key, type)
if (type instanceof Y.Array) { if (type instanceof Y.Array) {
type.insert(0, [1, 2, 3, 4]) type.insert(0, [1, 2, 3, 4])
@ -310,53 +291,67 @@ var mapTransactions = [
type.set('deepkey', 'deepvalue') type.set('deepkey', 'deepvalue')
} }
}, },
function _delete (t, user, prng) { function _delete (tc, user, gen) {
let key = random.oneOf(prng, ['one', 'two']) let key = prng.oneOf(gen, ['one', 'two'])
user.define('map', Y.Map).delete(key) user.define('map', Y.Map).delete(key)
} }
] ]
test('y-map: Random tests (42)', async function randomMap42 (t) { export const testRepeatGeneratingYmapTests20 = tc => {
await applyRandomTests(t, mapTransactions, 42) applyRandomTests(tc, mapTransactions, 20)
}) }
test('y-map: Random tests (43)', async function randomMap43 (t) { export const testRepeatGeneratingYmapTests40 = tc => {
await applyRandomTests(t, mapTransactions, 43) applyRandomTests(tc, mapTransactions, 40)
}) }
test('y-map: Random tests (44)', async function randomMap44 (t) { export const testRepeatGeneratingYmapTests42 = tc => {
await applyRandomTests(t, mapTransactions, 44) applyRandomTests(tc, mapTransactions, 42)
}) }
test('y-map: Random tests (45)', async function randomMap45 (t) { export const testRepeatGeneratingYmapTests43 = tc => {
await applyRandomTests(t, mapTransactions, 45) applyRandomTests(tc, mapTransactions, 43)
}) }
test('y-map: Random tests (46)', async function randomMap46 (t) { export const testRepeatGeneratingYmapTests44 = tc => {
await applyRandomTests(t, mapTransactions, 46) applyRandomTests(tc, mapTransactions, 44)
}) }
test('y-map: Random tests (47)', async function randomMap47 (t) { export const testRepeatGeneratingYmapTests45 = tc => {
await applyRandomTests(t, mapTransactions, 47) applyRandomTests(tc, mapTransactions, 45)
}) }
test('y-map: Random tests (200)', async function randomMap200 (t) { export const testRepeatGeneratingYmapTests46 = tc => {
await applyRandomTests(t, mapTransactions, 200) applyRandomTests(tc, mapTransactions, 46)
}) }
test('y-map: Random tests (300)', async function randomMap300 (t) { export const testRepeatGeneratingYmapTests300 = tc => {
await applyRandomTests(t, mapTransactions, 300) applyRandomTests(tc, mapTransactions, 300)
}) }
test('y-map: Random tests (500)', async function randomMap500 (t) { /* TODO: implement something like difficutly in lib0
await applyRandomTests(t, mapTransactions, 500)
})
test('y-map: Random tests (1000)', async function randomMap1000 (t) { export const testRepeatGeneratingYmapTests400 = tc => {
await applyRandomTests(t, mapTransactions, 1000) applyRandomTests(tc, mapTransactions, 400)
}) }
test('y-map: Random tests (1800)', async function randomMap1800 (t) { export const testRepeatGeneratingYmapTests500 = tc => {
await applyRandomTests(t, mapTransactions, 1800) applyRandomTests(tc, mapTransactions, 500)
}) }
export const testRepeatGeneratingYmapTests600 = tc => {
applyRandomTests(tc, mapTransactions, 600)
}
export const testRepeatGeneratingYmapTests1000 = tc => {
applyRandomTests(tc, mapTransactions, 1000)
}
export const testRepeatGeneratingYmapTests1800 = tc => {
applyRandomTests(tc, mapTransactions, 1800)
}
export const testRepeatGeneratingYmapTests10000 = tc => {
applyRandomTests(tc, mapTransactions, 10000)
}
*/ */