Proxy methods after #connect/#start

This commit is contained in:
Andrew Stewart 2014-10-01 17:58:14 -07:00
parent 198a792b22
commit 35c3a4b9e9
4 changed files with 54 additions and 50 deletions

View File

@ -39,12 +39,6 @@ var Connection = module.exports = function Connection(opts) {
this.details[opt] = opts[opt]; this.details[opt] = opts[opt];
} }
} }
for (var opt in this.adaptor) {
if (!this[opt] && typeof this.adaptor[opt] === 'function') {
this[opt] = this.adaptor[opt].bind(this.adaptor);
}
}
}; };
Utils.subclass(Connection, EventEmitter); Utils.subclass(Connection, EventEmitter);
@ -68,7 +62,15 @@ Connection.prototype.toJSON = function() {
Connection.prototype.connect = function(callback) { Connection.prototype.connect = function(callback) {
var msg = this._logstring("Connecting to"); var msg = this._logstring("Connecting to");
Logger.info(msg); Logger.info(msg);
this.adaptor.connect(callback); this.adaptor.connect(function() {
for (var opt in this.adaptor) {
if (!this[opt] && typeof this.adaptor[opt] === 'function') {
this[opt] = this.adaptor[opt].bind(this.adaptor);
}
}
callback.apply(this, arguments);
}.bind(this));
}; };
// Public: Disconnect the adaptor's connection // Public: Disconnect the adaptor's connection

View File

@ -44,12 +44,6 @@ var Device = module.exports = function Device(opts) {
this.details[opt] = opts[opt]; this.details[opt] = opts[opt];
} }
} }
for (var opt in this.driver) {
if (!this[opt] && typeof this.driver[opt] === 'function') {
this[opt] = this.driver[opt].bind(this.driver);
}
}
}; };
Utils.subclass(Device, EventEmitter); Utils.subclass(Device, EventEmitter);
@ -69,7 +63,15 @@ Device.prototype.start = function(callback) {
msg += "."; msg += ".";
Logger.info(msg); Logger.info(msg);
this.driver.start(callback); this.driver.start(function() {
for (var opt in this.driver) {
if (!this[opt] && typeof this.driver[opt] === 'function') {
this[opt] = this.driver[opt].bind(this.driver);
}
}
callback.apply(this, arguments);
}.bind(this));
}; };
// Public: Halt the device driver // Public: Halt the device driver

View File

@ -31,20 +31,6 @@ describe("Connection", function() {
expect(connection.port.toString()).to.be.eql("/dev/null"); expect(connection.port.toString()).to.be.eql("/dev/null");
}); });
it("proxies methods from the Adaptor", function() {
Loopback.prototype.test = function() { return "Test" };
robot = new Robot({
name: "Robby",
connection: { name: 'loopback', adaptor: 'loopback', port: "/dev/null" }
});
connection = robot.connections.loopback;
expect(connection.test()).to.be.eql("Test");
delete Loopback.prototype.test;
})
}); });
describe("#toJSON", function() { describe("#toJSON", function() {
@ -72,27 +58,38 @@ describe("Connection", function() {
}); });
describe("#connect", function() { describe("#connect", function() {
var callback = function() { }; var callback;
beforeEach(function() { beforeEach(function() {
callback = spy();
Loopback.prototype.test = function() { return "Test" };
stub(Logger, 'info').returns(true); stub(Logger, 'info').returns(true);
connection.adaptor.connect = stub().returns(true); connection.adaptor.connect = stub();
connection.connect(callback); connection.connect(callback);
}); });
afterEach(function() { afterEach(function() {
Logger.info.restore(); Logger.info.restore();
}); delete Loopback.prototype.test;
})
it("logs that it's connecting the device", function() { it("logs that it's connecting the device", function() {
var message = "Connecting to 'loopback' on port /dev/null."; var message = "Connecting to 'loopback' on port /dev/null.";
expect(Logger.info).to.be.calledWith(message); expect(Logger.info).to.be.calledWith(message);
}); });
it("calls the adaptor's connect method with the provided callback", function() { it("triggers the provided callback after the adaptor finishes connecting", function() {
expect(connection.adaptor.connect).to.be.calledWith(callback); expect(callback).to.not.be.called;
connection.adaptor.connect.yield();
expect(callback).to.be.called;
}); });
it("proxies methods from the Adaptor", function() {
connection.adaptor.connect.yield();
expect(connection.test()).to.be.eql("Test");
})
}); });
describe("#disconnect", function() { describe("#disconnect", function() {

View File

@ -59,39 +59,42 @@ describe("Device", function() {
initDriver.restore(); initDriver.restore();
}); });
it("binds driver methods to the device", function() {
expect(device.string).to.be.eql(undefined);
device.cmd();
expect(driver.cmd).to.be.called;
});
it("does not override existing functions", function() { it("does not override existing functions", function() {
expect(device.robot).to.not.be.a('function'); expect(device.robot).to.not.be.a('function');
}); });
}); });
describe("#start", function() { describe("#start", function() {
beforeEach(function() { var callback;
driver.start = stub().returns(true);
});
it("starts the driver, passing along a callback", function() { beforeEach(function() {
var callback = function() { }; callback = spy();
stub(Logger, 'info');
driver.start = stub();
device.start(callback); device.start(callback);
});
expect(driver.start).to.be.calledWith(callback); afterEach(function() {
Logger.info.restore();
}); });
it("logs that it's starting the device", function() { it("logs that it's starting the device", function() {
stub(Logger, 'info');
var message = "Starting device 'ping' on pin 13."; var message = "Starting device 'ping' on pin 13.";
device.start()
expect(Logger.info).to.be.calledWith(message); expect(Logger.info).to.be.calledWith(message);
});
Logger.info.restore(); it("triggers the provided callback after the adaptor finishes connecting", function() {
expect(callback).to.not.be.called;
driver.start.yield();
expect(callback).to.be.called;
});
it("binds driver methods to the device", function() {
driver.start.yield();
device.cmd();
expect(driver.cmd).to.be.called;
}); });
}); });