mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-28 11:26:16 -04: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
|
@ -2,6 +2,11 @@ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
|||
var AttributePool = require("ep_etherpad-lite/static/js/AttributePool");
|
||||
var helper = require("./helper.js")
|
||||
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(){
|
||||
it("inverseRandom",function(done){
|
||||
|
@ -51,224 +56,4 @@ describe("inverseRandom",function(){
|
|||
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)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue