Proxy all opts that aren't already used internally

This commit is contained in:
Andrew Stewart 2014-10-27 13:40:38 -07:00
parent d65742bb49
commit a8b16a3469
2 changed files with 51 additions and 20 deletions

View File

@ -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') {

View File

@ -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() {