|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Changeset = require('../../../static/js/Changeset'); |
| 4 | +const AttributePool = require('../../../static/js/AttributePool'); |
| 5 | +const expect = require('../../../tests/frontend/lib/expect'); |
| 6 | +const {randomMultiline, randomTestChangeset} = require('./easysync-helper.js'); |
| 7 | + |
| 8 | +describe('easysync', function () { |
| 9 | + describe('follow & compose', function () { |
| 10 | + const testFollow = (randomSeed) => { |
| 11 | + it(`testFollow#${randomSeed}`, async function () { |
| 12 | + const p = new AttributePool(); |
| 13 | + |
| 14 | + const startText = `${randomMultiline(10, 20)}\n`; |
| 15 | + |
| 16 | + const cs1 = randomTestChangeset(startText)[0]; |
| 17 | + const cs2 = randomTestChangeset(startText)[0]; |
| 18 | + |
| 19 | + const afb = Changeset.checkRep(Changeset.follow(cs1, cs2, false, p)); |
| 20 | + const bfa = Changeset.checkRep(Changeset.follow(cs2, cs1, true, p)); |
| 21 | + |
| 22 | + const merge1 = Changeset.checkRep(Changeset.compose(cs1, afb)); |
| 23 | + const merge2 = Changeset.checkRep(Changeset.compose(cs2, bfa)); |
| 24 | + |
| 25 | + expect(merge2).to.equal(merge1); |
| 26 | + }); |
| 27 | + }; |
| 28 | + for (let i = 0; i < 30; i++) testFollow(i); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('followAttributes & composeAttributes', function () { |
| 32 | + const p = new AttributePool(); |
| 33 | + p.putAttrib(['x', '']); |
| 34 | + p.putAttrib(['x', 'abc']); |
| 35 | + p.putAttrib(['x', 'def']); |
| 36 | + p.putAttrib(['y', '']); |
| 37 | + p.putAttrib(['y', 'abc']); |
| 38 | + p.putAttrib(['y', 'def']); |
| 39 | + let n = 0; |
| 40 | + |
| 41 | + const testFollow = (a, b, afb, bfa, merge) => { |
| 42 | + it(`manual #${++n}`, async function () { |
| 43 | + expect(Changeset.followAttributes(a, b, p)).to.equal(afb); |
| 44 | + expect(Changeset.followAttributes(b, a, p)).to.equal(bfa); |
| 45 | + expect(Changeset.composeAttributes(a, afb, true, p)).to.equal(merge); |
| 46 | + expect(Changeset.composeAttributes(b, bfa, true, p)).to.equal(merge); |
| 47 | + }); |
| 48 | + }; |
| 49 | + |
| 50 | + testFollow('', '', '', '', ''); |
| 51 | + testFollow('*0', '', '', '*0', '*0'); |
| 52 | + testFollow('*0', '*0', '', '', '*0'); |
| 53 | + testFollow('*0', '*1', '', '*0', '*0'); |
| 54 | + testFollow('*1', '*2', '', '*1', '*1'); |
| 55 | + testFollow('*0*1', '', '', '*0*1', '*0*1'); |
| 56 | + testFollow('*0*4', '*2*3', '*3', '*0', '*0*3'); |
| 57 | + testFollow('*0*4', '*2', '', '*0*4', '*0*4'); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('chracterRangeFollow', function () { |
| 61 | + const testCharacterRangeFollow = (testId, cs, oldRange, insertionsAfter, correctNewRange) => { |
| 62 | + it(`testCharacterRangeFollow#${testId}`, async function () { |
| 63 | + cs = Changeset.checkRep(cs); |
| 64 | + expect(Changeset.characterRangeFollow(cs, oldRange[0], oldRange[1], insertionsAfter)) |
| 65 | + .to.eql(correctNewRange); |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + testCharacterRangeFollow(1, 'Z:z>9*0=1=4-3+9=1|1-4-4+1*0+a$123456789abcdefghijk', |
| 70 | + [7, 10], false, [14, 15]); |
| 71 | + testCharacterRangeFollow(2, 'Z:bc<6|x=b4|2-6$', [400, 407], false, [400, 401]); |
| 72 | + testCharacterRangeFollow(3, 'Z:4>0-3+3$abc', [0, 3], false, [3, 3]); |
| 73 | + testCharacterRangeFollow(4, 'Z:4>0-3+3$abc', [0, 3], true, [0, 0]); |
| 74 | + testCharacterRangeFollow(5, 'Z:5>1+1=1-3+3$abcd', [1, 4], false, [5, 5]); |
| 75 | + testCharacterRangeFollow(6, 'Z:5>1+1=1-3+3$abcd', [1, 4], true, [2, 2]); |
| 76 | + testCharacterRangeFollow(7, 'Z:5>1+1=1-3+3$abcd', [0, 6], false, [1, 7]); |
| 77 | + testCharacterRangeFollow(8, 'Z:5>1+1=1-3+3$abcd', [0, 3], false, [1, 2]); |
| 78 | + testCharacterRangeFollow(9, 'Z:5>1+1=1-3+3$abcd', [2, 5], false, [5, 6]); |
| 79 | + testCharacterRangeFollow(10, 'Z:2>1+1$a', [0, 0], false, [1, 1]); |
| 80 | + testCharacterRangeFollow(11, 'Z:2>1+1$a', [0, 0], true, [0, 0]); |
| 81 | + }); |
| 82 | +}); |
0 commit comments