Use _.result to get commands from options.

This commit is contained in:
Andrew Stewart 2014-12-17 15:07:03 -08:00
parent 6cbf2db122
commit c9e16fe8c2
2 changed files with 7 additions and 13 deletions

View File

@ -84,20 +84,14 @@ var Robot = module.exports = function Robot(opts) {
}, this); }, this);
if (opts.commands) { if (opts.commands) {
var cmds = opts.commands; var cmds = _.result(opts, "commands");
if (_.isObject(cmds)) { if (_.isObject(cmds) && !_.isArray(cmds)) {
this.commands = cmds; this.commands = cmds;
} } else {
var err = "#commands must be an object ";
if (_.isFunction(cmds)) { err += "or a function that returns an object";
var result = cmds.call(this, this); throw new Error(err);
if (_.isObject(result) && !_.isArray(result)) {
this.commands = result;
} else {
throw new Error("#commands function must return an object");
}
} }
} }

View File

@ -139,7 +139,7 @@ describe("Robot", function() {
it("throws an error", function() { it("throws an error", function() {
expect(fn).to.throw( expect(fn).to.throw(
Error, Error,
"#commands function must return an object" "#commands must be an object or a function that returns an object"
); );
}); });
}); });