2014-03-11 00:49:46 +08:00
|
|
|
"use strict";
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-09-30 06:25:51 +08:00
|
|
|
var Loopback = source('test/loopback'),
|
|
|
|
Robot = source("robot"),
|
2014-06-06 03:11:37 +08:00
|
|
|
Logger = source('logger'),
|
|
|
|
Utils = source('utils');
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-06-11 02:33:52 +08:00
|
|
|
describe("Connection", function() {
|
2014-09-30 06:25:51 +08:00
|
|
|
var robot, connection;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
robot = new Robot({
|
|
|
|
name: "Robby",
|
|
|
|
connection: { name: 'loopback', adaptor: 'loopback', port: "/dev/null" }
|
|
|
|
});
|
|
|
|
|
|
|
|
connection = robot.connections.loopback;
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-03-11 00:49:46 +08:00
|
|
|
describe("#constructor", function() {
|
|
|
|
it("sets @robot to the passed robot", function() {
|
|
|
|
expect(connection.robot).to.be.eql(robot);
|
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-03-11 00:49:46 +08:00
|
|
|
it("sets @name to the passed name", function() {
|
|
|
|
expect(connection.name).to.be.eql('loopback');
|
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-03-11 00:49:46 +08:00
|
|
|
it("sets @port to the passed port", function() {
|
|
|
|
expect(connection.port.toString()).to.be.eql("/dev/null");
|
|
|
|
});
|
2014-09-30 06:25:51 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
})
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
2014-03-04 04:24:29 +08:00
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
describe("#toJSON", function() {
|
2014-09-30 06:25:51 +08:00
|
|
|
var json;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
json = connection.toJSON();
|
|
|
|
});
|
2014-03-04 04:24:29 +08:00
|
|
|
|
|
|
|
it("returns an object", function() {
|
2014-06-07 05:11:35 +08:00
|
|
|
expect(json).to.be.an('object');
|
2014-03-04 04:24:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the connection's name", function() {
|
2014-06-07 05:11:35 +08:00
|
|
|
expect(json.name).to.be.eql("loopback");
|
2014-03-04 04:24:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the connection's port", function() {
|
2014-07-26 06:55:16 +08:00
|
|
|
expect(json.details.port).to.be.eql("/dev/null");
|
2014-03-04 04:24:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the connection's adaptor name", function() {
|
2014-06-07 05:11:35 +08:00
|
|
|
expect(json.adaptor).to.be.eql("Loopback");
|
2014-03-04 04:24:29 +08:00
|
|
|
});
|
2014-03-11 00:49:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#connect", function() {
|
|
|
|
var callback = function() { };
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
beforeEach(function() {
|
2014-03-11 00:49:46 +08:00
|
|
|
stub(Logger, 'info').returns(true);
|
2014-09-09 00:27:18 +08:00
|
|
|
connection.adaptor.connect = stub().returns(true);
|
2014-03-11 00:49:46 +08:00
|
|
|
|
|
|
|
connection.connect(callback);
|
|
|
|
});
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
afterEach(function() {
|
2014-03-11 00:49:46 +08:00
|
|
|
Logger.info.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("logs that it's connecting the device", function() {
|
2014-06-11 02:33:52 +08:00
|
|
|
var message = "Connecting to 'loopback' on port /dev/null.";
|
2014-03-11 00:49:46 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#disconnect", function() {
|
2014-07-05 00:49:29 +08:00
|
|
|
beforeEach(function() {
|
2014-03-11 00:49:46 +08:00
|
|
|
stub(Logger, 'info').returns(true);
|
2014-09-05 23:43:40 +08:00
|
|
|
stub(connection, 'removeAllListeners');
|
2014-03-11 00:49:46 +08:00
|
|
|
|
2014-09-06 01:34:03 +08:00
|
|
|
connection.adaptor.disconnect = stub().returns(true);
|
|
|
|
|
2014-03-11 00:49:46 +08:00
|
|
|
connection.disconnect();
|
|
|
|
});
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
afterEach(function() {
|
2014-09-05 23:43:40 +08:00
|
|
|
connection.removeAllListeners.restore();
|
2014-03-11 00:49:46 +08:00
|
|
|
Logger.info.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("logs that it's disconnecting from the device", function() {
|
2014-06-11 02:33:52 +08:00
|
|
|
var message = "Disconnecting from 'loopback' on port /dev/null.";
|
2014-03-11 00:49:46 +08:00
|
|
|
expect(Logger.info).to.be.calledWith(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("tells the adaptor to disconnect", function() {
|
|
|
|
expect(connection.adaptor.disconnect).to.be.called;
|
|
|
|
});
|
2014-09-05 23:43:40 +08:00
|
|
|
|
|
|
|
it("disconnects all event listeners", function() {
|
|
|
|
expect(connection.removeAllListeners).to.be.called;
|
|
|
|
});
|
2014-03-11 00:49:46 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|