From d894169d5531c167356557eba58881d7dedf51a6 Mon Sep 17 00:00:00 2001 From: Nikolas Poniros Date: Tue, 26 Apr 2016 07:47:18 +0200 Subject: [PATCH] Change setNodeData to throw an Error The setNodeData method should throw an error if we try to set data for a non-existing node. Update tests and README --- README.md | 14 +++++++------- lib/dep_graph.js | 4 +++- specs/dep_graph_spec.js | 10 +++++----- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 85f94c8..97bf0ae 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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' \ No newline at end of file + + graph.getNodeData('d'); // 'newData' diff --git a/lib/dep_graph.js b/lib/dep_graph.js index f4209ac..9a56642 100644 --- a/lib/dep_graph.js +++ b/lib/dep_graph.js @@ -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); } }, /** diff --git a/specs/dep_graph_spec.js b/specs/dep_graph_spec.js index c811f4e..89aab2a 100644 --- a/specs/dep_graph_spec.js +++ b/specs/dep_graph_spec.js @@ -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 () { @@ -299,4 +299,4 @@ describe('DepGraph', function () { expect(graph.dependenciesOf('a')).toEqual(['b']); }); -}); \ No newline at end of file +});