diff --git a/lib/robot.js b/lib/robot.js index ba93797..3956e26 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -102,31 +102,37 @@ var Robot = module.exports = function Robot(opts) { } for (var n in opts) { - var opt = opts[n], - reserved = ['connection', 'connections', 'device', 'devices', 'work', 'commands']; + var opt = opts[n]; - if (reserved.indexOf(n) < 0) { - this[n] = opt; + if (this[n] !== undefined) { + continue; + } - if (opts.commands == null && typeof(opt) === 'function') { - this.commands[n] = opt; + this[n] = opt; + + if (typeof opt === 'function' && opts.commands == null) { + this.commands[n] = opt; + } + } + + if (opts.commands) { + var cmds = opts.commands; + + if (typeof cmds === 'object') { + this.commands = cmds; + } + + if (typeof cmds === 'function') { + var result = cmds.call(this, this); + + if (typeof result === 'object' && !Array.isArray(result)) { + this.commands = result; + } else { + throw new Error("#commands function must return an object"); } } } - if (typeof opts.commands === 'function') { - var result = opts.commands.call(this, this); - if (typeof result === 'object' && !Array.isArray(result)) { - this.commands = result; - } else { - throw new Error("commands must be an object or a function that returns an object"); - } - } - - if (typeof opts.commands === 'object') { - this.commands = opts.commands; - } - var mode = Utils.fetch(Config, 'mode', 'manual'); if (mode === 'auto') { diff --git a/spec/lib/robot.spec.js b/spec/lib/robot.spec.js index 541f7d3..be93fd5 100644 --- a/spec/lib/robot.spec.js +++ b/spec/lib/robot.spec.js @@ -141,7 +141,7 @@ describe("Robot", function() { }); it("throws an error", function() { - expect(fn).to.throw(Error, "commands must be an object or a function that returns an object"); + expect(fn).to.throw(Error, "#commands function must return an object"); }); }) }); @@ -165,6 +165,31 @@ describe("Robot", function() { expect(robot.commands.say_hello).to.be.a('function'); }); }); + + context("arbitrary arguments", function() { + beforeEach(function() { + robot = new Robot({ + name: 'NewBot', + + hiThere: 'hi there', + + sayHi: function() { + return 'hi'; + }, + + start: "start" + }) + }); + + it("passes them through if they don't conflict with built-ins", function() { + expect(robot.hiThere).to.be.eql("hi there"); + expect(robot.sayHi()).to.be.eql("hi"); + }); + + it("doesn't work if they conflict with built-in properties", function() { + expect(robot.start).to.be.a('function'); + }); + }); }); describe("all work and no play", function() {