Add Driver#setupCommands method

This commit is contained in:
Andrew Stewart 2014-08-11 14:10:59 -07:00
parent bde6132377
commit 1f0b214c97
2 changed files with 70 additions and 8 deletions

View File

@ -31,6 +31,26 @@ var Driver = module.exports = function Driver(opts) {
Utils.subclass(Driver, Basestar);
Driver.prototype.setupCommands = function(commands) {
this.proxyMethods(commands, this.connection, this);
for (var i = 0; i < commands.length; i++) {
var command = commands[i];
var snake_case = command.replace(/[A-Z]+/g, function(match) {
if (match.length > 1) {
match = match.replace(/[A-Z]$/, function(m) {
return "_" + m.toLowerCase();
});
}
return "_" + match.toLowerCase();
}).replace(/^_/, '');
this.commands[snake_case] = this[command].bind(this);
}
}
// Public: Starts up the driver, and triggers the provided callback when done.
//
// callback - function to run when the driver is started

View File

@ -7,14 +7,18 @@ var Driver = source("driver"),
Utils = source('utils');
describe("Driver", function() {
var device = {
connection: {},
emit: spy()
};
var device, driver;
var driver = new Driver({
name: 'driver',
device: device
beforeEach(function() {
device = {
connection: {},
emit: spy()
};
driver = new Driver({
name: 'driver',
device: device
});
});
describe("#constructor", function() {
@ -30,11 +34,49 @@ describe("Driver", function() {
expect(driver.connection).to.be.eql(device.connection);
});
it("sets @commands to an empty array by default", function() {
it("sets @commands to an empty object by default", function() {
expect(driver.commands).to.be.eql({});
});
});
describe("#setupCommands", function() {
beforeEach(function() {
driver.proxyMethods = spy();
});
it("snake_cases and proxies methods to @commands for the API", function() {
var commands = ["helloWorld", "otherTestCommand"],
snake_case = ["hello_world", "other_test_command"];
commands.forEach(function(cmd) {
driver[cmd] = spy();
});
driver.setupCommands(commands);
for (var i = 0; i < commands.length; i++) {
var cmd = commands[i],
snake = snake_case[i];
driver.commands[snake]();
expect(driver[cmd]).to.be.called;
}
});
it("handles edge cases", function() {
var commands = ["HelloWorld", "getPNGStream", "getHSetting"],
snake_case = ["hello_world", "get_png_stream", "get_h_setting"];
commands.forEach(function(cmd) {
driver[cmd] = function() {};
});
driver.setupCommands(commands);
expect(Object.keys(driver.commands)).to.be.eql(snake_case);
});
});
describe("#start", function() {
var callback;