Skip to content

Commit 8cdd474

Browse files
webzwo0irhansen
authored andcommitted
easysync tests: move to separate files
1 parent 8b73b91 commit 8cdd474

File tree

9 files changed

+978
-924
lines changed

9 files changed

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

0 commit comments

Comments
 (0)