2014-03-05 05:03:31 +08:00
|
|
|
"use strict";
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
var Device = source('device'),
|
|
|
|
Connection = source('connection'),
|
|
|
|
Robot = source("robot");
|
2014-05-07 10:18:13 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
describe("Robot", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
var work = spy();
|
|
|
|
var extraFunction = spy();
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
var robot = new Robot({
|
2014-03-05 05:03:31 +08:00
|
|
|
name: "Robby",
|
|
|
|
work: work,
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
extraFunction: extraFunction,
|
|
|
|
extraValue: "Hello World",
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
master: { master: true }
|
2013-10-25 05:25:42 +08:00
|
|
|
});
|
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
describe("constructor", function() {
|
|
|
|
it("sets a @robot variable as a circular reference to the robot", function() {
|
|
|
|
expect(robot.robot).to.be.eql(robot);
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
describe("name", function() {
|
|
|
|
context("if provided", function() {
|
|
|
|
it("is set to the passed value", function() {
|
|
|
|
expect(robot.name).to.be.eql("Robby")
|
|
|
|
});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
context("if not provided", function() {
|
|
|
|
before(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
stub(Robot, 'randomName').returns("New Robot");
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
after(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
Robot.randomName.restore();
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
it("is set to a random name", function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
var bot = new Robot({});
|
2014-03-05 05:03:31 +08:00
|
|
|
expect(bot.name).to.be.eql("New Robot");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @master to the passed Master object", function() {
|
|
|
|
expect(robot.master).to.be.eql({ master: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @connections to an empty object by default", function() {
|
|
|
|
expect(robot.connections).to.be.eql({});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
it("sets @devices to an empty object by default", function() {
|
|
|
|
expect(robot.devices).to.be.eql({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @adaptors to an object containing all adaptors the Robot knows of", function() {
|
|
|
|
expect(robot.adaptors).to.have.keys(["loopback", "test"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @drivers to an object containing all drivers the Robot knows of", function() {
|
|
|
|
expect(robot.drivers).to.have.keys(["ping", "test"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @work to the passed work function", function() {
|
|
|
|
expect(robot.work).to.be.eql(work);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets other obj params as values on the robot", function() {
|
|
|
|
expect(robot.extraFunction).to.be.eql(extraFunction);
|
|
|
|
expect(robot.extraValue).to.be.eql("Hello World");
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
});
|
2014-03-01 12:27:05 +08:00
|
|
|
|
2014-06-05 07:33:20 +08:00
|
|
|
describe("all work and no play", function() {
|
|
|
|
var play = spy();
|
|
|
|
|
|
|
|
var playBot = new Robot({
|
|
|
|
play: play
|
|
|
|
});
|
|
|
|
|
|
|
|
it('makes Jack a dull boy', function() {
|
|
|
|
expect(playBot.work).to.be.eql(play);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-01 12:27:05 +08:00
|
|
|
describe("#data", function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
var bot = new Robot({
|
2014-03-01 12:27:05 +08:00
|
|
|
connection: { name: 'loopback', adaptor: 'loopback' },
|
|
|
|
device: { name: 'ping', driver: 'ping' }
|
|
|
|
});
|
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
var data = bot.data();
|
2014-03-01 12:27:05 +08:00
|
|
|
|
|
|
|
it("returns an object", function() {
|
|
|
|
expect(data).to.be.a('object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's name", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
expect(data.name).to.eql(bot.name);
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's commands", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
expect(data.commands).to.eql(bot.commands);
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's devices", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
var deviceData = bot.devices.ping.data();
|
2014-03-01 12:27:05 +08:00
|
|
|
expect(data.devices).to.eql([deviceData]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's connections", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
var connectionData = bot.connections.loopback.data();
|
2014-03-01 12:27:05 +08:00
|
|
|
expect(data.connections).to.eql([connectionData]);
|
|
|
|
});
|
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
|
|
|
|
describe("initConnections", function() {
|
|
|
|
context("when not passed anything", function() {
|
|
|
|
it("returns immediately", function() {
|
|
|
|
expect(robot.initConnections()).to.be.eql(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("when passed a connection object", function() {
|
|
|
|
it("instantiates a new connection with the provided object", function() {
|
|
|
|
var connection = { name: 'loopback', adaptor: 'loopback' };
|
|
|
|
robot.initConnections(connection);
|
2014-05-07 23:38:06 +08:00
|
|
|
expect(robot.connections['loopback']).to.be.instanceOf(Connection);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("when passed an array of connection objects", function() {
|
|
|
|
it("instantiates a new connection with each of the provided objects", function() {
|
|
|
|
var connections = [{ name: 'loopback', adaptor: 'loopback' }]
|
|
|
|
robot.initConnections(connections);
|
2014-05-07 23:38:06 +08:00
|
|
|
expect(robot.connections['loopback']).to.be.instanceOf(Connection);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("initDevices", function() {
|
|
|
|
context("when not passed anything", function() {
|
|
|
|
it("returns immediately", function() {
|
|
|
|
expect(robot.initDevices()).to.be.eql(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("when passed a connection object", function() {
|
2014-05-07 10:18:13 +08:00
|
|
|
afterEach(function() { robot.devices = {}; });
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2014-05-07 10:18:13 +08:00
|
|
|
it("instantiates a new device with the provided object", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
var device = { name: 'ping', driver: 'ping' };
|
|
|
|
robot.initDevices(device);
|
2014-05-07 23:38:06 +08:00
|
|
|
expect(robot.devices['ping']).to.be.instanceOf(Device);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("when passed an array of device objects", function() {
|
2014-05-07 10:18:13 +08:00
|
|
|
afterEach(function() { robot.devices = {}; });
|
2014-03-05 05:03:31 +08:00
|
|
|
|
|
|
|
it("instantiates a new device with each of the provided objects", function() {
|
|
|
|
var devices = [{ name: 'ping', driver: 'ping' }]
|
|
|
|
robot.initDevices(devices);
|
2014-05-07 10:18:13 +08:00
|
|
|
|
2014-05-07 23:38:06 +08:00
|
|
|
expect(robot.devices['ping']).to.be.instanceOf(Device);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#start", function() {
|
|
|
|
before(function() {
|
|
|
|
stub(robot, 'startConnections').callsArg(0);
|
|
|
|
stub(robot, 'startDevices').callsArg(0);
|
|
|
|
stub(robot, 'emit').returns(null);
|
|
|
|
|
|
|
|
robot.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
robot.startConnections.restore();
|
|
|
|
robot.startDevices.restore();
|
|
|
|
robot.emit.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's connections", function() {
|
|
|
|
expect(robot.startConnections).to.be.called;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's devices", function() {
|
|
|
|
expect(robot.startDevices).to.be.called;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's work", function() {
|
|
|
|
expect(robot.work).to.be.called;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("emits the 'working' event", function() {
|
|
|
|
expect(robot.emit).to.be.calledWith("working")
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#startConnections", function() {
|
|
|
|
var bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
bot = new Robot({
|
2014-03-05 05:03:31 +08:00
|
|
|
connections: [
|
|
|
|
{ name: 'alpha', adaptor: 'loopback' },
|
|
|
|
{ name: 'bravo', adaptor: 'loopback' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
stub(bot.connections.alpha, 'connect').returns(true);
|
|
|
|
stub(bot.connections.bravo, 'connect').returns(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("runs #connect on each connection", function() {
|
|
|
|
bot.startConnections();
|
|
|
|
|
|
|
|
expect(bot.connections.alpha.connect).to.be.called;
|
|
|
|
expect(bot.connections.bravo.connect).to.be.called;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#startDevices", function() {
|
|
|
|
var bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
bot = new Robot({
|
2014-03-05 05:03:31 +08:00
|
|
|
devices: [
|
|
|
|
{ name: 'alpha', driver: 'ping' },
|
|
|
|
{ name: 'bravo', driver: 'ping' }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
stub(bot.devices.alpha, 'start').returns(true);
|
|
|
|
stub(bot.devices.bravo, 'start').returns(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("runs #start on each device", function() {
|
|
|
|
bot.startDevices();
|
|
|
|
|
|
|
|
expect(bot.devices.alpha.start).to.be.called;
|
|
|
|
expect(bot.devices.bravo.start).to.be.called;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
describe("#halt", function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
var bot = new Robot({
|
2014-03-05 05:03:31 +08:00
|
|
|
device: { name: 'ping', driver: 'ping' },
|
|
|
|
connection: { name: 'loopback', adaptor: 'loopback' }
|
|
|
|
});
|
|
|
|
|
|
|
|
var device = bot.devices.ping,
|
|
|
|
connection = bot.connections.loopback;
|
|
|
|
|
|
|
|
before(function() {
|
2014-03-12 01:44:11 +08:00
|
|
|
stub(device, 'halt').returns(true);
|
|
|
|
stub(connection, 'halt').returns(true);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
2014-03-12 01:44:11 +08:00
|
|
|
device.halt.restore();
|
|
|
|
connection.halt.restore();
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
it("calls #halt on all devices and connections", function() {
|
|
|
|
bot.halt();
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
expect(device.halt).to.be.called;
|
|
|
|
expect(connection.halt).to.be.called;
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#toString", function() {
|
|
|
|
it("returns basic information about the robot", function() {
|
|
|
|
expect(robot.toString()).to.be.eql("[Robot name='Robby']");
|
|
|
|
});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|