Skip to content

Commit

Permalink
Add more compose tests
Browse files Browse the repository at this point in the history
Iterator next() at end will return Infinity again so callers can tell if
a retain is explicit or implied. Behavior differs when composing.
  • Loading branch information
jhchen committed Sep 2, 2014
1 parent 9bdc9b2 commit 4220d2d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 66 deletions.
20 changes: 11 additions & 9 deletions lib/delta.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,22 @@ Delta.prototype.compose = function(other) {
var length = Math.min(thisIter.peekLength(), otherIter.peekLength());
var thisOp = thisIter.next(length);
var otherOp = otherIter.next(length);
if (is.number(otherOp.delete)) {
if (is.number(thisOp.retain) && otherOp.retain !== Infinity) {
this._push(otherOp);
}
} else {
// Other op must be a retain
if (is.number(otherOp.retain)) {
var newOp = {};
if (is.number(thisOp.retain)) newOp.retain = thisOp.retain;
// Embed inserts do not have insert key so cannot check isInsert
if (is.string(thisOp.insert)) newOp.insert = thisOp.insert;
if (is.number(thisOp.retain)) {
newOp.retain = length;
} else if (is.string(thisOp.insert)) {
newOp.insert = thisOp.insert;
}
// Avoid being assigned null or undefined
if (is.object(thisOp.formats) || is.object(otherOp.formats)) {
newOp.formats = op.composeFormats(thisOp.formats, otherOp.formats);
}
this._push(newOp);
// If other op is a delete, we generally cancel each other out,
// but if we are an implied retain
} else if (is.number(otherOp.delete) && thisOp.retain === Infinity) {
this._push(otherOp);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Iterator.prototype.next = function(length) {
return retOp;
}
} else {
return { retain: length };
return { retain: Infinity };
}
};

Expand Down
132 changes: 77 additions & 55 deletions test/delta/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,90 @@ var expect = require('chai').expect;


describe('compose', function() {
describe('single ops', function() {
it('insert + insert', function() {
var a = new Delta().insert('A');
var b = new Delta().insert('B');
var expected = new Delta().insert('B').insert('A');
expect(a.compose(b)).to.deep.equal(expected);
});
it('insert + insert', function() {
var a = new Delta().insert('A');
var b = new Delta().insert('B');
var expected = new Delta().insert('B').insert('A');
expect(a.compose(b)).to.deep.equal(expected);
});

it('insert + retain', function() {
var a = new Delta().insert('A');
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().insert('A', { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});

it('insert + delete', function() {
var a = new Delta().insert('A');
var b = new Delta().delete(-1);
var expected = new Delta();
expect(a.compose(b)).to.deep.equal(expected);
});

it('delete + insert', function() {
var a = new Delta().delete(-1);
var b = new Delta().insert('B');
var expected = new Delta().insert('B').delete(-1);
expect(a.compose(b)).to.deep.equal(expected);
});

it('insert + retain', function() {
var a = new Delta().insert('A');
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().insert('A', { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});
it('delete + retain', function() {
var a = new Delta().delete(-1);
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().delete(-1).retain(1, { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});

it('insert + delete', function() {
var a = new Delta().insert('A');
var b = new Delta().delete(-1);
var expected = new Delta();
expect(a.compose(b)).to.deep.equal(expected);
});
it('delete + delete', function() {
var a = new Delta().delete(-1);
var b = new Delta().delete(-1);
var expected = new Delta().delete(-2);
expect(a.compose(b)).to.deep.equal(expected);
});

it('delete + insert', function() {
var a = new Delta().delete(-1);
var b = new Delta().insert('B');
var expected = new Delta().insert('B').delete(-1);
expect(a.compose(b)).to.deep.equal(expected);
});
it('retain + insert', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().insert('B');
var expected = new Delta().insert('B').retain(1, { color: 'blue' });
expect(a.compose(b)).to.deep.equal(expected);
});

it('delete + retain', function() {
var a = new Delta().delete(-1);
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().delete(-1).retain(1, { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});
it('retain + retain', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().retain(1, { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});

it('delete + delete', function() {
var a = new Delta().delete(-1).retain(1); // Need end length to be 1 to apply another delete
var b = new Delta().delete(-1);
var expected = new Delta().delete(-1).delete(-1);
expect(a.compose(b)).to.deep.equal(expected);
});
it('retain + delete', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().delete(-1);
var expected = new Delta();
expect(a.compose(b)).to.deep.equal(expected);
});

it('retain + insert', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().insert('B');
var expected = new Delta().insert('B').retain(1, { color: 'blue' });
expect(a.compose(b)).to.deep.equal(expected);
});
it('insert in middle of text', function() {
var a = new Delta().insert('Hello');
var b = new Delta().retain(3).insert('X');
var expected = new Delta().insert('HelXlo');
expect(a.compose(b)).to.deep.equal(expected);
});

it('retain + retain', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().retain(1, { bold: true, color: 'red' });
var expected = new Delta().retain(1, { bold: true, color: 'red' });
expect(a.compose(b)).to.deep.equal(expected);
});
it('insert and delete ordering', function() {
var a = new Delta().insert('Hello');
var b = new Delta().insert('Hello');
var insertFirst = new Delta().retain(3).insert('X').delete(-1);
var deleteFirst = new Delta().retain(3).delete(-1).insert('X');
var expected = new Delta().insert('HelXo');
expect(a.compose(insertFirst)).to.deep.equal(expected);
expect(b.compose(deleteFirst)).to.deep.equal(expected);
});

it('retain + delete', function() {
var a = new Delta().retain(1, { color: 'blue' });
var b = new Delta().delete(-1);
var expected = new Delta().delete(-1);
expect(a.compose(b)).to.deep.equal(expected);
});
it('delete entire text', function() {
var a = new Delta().retain(4).insert('Hello');
var b = new Delta().delete(-9);
var expected = new Delta();
expect(a.compose(b)).to.deep.equal(expected);
});
});
2 changes: 1 addition & 1 deletion test/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('op', function() {
expect(iter.next()).to.deep.equal(this.delta.ops[i]);
}
expect(iter.next()).to.deep.equal({ retain: Infinity });
expect(iter.next(4)).to.deep.equal({ retain: 4 });
expect(iter.next(4)).to.deep.equal({ retain: Infinity });
expect(iter.next()).to.deep.equal({ retain: Infinity });
});

Expand Down

0 comments on commit 4220d2d

Please sign in to comment.