Skip to content

Commit 29048f9

Browse files
committed
easysync tests: move to separate files
1 parent ef391f4 commit 29048f9

File tree

9 files changed

+984
-924
lines changed

9 files changed

+984
-924
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const Changeset = require('../../../static/js/Changeset');
4+
const expect = require('../../../tests/frontend/lib/expect');
5+
6+
describe('easysync', function () {
7+
it('opAssembler', async function () {
8+
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
9+
const iter = Changeset.opIterator(x);
10+
const assem = Changeset.opAssembler();
11+
while (iter.hasNext()) assem.append(iter.next());
12+
expect(assem.toString()).to.equal(x);
13+
});
14+
15+
it('smartOpAssembler', async function () {
16+
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
17+
const iter = Changeset.opIterator(x);
18+
const assem = Changeset.smartOpAssembler();
19+
while (iter.hasNext()) assem.append(iter.next());
20+
assem.endDocument();
21+
expect(assem.toString()).to.equal(x);
22+
});
23+
24+
describe('append atext to assembler', function () {
25+
const testAppendATextToAssembler = (testId, atext, correctOps) => {
26+
it(`testAppendATextToAssembler#${testId}`, async function () {
27+
const assem = Changeset.smartOpAssembler();
28+
Changeset.appendATextToAssembler(atext, assem);
29+
expect(assem.toString()).to.equal(correctOps);
30+
});
31+
};
32+
33+
testAppendATextToAssembler(1, {
34+
text: '\n',
35+
attribs: '|1+1',
36+
}, '');
37+
testAppendATextToAssembler(2, {
38+
text: '\n\n',
39+
attribs: '|2+2',
40+
}, '|1+1');
41+
testAppendATextToAssembler(3, {
42+
text: '\n\n',
43+
attribs: '*x|2+2',
44+
}, '*x|1+1');
45+
testAppendATextToAssembler(4, {
46+
text: '\n\n',
47+
attribs: '*x|1+1|1+1',
48+
}, '*x|1+1');
49+
testAppendATextToAssembler(5, {
50+
text: 'foo\n',
51+
attribs: '|1+4',
52+
}, '+3');
53+
testAppendATextToAssembler(6, {
54+
text: '\nfoo\n',
55+
attribs: '|2+5',
56+
}, '|1+1+3');
57+
testAppendATextToAssembler(7, {
58+
text: '\nfoo\n',
59+
attribs: '*x|2+5',
60+
}, '*x|1+1*x+3');
61+
testAppendATextToAssembler(8, {
62+
text: '\n\n\nfoo\n',
63+
attribs: '|2+2*x|2+5',
64+
}, '|2+2*x|1+1*x+3'); //TODO: why isn't this |2+2*x|1+4 ?
65+
});
66+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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('compose', function () {
10+
const testCompose = (randomSeed) => {
11+
it(`testCompose#${randomSeed}`, async function () {
12+
const p = new AttributePool();
13+
14+
const startText = `${randomMultiline(10, 20)}\n`;
15+
16+
const x1 = randomTestChangeset(startText);
17+
const change1 = x1[0];
18+
const text1 = x1[1];
19+
20+
const x2 = randomTestChangeset(text1);
21+
const change2 = x2[0];
22+
const text2 = x2[1];
23+
24+
const x3 = randomTestChangeset(text2);
25+
const change3 = x3[0];
26+
const text3 = x3[1];
27+
28+
const change12 = Changeset.checkRep(Changeset.compose(change1, change2, p));
29+
const change23 = Changeset.checkRep(Changeset.compose(change2, change3, p));
30+
const change123 = Changeset.checkRep(Changeset.compose(change12, change3, p));
31+
const change123a = Changeset.checkRep(Changeset.compose(change1, change23, p));
32+
expect(change123a).to.equal(change123);
33+
34+
expect(Changeset.applyToText(change12, startText)).to.equal(text2);
35+
expect(Changeset.applyToText(change23, text1)).to.equal(text3);
36+
expect(Changeset.applyToText(change123, startText)).to.equal(text3);
37+
});
38+
};
39+
40+
for (let i = 0; i < 30; i++) testCompose(i);
41+
});
42+
43+
describe('compose attributes', function () {
44+
it('simpleComposeAttributesTest', async function () {
45+
const p = new AttributePool();
46+
p.putAttrib(['bold', '']);
47+
p.putAttrib(['bold', 'true']);
48+
const cs1 = Changeset.checkRep('Z:2>1*1+1*1=1$x');
49+
const cs2 = Changeset.checkRep('Z:3>0*0|1=3$');
50+
const cs12 = Changeset.checkRep(Changeset.compose(cs1, cs2, p));
51+
expect(cs12).to.equal('Z:2>1+1*0|1=2$x');
52+
});
53+
});
54+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)