Skip to content

Commit

Permalink
Change setNodeData to throw an Error
Browse files Browse the repository at this point in the history
The setNodeData method should throw an error if we try to set data for a
non-existing node.

Update tests and README
  • Loading branch information
nponiros committed Apr 26, 2016
1 parent 5b027db commit d894169
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Nodes in the graph are just simple strings with optional data associated with th
- `addNode(name, data)` - add a node in the graph with optional data. If `data` is not given, `name` will be used as data
- `removeNode(name)` - remove a node from the graph
- `hasNode(name)` - check if a node exists in the graph
- `getNodeData(name)` - get the data associated with a node (will throw an Error if the node does not exist)
- `setNodeData(name, data)` - set the data for an existing node. It will do nothing if the node does not exist
- `getNodeData(name)` - get the data associated with a node (will throw an Error if the node does not exist)
- `setNodeData(name, data)` - set the data for an existing node (will throw an Error if the node does not exist)
- `addDependency(from, to)` - add a dependency between two nodes (will throw an Error if one of the nodes does not exist)
- `removeDependency(from, to)` - remove a dependency between two nodes
- `dependenciesOf(name, leavesOnly)` - get an array containing the nodes that the specified node depends on (transitively). If `leavesOnly` is true, only nodes that do not depend on any other nodes will be returned in the array.
Expand All @@ -45,11 +45,11 @@ Dependency Cycles are detected when running `dependenciesOf`, `dependantsOf`, an

graph.overallOrder(); // ['c', 'b', 'a']
graph.overallOrder(true); // ['c']

graph.addNode('d', 'data');

graph.getNodeData('d'); // 'data'

graph.setNodeData('d', 'newData');
graph.getNodeData('d'); // 'newData'

graph.getNodeData('d'); // 'newData'
4 changes: 3 additions & 1 deletion lib/dep_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ DepGraph.prototype = {
}
},
/**
* Set the associated data for a given node name. If the node does not exist, this method will do nothing
* Set the associated data for a given node name. If the node does not exist, this method will throw an error
*/
setNodeData:function (node, data) {
if (this.hasNode(node)) {
this.nodes[node] = data;
} else {
throw new Error('Node does not exist: ' + node);
}
},
/**
Expand Down
10 changes: 5 additions & 5 deletions specs/dep_graph_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ describe('DepGraph', function () {
expect(graph.getNodeData('Foo')).toBe('data2');
});

it('should do nothing if we try to set data for a non-existing node', function () {
it('should throw an error if we try to set data for a non-existing node', function () {
var graph = new DepGraph();

graph.setNodeData('Foo', 'data');

expect(graph.hasNode('Foo')).toBe(false);
expect(function () {
graph.setNodeData('Foo', 'data');
}).toThrow(new Error('Node does not exist: Foo'));
});

it('should throw an error if the node does not exists and we try to get data', function () {
Expand Down Expand Up @@ -299,4 +299,4 @@ describe('DepGraph', function () {
expect(graph.dependenciesOf('a')).toEqual(['b']);
});

});
});

0 comments on commit d894169

Please sign in to comment.