Graph#revert (see #537)

This commit is contained in:
Bryan Housel
2015-04-26 01:07:48 -04:00
parent 3b18f1d33a
commit 6b18066dd6
2 changed files with 72 additions and 7 deletions
+10
View File
@@ -251,6 +251,16 @@ iD.Graph.prototype = {
});
},
revert: function(entity) {
if (this.entities[entity.id] === this.base().entities[entity.id])
return this;
return this.update(function() {
this._updateCalculated(entity, this.base().entities[entity.id]);
delete this.entities[entity.id];
});
},
update: function() {
var graph = this.frozen ? iD.Graph(this, true) : this;
+62 -7
View File
@@ -334,28 +334,83 @@ describe('iD.Graph', function() {
expect(graph.replace(w1).parentWays(node)).to.eql([w1]);
});
it("adds parentRels", function () {
it("adds parentRelations", function () {
var node = iD.Node({id: 'n' }),
r1 = iD.Relation({id: 'w', members: [{id: 'n'}]}),
graph = iD.Graph([node]);
r1 = iD.Relation({id: 'r', members: [{id: 'n'}]}),
graph = iD.Graph([node]);
expect(graph.replace(r1).parentRelations(node)).to.eql([r1]);
});
it("removes parentRelations", function () {
var node = iD.Node({id: 'n' }),
r1 = iD.Relation({id: 'w', members: [{id: 'n'}]}),
graph = iD.Graph([node, r1]);
r1 = iD.Relation({id: 'r', members: [{id: 'n'}]}),
graph = iD.Graph([node, r1]);
expect(graph.remove(r1).parentRelations(node)).to.eql([]);
});
it("doesn't add duplicate parentRelations", function () {
var node = iD.Node({id: 'n' }),
r1 = iD.Relation({id: 'w', members: [{id: 'n'}]}),
graph = iD.Graph([node, r1]);
r1 = iD.Relation({id: 'r', members: [{id: 'n'}]}),
graph = iD.Graph([node, r1]);
expect(graph.replace(r1).parentRelations(node)).to.eql([r1]);
});
});
describe("#revert", function () {
it("is a no-op if the entity is identical to the base entity", function () {
var n1 = iD.Node({id: 'n' }),
graph = iD.Graph([n1]);
expect(graph.revert(n1)).to.equal(graph);
});
it("returns a new graph", function () {
var n1 = iD.Node({id: 'n' }),
n2 = n1.update({}),
graph = iD.Graph([n1]).replace(n2);
expect(graph.revert(n2)).not.to.equal(graph);
});
it("doesn't modify the receiver", function () {
var n1 = iD.Node({id: 'n' }),
n2 = n1.update({}),
graph = iD.Graph([n1]).replace(n2);
graph.revert(n2);
expect(graph.entity(n2.id)).to.equal(n2);
});
it("reverts the entity to the base version", function () {
var n1 = iD.Node({id: 'n' }),
n2 = n1.update({}),
graph = iD.Graph([n1]).replace(n2);
expect(graph.entity('n')).to.equal(n2);
graph = graph.revert(n2);
expect(graph.entity('n')).to.equal(n1);
});
it("reverts parentWays", function () {
var n1 = iD.Node({id: 'n' }),
w1 = iD.Way({id: 'w', nodes: ['n']}),
w2 = w1.removeNode('n'),
graph = iD.Graph([n1, w1]).replace(w2);
expect(graph.parentWays(graph.entity('n'))).to.eql([]);
graph = graph.revert(w2);
expect(graph.parentWays(graph.entity('n'))).to.eql([w1]);
});
it("reverts parentRelations", function () {
var n1 = iD.Node({id: 'n' }),
r1 = iD.Relation({id: 'r', members: [{id: 'n'}]}),
r2 = r1.removeMembersWithID('n'),
graph = iD.Graph([n1, r1]).replace(r2);
expect(graph.parentRelations(graph.entity('n'))).to.eql([]);
graph = graph.revert(r2);
expect(graph.parentRelations(graph.entity('n'))).to.eql([r1]);
});
});
describe("#update", function () {
it("returns a new graph if self is frozen", function () {
var graph = iD.Graph();