Proxy methods after #connect/#start
This commit is contained in:
parent
198a792b22
commit
35c3a4b9e9
|
@ -39,12 +39,6 @@ var Connection = module.exports = function Connection(opts) {
|
|||
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);
|
||||
|
@ -68,7 +62,15 @@ Connection.prototype.toJSON = function() {
|
|||
Connection.prototype.connect = function(callback) {
|
||||
var msg = this._logstring("Connecting to");
|
||||
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
|
||||
|
|
|
@ -44,12 +44,6 @@ var Device = module.exports = function Device(opts) {
|
|||
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);
|
||||
|
@ -69,7 +63,15 @@ Device.prototype.start = function(callback) {
|
|||
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
|
||||
|
|
|
@ -31,20 +31,6 @@ describe("Connection", function() {
|
|||
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() {
|
||||
|
@ -72,27 +58,38 @@ describe("Connection", function() {
|
|||
});
|
||||
|
||||
describe("#connect", function() {
|
||||
var callback = function() { };
|
||||
var callback;
|
||||
|
||||
beforeEach(function() {
|
||||
callback = spy();
|
||||
Loopback.prototype.test = function() { return "Test" };
|
||||
|
||||
stub(Logger, 'info').returns(true);
|
||||
connection.adaptor.connect = stub().returns(true);
|
||||
connection.adaptor.connect = stub();
|
||||
|
||||
connection.connect(callback);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
Logger.info.restore();
|
||||
});
|
||||
delete Loopback.prototype.test;
|
||||
})
|
||||
|
||||
it("logs that it's connecting the device", function() {
|
||||
var message = "Connecting to 'loopback' on port /dev/null.";
|
||||
expect(Logger.info).to.be.calledWith(message);
|
||||
});
|
||||
|
||||
it("calls the adaptor's connect method with the provided callback", function() {
|
||||
expect(connection.adaptor.connect).to.be.calledWith(callback);
|
||||
it("triggers the provided callback after the adaptor finishes connecting", function() {
|
||||
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() {
|
||||
|
|
|
@ -59,39 +59,42 @@ describe("Device", function() {
|
|||
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() {
|
||||
expect(device.robot).to.not.be.a('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe("#start", function() {
|
||||
beforeEach(function() {
|
||||
driver.start = stub().returns(true);
|
||||
});
|
||||
var callback;
|
||||
|
||||
it("starts the driver, passing along a callback", function() {
|
||||
var callback = function() { };
|
||||
beforeEach(function() {
|
||||
callback = spy();
|
||||
|
||||
stub(Logger, 'info');
|
||||
driver.start = stub();
|
||||
|
||||
device.start(callback);
|
||||
});
|
||||
|
||||
expect(driver.start).to.be.calledWith(callback);
|
||||
afterEach(function() {
|
||||
Logger.info.restore();
|
||||
});
|
||||
|
||||
it("logs that it's starting the device", function() {
|
||||
stub(Logger, 'info');
|
||||
var message = "Starting device 'ping' on pin 13.";
|
||||
|
||||
device.start()
|
||||
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue