pad.libre-service.eu-etherpad/src/node/easysync_tests.js

982 lines
32 KiB
JavaScript
Raw Normal View History

2021-01-21 22:06:52 +01:00
'use strict';
2011-03-26 14:10:41 +01:00
/**
2021-01-21 22:06:52 +01:00
* I found this tests in the old Etherpad and used it to test if the Changeset library can be run on
* node.js. It has no use for ep-lite, but I thought I keep it cause it may help someone to
* understand the Changeset library
2011-05-30 16:53:11 +02:00
* https://github.com/ether/pad/blob/master/infrastructure/ace/www/easysync2_tests.js
*/
/*
* Copyright 2009 Google Inc., 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
2011-03-26 14:10:41 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2012-02-28 21:19:10 +01:00
2021-01-21 22:06:52 +01:00
const Changeset = require('../static/js/Changeset');
const AttributePool = require('../static/js/AttributePool');
2011-03-26 14:10:41 +01:00
function random() {
2021-01-21 22:06:52 +01:00
this.nextInt = (maxValue) => Math.floor(Math.random() * maxValue);
this.nextDouble = (maxValue) => Math.random();
2011-03-26 14:10:41 +01:00
}
2021-01-21 22:06:52 +01:00
const runTests = () => {
const print = (str) => {
2011-03-26 14:10:41 +01:00
console.log(str);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const assert = (code, optMsg) => {
if (!eval(code)) throw new Error(`FALSE: ${optMsg || code}`); /* eslint-disable-line no-eval */
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const literal = (v) => {
2020-11-23 19:24:19 +01:00
if ((typeof v) === 'string') {
return `"${v.replace(/[\\"]/g, '\\$1').replace(/\n/g, '\\n')}"`;
2020-11-23 19:24:19 +01:00
} else { return JSON.stringify(v); }
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const assertEqualArrays = (a, b) => {
2020-11-23 19:24:19 +01:00
assert(`JSON.stringify(${literal(a)}) == JSON.stringify(${literal(b)})`);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const assertEqualStrings = (a, b) => {
2020-11-23 19:24:19 +01:00
assert(`${literal(a)} == ${literal(b)}`);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const throughIterator = (opsStr) => {
2020-11-23 19:24:19 +01:00
const iter = Changeset.opIterator(opsStr);
const assem = Changeset.opAssembler();
2011-03-26 14:10:41 +01:00
while (iter.hasNext()) {
assem.append(iter.next());
}
return assem.toString();
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const throughSmartAssembler = (opsStr) => {
2020-11-23 19:24:19 +01:00
const iter = Changeset.opIterator(opsStr);
const assem = Changeset.smartOpAssembler();
2011-03-26 14:10:41 +01:00
while (iter.hasNext()) {
assem.append(iter.next());
}
assem.endDocument();
return assem.toString();
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
(() => {
2020-11-23 19:24:19 +01:00
print('> throughIterator');
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
assert(`throughIterator(${literal(x)}) == ${literal(x)}`);
2011-03-26 14:10:41 +01:00
})();
2021-01-21 22:06:52 +01:00
(() => {
2020-11-23 19:24:19 +01:00
print('> throughSmartAssembler');
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
assert(`throughSmartAssembler(${literal(x)}) == ${literal(x)}`);
2011-03-26 14:10:41 +01:00
})();
2021-01-21 22:06:52 +01:00
const applyMutations = (mu, arrayOfArrays) => {
2020-11-23 19:24:19 +01:00
arrayOfArrays.forEach((a) => {
const result = mu[a[0]].apply(mu, a.slice(1));
2021-01-21 22:06:52 +01:00
if (a[0] === 'remove' && a[3]) {
2011-03-26 14:10:41 +01:00
assertEqualStrings(a[3], result);
}
});
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const mutationsToChangeset = (oldLen, arrayOfArrays) => {
2020-11-23 19:24:19 +01:00
const assem = Changeset.smartOpAssembler();
const op = Changeset.newOp();
const bank = Changeset.stringAssembler();
let oldPos = 0;
let newLen = 0;
arrayOfArrays.forEach((a) => {
2021-01-21 22:06:52 +01:00
if (a[0] === 'skip') {
2011-03-26 14:10:41 +01:00
op.opcode = '=';
op.chars = a[1];
op.lines = (a[2] || 0);
assem.append(op);
oldPos += op.chars;
newLen += op.chars;
2021-01-21 22:06:52 +01:00
} else if (a[0] === 'remove') {
2011-03-26 14:10:41 +01:00
op.opcode = '-';
op.chars = a[1];
op.lines = (a[2] || 0);
assem.append(op);
oldPos += op.chars;
2021-01-21 22:06:52 +01:00
} else if (a[0] === 'insert') {
2011-03-26 14:10:41 +01:00
op.opcode = '+';
bank.append(a[1]);
op.chars = a[1].length;
op.lines = (a[2] || 0);
assem.append(op);
newLen += op.chars;
}
});
newLen += oldLen - oldPos;
assem.endDocument();
return Changeset.pack(oldLen, newLen, assem.toString(), bank.toString());
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const runMutationTest = (testId, origLines, muts, correct) => {
2020-11-23 19:24:19 +01:00
print(`> runMutationTest#${testId}`);
let lines = origLines.slice();
const mu = Changeset.textLinesMutator(lines);
2011-03-26 14:10:41 +01:00
applyMutations(mu, muts);
mu.close();
assertEqualArrays(correct, lines);
2020-11-23 19:24:19 +01:00
const inText = origLines.join('');
const cs = mutationsToChangeset(inText.length, muts);
2011-03-26 14:10:41 +01:00
lines = origLines.slice();
Changeset.mutateTextLines(cs, lines);
assertEqualArrays(correct, lines);
2020-11-23 19:24:19 +01:00
const correctText = correct.join('');
// print(literal(cs));
const outText = Changeset.applyToText(cs, inText);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctText, outText);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(1, ['apple\n', 'banana\n', 'cabbage\n', 'duffle\n', 'eggplant\n'], [
['remove', 1, 0, 'a'],
['insert', 'tu'],
['remove', 1, 0, 'p'],
2011-03-26 14:10:41 +01:00
['skip', 4, 1],
['skip', 7, 1],
2020-11-23 19:24:19 +01:00
['insert', 'cream\npie\n', 2],
2011-03-26 14:10:41 +01:00
['skip', 2],
2020-11-23 19:24:19 +01:00
['insert', 'bot'],
['insert', '\n', 1],
['insert', 'bu'],
2011-03-26 14:10:41 +01:00
['skip', 3],
2020-11-23 19:24:19 +01:00
['remove', 3, 1, 'ge\n'],
['remove', 6, 0, 'duffle'],
], ['tuple\n', 'banana\n', 'cream\n', 'pie\n', 'cabot\n', 'bubba\n', 'eggplant\n']);
runMutationTest(2, ['apple\n', 'banana\n', 'cabbage\n', 'duffle\n', 'eggplant\n'], [
['remove', 1, 0, 'a'],
['remove', 1, 0, 'p'],
['insert', 'tu'],
2011-03-26 14:10:41 +01:00
['skip', 11, 2],
2020-11-23 19:24:19 +01:00
['insert', 'cream\npie\n', 2],
2011-03-26 14:10:41 +01:00
['skip', 2],
2020-11-23 19:24:19 +01:00
['insert', 'bot'],
['insert', '\n', 1],
['insert', 'bu'],
2011-03-26 14:10:41 +01:00
['skip', 3],
2020-11-23 19:24:19 +01:00
['remove', 3, 1, 'ge\n'],
['remove', 6, 0, 'duffle'],
], ['tuple\n', 'banana\n', 'cream\n', 'pie\n', 'cabot\n', 'bubba\n', 'eggplant\n']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(3, ['apple\n', 'banana\n', 'cabbage\n', 'duffle\n', 'eggplant\n'], [
['remove', 6, 1, 'apple\n'],
2011-03-26 14:10:41 +01:00
['skip', 15, 2],
['skip', 6],
2020-11-23 19:24:19 +01:00
['remove', 1, 1, '\n'],
['remove', 8, 0, 'eggplant'],
['skip', 1, 1],
], ['banana\n', 'cabbage\n', 'duffle\n']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(4, ['15\n'], [
2011-03-26 14:10:41 +01:00
['skip', 1],
2020-11-23 19:24:19 +01:00
['insert', '\n2\n3\n4\n', 4],
['skip', 2, 1],
], ['1\n', '2\n', '3\n', '4\n', '5\n']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(5, ['1\n', '2\n', '3\n', '4\n', '5\n'], [
2011-03-26 14:10:41 +01:00
['skip', 1],
2020-11-23 19:24:19 +01:00
['remove', 7, 4, '\n2\n3\n4\n'],
['skip', 2, 1],
], ['15\n']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(6, ['123\n', 'abc\n', 'def\n', 'ghi\n', 'xyz\n'], [
['insert', '0'],
2011-03-26 14:10:41 +01:00
['skip', 4, 1],
['skip', 4, 1],
2020-11-23 19:24:19 +01:00
['remove', 8, 2, 'def\nghi\n'],
['skip', 4, 1],
], ['0123\n', 'abc\n', 'xyz\n']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
runMutationTest(7, ['apple\n', 'banana\n', 'cabbage\n', 'duffle\n', 'eggplant\n'], [
['remove', 6, 1, 'apple\n'],
2011-03-26 14:10:41 +01:00
['skip', 15, 2, true],
['skip', 6, 0, true],
2020-11-23 19:24:19 +01:00
['remove', 1, 1, '\n'],
['remove', 8, 0, 'eggplant'],
['skip', 1, 1, true],
], ['banana\n', 'cabbage\n', 'duffle\n']);
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const poolOrArray = (attribs) => {
2011-03-26 14:10:41 +01:00
if (attribs.getAttrib) {
return attribs; // it's already an attrib pool
} else {
// assume it's an array of attrib strings to be split and added
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
attribs.forEach((kv) => {
2011-03-26 14:10:41 +01:00
p.putAttrib(kv.split(','));
});
return p;
}
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const runApplyToAttributionTest = (testId, attribs, cs, inAttr, outCorrect) => {
2020-11-23 19:24:19 +01:00
print(`> applyToAttribution#${testId}`);
const p = poolOrArray(attribs);
const result = Changeset.applyToAttribution(
Changeset.checkRep(cs), inAttr, p);
2011-03-26 14:10:41 +01:00
assertEqualStrings(outCorrect, result);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
// turn c<b>a</b>ctus\n into a<b>c</b>tusabcd\n
2021-01-21 22:06:52 +01:00
runApplyToAttributionTest(1,
['bold,', 'bold,true'], 'Z:7>3-1*0=1*1=1=3+4$abcd', '+1*1+1|1+5', '+1*1+1|1+8');
2011-03-26 14:10:41 +01:00
// turn "david\ngreenspan\n" into "<b>david\ngreen</b>\n"
2021-01-21 22:06:52 +01:00
runApplyToAttributionTest(2,
['bold,', 'bold,true'], 'Z:g<4*1|1=6*1=5-4$', '|2+g', '*1|1+6*1+5|1+1');
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
(() => {
2020-11-23 19:24:19 +01:00
print('> mutatorHasMore');
const lines = ['1\n', '2\n', '3\n', '4\n'];
let mu;
2011-03-26 14:10:41 +01:00
mu = Changeset.textLinesMutator(lines);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.skip(8, 4);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
mu.close();
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
// still 1,2,3,4
mu = Changeset.textLinesMutator(lines);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.remove(2, 1);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.skip(2, 1);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.skip(2, 1);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.skip(2, 1);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
mu.insert('5\n', 1);
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
mu.close();
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
// 2,3,4,5 now
mu = Changeset.textLinesMutator(lines);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.remove(6, 3);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == true`);
2011-03-26 14:10:41 +01:00
mu.remove(2, 1);
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
mu.insert('hello\n', 1);
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
mu.close();
2020-11-23 19:24:19 +01:00
assert(`${mu.hasMore()} == false`);
2011-03-26 14:10:41 +01:00
})();
2021-01-21 22:06:52 +01:00
const runMutateAttributionTest = (testId, attribs, cs, alines, outCorrect) => {
2020-11-23 19:24:19 +01:00
print(`> runMutateAttributionTest#${testId}`);
const p = poolOrArray(attribs);
const alines2 = Array.prototype.slice.call(alines);
const result = Changeset.mutateAttributionLines(
Changeset.checkRep(cs), alines2, p);
2011-03-26 14:10:41 +01:00
assertEqualArrays(outCorrect, alines2);
2020-11-23 19:24:19 +01:00
print(`> runMutateAttributionTest#${testId}.applyToAttribution`);
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const removeQuestionMarks = (a) => a.replace(/\?/g, '');
2020-11-23 19:24:19 +01:00
const inMerged = Changeset.joinAttributionLines(alines.map(removeQuestionMarks));
const correctMerged = Changeset.joinAttributionLines(outCorrect.map(removeQuestionMarks));
const mergedResult = Changeset.applyToAttribution(cs, inMerged, p);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctMerged, mergedResult);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
// turn 123\n 456\n 789\n into 123\n 4<b>5</b>6\n 789\n
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(1,
['bold,true'], 'Z:c>0|1=4=1*0=1$', ['|1+4', '|1+4', '|1+4'], ['|1+4', '+1*0+1|1+2', '|1+4']);
2011-03-26 14:10:41 +01:00
// make a document bold
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(2,
['bold,true'], 'Z:c>0*0|3=c$', ['|1+4', '|1+4', '|1+4'], ['*0|1+4', '*0|1+4', '*0|1+4']);
2011-03-26 14:10:41 +01:00
// clear bold on document
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(3,
['bold,', 'bold,true'], 'Z:c>0*0|3=c$',
['*1+1+1*1+1|1+1', '+1*1+1|1+2', '*1+1+1*1+1|1+1'], ['|1+4', '|1+4', '|1+4']);
2011-03-26 14:10:41 +01:00
// add a character on line 3 of a document with 5 blank lines, and make sure
// the optimization that skips purely-kept lines is working; if any attribution string
// with a '?' is parsed it will cause an error.
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(4,
['foo,bar', 'line,1', 'line,2', 'line,3', 'line,4', 'line,5'],
'Z:5>1|2=2+1$x', ['?*1|1+1', '?*2|1+1', '*3|1+1', '?*4|1+1', '?*5|1+1'],
['?*1|1+1', '?*2|1+1', '+1*3|1+1', '?*4|1+1', '?*5|1+1']);
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testPoolWithChars = (() => {
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
p.putAttrib(['char', 'newline']);
2020-11-23 19:24:19 +01:00
for (let i = 1; i < 36; i++) {
2011-03-26 14:10:41 +01:00
p.putAttrib(['char', Changeset.numToString(i)]);
}
p.putAttrib(['char', '']);
return p;
})();
// based on runMutationTest#1
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(5, testPoolWithChars,
'Z:11>7-2*t+1*u+1|2=b|2+a=2*b+1*o+1*t+1*0|1+1*b+1*u+1=3|1-3-6$' + 'tucream\npie\nbot\nbu', ['*a+1*p+2*l+1*e+1*0|1+1', '*b+1*a+1*n+1*a+1*n+1*a+1*0|1+1', '*c+1*a+1*b+2*a+1*g+1*e+1*0|1+1', '*d+1*u+1*f+2*l+1*e+1*0|1+1', '*e+1*g+2*p+1*l+1*a+1*n+1*t+1*0|1+1'], ['*t+1*u+1*p+1*l+1*e+1*0|1+1', '*b+1*a+1*n+1*a+1*n+1*a+1*0|1+1', '|1+6', '|1+4', '*c+1*a+1*b+1*o+1*t+1*0|1+1', '*b+1*u+1*b+2*a+1*0|1+1', '*e+1*g+2*p+1*l+1*a+1*n+1*t+1*0|1+1']);
2011-03-26 14:10:41 +01:00
// based on runMutationTest#3
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(6, testPoolWithChars,
'Z:11<f|1-6|2=f=6|1-1-8$', ['*a|1+6', '*b|1+7', '*c|1+8', '*d|1+7', '*e|1+9'],
['*b|1+7', '*c|1+8', '*d+6*e|1+1']);
2011-03-26 14:10:41 +01:00
// based on runMutationTest#4
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(7, testPoolWithChars, 'Z:3>7=1|4+7$\n2\n3\n4\n',
['*1+1*5|1+2'], ['*1+1|1+1', '|1+2', '|1+2', '|1+2', '*5|1+2']);
2011-03-26 14:10:41 +01:00
// based on runMutationTest#5
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(8, testPoolWithChars, 'Z:a<7=1|4-7$',
['*1|1+2', '*2|1+2', '*3|1+2', '*4|1+2', '*5|1+2'], ['*1+1*5|1+2']);
2011-03-26 14:10:41 +01:00
// based on runMutationTest#6
2021-01-21 22:06:52 +01:00
runMutateAttributionTest(9, testPoolWithChars, 'Z:k<7*0+1*10|2=8|2-8$0',
[
'*1+1*2+1*3+1|1+1',
'*a+1*b+1*c+1|1+1',
'*d+1*e+1*f+1|1+1',
'*g+1*h+1*i+1|1+1',
'?*x+1*y+1*z+1|1+1',
],
['*0+1|1+4', '|1+4', '?*x+1*y+1*z+1|1+1']);
runMutateAttributionTest(10, testPoolWithChars, 'Z:6>4=1+1=1+1|1=1+1=1*0+1$abcd',
['|1+3', '|1+3'], ['|1+5', '+2*0+1|1+2']);
runMutateAttributionTest(11, testPoolWithChars, 'Z:s>1|1=4=6|1+1$\n',
['*0|1+4', '*0|1+8', '*0+5|1+1', '*0|1+1', '*0|1+5', '*0|1+1', '*0|1+1', '*0|1+1', '|1+1'],
[
'*0|1+4',
'*0+6|1+1',
'*0|1+2',
'*0+5|1+1',
'*0|1+1',
'*0|1+5',
'*0|1+1',
'*0|1+1',
'*0|1+1',
'|1+1',
]);
const randomInlineString = (len, rand) => {
2020-11-23 19:24:19 +01:00
const assem = Changeset.stringAssembler();
for (let i = 0; i < len; i++) {
2011-03-26 14:10:41 +01:00
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
}
return assem.toString();
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const randomMultiline = (approxMaxLines, approxMaxCols, rand) => {
2020-11-23 19:24:19 +01:00
const numParts = rand.nextInt(approxMaxLines * 2) + 1;
const txt = Changeset.stringAssembler();
2011-03-26 14:10:41 +01:00
txt.append(rand.nextInt(2) ? '\n' : '');
2020-11-23 19:24:19 +01:00
for (let i = 0; i < numParts; i++) {
2021-01-21 22:06:52 +01:00
if ((i % 2) === 0) {
2011-03-26 14:10:41 +01:00
if (rand.nextInt(10)) {
txt.append(randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
} else {
txt.append('\n');
}
} else {
txt.append('\n');
}
}
return txt.toString();
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const randomStringOperation = (numCharsLeft, rand) => {
2020-11-23 19:24:19 +01:00
let result;
2011-03-26 14:10:41 +01:00
switch (rand.nextInt(9)) {
2020-11-23 19:24:19 +01:00
case 0:
2011-03-26 14:10:41 +01:00
{
// insert char
result = {
2020-11-23 19:24:19 +01:00
insert: randomInlineString(1, rand),
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 1:
2011-03-26 14:10:41 +01:00
{
// delete char
result = {
2020-11-23 19:24:19 +01:00
remove: 1,
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 2:
2011-03-26 14:10:41 +01:00
{
// skip char
result = {
2020-11-23 19:24:19 +01:00
skip: 1,
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 3:
2011-03-26 14:10:41 +01:00
{
// insert small
result = {
2020-11-23 19:24:19 +01:00
insert: randomInlineString(rand.nextInt(4) + 1, rand),
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 4:
2011-03-26 14:10:41 +01:00
{
// delete small
result = {
2020-11-23 19:24:19 +01:00
remove: rand.nextInt(4) + 1,
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 5:
2011-03-26 14:10:41 +01:00
{
// skip small
result = {
2020-11-23 19:24:19 +01:00
skip: rand.nextInt(4) + 1,
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 6:
2011-03-26 14:10:41 +01:00
{
// insert multiline;
result = {
2020-11-23 19:24:19 +01:00
insert: randomMultiline(5, 20, rand),
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 7:
2011-03-26 14:10:41 +01:00
{
// delete multiline
result = {
2020-11-23 19:24:19 +01:00
remove: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble()),
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 8:
2011-03-26 14:10:41 +01:00
{
// skip multiline
result = {
2020-11-23 19:24:19 +01:00
skip: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble()),
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 9:
2011-03-26 14:10:41 +01:00
{
// delete to end
result = {
2020-11-23 19:24:19 +01:00
remove: numCharsLeft,
2011-03-26 14:10:41 +01:00
};
break;
}
2020-11-23 19:24:19 +01:00
case 10:
2011-03-26 14:10:41 +01:00
{
// skip to end
result = {
2020-11-23 19:24:19 +01:00
skip: numCharsLeft,
2011-03-26 14:10:41 +01:00
};
break;
}
}
2020-11-23 19:24:19 +01:00
const maxOrig = numCharsLeft - 1;
2011-03-26 14:10:41 +01:00
if ('remove' in result) {
result.remove = Math.min(result.remove, maxOrig);
} else if ('skip' in result) {
result.skip = Math.min(result.skip, maxOrig);
}
return result;
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const randomTwoPropAttribs = (opcode, rand) => {
2011-03-26 14:10:41 +01:00
// assumes attrib pool like ['apple,','apple,true','banana,','banana,true']
2021-01-21 22:06:52 +01:00
if (opcode === '-' || rand.nextInt(3)) {
2011-03-26 14:10:41 +01:00
return '';
} else if (rand.nextInt(3)) {
2021-01-21 22:06:52 +01:00
if (opcode === '+' || rand.nextInt(2)) {
2020-11-23 19:24:19 +01:00
return `*${Changeset.numToString(rand.nextInt(2) * 2 + 1)}`;
2011-03-26 14:10:41 +01:00
} else {
2020-11-23 19:24:19 +01:00
return `*${Changeset.numToString(rand.nextInt(2) * 2)}`;
2011-03-26 14:10:41 +01:00
}
2021-01-21 22:06:52 +01:00
} else if (opcode === '+' || rand.nextInt(4) === 0) {
2020-11-23 19:24:19 +01:00
return '*1*3';
2011-03-26 14:10:41 +01:00
} else {
2020-11-23 19:24:19 +01:00
return ['*0*2', '*0*3', '*1*2'][rand.nextInt(3)];
2011-03-26 14:10:41 +01:00
}
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const randomTestChangeset = (origText, rand, withAttribs) => {
2020-11-23 19:24:19 +01:00
const charBank = Changeset.stringAssembler();
let textLeft = origText; // always keep final newline
const outTextAssem = Changeset.stringAssembler();
const opAssem = Changeset.smartOpAssembler();
const oldLen = origText.length;
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const nextOp = Changeset.newOp();
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const appendMultilineOp = (opcode, txt) => {
2011-03-26 14:10:41 +01:00
nextOp.opcode = opcode;
if (withAttribs) {
nextOp.attribs = randomTwoPropAttribs(opcode, rand);
}
2020-11-23 19:24:19 +01:00
txt.replace(/\n|[^\n]+/g, (t) => {
2021-01-21 22:06:52 +01:00
if (t === '\n') {
2011-03-26 14:10:41 +01:00
nextOp.chars = 1;
nextOp.lines = 1;
opAssem.append(nextOp);
} else {
nextOp.chars = t.length;
nextOp.lines = 0;
opAssem.append(nextOp);
}
return '';
});
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const doOp = () => {
2020-11-23 19:24:19 +01:00
const o = randomStringOperation(textLeft.length, rand);
2011-03-26 14:10:41 +01:00
if (o.insert) {
2021-01-21 22:06:52 +01:00
const txt = o.insert;
2011-03-26 14:10:41 +01:00
charBank.append(txt);
outTextAssem.append(txt);
appendMultilineOp('+', txt);
} else if (o.skip) {
2021-01-21 22:06:52 +01:00
const txt = textLeft.substring(0, o.skip);
2011-03-26 14:10:41 +01:00
textLeft = textLeft.substring(o.skip);
outTextAssem.append(txt);
appendMultilineOp('=', txt);
} else if (o.remove) {
2021-01-21 22:06:52 +01:00
const txt = textLeft.substring(0, o.remove);
2011-03-26 14:10:41 +01:00
textLeft = textLeft.substring(o.remove);
appendMultilineOp('-', txt);
}
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
while (textLeft.length > 1) doOp();
2020-11-23 19:24:19 +01:00
for (let i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
const outText = `${outTextAssem.toString()}\n`;
2011-03-26 14:10:41 +01:00
opAssem.endDocument();
2020-11-23 19:24:19 +01:00
const cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
2011-03-26 14:10:41 +01:00
Changeset.checkRep(cs);
return [cs, outText];
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testCompose = (randomSeed) => {
2020-11-23 19:24:19 +01:00
const rand = new random();
print(`> testCompose#${randomSeed}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const startText = `${randomMultiline(10, 20, rand)}\n`;
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const x1 = randomTestChangeset(startText, rand);
const change1 = x1[0];
const text1 = x1[1];
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const x2 = randomTestChangeset(text1, rand);
const change2 = x2[0];
const text2 = x2[1];
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const x3 = randomTestChangeset(text2, rand);
const change3 = x3[0];
const text3 = x3[1];
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
// print(literal(Changeset.toBaseTen(startText)));
// print(literal(Changeset.toBaseTen(change1)));
// print(literal(Changeset.toBaseTen(change2)));
const change12 = Changeset.checkRep(Changeset.compose(change1, change2, p));
const change23 = Changeset.checkRep(Changeset.compose(change2, change3, p));
const change123 = Changeset.checkRep(Changeset.compose(change12, change3, p));
const change123a = Changeset.checkRep(Changeset.compose(change1, change23, p));
2011-03-26 14:10:41 +01:00
assertEqualStrings(change123, change123a);
assertEqualStrings(text2, Changeset.applyToText(change12, startText));
assertEqualStrings(text3, Changeset.applyToText(change23, text1));
assertEqualStrings(text3, Changeset.applyToText(change123, startText));
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
for (let i = 0; i < 30; i++) testCompose(i);
2011-03-26 14:10:41 +01:00
(function simpleComposeAttributesTest() {
2020-11-23 19:24:19 +01:00
print('> simpleComposeAttributesTest');
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
p.putAttrib(['bold', '']);
p.putAttrib(['bold', 'true']);
2020-11-23 19:24:19 +01:00
const cs1 = Changeset.checkRep('Z:2>1*1+1*1=1$x');
const cs2 = Changeset.checkRep('Z:3>0*0|1=3$');
const cs12 = Changeset.checkRep(Changeset.compose(cs1, cs2, p));
assertEqualStrings('Z:2>1+1*0|1=2$x', cs12);
2011-03-26 14:10:41 +01:00
})();
(function followAttributesTest() {
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
p.putAttrib(['x', '']);
p.putAttrib(['x', 'abc']);
p.putAttrib(['x', 'def']);
p.putAttrib(['y', '']);
p.putAttrib(['y', 'abc']);
p.putAttrib(['y', 'def']);
2021-01-21 22:06:52 +01:00
const testFollow = (a, b, afb, bfa, merge) => {
2011-03-26 14:10:41 +01:00
assertEqualStrings(afb, Changeset.followAttributes(a, b, p));
assertEqualStrings(bfa, Changeset.followAttributes(b, a, p));
assertEqualStrings(merge, Changeset.composeAttributes(a, afb, true, p));
assertEqualStrings(merge, Changeset.composeAttributes(b, bfa, true, p));
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
testFollow('', '', '', '', '');
testFollow('*0', '', '', '*0', '*0');
testFollow('*0', '*0', '', '', '*0');
testFollow('*0', '*1', '', '*0', '*0');
testFollow('*1', '*2', '', '*1', '*1');
testFollow('*0*1', '', '', '*0*1', '*0*1');
testFollow('*0*4', '*2*3', '*3', '*0', '*0*3');
testFollow('*0*4', '*2', '', '*0*4', '*0*4');
})();
2021-01-21 22:06:52 +01:00
const testFollow = (randomSeed) => {
2020-11-23 19:24:19 +01:00
const rand = new random();
print(`> testFollow#${randomSeed}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const startText = `${randomMultiline(10, 20, rand)}\n`;
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const cs1 = randomTestChangeset(startText, rand)[0];
const cs2 = randomTestChangeset(startText, rand)[0];
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const afb = Changeset.checkRep(Changeset.follow(cs1, cs2, false, p));
const bfa = Changeset.checkRep(Changeset.follow(cs2, cs1, true, p));
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const merge1 = Changeset.checkRep(Changeset.compose(cs1, afb));
const merge2 = Changeset.checkRep(Changeset.compose(cs2, bfa));
2011-03-26 14:10:41 +01:00
assertEqualStrings(merge1, merge2);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
for (let i = 0; i < 30; i++) testFollow(i);
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testSplitJoinAttributionLines = (randomSeed) => {
2020-11-23 19:24:19 +01:00
const rand = new random();
print(`> testSplitJoinAttributionLines#${randomSeed}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const doc = `${randomMultiline(10, 20, rand)}\n`;
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const stringToOps = (str) => {
2020-11-23 19:24:19 +01:00
const assem = Changeset.mergingOpAssembler();
const o = Changeset.newOp('+');
2011-03-26 14:10:41 +01:00
o.chars = 1;
2020-11-23 19:24:19 +01:00
for (let i = 0; i < str.length; i++) {
const c = str.charAt(i);
2021-01-21 22:06:52 +01:00
o.lines = (c === '\n' ? 1 : 0);
o.attribs = (c === 'a' || c === 'b' ? `*${c}` : '');
2011-03-26 14:10:41 +01:00
assem.append(o);
}
return assem.toString();
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const theJoined = stringToOps(doc);
const theSplit = doc.match(/[^\n]*\n/g).map(stringToOps);
2011-03-26 14:10:41 +01:00
assertEqualArrays(theSplit, Changeset.splitAttributionLines(theJoined, doc));
assertEqualStrings(theJoined, Changeset.joinAttributionLines(theSplit));
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
for (let i = 0; i < 10; i++) testSplitJoinAttributionLines(i);
2011-03-26 14:10:41 +01:00
(function testMoveOpsToNewPool() {
2020-11-23 19:24:19 +01:00
print('> testMoveOpsToNewPool');
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const pool1 = new AttributePool();
const pool2 = new AttributePool();
2011-03-26 14:10:41 +01:00
pool1.putAttrib(['baz', 'qux']);
pool1.putAttrib(['foo', 'bar']);
pool2.putAttrib(['foo', 'bar']);
2021-01-21 22:06:52 +01:00
assertEqualStrings(
Changeset.moveOpsToNewPool('Z:1>2*1+1*0+1$ab', pool1, pool2), 'Z:1>2*0+1*1+1$ab');
assertEqualStrings(
Changeset.moveOpsToNewPool('*1+1*0+1', pool1, pool2), '*0+1*1+1');
2011-03-26 14:10:41 +01:00
})();
(function testMakeSplice() {
2020-11-23 19:24:19 +01:00
print('> testMakeSplice');
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const t = 'a\nb\nc\n';
const t2 = Changeset.applyToText(Changeset.makeSplice(t, 5, 0, 'def'), t);
assertEqualStrings('a\nb\ncdef\n', t2);
2011-03-26 14:10:41 +01:00
})();
(function testToSplices() {
2020-11-23 19:24:19 +01:00
print('> testToSplices');
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const cs = Changeset.checkRep('Z:z>9*0=1=4-3+9=1|1-4-4+1*0+a$123456789abcdefghijk');
const correctSplices = [
[5, 8, '123456789'],
[9, 17, 'abcdefghijk'],
2011-03-26 14:10:41 +01:00
];
assertEqualArrays(correctSplices, Changeset.toSplices(cs));
})();
2021-01-21 22:06:52 +01:00
const testCharacterRangeFollow = (testId, cs, oldRange, insertionsAfter, correctNewRange) => {
2020-11-23 19:24:19 +01:00
print(`> testCharacterRangeFollow#${testId}`);
2021-01-21 22:06:52 +01:00
cs = Changeset.checkRep(cs);
assertEqualArrays(correctNewRange, Changeset.characterRangeFollow(
cs, oldRange[0], oldRange[1], insertionsAfter));
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
testCharacterRangeFollow(1, 'Z:z>9*0=1=4-3+9=1|1-4-4+1*0+a$123456789abcdefghijk',
[7, 10], false, [14, 15]);
2020-11-23 19:24:19 +01:00
testCharacterRangeFollow(2, 'Z:bc<6|x=b4|2-6$', [400, 407], false, [400, 401]);
testCharacterRangeFollow(3, 'Z:4>0-3+3$abc', [0, 3], false, [3, 3]);
testCharacterRangeFollow(4, 'Z:4>0-3+3$abc', [0, 3], true, [0, 0]);
testCharacterRangeFollow(5, 'Z:5>1+1=1-3+3$abcd', [1, 4], false, [5, 5]);
testCharacterRangeFollow(6, 'Z:5>1+1=1-3+3$abcd', [1, 4], true, [2, 2]);
testCharacterRangeFollow(7, 'Z:5>1+1=1-3+3$abcd', [0, 6], false, [1, 7]);
testCharacterRangeFollow(8, 'Z:5>1+1=1-3+3$abcd', [0, 3], false, [1, 2]);
testCharacterRangeFollow(9, 'Z:5>1+1=1-3+3$abcd', [2, 5], false, [5, 6]);
testCharacterRangeFollow(10, 'Z:2>1+1$a', [0, 0], false, [1, 1]);
testCharacterRangeFollow(11, 'Z:2>1+1$a', [0, 0], true, [0, 0]);
2011-03-26 14:10:41 +01:00
(function testOpAttributeValue() {
2020-11-23 19:24:19 +01:00
print('> testOpAttributeValue');
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const p = new AttributePool();
2011-03-26 14:10:41 +01:00
p.putAttrib(['name', 'david']);
p.putAttrib(['color', 'green']);
2021-01-21 22:06:52 +01:00
assertEqualStrings('david',
Changeset.opAttributeValue(Changeset.stringOp('*0*1+1'), 'name', p));
assertEqualStrings('david',
Changeset.opAttributeValue(Changeset.stringOp('*0+1'), 'name', p));
assertEqualStrings('',
Changeset.opAttributeValue(Changeset.stringOp('*1+1'), 'name', p));
assertEqualStrings('',
Changeset.opAttributeValue(Changeset.stringOp('+1'), 'name', p));
assertEqualStrings('green',
Changeset.opAttributeValue(Changeset.stringOp('*0*1+1'), 'color', p));
assertEqualStrings('green',
Changeset.opAttributeValue(Changeset.stringOp('*1+1'), 'color', p));
assertEqualStrings('',
Changeset.opAttributeValue(Changeset.stringOp('*0+1'), 'color', p));
assertEqualStrings('',
Changeset.opAttributeValue(Changeset.stringOp('+1'), 'color', p));
2011-03-26 14:10:41 +01:00
})();
2021-01-21 22:06:52 +01:00
const testAppendATextToAssembler = (testId, atext, correctOps) => {
2020-11-23 19:24:19 +01:00
print(`> testAppendATextToAssembler#${testId}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const assem = Changeset.smartOpAssembler();
2011-03-26 14:10:41 +01:00
Changeset.appendATextToAssembler(atext, assem);
assertEqualStrings(correctOps, assem.toString());
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(1, {
2020-11-23 19:24:19 +01:00
text: '\n',
attribs: '|1+1',
}, '');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(2, {
2020-11-23 19:24:19 +01:00
text: '\n\n',
attribs: '|2+2',
}, '|1+1');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(3, {
2020-11-23 19:24:19 +01:00
text: '\n\n',
attribs: '*x|2+2',
}, '*x|1+1');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(4, {
2020-11-23 19:24:19 +01:00
text: '\n\n',
attribs: '*x|1+1|1+1',
}, '*x|1+1');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(5, {
2020-11-23 19:24:19 +01:00
text: 'foo\n',
attribs: '|1+4',
}, '+3');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(6, {
2020-11-23 19:24:19 +01:00
text: '\nfoo\n',
attribs: '|2+5',
}, '|1+1+3');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(7, {
2020-11-23 19:24:19 +01:00
text: '\nfoo\n',
attribs: '*x|2+5',
}, '*x|1+1*x+3');
2011-03-26 14:10:41 +01:00
testAppendATextToAssembler(8, {
2020-11-23 19:24:19 +01:00
text: '\n\n\nfoo\n',
attribs: '|2+2*x|2+5',
}, '|2+2*x|1+1*x+3');
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testMakeAttribsString = (testId, pool, opcode, attribs, correctString) => {
2020-11-23 19:24:19 +01:00
print(`> testMakeAttribsString#${testId}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const p = poolOrArray(pool);
const str = Changeset.makeAttribsString(opcode, attribs, p);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctString, str);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
testMakeAttribsString(1, ['bold,'], '+', [
2020-11-23 19:24:19 +01:00
['bold', ''],
2011-03-26 14:10:41 +01:00
], '');
testMakeAttribsString(2, ['abc,def', 'bold,'], '=', [
2020-11-23 19:24:19 +01:00
['bold', ''],
2011-03-26 14:10:41 +01:00
], '*1');
testMakeAttribsString(3, ['abc,def', 'bold,true'], '+', [
['abc', 'def'],
2020-11-23 19:24:19 +01:00
['bold', 'true'],
2011-03-26 14:10:41 +01:00
], '*0*1');
testMakeAttribsString(4, ['abc,def', 'bold,true'], '+', [
['bold', 'true'],
2020-11-23 19:24:19 +01:00
['abc', 'def'],
2011-03-26 14:10:41 +01:00
], '*0*1');
2021-01-21 22:06:52 +01:00
const testSubattribution = (testId, astr, start, end, correctOutput) => {
2020-11-23 19:24:19 +01:00
print(`> testSubattribution#${testId}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const str = Changeset.subattribution(astr, start, end);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctOutput, str);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
testSubattribution(1, '+1', 0, 0, '');
testSubattribution(2, '+1', 0, 1, '+1');
testSubattribution(3, '+1', 0, undefined, '+1');
testSubattribution(4, '|1+1', 0, 0, '');
testSubattribution(5, '|1+1', 0, 1, '|1+1');
testSubattribution(6, '|1+1', 0, undefined, '|1+1');
testSubattribution(7, '*0+1', 0, 0, '');
testSubattribution(8, '*0+1', 0, 1, '*0+1');
testSubattribution(9, '*0+1', 0, undefined, '*0+1');
testSubattribution(10, '*0|1+1', 0, 0, '');
testSubattribution(11, '*0|1+1', 0, 1, '*0|1+1');
testSubattribution(12, '*0|1+1', 0, undefined, '*0|1+1');
testSubattribution(13, '*0+2+1*1+3', 0, 1, '*0+1');
testSubattribution(14, '*0+2+1*1+3', 0, 2, '*0+2');
testSubattribution(15, '*0+2+1*1+3', 0, 3, '*0+2+1');
testSubattribution(16, '*0+2+1*1+3', 0, 4, '*0+2+1*1+1');
testSubattribution(17, '*0+2+1*1+3', 0, 5, '*0+2+1*1+2');
testSubattribution(18, '*0+2+1*1+3', 0, 6, '*0+2+1*1+3');
testSubattribution(19, '*0+2+1*1+3', 0, 7, '*0+2+1*1+3');
testSubattribution(20, '*0+2+1*1+3', 0, undefined, '*0+2+1*1+3');
testSubattribution(21, '*0+2+1*1+3', 1, undefined, '*0+1+1*1+3');
testSubattribution(22, '*0+2+1*1+3', 2, undefined, '+1*1+3');
testSubattribution(23, '*0+2+1*1+3', 3, undefined, '*1+3');
testSubattribution(24, '*0+2+1*1+3', 4, undefined, '*1+2');
testSubattribution(25, '*0+2+1*1+3', 5, undefined, '*1+1');
testSubattribution(26, '*0+2+1*1+3', 6, undefined, '');
testSubattribution(27, '*0+2+1*1|1+3', 0, 1, '*0+1');
testSubattribution(28, '*0+2+1*1|1+3', 0, 2, '*0+2');
testSubattribution(29, '*0+2+1*1|1+3', 0, 3, '*0+2+1');
testSubattribution(30, '*0+2+1*1|1+3', 0, 4, '*0+2+1*1+1');
testSubattribution(31, '*0+2+1*1|1+3', 0, 5, '*0+2+1*1+2');
testSubattribution(32, '*0+2+1*1|1+3', 0, 6, '*0+2+1*1|1+3');
testSubattribution(33, '*0+2+1*1|1+3', 0, 7, '*0+2+1*1|1+3');
testSubattribution(34, '*0+2+1*1|1+3', 0, undefined, '*0+2+1*1|1+3');
testSubattribution(35, '*0+2+1*1|1+3', 1, undefined, '*0+1+1*1|1+3');
testSubattribution(36, '*0+2+1*1|1+3', 2, undefined, '+1*1|1+3');
testSubattribution(37, '*0+2+1*1|1+3', 3, undefined, '*1|1+3');
testSubattribution(38, '*0+2+1*1|1+3', 4, undefined, '*1|1+2');
testSubattribution(39, '*0+2+1*1|1+3', 5, undefined, '*1|1+1');
testSubattribution(40, '*0+2+1*1|1+3', 1, 5, '*0+1+1*1+2');
testSubattribution(41, '*0+2+1*1|1+3', 2, 6, '+1*1|1+3');
testSubattribution(42, '*0+2+1*1+3', 2, 6, '+1*1+3');
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testFilterAttribNumbers = (testId, cs, filter, correctOutput) => {
2020-11-23 19:24:19 +01:00
print(`> testFilterAttribNumbers#${testId}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const str = Changeset.filterAttribNumbers(cs, filter);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctOutput, str);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
testFilterAttribNumbers(1, '*0*1+1+2+3*1+4*2+5*0*2*1*b*c+6',
(n) => (n % 2) === 0, '*0+1+2+3+4*2+5*0*2*c+6');
testFilterAttribNumbers(2, '*0*1+1+2+3*1+4*2+5*0*2*1*b*c+6',
(n) => (n % 2) === 1, '*1+1+2+3*1+4+5*1*b+6');
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testInverse = (testId, cs, lines, alines, pool, correctOutput) => {
2020-11-23 19:24:19 +01:00
print(`> testInverse#${testId}`);
2011-03-26 14:10:41 +01:00
pool = poolOrArray(pool);
2020-11-23 19:24:19 +01:00
const str = Changeset.inverse(Changeset.checkRep(cs), lines, alines, pool);
2011-03-26 14:10:41 +01:00
assertEqualStrings(correctOutput, str);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
// take "FFFFTTTTT" and apply "-FT--FFTT", the inverse of which is "--F--TT--"
2021-01-21 22:06:52 +01:00
testInverse(1, 'Z:9>0=1*0=1*1=1=2*0=2*1|1=2$', null,
['+4*1+5'], ['bold,', 'bold,true'], 'Z:9>0=2*0=1=2*1=2$');
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testMutateTextLines = (testId, cs, lines, correctLines) => {
2020-11-23 19:24:19 +01:00
print(`> testMutateTextLines#${testId}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const a = lines.slice();
2011-03-26 14:10:41 +01:00
Changeset.mutateTextLines(cs, a);
assertEqualArrays(correctLines, a);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
testMutateTextLines(1, 'Z:4<1|1-2-1|1+1+1$\nc', ['a\n', 'b\n'], ['\n', 'c\n']);
testMutateTextLines(2, 'Z:4>0|1-2-1|2+3$\nc\n', ['a\n', 'b\n'], ['\n', 'c\n', '\n']);
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
const testInverseRandom = (randomSeed) => {
2020-11-23 19:24:19 +01:00
const rand = new random();
print(`> testInverseRandom#${randomSeed}`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const p = poolOrArray(['apple,', 'apple,true', 'banana,', 'banana,true']);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const startText = `${randomMultiline(10, 20, rand)}\n`;
const alines = Changeset.splitAttributionLines(Changeset.makeAttribution(startText), startText);
const lines = startText.slice(0, -1).split('\n').map((s) => `${s}\n`);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const stylifier = randomTestChangeset(startText, rand, true)[0];
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
// print(alines.join('\n'));
2011-03-26 14:10:41 +01:00
Changeset.mutateAttributionLines(stylifier, alines, p);
2020-11-23 19:24:19 +01:00
// print(stylifier);
// print(alines.join('\n'));
2011-03-26 14:10:41 +01:00
Changeset.mutateTextLines(stylifier, lines);
2020-11-23 19:24:19 +01:00
const changeset = randomTestChangeset(lines.join(''), rand, true)[0];
const inverseChangeset = Changeset.inverse(changeset, lines, alines, p);
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const origLines = lines.slice();
const origALines = alines.slice();
2011-03-26 14:10:41 +01:00
Changeset.mutateTextLines(changeset, lines);
Changeset.mutateAttributionLines(changeset, alines, p);
2020-11-23 19:24:19 +01:00
// print(origALines.join('\n'));
// print(changeset);
// print(inverseChangeset);
// print(origLines.map(function(s) { return '1: '+s.slice(0,-1); }).join('\n'));
// print(lines.map(function(s) { return '2: '+s.slice(0,-1); }).join('\n'));
// print(alines.join('\n'));
2011-03-26 14:10:41 +01:00
Changeset.mutateTextLines(inverseChangeset, lines);
Changeset.mutateAttributionLines(inverseChangeset, alines, p);
2020-11-23 19:24:19 +01:00
// print(lines.map(function(s) { return '3: '+s.slice(0,-1); }).join('\n'));
2011-03-26 14:10:41 +01:00
assertEqualArrays(origLines, lines);
assertEqualArrays(origALines, alines);
2021-01-21 22:06:52 +01:00
};
2011-03-26 14:10:41 +01:00
2021-01-21 22:06:52 +01:00
for (let i = 0; i < 30; i++) testInverseRandom(i);
};
2011-03-26 14:10:41 +01:00
runTests();