Skip to content

Commit

Permalink
Bug fixes for parent handling in compound graph
Browse files Browse the repository at this point in the history
  • Loading branch information
cpettitt committed Sep 24, 2014
1 parent ff30b5e commit f8a1809
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/base-compound-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function mixin(prototype) {
this.setNode(v);
}

if (!this.hasNode(parent)) {
if (parent !== undefined && !this.hasNode(parent)) {
this.setNode(parent);
}

Expand All @@ -22,7 +22,13 @@ function mixin(prototype) {
ancestor = this.getParent(ancestor);
}

this._nestingTree.setEdge(parent, v);
_.each(this._nestingTree.predecessors(v), function(u) {
this._nestingTree.removeEdge(u, v);
}, this);

if (parent !== undefined) {
this._nestingTree.setEdge(parent, v);
}
};

prototype.getParent = function(v) {
Expand Down Expand Up @@ -54,6 +60,7 @@ function mixin(prototype) {
_.each(this._nestingTree.successors(v), function(w) {
this.removeNode(w);
}, this);
this._nestingTree.removeNode(v);
oldOnRemoveNode.call(this, v);
};
}
18 changes: 18 additions & 0 deletions test/base-compound-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ function tests(GraphConstructor) {
expect(g.getParent("n1")).to.equal("parent");
});

it("sets the parent to undefined if the parent is undefined", function() {
g.setNode("n1");
g.setParent("n1", "parent");
g.setParent("n1");
expect(g.getParent("n1")).to.be.undefined;
// We should also not create an "undefined" node
expect(_.sortBy(g.nodeIds())).to.eql(["n1", "parent"]);
});

it("removes the previous parent", function() {
g.setNode("n1");
g.setParent("n1", "parent1");
g.setParent("n1", "parent2");
expect(g.getParent("n1")).to.equal("parent2");
expect(g.getChildren("parent1")).to.be.empty;
});

it("does not allow a cycle in the nesting tree", function() {
g.setParent("b", "a");
expect(function() { g.setParent("a", "b"); }).to.throw();
Expand All @@ -85,6 +102,7 @@ function tests(GraphConstructor) {
expect(g.hasNode("n4")).to.be.false;
expect(g.hasNode("root")).to.be.true;
expect(g.hasNode("other")).to.be.true;
expect(g.getNestingTree().hasNode("n1")).to.be.false;
});
});

Expand Down

0 comments on commit f8a1809

Please sign in to comment.