Skip to content

Commit

Permalink
Fix #9: Throw if an unknown node is involved
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed May 13, 2016
1 parent 60df92c commit de225fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -29,6 +29,10 @@ function toposort(nodes, edges) {
throw new Error('Cyclic dependency: '+JSON.stringify(node))
}

if (!~nodes.indexOf(node)) {
throw new Error('Found unknown node. Make sure to provided all involved nodes. Unknown node: '+JSON.stringify(node))
}

if (visited[i]) return;
visited[i] = true

Expand Down
16 changes: 15 additions & 1 deletion test.js
Expand Up @@ -80,6 +80,20 @@ suite.addBatch(
assert.instanceOf(val, Error)
}
}
, 'unknown nodes in edges':
{ topic: function() {
return toposort.array(['bla']
[ ["foo", 'bar']
, ["bar", "ron"]
, ["john", "bar"]
, ["tom", "john"]
, ["ron", "tom"]
])
}
, 'should throw an exception': function(_, val) {
assert.instanceOf(val, Error)
}
}
, 'triangular dependency':
{ topic: function() {
/*
Expand Down Expand Up @@ -121,4 +135,4 @@ suite.addBatch(
})
.run(null, function() {
(suite.results.broken+suite.results.errored) > 0 && process.exit(1)
})
})

0 comments on commit de225fa

Please sign in to comment.