mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
move duplicated code to helper.js
This commit is contained in:
parent
dcb061fb1b
commit
2af8948f68
8 changed files with 248 additions and 672 deletions
|
@ -3,6 +3,7 @@ var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
var assertEqualArrays = helper.assertEqualArrays;
|
var assertEqualArrays = helper.assertEqualArrays;
|
||||||
|
var poolOrArray = helper.poolOrArray;
|
||||||
|
|
||||||
describe("attribution line mutations",function(){
|
describe("attribution line mutations",function(){
|
||||||
it("turns 123\\n 456\\n 789\\n into 123\\n 4<b>5</b>6\\n 789\\n",function(done){
|
it("turns 123\\n 456\\n 789\\n into 123\\n 4<b>5</b>6\\n 789\\n",function(done){
|
||||||
|
@ -92,18 +93,6 @@ var testPoolWithChars = (function () {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
function poolOrArray(attribs) {
|
|
||||||
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
|
|
||||||
var p = new AttributePool();
|
|
||||||
attribs.forEach(function (kv) {
|
|
||||||
p.putAttrib(kv.split(','));
|
|
||||||
});
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,10 @@ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
|
var random = helper.random;
|
||||||
|
var randomMultiline = helper.randomMultiline;
|
||||||
|
var randomInlineString = helper.randomInlineString;
|
||||||
|
var randomTestChangeset = helper.randomTestChangeset;
|
||||||
|
|
||||||
describe("Changeset.compose",function(){
|
describe("Changeset.compose",function(){
|
||||||
it("composes all the changesets - BUT THIS only calls applyToText?!?",function(done){
|
it("composes all the changesets - BUT THIS only calls applyToText?!?",function(done){
|
||||||
|
@ -53,192 +57,3 @@ function testCompose(randomSeed) {
|
||||||
assertEqualStrings(text3, Changeset.applyToText(change123, startText));
|
assertEqualStrings(text3, Changeset.applyToText(change123, startText));
|
||||||
}
|
}
|
||||||
|
|
||||||
function random() {
|
|
||||||
this.nextInt = function (maxValue) {
|
|
||||||
return Math.floor(Math.random() * maxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// maxValue is not used
|
|
||||||
this.nextDouble = function (maxValue) {
|
|
||||||
return Math.random();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function randomMultiline(approxMaxLines, approxMaxCols, rand) {
|
|
||||||
var numParts = rand.nextInt(approxMaxLines * 2) + 1;
|
|
||||||
var txt = Changeset.stringAssembler();
|
|
||||||
txt.append(rand.nextInt(2) ? '\n' : '');
|
|
||||||
for (var i = 0; i < numParts; i++) {
|
|
||||||
if ((i % 2) == 0) {
|
|
||||||
if (rand.nextInt(10)) {
|
|
||||||
txt.append(randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return txt.toString();
|
|
||||||
}
|
|
||||||
function randomInlineString(len, rand) {
|
|
||||||
var assem = Changeset.stringAssembler();
|
|
||||||
for (var i = 0; i < len; i++) {
|
|
||||||
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
|
|
||||||
}
|
|
||||||
return assem.toString();
|
|
||||||
}
|
|
||||||
function randomTestChangeset(origText, rand, withAttribs) {
|
|
||||||
var charBank = Changeset.stringAssembler();
|
|
||||||
var textLeft = origText; // always keep final newline
|
|
||||||
var outTextAssem = Changeset.stringAssembler();
|
|
||||||
var opAssem = Changeset.smartOpAssembler();
|
|
||||||
var oldLen = origText.length;
|
|
||||||
|
|
||||||
var nextOp = Changeset.newOp();
|
|
||||||
|
|
||||||
function appendMultilineOp(opcode, txt) {
|
|
||||||
nextOp.opcode = opcode;
|
|
||||||
if (withAttribs) {
|
|
||||||
nextOp.attribs = randomTwoPropAttribs(opcode, rand);
|
|
||||||
}
|
|
||||||
txt.replace(/\n|[^\n]+/g, function (t) {
|
|
||||||
if (t == '\n') {
|
|
||||||
nextOp.chars = 1;
|
|
||||||
nextOp.lines = 1;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
} else {
|
|
||||||
nextOp.chars = t.length;
|
|
||||||
nextOp.lines = 0;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function doOp() {
|
|
||||||
var o = randomStringOperation(textLeft.length, rand);
|
|
||||||
if (o.insert) {
|
|
||||||
var txt = o.insert;
|
|
||||||
charBank.append(txt);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('+', txt);
|
|
||||||
} else if (o.skip) {
|
|
||||||
txt = textLeft.substring(0, o.skip);
|
|
||||||
textLeft = textLeft.substring(o.skip);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('=', txt);
|
|
||||||
} else if (o.remove) {
|
|
||||||
txt = textLeft.substring(0, o.remove);
|
|
||||||
textLeft = textLeft.substring(o.remove);
|
|
||||||
appendMultilineOp('-', txt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (textLeft.length > 1) doOp();
|
|
||||||
for (var i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
|
|
||||||
var outText = outTextAssem.toString() + '\n';
|
|
||||||
opAssem.endDocument();
|
|
||||||
var cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
|
|
||||||
Changeset.checkRep(cs);
|
|
||||||
return [cs, outText];
|
|
||||||
}
|
|
||||||
function randomStringOperation(numCharsLeft, rand) {
|
|
||||||
var result;
|
|
||||||
switch (rand.nextInt(9)) {
|
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
// insert char
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 1:
|
|
||||||
{
|
|
||||||
// delete char
|
|
||||||
result = {
|
|
||||||
remove: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
// skip char
|
|
||||||
result = {
|
|
||||||
skip: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
// insert small
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(rand.nextInt(4) + 1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 4:
|
|
||||||
{
|
|
||||||
// delete small
|
|
||||||
result = {
|
|
||||||
remove: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 5:
|
|
||||||
{
|
|
||||||
// skip small
|
|
||||||
result = {
|
|
||||||
skip: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 6:
|
|
||||||
{
|
|
||||||
// insert multiline;
|
|
||||||
result = {
|
|
||||||
insert: randomMultiline(5, 20, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 7:
|
|
||||||
{
|
|
||||||
// delete multiline
|
|
||||||
result = {
|
|
||||||
remove: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 8:
|
|
||||||
{
|
|
||||||
// skip multiline
|
|
||||||
result = {
|
|
||||||
skip: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 9:
|
|
||||||
{
|
|
||||||
// delete to end
|
|
||||||
result = {
|
|
||||||
remove: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 10:
|
|
||||||
{
|
|
||||||
// skip to end
|
|
||||||
result = {
|
|
||||||
skip: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var maxOrig = numCharsLeft - 1;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,10 @@ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
var assertEqualArrays = helper.assertEqualArrays;
|
var assertEqualArrays = helper.assertEqualArrays;
|
||||||
|
var random = helper.random;
|
||||||
|
var randomMultiline = helper.randomMultiline;
|
||||||
|
var randomInlineString = helper.randomInlineString;
|
||||||
|
var randomTestChangeset = helper.randomTestChangeset;
|
||||||
|
|
||||||
describe("follow",function(){
|
describe("follow",function(){
|
||||||
it("follows",function(done){
|
it("follows",function(done){
|
||||||
|
@ -50,192 +54,3 @@ describe("follow",function(){
|
||||||
assertEqualStrings(merge1, merge2);
|
assertEqualStrings(merge1, merge2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function random() {
|
|
||||||
this.nextInt = function (maxValue) {
|
|
||||||
return Math.floor(Math.random() * maxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// maxValue is not used
|
|
||||||
this.nextDouble = function (maxValue) {
|
|
||||||
return Math.random();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function randomMultiline(approxMaxLines, approxMaxCols, rand) {
|
|
||||||
var numParts = rand.nextInt(approxMaxLines * 2) + 1;
|
|
||||||
var txt = Changeset.stringAssembler();
|
|
||||||
txt.append(rand.nextInt(2) ? '\n' : '');
|
|
||||||
for (var i = 0; i < numParts; i++) {
|
|
||||||
if ((i % 2) == 0) {
|
|
||||||
if (rand.nextInt(10)) {
|
|
||||||
txt.append(randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return txt.toString();
|
|
||||||
}
|
|
||||||
function randomInlineString(len, rand) {
|
|
||||||
var assem = Changeset.stringAssembler();
|
|
||||||
for (var i = 0; i < len; i++) {
|
|
||||||
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
|
|
||||||
}
|
|
||||||
return assem.toString();
|
|
||||||
}
|
|
||||||
function randomTestChangeset(origText, rand, withAttribs) {
|
|
||||||
var charBank = Changeset.stringAssembler();
|
|
||||||
var textLeft = origText; // always keep final newline
|
|
||||||
var outTextAssem = Changeset.stringAssembler();
|
|
||||||
var opAssem = Changeset.smartOpAssembler();
|
|
||||||
var oldLen = origText.length;
|
|
||||||
|
|
||||||
var nextOp = Changeset.newOp();
|
|
||||||
|
|
||||||
function appendMultilineOp(opcode, txt) {
|
|
||||||
nextOp.opcode = opcode;
|
|
||||||
if (withAttribs) {
|
|
||||||
nextOp.attribs = randomTwoPropAttribs(opcode, rand);
|
|
||||||
}
|
|
||||||
txt.replace(/\n|[^\n]+/g, function (t) {
|
|
||||||
if (t == '\n') {
|
|
||||||
nextOp.chars = 1;
|
|
||||||
nextOp.lines = 1;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
} else {
|
|
||||||
nextOp.chars = t.length;
|
|
||||||
nextOp.lines = 0;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function doOp() {
|
|
||||||
var o = randomStringOperation(textLeft.length, rand);
|
|
||||||
if (o.insert) {
|
|
||||||
var txt = o.insert;
|
|
||||||
charBank.append(txt);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('+', txt);
|
|
||||||
} else if (o.skip) {
|
|
||||||
txt = textLeft.substring(0, o.skip);
|
|
||||||
textLeft = textLeft.substring(o.skip);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('=', txt);
|
|
||||||
} else if (o.remove) {
|
|
||||||
txt = textLeft.substring(0, o.remove);
|
|
||||||
textLeft = textLeft.substring(o.remove);
|
|
||||||
appendMultilineOp('-', txt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (textLeft.length > 1) doOp();
|
|
||||||
for (var i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
|
|
||||||
var outText = outTextAssem.toString() + '\n';
|
|
||||||
opAssem.endDocument();
|
|
||||||
var cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
|
|
||||||
Changeset.checkRep(cs);
|
|
||||||
return [cs, outText];
|
|
||||||
}
|
|
||||||
function randomStringOperation(numCharsLeft, rand) {
|
|
||||||
var result;
|
|
||||||
switch (rand.nextInt(9)) {
|
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
// insert char
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 1:
|
|
||||||
{
|
|
||||||
// delete char
|
|
||||||
result = {
|
|
||||||
remove: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
// skip char
|
|
||||||
result = {
|
|
||||||
skip: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
// insert small
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(rand.nextInt(4) + 1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 4:
|
|
||||||
{
|
|
||||||
// delete small
|
|
||||||
result = {
|
|
||||||
remove: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 5:
|
|
||||||
{
|
|
||||||
// skip small
|
|
||||||
result = {
|
|
||||||
skip: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 6:
|
|
||||||
{
|
|
||||||
// insert multiline;
|
|
||||||
result = {
|
|
||||||
insert: randomMultiline(5, 20, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 7:
|
|
||||||
{
|
|
||||||
// delete multiline
|
|
||||||
result = {
|
|
||||||
remove: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 8:
|
|
||||||
{
|
|
||||||
// skip multiline
|
|
||||||
result = {
|
|
||||||
skip: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 9:
|
|
||||||
{
|
|
||||||
// delete to end
|
|
||||||
result = {
|
|
||||||
remove: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 10:
|
|
||||||
{
|
|
||||||
// skip to end
|
|
||||||
result = {
|
|
||||||
skip: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var maxOrig = numCharsLeft - 1;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||||
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset")
|
||||||
|
|
||||||
exports.assert = function (code, optMsg) {
|
exports.assert = function (code, optMsg) {
|
||||||
if (!eval(code)) throw new Error("FALSE: " + (optMsg || code));
|
if (!eval(code)) throw new Error("FALSE: " + (optMsg || code));
|
||||||
};
|
};
|
||||||
|
@ -37,3 +40,229 @@ exports.throughSmartAssembler = function (opsStr) {
|
||||||
assem.endDocument();
|
assem.endDocument();
|
||||||
return assem.toString();
|
return assem.toString();
|
||||||
}
|
}
|
||||||
|
exports.poolOrArray = function (attribs) {
|
||||||
|
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
|
||||||
|
var p = new AttributePool();
|
||||||
|
attribs.forEach(function (kv) {
|
||||||
|
p.putAttrib(kv.split(','));
|
||||||
|
});
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.random = function () {
|
||||||
|
this.nextInt = function (maxValue) {
|
||||||
|
return Math.floor(Math.random() * maxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxValue is not used
|
||||||
|
this.nextDouble = function (maxValue) {
|
||||||
|
return Math.random();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.randomInlineString = function (len, rand) {
|
||||||
|
var assem = Changeset.stringAssembler();
|
||||||
|
for (var i = 0; i < len; i++) {
|
||||||
|
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
|
||||||
|
}
|
||||||
|
return assem.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.randomMultiline = function (approxMaxLines, approxMaxCols, rand) {
|
||||||
|
var numParts = rand.nextInt(approxMaxLines * 2) + 1;
|
||||||
|
var txt = Changeset.stringAssembler();
|
||||||
|
txt.append(rand.nextInt(2) ? '\n' : '');
|
||||||
|
for (var i = 0; i < numParts; i++) {
|
||||||
|
if ((i % 2) == 0) {
|
||||||
|
if (rand.nextInt(10)) {
|
||||||
|
txt.append(exports.randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
|
||||||
|
} else {
|
||||||
|
txt.append('\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
txt.append('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return txt.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.randomStringOperation = function (numCharsLeft, rand) {
|
||||||
|
var result;
|
||||||
|
switch (rand.nextInt(9)) {
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
// insert char
|
||||||
|
result = {
|
||||||
|
insert: exports.randomInlineString(1, rand)
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
// delete char
|
||||||
|
result = {
|
||||||
|
remove: 1
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
// skip char
|
||||||
|
result = {
|
||||||
|
skip: 1
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
{
|
||||||
|
// insert small
|
||||||
|
result = {
|
||||||
|
insert: exports.randomInlineString(rand.nextInt(4) + 1, rand)
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 4:
|
||||||
|
{
|
||||||
|
// delete small
|
||||||
|
result = {
|
||||||
|
remove: rand.nextInt(4) + 1
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 5:
|
||||||
|
{
|
||||||
|
// skip small
|
||||||
|
result = {
|
||||||
|
skip: rand.nextInt(4) + 1
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 6:
|
||||||
|
{
|
||||||
|
// insert multiline;
|
||||||
|
result = {
|
||||||
|
insert: exports.randomMultiline(5, 20, rand)
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 7:
|
||||||
|
{
|
||||||
|
// delete multiline
|
||||||
|
result = {
|
||||||
|
remove: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 8:
|
||||||
|
{
|
||||||
|
// skip multiline
|
||||||
|
result = {
|
||||||
|
skip: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// this is never tested
|
||||||
|
case 9:
|
||||||
|
{
|
||||||
|
// delete to end
|
||||||
|
result = {
|
||||||
|
remove: numCharsLeft
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// this is never tested
|
||||||
|
case 10:
|
||||||
|
{
|
||||||
|
// skip to end
|
||||||
|
result = {
|
||||||
|
skip: numCharsLeft
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var maxOrig = numCharsLeft - 1;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.randomTwoPropAttribs = function (opcode, rand) {
|
||||||
|
// assumes attrib pool like ['apple,','apple,true','banana,','banana,true']
|
||||||
|
if (opcode == '-' || rand.nextInt(3)) {
|
||||||
|
return '';
|
||||||
|
// always true
|
||||||
|
} else if (rand.nextInt(3)) {
|
||||||
|
if (opcode == '+' || rand.nextInt(2)) {
|
||||||
|
return '*' + Changeset.numToString(rand.nextInt(2) * 2 + 1);
|
||||||
|
} else {
|
||||||
|
return '*' + Changeset.numToString(rand.nextInt(2) * 2);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (opcode == '+' || rand.nextInt(4) == 0) {
|
||||||
|
return '*1*3';
|
||||||
|
} else {
|
||||||
|
return ['*0*2', '*0*3', '*1*2'][rand.nextInt(3)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.randomTestChangeset = function (origText, rand, withAttribs) {
|
||||||
|
var charBank = Changeset.stringAssembler();
|
||||||
|
var textLeft = origText; // always keep final newline
|
||||||
|
var outTextAssem = Changeset.stringAssembler();
|
||||||
|
var opAssem = Changeset.smartOpAssembler();
|
||||||
|
var oldLen = origText.length;
|
||||||
|
|
||||||
|
var nextOp = Changeset.newOp();
|
||||||
|
|
||||||
|
function appendMultilineOp(opcode, txt) {
|
||||||
|
nextOp.opcode = opcode;
|
||||||
|
if (withAttribs) {
|
||||||
|
nextOp.attribs = exports.randomTwoPropAttribs(opcode, rand);
|
||||||
|
}
|
||||||
|
txt.replace(/\n|[^\n]+/g, function (t) {
|
||||||
|
if (t == '\n') {
|
||||||
|
nextOp.chars = 1;
|
||||||
|
nextOp.lines = 1;
|
||||||
|
opAssem.append(nextOp);
|
||||||
|
} else {
|
||||||
|
nextOp.chars = t.length;
|
||||||
|
nextOp.lines = 0;
|
||||||
|
opAssem.append(nextOp);
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function doOp() {
|
||||||
|
var o = exports.randomStringOperation(textLeft.length, rand);
|
||||||
|
if (o.insert) {
|
||||||
|
var txt = o.insert;
|
||||||
|
charBank.append(txt);
|
||||||
|
outTextAssem.append(txt);
|
||||||
|
appendMultilineOp('+', txt);
|
||||||
|
} else if (o.skip) {
|
||||||
|
var txt = textLeft.substring(0, o.skip);
|
||||||
|
textLeft = textLeft.substring(o.skip);
|
||||||
|
outTextAssem.append(txt);
|
||||||
|
appendMultilineOp('=', txt);
|
||||||
|
} else if (o.remove) {
|
||||||
|
var txt = textLeft.substring(0, o.remove);
|
||||||
|
textLeft = textLeft.substring(o.remove);
|
||||||
|
appendMultilineOp('-', txt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (textLeft.length > 1) doOp();
|
||||||
|
for (var i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
|
||||||
|
var outText = outTextAssem.toString() + '\n';
|
||||||
|
opAssem.endDocument();
|
||||||
|
var cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
|
||||||
|
Changeset.checkRep(cs);
|
||||||
|
return [cs, outText];
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,11 @@ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualArrays = helper.assertEqualArrays;
|
var assertEqualArrays = helper.assertEqualArrays;
|
||||||
|
var random = helper.random;
|
||||||
|
var poolOrArray = helper.poolOrArray;
|
||||||
|
var randomMultiline = helper.randomMultiline;
|
||||||
|
var randomInlineString = helper.randomInlineString;
|
||||||
|
var randomTestChangeset = helper.randomTestChangeset;
|
||||||
|
|
||||||
describe("inverseRandom",function(){
|
describe("inverseRandom",function(){
|
||||||
it("inverseRandom",function(done){
|
it("inverseRandom",function(done){
|
||||||
|
@ -51,224 +56,4 @@ describe("inverseRandom",function(){
|
||||||
assertEqualArrays(origALines, alines);
|
assertEqualArrays(origALines, alines);
|
||||||
}
|
}
|
||||||
|
|
||||||
function random() {
|
|
||||||
this.nextInt = function (maxValue) {
|
|
||||||
return Math.floor(Math.random() * maxValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// maxValue is not used
|
|
||||||
this.nextDouble = function (maxValue) {
|
|
||||||
return Math.random();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function poolOrArray(attribs) {
|
|
||||||
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
|
|
||||||
var p = new AttributePool();
|
|
||||||
attribs.forEach(function (kv) {
|
|
||||||
p.putAttrib(kv.split(','));
|
|
||||||
});
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function randomMultiline(approxMaxLines, approxMaxCols, rand) {
|
|
||||||
var numParts = rand.nextInt(approxMaxLines * 2) + 1;
|
|
||||||
var txt = Changeset.stringAssembler();
|
|
||||||
txt.append(rand.nextInt(2) ? '\n' : '');
|
|
||||||
for (var i = 0; i < numParts; i++) {
|
|
||||||
if ((i % 2) == 0) {
|
|
||||||
if (rand.nextInt(10)) {
|
|
||||||
txt.append(randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return txt.toString();
|
|
||||||
}
|
|
||||||
function randomInlineString(len, rand) {
|
|
||||||
var assem = Changeset.stringAssembler();
|
|
||||||
for (var i = 0; i < len; i++) {
|
|
||||||
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
|
|
||||||
}
|
|
||||||
return assem.toString();
|
|
||||||
}
|
|
||||||
function randomTestChangeset(origText, rand, withAttribs) {
|
|
||||||
var charBank = Changeset.stringAssembler();
|
|
||||||
var textLeft = origText; // always keep final newline
|
|
||||||
var outTextAssem = Changeset.stringAssembler();
|
|
||||||
var opAssem = Changeset.smartOpAssembler();
|
|
||||||
var oldLen = origText.length;
|
|
||||||
|
|
||||||
var nextOp = Changeset.newOp();
|
|
||||||
|
|
||||||
function appendMultilineOp(opcode, txt) {
|
|
||||||
nextOp.opcode = opcode;
|
|
||||||
if (withAttribs) {
|
|
||||||
nextOp.attribs = randomTwoPropAttribs(opcode, rand);
|
|
||||||
}
|
|
||||||
txt.replace(/\n|[^\n]+/g, function (t) {
|
|
||||||
if (t == '\n') {
|
|
||||||
nextOp.chars = 1;
|
|
||||||
nextOp.lines = 1;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
} else {
|
|
||||||
nextOp.chars = t.length;
|
|
||||||
nextOp.lines = 0;
|
|
||||||
opAssem.append(nextOp);
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function doOp() {
|
|
||||||
var o = randomStringOperation(textLeft.length, rand);
|
|
||||||
if (o.insert) {
|
|
||||||
var txt = o.insert;
|
|
||||||
charBank.append(txt);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('+', txt);
|
|
||||||
} else if (o.skip) {
|
|
||||||
txt = textLeft.substring(0, o.skip);
|
|
||||||
textLeft = textLeft.substring(o.skip);
|
|
||||||
outTextAssem.append(txt);
|
|
||||||
appendMultilineOp('=', txt);
|
|
||||||
} else if (o.remove) {
|
|
||||||
txt = textLeft.substring(0, o.remove);
|
|
||||||
textLeft = textLeft.substring(o.remove);
|
|
||||||
appendMultilineOp('-', txt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (textLeft.length > 1) doOp();
|
|
||||||
for (var i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
|
|
||||||
var outText = outTextAssem.toString() + '\n';
|
|
||||||
opAssem.endDocument();
|
|
||||||
var cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
|
|
||||||
Changeset.checkRep(cs);
|
|
||||||
return [cs, outText];
|
|
||||||
}
|
|
||||||
function randomStringOperation(numCharsLeft, rand) {
|
|
||||||
var result;
|
|
||||||
switch (rand.nextInt(9)) {
|
|
||||||
case 0:
|
|
||||||
{
|
|
||||||
// insert char
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 1:
|
|
||||||
{
|
|
||||||
// delete char
|
|
||||||
result = {
|
|
||||||
remove: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
// skip char
|
|
||||||
result = {
|
|
||||||
skip: 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
{
|
|
||||||
// insert small
|
|
||||||
result = {
|
|
||||||
insert: randomInlineString(rand.nextInt(4) + 1, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 4:
|
|
||||||
{
|
|
||||||
// delete small
|
|
||||||
result = {
|
|
||||||
remove: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 5:
|
|
||||||
{
|
|
||||||
// skip small
|
|
||||||
result = {
|
|
||||||
skip: rand.nextInt(4) + 1
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 6:
|
|
||||||
{
|
|
||||||
// insert multiline;
|
|
||||||
result = {
|
|
||||||
insert: randomMultiline(5, 20, rand)
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 7:
|
|
||||||
{
|
|
||||||
// delete multiline
|
|
||||||
result = {
|
|
||||||
remove: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 8:
|
|
||||||
{
|
|
||||||
// skip multiline
|
|
||||||
result = {
|
|
||||||
skip: Math.round(numCharsLeft * rand.nextDouble() * rand.nextDouble())
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 9:
|
|
||||||
{
|
|
||||||
// delete to end
|
|
||||||
result = {
|
|
||||||
remove: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 10:
|
|
||||||
{
|
|
||||||
// skip to end
|
|
||||||
result = {
|
|
||||||
skip: numCharsLeft
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var maxOrig = numCharsLeft - 1;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
function randomTwoPropAttribs(opcode, rand) {
|
|
||||||
// assumes attrib pool like ['apple,','apple,true','banana,','banana,true']
|
|
||||||
if (opcode == '-' || rand.nextInt(3)) {
|
|
||||||
return '';
|
|
||||||
// always true
|
|
||||||
} else if (rand.nextInt(3)) {
|
|
||||||
if (opcode == '+' || rand.nextInt(2)) {
|
|
||||||
return '*' + Changeset.numToString(rand.nextInt(2) * 2 + 1);
|
|
||||||
} else {
|
|
||||||
return '*' + Changeset.numToString(rand.nextInt(2) * 2);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (opcode == '+' || rand.nextInt(4) == 0) {
|
|
||||||
return '*1*3';
|
|
||||||
} else {
|
|
||||||
return ['*0*2', '*0*3', '*1*2'][rand.nextInt(3)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
|
var poolOrArray = helper.poolOrArray;
|
||||||
describe("make attribs string",function(){
|
describe("make attribs string",function(){
|
||||||
it("make attribs string",function(done){
|
it("make attribs string",function(done){
|
||||||
testMakeAttribsString(1, ['bold,'], '+', [
|
testMakeAttribsString(1, ['bold,'], '+', [
|
||||||
|
@ -32,15 +31,3 @@ describe("make attribs string",function(){
|
||||||
assertEqualStrings(correctString, str);
|
assertEqualStrings(correctString, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
function poolOrArray(attribs) {
|
|
||||||
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
|
|
||||||
var p = new AttributePool();
|
|
||||||
attribs.forEach(function (kv) {
|
|
||||||
p.putAttrib(kv.split(','));
|
|
||||||
});
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
|
||||||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
var assertEqualArrays = helper.assertEqualArrays;
|
var assertEqualArrays = helper.assertEqualArrays;
|
||||||
var assert = helper.assert;
|
var assert = helper.assert;
|
||||||
|
var poolOrArray = helper.poolOrArray;
|
||||||
|
|
||||||
|
|
||||||
describe("other",function(){
|
describe("other",function(){
|
||||||
|
@ -147,21 +147,3 @@ describe("other",function(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function poolOrArray(attribs) {
|
|
||||||
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
|
|
||||||
var p = new AttributePool();
|
|
||||||
attribs.forEach(function (kv) {
|
|
||||||
p.putAttrib(kv.split(','));
|
|
||||||
});
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function literal(v) {
|
|
||||||
if ((typeof v) == "string") {
|
|
||||||
return '"' + v.replace(/[\\\"]/g, '\\$1').replace(/\n/g, '\\n') + '"';
|
|
||||||
} else
|
|
||||||
return JSON.stringify(v);
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,9 @@ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||||
var helper = require("./helper.js")
|
var helper = require("./helper.js")
|
||||||
var assertEqualStrings = helper.assertEqualStrings;
|
var assertEqualStrings = helper.assertEqualStrings;
|
||||||
var assertEqualArrays = helper.assertEqualArrays;
|
var assertEqualArrays = helper.assertEqualArrays;
|
||||||
|
var random = helper.random;
|
||||||
|
var randomMultiline = helper.randomMultiline;
|
||||||
|
var randomInlineString = helper.randomInlineString;
|
||||||
|
|
||||||
describe("testSplitJoinAttributionLines",function(){
|
describe("testSplitJoinAttributionLines",function(){
|
||||||
it("testSplitJoinAttributionLines",function(done){
|
it("testSplitJoinAttributionLines",function(done){
|
||||||
|
@ -35,32 +38,3 @@ describe("testSplitJoinAttributionLines",function(){
|
||||||
assertEqualStrings(theJoined, Changeset.joinAttributionLines(theSplit));
|
assertEqualStrings(theJoined, Changeset.joinAttributionLines(theSplit));
|
||||||
}
|
}
|
||||||
|
|
||||||
function random() {
|
|
||||||
this.nextInt = function (maxValue) {
|
|
||||||
return Math.floor(Math.random() * maxValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function randomMultiline(approxMaxLines, approxMaxCols, rand) {
|
|
||||||
var numParts = rand.nextInt(approxMaxLines * 2) + 1;
|
|
||||||
var txt = Changeset.stringAssembler();
|
|
||||||
txt.append(rand.nextInt(2) ? '\n' : '');
|
|
||||||
for (var i = 0; i < numParts; i++) {
|
|
||||||
if ((i % 2) == 0) {
|
|
||||||
if (rand.nextInt(10)) {
|
|
||||||
txt.append(randomInlineString(rand.nextInt(approxMaxCols) + 1, rand));
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
txt.append('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return txt.toString();
|
|
||||||
}
|
|
||||||
function randomInlineString(len, rand) {
|
|
||||||
var assem = Changeset.stringAssembler();
|
|
||||||
for (var i = 0; i < len; i++) {
|
|
||||||
assem.append(String.fromCharCode(rand.nextInt(26) + 97));
|
|
||||||
}
|
|
||||||
return assem.toString();
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue