Changeset: Migrate from smartOpAssembler() to canonicalizeOps()

This commit is contained in:
Richard Hansen 2021-11-23 21:31:38 -05:00
parent 23e7809b4a
commit d3d2090ca5
8 changed files with 162 additions and 164 deletions

View file

@ -168,26 +168,22 @@ const randomTestChangeset = (origText, withAttribs) => {
const charBank = Changeset.stringAssembler();
let textLeft = origText; // always keep final newline
const outTextAssem = Changeset.stringAssembler();
const opAssem = Changeset.smartOpAssembler();
const ops = [];
const oldLen = origText.length;
const nextOp = new Changeset.Op();
const appendMultilineOp = (opcode, txt) => {
nextOp.opcode = opcode;
if (withAttribs) {
nextOp.attribs = randomTwoPropAttribs(opcode);
}
const attribs = withAttribs ? randomTwoPropAttribs(opcode) : '';
txt.replace(/\n|[^\n]+/g, (t) => {
const nextOp = new Changeset.Op(opcode);
nextOp.attribs = attribs;
if (t === '\n') {
nextOp.chars = 1;
nextOp.lines = 1;
opAssem.append(nextOp);
} else {
nextOp.chars = t.length;
nextOp.lines = 0;
opAssem.append(nextOp);
}
ops.push(nextOp);
return '';
});
};
@ -214,8 +210,8 @@ const randomTestChangeset = (origText, withAttribs) => {
while (textLeft.length > 1) doOp();
for (let i = 0; i < 5; i++) doOp(); // do some more (only insertions will happen)
const outText = `${outTextAssem.toString()}\n`;
opAssem.endDocument();
const cs = Changeset.pack(oldLen, outText.length, opAssem.toString(), charBank.toString());
const serializedOps = Changeset.serializeOps(Changeset.canonicalizeOps(ops, true));
const cs = Changeset.pack(oldLen, outText.length, serializedOps, charBank.toString());
Changeset.checkRep(cs);
return [cs, outText];
};

View file

@ -8,20 +8,18 @@ describe('easysync-assembler', function () {
expect(Changeset.serializeOps(Changeset.deserializeOps(x))).to.equal(x);
});
it('smartOpAssembler', async function () {
it('canonicalizeOps', async function () {
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
const assem = Changeset.smartOpAssembler();
for (const op of Changeset.deserializeOps(x)) assem.append(op);
assem.endDocument();
expect(assem.toString()).to.equal(x);
expect(Changeset.serializeOps(Changeset.canonicalizeOps(Changeset.deserializeOps(x), true)))
.to.equal(x);
});
describe('append atext to assembler', function () {
const testAppendATextToAssembler = (testId, atext, correctOps) => {
it(`testAppendATextToAssembler#${testId}`, async function () {
const assem = Changeset.smartOpAssembler();
for (const op of Changeset.opsFromAText(atext)) assem.append(op);
expect(assem.toString()).to.equal(correctOps);
const serializedOps =
Changeset.serializeOps(Changeset.canonicalizeOps(Changeset.opsFromAText(atext), false));
expect(serializedOps).to.equal(correctOps);
});
};

View file

@ -15,37 +15,36 @@ describe('easysync-mutations', function () {
};
const mutationsToChangeset = (oldLen, arrayOfArrays) => {
const assem = Changeset.smartOpAssembler();
const op = new Changeset.Op();
const bank = Changeset.stringAssembler();
let oldPos = 0;
let newLen = 0;
arrayOfArrays.forEach((a) => {
if (a[0] === 'skip') {
op.opcode = '=';
op.chars = a[1];
op.lines = (a[2] || 0);
assem.append(op);
oldPos += op.chars;
newLen += op.chars;
} else if (a[0] === 'remove') {
op.opcode = '-';
op.chars = a[1];
op.lines = (a[2] || 0);
assem.append(op);
oldPos += op.chars;
} else if (a[0] === 'insert') {
op.opcode = '+';
bank.append(a[1]);
op.chars = a[1].length;
op.lines = (a[2] || 0);
assem.append(op);
newLen += op.chars;
const ops = (function* () {
for (const a of arrayOfArrays) {
const op = new Changeset.Op();
if (a[0] === 'skip') {
op.opcode = '=';
op.chars = a[1];
op.lines = (a[2] || 0);
oldPos += op.chars;
newLen += op.chars;
} else if (a[0] === 'remove') {
op.opcode = '-';
op.chars = a[1];
op.lines = (a[2] || 0);
oldPos += op.chars;
} else if (a[0] === 'insert') {
op.opcode = '+';
bank.append(a[1]);
op.chars = a[1].length;
op.lines = (a[2] || 0);
newLen += op.chars;
}
yield op;
}
});
})();
const serializedOps = Changeset.serializeOps(Changeset.canonicalizeOps(ops, true));
newLen += oldLen - oldPos;
assem.endDocument();
return Changeset.pack(oldLen, newLen, assem.toString(), bank.toString());
return Changeset.pack(oldLen, newLen, serializedOps, bank.toString());
};
const runMutationTest = (testId, origLines, muts, correct) => {