WIP on cleaning up specs

This commit is contained in:
Andrew Stewart 2014-12-14 15:21:28 -08:00
parent f56b680837
commit 7f761c7268
16 changed files with 315 additions and 302 deletions

View File

@ -1,15 +1,16 @@
'use strict'; /* jshint expr:true */
"use strict";
var Adaptor = source("adaptor"), var Adaptor = source("adaptor"),
Logger = source('logger'), Logger = source("logger"),
Utils = source('utils'); Utils = source("utils");
describe("Adaptor", function() { describe("Adaptor", function() {
var adaptor = new Adaptor({ name: 'adaptor' }); var adaptor = new Adaptor({ name: "adaptor" });
describe("#constructor", function() { describe("#constructor", function() {
it("sets @name to the provided name", function() { it("sets @name to the provided name", function() {
expect(adaptor.name).to.be.eql('adaptor'); expect(adaptor.name).to.be.eql("adaptor");
}); });
}); });
}); });

View File

@ -1,16 +1,17 @@
'use strict'; /* jshint expr:true */
"use strict";
var express = require('express'), var express = require("express"),
https = require('https'), https = require("https"),
fs = require('fs'), fs = require("fs"),
path = require('path'); path = require("path");
var API = source('api'), var API = source("api"),
Utils = source('utils'), Utils = source("utils"),
Logger = source('logger'); Logger = source("logger");
var MockRequest = require('../support/mock_request'), var MockRequest = require("../support/mock_request"),
MockResponse = require('../support/mock_response'); MockResponse = require("../support/mock_response");
describe("API", function() { describe("API", function() {
var api; var api;
@ -30,24 +31,24 @@ describe("API", function() {
}); });
it("sets @express to an Express instance", function() { it("sets @express to an Express instance", function() {
expect(api.express.listen).to.be.a('function'); expect(api.express.listen).to.be.a("function");
}) })
it("sets default values", function() { it("sets default values", function() {
var sslPath = path.normalize(__dirname + "/../../lib/api/ssl/"); var sslPath = path.normalize(__dirname + "/../../lib/api/ssl/");
expect(api.host).to.be.eql('127.0.0.1'); expect(api.host).to.be.eql("127.0.0.1");
expect(api.port).to.be.eql('3000'); expect(api.port).to.be.eql("3000");
}); });
it("overrides default values if passed to constructor", function() { it("overrides default values if passed to constructor", function() {
expect(mod.host).to.be.eql('0.0.0.0'); expect(mod.host).to.be.eql("0.0.0.0");
expect(mod.port).to.be.eql('1234'); expect(mod.port).to.be.eql("1234");
}); });
it("sets the server title", function() { it("sets the server title", function() {
var title = api.express.get('title'); var title = api.express.get("title");
expect(title).to.be.eql('Cylon API Server'); expect(title).to.be.eql("Cylon API Server");
}); });
}); });
@ -55,11 +56,11 @@ describe("API", function() {
var d = API.prototype.defaults; var d = API.prototype.defaults;
it("host", function() { it("host", function() {
expect(d.host).to.be.eql('127.0.0.1'); expect(d.host).to.be.eql("127.0.0.1");
}); });
it("port", function() { it("port", function() {
expect(d.port).to.be.eql('3000'); expect(d.port).to.be.eql("3000");
}); });
it("auth", function() { it("auth", function() {
@ -67,7 +68,7 @@ describe("API", function() {
}); });
it("CORS", function() { it("CORS", function() {
expect(d.CORS).to.be.eql(''); expect(d.CORS).to.be.eql("");
}); });
it("ssl", function() { it("ssl", function() {
@ -81,7 +82,7 @@ describe("API", function() {
it("sets @express to an express server", function() { it("sets @express to an express server", function() {
api.express = null; api.express = null;
api.createServer(); api.createServer();
expect(api.express).to.be.a('function'); expect(api.express).to.be.a("function");
}); });
context("if SSL is configured", function() { context("if SSL is configured", function() {
@ -94,7 +95,7 @@ describe("API", function() {
context("if SSL is not configured", function() { context("if SSL is not configured", function() {
beforeEach(function() { beforeEach(function() {
api.ssl = false; api.ssl = false;
stub(Logger, 'warn'); stub(Logger, "warn");
api.createServer(); api.createServer();
}); });
@ -115,16 +116,16 @@ describe("API", function() {
describe("#setupAuth", function() { describe("#setupAuth", function() {
context("when auth.type is basic", function() { context("when auth.type is basic", function() {
beforeEach(function() { beforeEach(function() {
api.auth = { type: 'basic', user: 'user', pass: 'pass' } api.auth = { type: "basic", user: "user", pass: "pass" }
}); });
it('returns a basic auth middleware function', function() { it("returns a basic auth middleware function", function() {
var fn = api.setupAuth(), var fn = api.setupAuth(),
req = new MockRequest(), req = new MockRequest(),
res = new MockResponse(), res = new MockResponse(),
next = spy(); next = spy();
var auth = 'Basic ' + new Buffer('user:pass').toString('base64') var auth = "Basic " + new Buffer("user:pass").toString("base64")
req.headers.authorization = auth; req.headers.authorization = auth;
@ -153,10 +154,10 @@ describe("API", function() {
// we create a plain HTTP server to avoid a log message from Node // we create a plain HTTP server to avoid a log message from Node
api.ssl = false; api.ssl = false;
api.createServer(); api.createServer();
api.express.set('title', 'Cylon API Server'); api.express.set("title", "Cylon API Server");
stub(api.server, 'listen').yields(); stub(api.server, "listen").yields();
stub(Logger, 'info'); stub(Logger, "info");
api.listen(); api.listen();
}); });
@ -167,7 +168,7 @@ describe("API", function() {
}); });
it("listens on the configured host and port", function() { it("listens on the configured host and port", function() {
expect(api.server.listen).to.be.calledWith('3000', '127.0.0.1'); expect(api.server.listen).to.be.calledWith("3000", "127.0.0.1");
}); });
context("when the server is running", function() { context("when the server is running", function() {

View File

@ -1,12 +1,13 @@
'use strict' /* jshint expr:true */
"use strict"
var basic = source('api/auth/basic'); var basic = source("api/auth/basic");
var MockRequest = require('../../../support/mock_request'), var MockRequest = require("../../../support/mock_request"),
MockResponse = require('../../../support/mock_response'); MockResponse = require("../../../support/mock_response");
describe("Basic Auth", function() { describe("Basic Auth", function() {
var opts = { user: 'user', pass: 'pass' }, var opts = { user: "user", pass: "pass" },
req, req,
res, res,
next; next;
@ -18,7 +19,7 @@ describe("Basic Auth", function() {
res = new MockResponse(); res = new MockResponse();
next = spy(); next = spy();
var auth = new Buffer("user:pass", "utf8").toString('base64'); var auth = new Buffer("user:pass", "utf8").toString("base64");
req.headers = { authorization: "Basic " + auth }; req.headers = { authorization: "Basic " + auth };
}); });
@ -69,7 +70,7 @@ describe("Basic Auth", function() {
context("if the user/pass don't match", function() { context("if the user/pass don't match", function() {
beforeEach(function() { beforeEach(function() {
var auth = new Buffer("bad:wrong", "utf8").toString('base64'); var auth = new Buffer("bad:wrong", "utf8").toString("base64");
req.headers = { authorization: "Basic " + auth }; req.headers = { authorization: "Basic " + auth };
}); });
@ -80,7 +81,7 @@ describe("Basic Auth", function() {
var result; var result;
beforeEach(function() { beforeEach(function() {
req.user = 'user'; req.user = "user";
result = basic(req, res, next); result = basic(req, res, next);
}); });
@ -103,7 +104,7 @@ describe("Basic Auth", function() {
context("the authorization type isn't Basic", function() { context("the authorization type isn't Basic", function() {
beforeEach(function() { beforeEach(function() {
var auth = new Buffer("user:pass", "utf8").toString('base64'); var auth = new Buffer("user:pass", "utf8").toString("base64");
req.headers = { authorization: "Digest " + auth }; req.headers = { authorization: "Digest " + auth };
}); });
@ -120,7 +121,7 @@ describe("Basic Auth", function() {
context("if the authorization header isn't formatted correctly", function() { context("if the authorization header isn't formatted correctly", function() {
beforeEach(function() { beforeEach(function() {
var auth = new Buffer("user-pass", "utf8").toString('base64'); var auth = new Buffer("user-pass", "utf8").toString("base64");
req.headers = { authorization: "Basic " + auth }; req.headers = { authorization: "Basic " + auth };
}); });

View File

@ -1,6 +1,7 @@
/* jshint expr:true */
"use strict"; "use strict";
var router = source('api/routes'); var router = source("api/routes");
describe("API routes", function() { describe("API routes", function() {
var routes = [ var routes = [

View File

@ -1,13 +1,14 @@
/* jshint expr:true */
"use strict"; "use strict";
var Basestar = source('basestar'), var Basestar = source("basestar"),
Utils = source('utils'); Utils = source("utils");
var EventEmitter = require('events').EventEmitter; var EventEmitter = require("events").EventEmitter;
describe('Basestar', function() { describe("Basestar", function() {
describe('#proxyMethods', function() { describe("#proxyMethods", function() {
var methods = ['asString', 'toString', 'returnString']; var methods = ["asString", "toString", "returnString"];
var ProxyClass = function ProxyClass() {} var ProxyClass = function ProxyClass() {}
@ -30,21 +31,21 @@ describe('Basestar', function() {
Utils.subclass(TestClass, Basestar); Utils.subclass(TestClass, Basestar);
it('can alias methods', function() { it("can alias methods", function() {
var testclass = new TestClass; var testclass = new TestClass;
expect(testclass.asString).to.be.a('function') expect(testclass.asString).to.be.a("function")
expect(testclass.asString()).to.be.equal("[object ProxyClass]"); expect(testclass.asString()).to.be.equal("[object ProxyClass]");
}); });
it('can alias existing methods if forced to', function() { it("can alias existing methods if forced to", function() {
var testclass = new TestClass; var testclass = new TestClass;
expect(testclass.toString).to.be.a('function') expect(testclass.toString).to.be.a("function")
expect(testclass.toString()).to.be.equal("[object ProxyClass]"); expect(testclass.toString()).to.be.equal("[object ProxyClass]");
}); });
it('can alias methods with arguments', function() { it("can alias methods with arguments", function() {
var testclass = new TestClass; var testclass = new TestClass;
expect(testclass.returnString).to.be.a('function') expect(testclass.returnString).to.be.a("function")
expect(testclass.returnString("testString")).to.be.equal("testString"); expect(testclass.returnString("testString")).to.be.equal("testString");
}); });
}); });
@ -72,10 +73,10 @@ describe('Basestar', function() {
testclass = new EmitterClass(), testclass = new EmitterClass(),
proxy = testclass.proxy; proxy = testclass.proxy;
proxy.on('testevent', eventSpy); proxy.on("testevent", eventSpy);
testclass.emit('testevent', 'data'); testclass.emit("testevent", "data");
assert(eventSpy.calledWith('data')) assert(eventSpy.calledWith("data"))
}); });
it("emits an 'update' event if told to", function() { it("emits an 'update' event if told to", function() {
@ -83,10 +84,10 @@ describe('Basestar', function() {
testclass = new EmitterClass(true), testclass = new EmitterClass(true),
proxy = testclass.proxy; proxy = testclass.proxy;
proxy.on('update', updateSpy); proxy.on("update", updateSpy);
testclass.emit('testevent', 'data'); testclass.emit("testevent", "data");
assert(updateSpy.calledWith('testevent', 'data')); assert(updateSpy.calledWith("testevent", "data"));
}); });
it("does not emit an 'update' event by default", function() { it("does not emit an 'update' event by default", function() {
@ -94,10 +95,10 @@ describe('Basestar', function() {
testclass = new EmitterClass(), testclass = new EmitterClass(),
proxy = testclass.proxy; proxy = testclass.proxy;
proxy.on('update', updateSpy); proxy.on("update", updateSpy);
testclass.emit('testevent', 'data'); testclass.emit("testevent", "data");
assert(!updateSpy.calledWith('testevent', 'data')); assert(!updateSpy.calledWith("testevent", "data"));
}); });
}); });
@ -112,22 +113,22 @@ describe('Basestar', function() {
it("proxies events between the connector and connection", function() { it("proxies events between the connector and connection", function() {
var eventSpy = spy(); var eventSpy = spy();
basestar.on('testevent', eventSpy); basestar.on("testevent", eventSpy);
basestar.defineAdaptorEvent({ eventName: "testevent" }); basestar.defineAdaptorEvent({ eventName: "testevent" });
basestar.connector.emit("testevent", "data"); basestar.connector.emit("testevent", "data");
assert(eventSpy.calledWith('data')); assert(eventSpy.calledWith("data"));
}); });
context("when given a string", function() { context("when given a string", function() {
it("uses it as the eventName", function() { it("uses it as the eventName", function() {
var eventSpy = spy(); var eventSpy = spy();
basestar.on('testevent', eventSpy); basestar.on("testevent", eventSpy);
basestar.defineAdaptorEvent("testevent"); basestar.defineAdaptorEvent("testevent");
basestar.connector.emit("testevent", "data"); basestar.connector.emit("testevent", "data");
assert(eventSpy.calledWith('data')); assert(eventSpy.calledWith("data"));
}); });
}); });
}); });
@ -143,22 +144,22 @@ describe('Basestar', function() {
it("proxies events between the connection and device", function() { it("proxies events between the connection and device", function() {
var eventSpy = spy(); var eventSpy = spy();
basestar.on('testevent', eventSpy); basestar.on("testevent", eventSpy);
basestar.defineDriverEvent({ eventName: "testevent" }); basestar.defineDriverEvent({ eventName: "testevent" });
basestar.connection.emit("testevent", "data"); basestar.connection.emit("testevent", "data");
assert(eventSpy.calledWith('data')); assert(eventSpy.calledWith("data"));
}); });
context("when given a string", function() { context("when given a string", function() {
it("uses it as the eventName", function() { it("uses it as the eventName", function() {
var eventSpy = spy(); var eventSpy = spy();
basestar.on('testevent', eventSpy); basestar.on("testevent", eventSpy);
basestar.defineDriverEvent("testevent"); basestar.defineDriverEvent("testevent");
basestar.connection.emit("testevent", "data"); basestar.connection.emit("testevent", "data");
assert(eventSpy.calledWith('data')); assert(eventSpy.calledWith("data"));
}); });
}); });
}); });

View File

@ -1,13 +1,14 @@
/* jshint expr:true */
"use strict"; "use strict";
var Loopback = source('test/loopback'), var Loopback = source("test/loopback"),
Connection = source("connection"); Connection = source("connection");
describe("Connection", function() { describe("Connection", function() {
it("returns a Adaptor instance", function() { it("returns a Adaptor instance", function() {
var conn = Connection({ var conn = Connection({
name: 'test', name: "test",
adaptor: 'loopback' adaptor: "loopback"
}); });
expect(conn).to.be.an.instanceOf(Loopback); expect(conn).to.be.an.instanceOf(Loopback);

View File

@ -1,14 +1,15 @@
/* jshint expr:true */
"use strict"; "use strict";
var Cylon = source("cylon"), var Cylon = source("cylon"),
Robot = source("robot"), Robot = source("robot"),
Utils = source('utils'); Utils = source("utils");
var API = source('api'), var API = source("api"),
Logger = source('logger'), Logger = source("logger"),
Adaptor = source('adaptor'), Adaptor = source("adaptor"),
Driver = source('driver'), Driver = source("driver"),
Config = source('config'); Config = source("config");
describe("Cylon", function() { describe("Cylon", function() {
describe("exports", function() { describe("exports", function() {
@ -47,7 +48,7 @@ describe("Cylon", function() {
var robot = Cylon.robot(opts); var robot = Cylon.robot(opts);
expect(robot.toString()).to.be.eql("[Robot name='Ultron']") expect(robot.toString()).to.be.eql("[Robot name='Ultron']")
expect(Cylon.robots['Ultron']).to.be.eql(robot); expect(Cylon.robots["Ultron"]).to.be.eql(robot);
}); });
it("avoids duplicating names", function() { it("avoids duplicating names", function() {
@ -61,22 +62,22 @@ describe("Cylon", function() {
describe("#api", function() { describe("#api", function() {
beforeEach(function() { beforeEach(function() {
stub(API.prototype, 'listen'); stub(API.prototype, "listen");
}); });
afterEach(function() { afterEach(function() {
API.prototype.listen.restore(); API.prototype.listen.restore();
}); });
it('creates a new API instance', function() { it("creates a new API instance", function() {
Cylon.api(); Cylon.api();
expect(Cylon.api_instance).to.be.an.instanceOf(API); expect(Cylon.api_instance).to.be.an.instanceOf(API);
}); });
it('passes configuration to the API constructor', function() { it("passes configuration to the API constructor", function() {
Cylon.config({ api: { port: '1234' }}); Cylon.config({ api: { port: "1234" }});
Cylon.api(); Cylon.api();
expect(Cylon.api_instance.port).to.be.eql('1234'); expect(Cylon.api_instance.port).to.be.eql("1234");
}) })
}); });
@ -86,8 +87,8 @@ describe("Cylon", function() {
bot2 = { start: spy() }; bot2 = { start: spy() };
Cylon.robots = { Cylon.robots = {
'bot1': bot1, "bot1": bot1,
'bot2': bot2 "bot2": bot2
}; };
Cylon.start(); Cylon.start();
@ -103,7 +104,7 @@ describe("Cylon", function() {
delete Config[c]; delete Config[c];
} }
stub(Logger, 'setup'); stub(Logger, "setup");
}); });
afterEach(function() { afterEach(function() {
@ -150,8 +151,8 @@ describe("Cylon", function() {
bot2 = { halt: spy() }; bot2 = { halt: spy() };
Cylon.robots = { Cylon.robots = {
'bot1': bot1, "bot1": bot1,
'bot2': bot2 "bot2": bot2
}; };
Cylon.halt(); Cylon.halt();
@ -168,7 +169,7 @@ describe("Cylon", function() {
bot1 = new Robot(); bot1 = new Robot();
bot2 = new Robot(); bot2 = new Robot();
Cylon.robots = { 'bot1': bot1, 'bot2': bot2 }; Cylon.robots = { "bot1": bot1, "bot2": bot2 };
Cylon.commands.echo = echo = function(arg) { return arg; }; Cylon.commands.echo = echo = function(arg) { return arg; };
json = Cylon.toJSON(); json = Cylon.toJSON();
@ -179,7 +180,7 @@ describe("Cylon", function() {
}); });
it("contains an array of MCP commands", function() { it("contains an array of MCP commands", function() {
expect(json.commands).to.be.eql(['echo']); expect(json.commands).to.be.eql(["echo"]);
}) })
}); });
}); });

View File

@ -1,13 +1,14 @@
/* jshint expr:true */
"use strict"; "use strict";
var Ping = source('test/ping'), var Ping = source("test/ping"),
Device = source("device"); Device = source("device");
describe("Device", function() { describe("Device", function() {
it("returns a Driver instance", function() { it("returns a Driver instance", function() {
var driver = Device({ var driver = Device({
name: 'test', name: "test",
driver: 'ping' driver: "ping"
}); });
expect(driver).to.be.an.instanceOf(Ping); expect(driver).to.be.an.instanceOf(Ping);

View File

@ -1,16 +1,17 @@
/* jshint expr:true */
"use strict"; "use strict";
var fs = require('fs'); var fs = require("fs");
var DigitalPin = source('io/digital-pin'), var DigitalPin = source("io/digital-pin"),
Utils = source('utils'); Utils = source("utils");
describe("Cylon.IO.DigitalPin", function() { describe("Cylon.IO.DigitalPin", function() {
var pin = new DigitalPin({ pin: '4', mode: 'w' }) var pin = new DigitalPin({ pin: "4", mode: "w" })
describe("constructor", function() { describe("constructor", function() {
it("sets @pinNum to the pin number passed in opts", function() { it("sets @pinNum to the pin number passed in opts", function() {
expect(pin.pinNum).to.be.eql('4'); expect(pin.pinNum).to.be.eql("4");
}); });
it("sets @status to 'low' by default", function() { it("sets @status to 'low' by default", function() {
@ -31,8 +32,8 @@ describe("Cylon.IO.DigitalPin", function() {
context("if the GPIO file for the pin exists", function() { context("if the GPIO file for the pin exists", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'exists').callsArgWith(1, true); stub(fs, "exists").callsArgWith(1, true);
stub(pin, '_openPin'); stub(pin, "_openPin");
}); });
afterEach(function() { afterEach(function() {
@ -49,8 +50,8 @@ describe("Cylon.IO.DigitalPin", function() {
context("if the GPIO file for the pin doesn't exist", function() { context("if the GPIO file for the pin doesn't exist", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'exists').callsArgWith(1, false); stub(fs, "exists").callsArgWith(1, false);
stub(pin, '_createGPIOPin'); stub(pin, "_createGPIOPin");
}); });
afterEach(function() { afterEach(function() {
@ -70,8 +71,8 @@ describe("Cylon.IO.DigitalPin", function() {
var path = "/sys/class/gpio/unexport"; var path = "/sys/class/gpio/unexport";
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFile').callsArgWith(2, false); stub(fs, "writeFile").callsArgWith(2, false);
stub(pin, '_closeCallback'); stub(pin, "_closeCallback");
}); });
afterEach(function() { afterEach(function() {
@ -81,7 +82,7 @@ describe("Cylon.IO.DigitalPin", function() {
it("writes to the GPIO unexport path with the pin's value", function() { it("writes to the GPIO unexport path with the pin's value", function() {
pin.close(); pin.close();
expect(fs.writeFile).to.be.calledWith(path, '4'); expect(fs.writeFile).to.be.calledWith(path, "4");
}); });
it("calls the closeCallback", function() { it("calls the closeCallback", function() {
@ -94,8 +95,8 @@ describe("Cylon.IO.DigitalPin", function() {
var path = "/sys/class/gpio/unexport"; var path = "/sys/class/gpio/unexport";
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFileSync'); stub(fs, "writeFileSync");
stub(pin, '_closeCallback'); stub(pin, "_closeCallback");
}); });
afterEach(function() { afterEach(function() {
@ -105,7 +106,7 @@ describe("Cylon.IO.DigitalPin", function() {
it("writes to the GPIO unexport path with the pin's value", function() { it("writes to the GPIO unexport path with the pin's value", function() {
pin.closeSync(); pin.closeSync();
expect(fs.writeFileSync).to.be.calledWith(path, '4'); expect(fs.writeFileSync).to.be.calledWith(path, "4");
}); });
it("calls the closeCallback", function() { it("calls the closeCallback", function() {
@ -119,8 +120,8 @@ describe("Cylon.IO.DigitalPin", function() {
context("if pin mode isn't 'w'", function() { context("if pin mode isn't 'w'", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFile'); stub(fs, "writeFile");
stub(pin, '_setMode'); stub(pin, "_setMode");
}); });
afterEach(function() { afterEach(function() {
@ -129,17 +130,17 @@ describe("Cylon.IO.DigitalPin", function() {
}); });
it("sets the pin mode to 'w'", function() { it("sets the pin mode to 'w'", function() {
pin.mode = 'r'; pin.mode = "r";
pin.digitalWrite(1); pin.digitalWrite(1);
expect(pin._setMode).to.be.calledWith('w'); expect(pin._setMode).to.be.calledWith("w");
}); });
}); });
context("when successful", function() { context("when successful", function() {
beforeEach(function() { beforeEach(function() {
pin.mode = 'w'; pin.mode = "w";
stub(fs, 'writeFile').callsArgWith(2, null); stub(fs, "writeFile").callsArgWith(2, null);
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -150,7 +151,7 @@ describe("Cylon.IO.DigitalPin", function() {
it("emits a digitalWrite event with the written value", function() { it("emits a digitalWrite event with the written value", function() {
pin.digitalWrite(1); pin.digitalWrite(1);
expect(fs.writeFile).to.be.calledWith(path, 1); expect(fs.writeFile).to.be.calledWith(path, 1);
expect(pin.emit).to.be.calledWith('digitalWrite', 1); expect(pin.emit).to.be.calledWith("digitalWrite", 1);
}); });
it("returns the passed value", function() { it("returns the passed value", function() {
@ -158,17 +159,17 @@ describe("Cylon.IO.DigitalPin", function() {
}); });
it("changes the pin's @status", function() { it("changes the pin's @status", function() {
pin.status = 'low'; pin.status = "low";
pin.digitalWrite(1); pin.digitalWrite(1);
expect(pin.status).to.be.eql('high'); expect(pin.status).to.be.eql("high");
}); });
}); });
context("when there is an error", function() { context("when there is an error", function() {
beforeEach(function() { beforeEach(function() {
pin.mode = 'w'; pin.mode = "w";
stub(fs, 'writeFile').callsArgWith(2, true); stub(fs, "writeFile").callsArgWith(2, true);
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -178,7 +179,7 @@ describe("Cylon.IO.DigitalPin", function() {
it("emits an error message", function() { it("emits an error message", function() {
pin.digitalWrite(1); pin.digitalWrite(1);
expect(pin.emit).to.be.calledWith('error'); expect(pin.emit).to.be.calledWith("error");
}); });
}); });
}); });
@ -196,8 +197,8 @@ describe("Cylon.IO.DigitalPin", function() {
context("if the mode isn't 'r'", function() { context("if the mode isn't 'r'", function() {
beforeEach(function() { beforeEach(function() {
stub(Utils, 'every'); stub(Utils, "every");
stub(pin, '_setMode'); stub(pin, "_setMode");
}); });
afterEach(function() { afterEach(function() {
@ -206,16 +207,16 @@ describe("Cylon.IO.DigitalPin", function() {
}); });
it("sets the pin mode to 'r'", function() { it("sets the pin mode to 'r'", function() {
pin.mode = 'w'; pin.mode = "w";
pin.digitalRead(500); pin.digitalRead(500);
expect(pin._setMode).to.be.calledWith('r'); expect(pin._setMode).to.be.calledWith("r");
}); });
}); });
context("when successful", function() { context("when successful", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'readFile').callsArgWith(1, null, 1); stub(fs, "readFile").callsArgWith(1, null, 1);
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -237,14 +238,14 @@ describe("Cylon.IO.DigitalPin", function() {
pin.digitalRead(500); pin.digitalRead(500);
this.clock.tick(510); this.clock.tick(510);
expect(pin.emit).to.be.calledWith('digitalRead', 1); expect(pin.emit).to.be.calledWith("digitalRead", 1);
}); });
}); });
context("when an error occurs", function() { context("when an error occurs", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'readFile').callsArgWith(1, true, null); stub(fs, "readFile").callsArgWith(1, true, null);
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -256,14 +257,14 @@ describe("Cylon.IO.DigitalPin", function() {
pin.digitalRead(500); pin.digitalRead(500);
this.clock.tick(500); this.clock.tick(500);
expect(pin.emit).to.be.calledWith('error'); expect(pin.emit).to.be.calledWith("error");
}); });
}); });
}); });
describe("#setHigh", function() { describe("#setHigh", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'digitalWrite'); stub(pin, "digitalWrite");
}); });
afterEach(function() { afterEach(function() {
@ -278,7 +279,7 @@ describe("Cylon.IO.DigitalPin", function() {
describe("#setLow", function() { describe("#setLow", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'digitalWrite'); stub(pin, "digitalWrite");
}); });
afterEach(function() { afterEach(function() {
@ -294,7 +295,7 @@ describe("Cylon.IO.DigitalPin", function() {
describe("#toggle", function() { describe("#toggle", function() {
context("when @status is 'high'", function() { context("when @status is 'high'", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'setLow') stub(pin, "setLow")
pin.status = "high"; pin.status = "high";
}); });
@ -310,7 +311,7 @@ describe("Cylon.IO.DigitalPin", function() {
context("when @status is 'low'", function() { context("when @status is 'low'", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'setHigh') stub(pin, "setHigh")
pin.status = "low"; pin.status = "low";
}); });
@ -330,7 +331,7 @@ describe("Cylon.IO.DigitalPin", function() {
context("when successful", function() { context("when successful", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFile').callsArgWith(2, null); stub(fs, "writeFile").callsArgWith(2, null);
stub(pin, "_openPin"); stub(pin, "_openPin");
}); });
@ -352,7 +353,7 @@ describe("Cylon.IO.DigitalPin", function() {
context("when an error occurs", function() { context("when an error occurs", function() {
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFile').callsArgWith(2, true); stub(fs, "writeFile").callsArgWith(2, true);
stub(pin, "emit"); stub(pin, "emit");
}); });
@ -363,15 +364,15 @@ describe("Cylon.IO.DigitalPin", function() {
it("emits an error", function() { it("emits an error", function() {
pin._createGPIOPin(); pin._createGPIOPin();
expect(pin.emit).to.be.calledWith('error'); expect(pin.emit).to.be.calledWith("error");
}); });
}); });
}) })
describe("#_openPin", function() { describe("#_openPin", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, '_setMode'); stub(pin, "_setMode");
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -386,14 +387,14 @@ describe("Cylon.IO.DigitalPin", function() {
it("emits the 'open' event", function() { it("emits the 'open' event", function() {
pin._openPin(); pin._openPin();
expect(pin.emit).to.be.calledWith('open'); expect(pin.emit).to.be.calledWith("open");
}); });
}); });
describe("_closeCallback", function() { describe("_closeCallback", function() {
context("if there is an error", function() { context("if there is an error", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'emit'); stub(pin, "emit");
pin._closeCallback(true); pin._closeCallback(true);
}); });
@ -402,13 +403,13 @@ describe("Cylon.IO.DigitalPin", function() {
}); });
it("emits an error", function() { it("emits an error", function() {
expect(pin.emit).to.be.calledWith('error'); expect(pin.emit).to.be.calledWith("error");
}); });
}); });
context("if there is no error", function() { context("if there is no error", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'emit'); stub(pin, "emit");
pin._closeCallback(false); pin._closeCallback(false);
}); });
@ -417,7 +418,7 @@ describe("Cylon.IO.DigitalPin", function() {
}); });
it("emits a 'close' event with the pin number", function() { it("emits a 'close' event with the pin number", function() {
expect(pin.emit).to.be.calledWith('close', '4'); expect(pin.emit).to.be.calledWith("close", "4");
}) })
}); });
}); });
@ -426,8 +427,8 @@ describe("Cylon.IO.DigitalPin", function() {
var path = "/sys/class/gpio/gpio4/direction"; var path = "/sys/class/gpio/gpio4/direction";
beforeEach(function() { beforeEach(function() {
stub(fs, 'writeFile').callsArgWith(2, 'error'); stub(fs, "writeFile").callsArgWith(2, "error");
stub(pin, '_setModeCallback'); stub(pin, "_setModeCallback");
}); });
afterEach(function() { afterEach(function() {
@ -437,27 +438,27 @@ describe("Cylon.IO.DigitalPin", function() {
context("when mode is 'w'", function() { context("when mode is 'w'", function() {
it("writes to the pin's direction path with 'out'", function() { it("writes to the pin's direction path with 'out'", function() {
pin._setMode('w'); pin._setMode("w");
expect(fs.writeFile).to.be.calledWith(path, 'out'); expect(fs.writeFile).to.be.calledWith(path, "out");
}); });
it("calls #_setModeCallback with any error message", function() { it("calls #_setModeCallback with any error message", function() {
pin._setMode('w', true); pin._setMode("w", true);
expect(pin._setModeCallback).to.be.calledWith('error', true); expect(pin._setModeCallback).to.be.calledWith("error", true);
}); });
}); });
context("when mode is 'r'", function() { context("when mode is 'r'", function() {
it("writes to the pin's direction path with 'in'", function() { it("writes to the pin's direction path with 'in'", function() {
pin._setMode('r'); pin._setMode("r");
expect(fs.writeFile).to.be.calledWith(path, 'in'); expect(fs.writeFile).to.be.calledWith(path, "in");
}); });
}); });
}); });
describe("#_setModeCallback", function() { describe("#_setModeCallback", function() {
beforeEach(function() { beforeEach(function() {
stub(pin, 'emit'); stub(pin, "emit");
}); });
afterEach(function() { afterEach(function() {
@ -474,15 +475,15 @@ describe("Cylon.IO.DigitalPin", function() {
context("when emitConnect is true", function() { context("when emitConnect is true", function() {
it("emits a 'connect' event with the pin's mode", function() { it("emits a 'connect' event with the pin's mode", function() {
pin._setModeCallback(false, true); pin._setModeCallback(false, true);
expect(pin.emit).to.be.calledWith('connect', pin.mode); expect(pin.emit).to.be.calledWith("connect", pin.mode);
}); });
}); });
}); });
context("when passed an error", function() { context("when passed an error", function() {
it('emits an error', function() { it("emits an error", function() {
pin._setModeCallback(true); pin._setModeCallback(true);
expect(pin.emit).to.be.calledWith('error'); expect(pin.emit).to.be.calledWith("error");
}); });
}); });
}); });

View File

@ -1,28 +1,29 @@
'use strict'; /* jshint expr:true */
"use strict";
var EventEmitter = require('events').EventEmitter; var EventEmitter = require("events").EventEmitter;
var Driver = source("driver"), var Driver = source("driver"),
Logger = source('logger'), Logger = source("logger"),
Utils = source('utils'); Utils = source("utils");
describe("Driver", function() { describe("Driver", function() {
var connection, device, driver; var connection, device, driver;
beforeEach(function() { beforeEach(function() {
connection = { connection = {
connection: 'connection' connection: "connection"
}; };
driver = new Driver({ driver = new Driver({
name: 'driver', name: "driver",
connection: connection, connection: connection,
}); });
}); });
describe("#constructor", function() { describe("#constructor", function() {
it("sets @name to the provided name", function() { it("sets @name to the provided name", function() {
expect(driver.name).to.be.eql('driver'); expect(driver.name).to.be.eql("driver");
}); });
it("sets @connection to the provided connection", function() { it("sets @connection to the provided connection", function() {
@ -37,7 +38,7 @@ describe("Driver", function() {
expect(driver.interval).to.be.eql(10); expect(driver.interval).to.be.eql(10);
driver = new Driver({ driver = new Driver({
name: 'driver', name: "driver",
connection: connection, connection: connection,
interval: 2000, interval: 2000,
}); });

View File

@ -1,6 +1,7 @@
/* jshint expr:true */
"use strict"; "use strict";
var Utils = source('io/utils.js'); var Utils = source("io/utils.js");
describe("IOUtils", function() { describe("IOUtils", function() {
describe("#periodAndDuty", function() { describe("#periodAndDuty", function() {
@ -12,12 +13,12 @@ describe("IOUtils", function() {
}); });
it("calculates values for servos", function() { it("calculates values for servos", function() {
var value = fn(0.5, 50, { min: 500, max: 2400 }, 'high'); var value = fn(0.5, 50, { min: 500, max: 2400 }, "high");
expect(value).to.be.eql({ duty: 1450000, period: 20000000 }) expect(value).to.be.eql({ duty: 1450000, period: 20000000 })
}); });
it("calculates values for different polarities", function() { it("calculates values for different polarities", function() {
var value = fn(0.5, 50, { min: 500, max: 2400 }, 'low'); var value = fn(0.5, 50, { min: 500, max: 2400 }, "low");
expect(value).to.be.eql({ duty: 18550000, period: 20000000 }) expect(value).to.be.eql({ duty: 18550000, period: 20000000 })
}); });
}); });

View File

@ -1,13 +1,14 @@
'use strict'; /* jshint expr:true */
"use strict";
var Logger = source('logger'), var Logger = source("logger"),
Config = source('config'), Config = source("config"),
Utils = source('utils'); Utils = source("utils");
describe('Logger', function() { describe("Logger", function() {
afterEach(function() { afterEach(function() {
// to be friendly to other specs // to be friendly to other specs
Config.logging = { logger: false, level: 'debug' }; Config.logging = { logger: false, level: "debug" };
Logger.setup(); Logger.setup();
}); });
@ -112,7 +113,7 @@ describe('Logger', function() {
Config.logging = { Config.logging = {
logger: logger, logger: logger,
level: 'warn' level: "warn"
} }
Logger.setup(); Logger.setup();
@ -131,9 +132,9 @@ describe('Logger', function() {
Logger.error("error message"); Logger.error("error message");
Logger.fatal("fatal message"); Logger.fatal("fatal message");
expect(logger.warn).to.be.calledWith('warn message'); expect(logger.warn).to.be.calledWith("warn message");
expect(logger.error).to.be.calledWith('error message'); expect(logger.error).to.be.calledWith("error message");
expect(logger.fatal).to.be.calledWith('fatal message'); expect(logger.fatal).to.be.calledWith("fatal message");
}); });
it("defaults to 'info' level", function() { it("defaults to 'info' level", function() {
@ -144,7 +145,7 @@ describe('Logger', function() {
Logger.info("info message"); Logger.info("info message");
expect(logger.debug).to.not.be.called; expect(logger.debug).to.not.be.called;
expect(logger.info).to.be.calledWith('info message'); expect(logger.info).to.be.calledWith("info message");
}) })
}); });
}); });

View File

@ -1,14 +1,15 @@
'use strict'; /* jshint expr:true */
"use strict";
var logger = source('logger/basic_logger'); var logger = source("logger/basic_logger");
var date = new Date(0).toISOString(); var date = new Date(0).toISOString();
describe('BasicLogger', function() { describe("BasicLogger", function() {
var clock; var clock;
beforeEach(function() { beforeEach(function() {
stub(console, 'log'); stub(console, "log");
clock = sinon.useFakeTimers(0); clock = sinon.useFakeTimers(0);
}); });
@ -19,7 +20,7 @@ describe('BasicLogger', function() {
describe("#toString", function() { describe("#toString", function() {
it("returns 'BasicLogger'", function() { it("returns 'BasicLogger'", function() {
expect(logger.toString()).to.be.eql('BasicLogger'); expect(logger.toString()).to.be.eql("BasicLogger");
}); });
}); });

View File

@ -1,10 +1,11 @@
/* jshint expr:true */
"use strict"; "use strict";
var Registry = source('registry'); var Registry = source("registry");
var path = './../spec/support/mock_module.js'; var path = "./../spec/support/mock_module.js";
var module = require('./../support/mock_module.js') var module = require("./../support/mock_module.js")
describe("Registry", function() { describe("Registry", function() {
var original; var original;
@ -27,8 +28,8 @@ describe("Registry", function() {
expect(Registry.data).to.be.eql({ expect(Registry.data).to.be.eql({
"./../spec/support/mock_module.js": { "./../spec/support/mock_module.js": {
module: module, module: module,
drivers: ['test-driver'], drivers: ["test-driver"],
adaptors: ['test-adaptor'], adaptors: ["test-adaptor"],
dependencies: [] dependencies: []
} }
}); });
@ -41,7 +42,7 @@ describe("Registry", function() {
}); });
it("finds the appropriate module containing the adaptor", function() { it("finds the appropriate module containing the adaptor", function() {
expect(Registry.findByAdaptor('test-adaptor')).to.be.eql(module); expect(Registry.findByAdaptor("test-adaptor")).to.be.eql(module);
}); });
}); });
@ -51,7 +52,7 @@ describe("Registry", function() {
}); });
it("finds the appropriate module containing the driver", function() { it("finds the appropriate module containing the driver", function() {
expect(Registry.findByDriver('test-driver')).to.be.eql(module); expect(Registry.findByDriver("test-driver")).to.be.eql(module);
}); });
}); });
}); });

View File

@ -1,9 +1,10 @@
/* jshint expr:true */
"use strict"; "use strict";
var Driver = source('driver'), var Driver = source("driver"),
Adaptor = source('adaptor'), Adaptor = source("adaptor"),
Robot = source("robot"), Robot = source("robot"),
Utils = source('utils'); Utils = source("utils");
describe("Robot", function() { describe("Robot", function() {
var work, extraFunction, robot; var work, extraFunction, robot;
@ -33,7 +34,7 @@ describe("Robot", function() {
context("if not provided", function() { context("if not provided", function() {
beforeEach(function() { beforeEach(function() {
stub(Robot, 'randomName').returns("New Robot"); stub(Robot, "randomName").returns("New Robot");
}); });
afterEach(function() { afterEach(function() {
@ -69,12 +70,12 @@ describe("Robot", function() {
}); });
context("if there are devices but no connections", function() { context("if there are devices but no connections", function() {
it('throws an error', function() { it("throws an error", function() {
var fn = function() { var fn = function() {
return new Robot({ return new Robot({
name: 'BrokenBot', name: "BrokenBot",
devices: { devices: {
ping: { driver: 'ping' } ping: { driver: "ping" }
} }
}); });
}; };
@ -88,8 +89,8 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
robot = new Robot({ robot = new Robot({
name: 'NewBot', name: "NewBot",
otherThings: { more: 'details' }, otherThings: { more: "details" },
sayHello: function() { return "Hello!" } sayHello: function() { return "Hello!" }
}); });
}); });
@ -104,7 +105,7 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
robot = new Robot({ robot = new Robot({
name: 'NewBot', name: "NewBot",
sayHello: function() { return this.name + " says hello" }, sayHello: function() { return this.name + " says hello" },
@ -125,7 +126,7 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
fn = function() { fn = function() {
new Robot({ new Robot({
name: 'NewBot', name: "NewBot",
commands: function() { commands: function() {
return []; return [];
@ -145,7 +146,7 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
robot = new Robot({ robot = new Robot({
name: 'NewBot', name: "NewBot",
sayHello: function() { return this.name + " says hello" }, sayHello: function() { return this.name + " says hello" },
@ -156,19 +157,19 @@ describe("Robot", function() {
}); });
it("sets #commands to the provided object", function() { it("sets #commands to the provided object", function() {
expect(robot.commands.say_hello).to.be.a('function'); expect(robot.commands.say_hello).to.be.a("function");
}); });
}); });
context("arbitrary arguments", function() { context("arbitrary arguments", function() {
beforeEach(function() { beforeEach(function() {
robot = new Robot({ robot = new Robot({
name: 'NewBot', name: "NewBot",
hiThere: 'hi there', hiThere: "hi there",
sayHi: function() { sayHi: function() {
return 'hi'; return "hi";
}, },
start: "start" start: "start"
@ -181,7 +182,7 @@ describe("Robot", function() {
}); });
it("doesn't work if they conflict with built-in properties", function() { it("doesn't work if they conflict with built-in properties", function() {
expect(robot.start).to.be.a('function'); expect(robot.start).to.be.a("function");
}); });
}); });
}); });
@ -193,7 +194,7 @@ describe("Robot", function() {
play: play play: play
}); });
it('makes Jack a dull boy', function() { it("makes Jack a dull boy", function() {
expect(playBot.work).to.be.eql(play); expect(playBot.work).to.be.eql(play);
}) })
}); });
@ -201,18 +202,18 @@ describe("Robot", function() {
describe("#toJSON", function() { describe("#toJSON", function() {
var bot = new Robot({ var bot = new Robot({
connections: { connections: {
loopback: { adaptor: 'loopback' } loopback: { adaptor: "loopback" }
}, },
devices: { devices: {
ping: { driver: 'ping' } ping: { driver: "ping" }
} }
}); });
var json = bot.toJSON(); var json = bot.toJSON();
it("returns an object", function() { it("returns an object", function() {
expect(json).to.be.a('object'); expect(json).to.be.a("object");
}); });
it("contains the robot's name", function() { it("contains the robot's name", function() {
@ -237,24 +238,24 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot(); bot = new Robot();
opts = { adaptor: 'loopback' }; opts = { adaptor: "loopback" };
}); });
it("creates and adds a new Connection", function() { it("creates and adds a new Connection", function() {
expect(bot.connections.loopback).to.be.eql(undefined); expect(bot.connections.loopback).to.be.eql(undefined);
bot.connection('loopback', opts); bot.connection("loopback", opts);
expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor); expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor);
}) })
it("sets @robot on the Connection to be the Robot initializing it", function() { it("sets @robot on the Connection to be the Robot initializing it", function() {
bot.connection('loopback', opts); bot.connection("loopback", opts);
expect(bot.connections.loopback.robot).to.be.eql(bot); expect(bot.connections.loopback.robot).to.be.eql(bot);
}) })
it("avoids name collisions", function() { it("avoids name collisions", function() {
bot.connection('loopback', opts); bot.connection("loopback", opts);
bot.connection('loopback', opts); bot.connection("loopback", opts);
expect(Object.keys(bot.connections)).to.be.eql(['loopback', 'loopback-1']); expect(Object.keys(bot.connections)).to.be.eql(["loopback", "loopback-1"]);
}); });
}); });
@ -274,24 +275,24 @@ describe("Robot", function() {
context("when passed a connection object", function() { context("when passed a connection object", function() {
it("instantiates a new connection with the provided object", function() { it("instantiates a new connection with the provided object", function() {
var connection = { name: 'loopback', adaptor: 'loopback' }; var connection = { name: "loopback", adaptor: "loopback" };
bot.initConnections({ connection: connection }); bot.initConnections({ connection: connection });
expect(bot.connections['loopback']).to.be.instanceOf(Adaptor); expect(bot.connections["loopback"]).to.be.instanceOf(Adaptor);
}); });
}); });
context("when passed an array of connection objects", function() { context("when passed an array of connection objects", function() {
it("instantiates a new connection with each of the provided objects", function() { it("instantiates a new connection with each of the provided objects", function() {
var connections = [{ name: 'loopback', adaptor: 'loopback' }] var connections = [{ name: "loopback", adaptor: "loopback" }]
bot.initConnections({ connections: connections }); bot.initConnections({ connections: connections });
expect(bot.connections['loopback']).to.be.instanceOf(Adaptor); expect(bot.connections["loopback"]).to.be.instanceOf(Adaptor);
}); });
it("avoids name collisions", function() { it("avoids name collisions", function() {
var opts = { var opts = {
connections: [ connections: [
{ name: 'loopback', adaptor: 'loopback' }, { name: "loopback", adaptor: "loopback" },
{ name: 'loopback', adaptor: 'loopback' } { name: "loopback", adaptor: "loopback" }
] ]
}; };
@ -308,24 +309,24 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot(); bot = new Robot();
opts = { driver: 'ping' }; opts = { driver: "ping" };
}); });
it("creates and adds a new Device", function() { it("creates and adds a new Device", function() {
expect(bot.devices.ping).to.be.eql(undefined); expect(bot.devices.ping).to.be.eql(undefined);
bot.device('ping', opts); bot.device("ping", opts);
expect(bot.devices.ping).to.be.an.instanceOf(Driver); expect(bot.devices.ping).to.be.an.instanceOf(Driver);
}) })
it("sets @robot on the Device to be the Robot initializing it", function() { it("sets @robot on the Device to be the Robot initializing it", function() {
bot.device('ping', opts); bot.device("ping", opts);
expect(bot.devices.ping.robot).to.be.eql(bot); expect(bot.devices.ping.robot).to.be.eql(bot);
}) })
it("avoids name collisions", function() { it("avoids name collisions", function() {
bot.device('ping', opts); bot.device("ping", opts);
bot.device('ping', opts); bot.device("ping", opts);
expect(Object.keys(bot.devices)).to.be.eql(['ping', 'ping-1']); expect(Object.keys(bot.devices)).to.be.eql(["ping", "ping-1"]);
}); });
}); });
@ -335,7 +336,7 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot({ bot = new Robot({
connections: { connections: {
loopback: { adaptor: 'loopback' } loopback: { adaptor: "loopback" }
} }
}); });
}); });
@ -349,25 +350,25 @@ describe("Robot", function() {
context("when passed a device object", function() { context("when passed a device object", function() {
it("instantiates a new driver with the provided object", function() { it("instantiates a new driver with the provided object", function() {
var device = { name: 'ping', driver: 'ping' }; var device = { name: "ping", driver: "ping" };
bot.initDevices({ device: device }); bot.initDevices({ device: device });
expect(bot.devices['ping']).to.be.instanceOf(Driver); expect(bot.devices["ping"]).to.be.instanceOf(Driver);
}); });
}); });
context("when passed an array of device objects", function() { context("when passed an array of device objects", function() {
it("instantiates a new driver with each of the provided objects", function() { it("instantiates a new driver with each of the provided objects", function() {
var devices = [{ name: 'ping', driver: 'ping' }] var devices = [{ name: "ping", driver: "ping" }]
bot.initDevices({ devices: devices}); bot.initDevices({ devices: devices});
expect(bot.devices['ping']).to.be.instanceOf(Driver); expect(bot.devices["ping"]).to.be.instanceOf(Driver);
}); });
it("avoids name collisions collisions", function() { it("avoids name collisions collisions", function() {
bot.initDevices({ bot.initDevices({
devices: [ devices: [
{ name: 'ping', driver: 'ping' }, { name: "ping", driver: "ping" },
{ name: 'ping', driver: 'ping' } { name: "ping", driver: "ping" }
] ]
}); });
@ -379,9 +380,9 @@ describe("Robot", function() {
describe("#start", function() { describe("#start", function() {
beforeEach(function() { beforeEach(function() {
stub(robot, 'startConnections').callsArg(0); stub(robot, "startConnections").callsArg(0);
stub(robot, 'startDevices').callsArg(0); stub(robot, "startDevices").callsArg(0);
stub(robot, 'emit').returns(null); stub(robot, "emit").returns(null);
robot.start(); robot.start();
}); });
@ -419,13 +420,13 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot({ bot = new Robot({
connections: { connections: {
alpha: { adaptor: 'loopback' }, alpha: { adaptor: "loopback" },
bravo: { adaptor: 'loopback' } bravo: { adaptor: "loopback" }
} }
}); });
stub(bot.connections.alpha, 'connect').returns(true); stub(bot.connections.alpha, "connect").returns(true);
stub(bot.connections.bravo, 'connect').returns(true); stub(bot.connections.bravo, "connect").returns(true);
}); });
it("runs #connect on each connection", function() { it("runs #connect on each connection", function() {
@ -442,17 +443,17 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot({ bot = new Robot({
connections: { connections: {
loopback: { adaptor: 'loopback' } loopback: { adaptor: "loopback" }
}, },
devices: { devices: {
alpha: { driver: 'ping' }, alpha: { driver: "ping" },
bravo: { driver: 'ping' } bravo: { driver: "ping" }
} }
}); });
stub(bot.devices.alpha, 'start').returns(true); stub(bot.devices.alpha, "start").returns(true);
stub(bot.devices.bravo, 'start').returns(true); stub(bot.devices.bravo, "start").returns(true);
}); });
it("runs #start on each device", function() { it("runs #start on each device", function() {
@ -469,19 +470,19 @@ describe("Robot", function() {
beforeEach(function() { beforeEach(function() {
bot = new Robot({ bot = new Robot({
devices: { devices: {
ping: { driver: 'ping' } ping: { driver: "ping" }
}, },
connections: { connections: {
loopback: { adaptor: 'loopback' } loopback: { adaptor: "loopback" }
} }
}); });
device = bot.devices.ping; device = bot.devices.ping;
connection = bot.connections.loopback; connection = bot.connections.loopback;
stub(device, 'halt').yields(true); stub(device, "halt").yields(true);
stub(connection, 'disconnect').yields(true); stub(connection, "disconnect").yields(true);
}); });
afterEach(function() { afterEach(function() {

View File

@ -1,3 +1,4 @@
/* jshint expr:true */
"use strict"; "use strict";
var utils = source("utils"); var utils = source("utils");
@ -23,15 +24,20 @@ describe("Utils", function() {
}); });
it("converts floats", function() { it("converts floats", function() {
expect(2.5.fromScale(0, 10)).to.be.eql(0.25); expect((2.5).fromScale(0, 10)).to.be.eql(0.25);
}); });
it("should return 1 if the number goes above the top of the scale", function() { context("if the number goes above the top of the scale", function() {
expect((15).fromScale(0, 10)).to.be.eql(1); it("should return 1", function() {
expect((15).fromScale(0, 10)).to.be.eql(1);
});
}); });
it("should return 0 if the number goes below the bottom of the scale", function() { context("if the number goes below the bottom of the scale", function() {
expect((5).fromScale(10, 20)).to.be.eql(0); it("should return 0", function() {
expect((15).fromScale(0, 10)).to.be.eql(1);
expect((5).fromScale(10, 20)).to.be.eql(0);
});
}); });
}); });
@ -49,7 +55,7 @@ describe("Utils", function() {
}); });
it("converts to floats", function() { it("converts to floats", function() {
expect(0.25.toScale(0, 10)).to.be.eql(2.5); expect((0.25).toScale(0, 10)).to.be.eql(2.5);
}); });
it("can be chained with #fromScale", function() { it("can be chained with #fromScale", function() {
@ -113,7 +119,7 @@ describe("Utils", function() {
describe("constantly", function() { describe("constantly", function() {
beforeEach(function() { beforeEach(function() {
stub(global, 'every').returns(0); stub(global, "every").returns(0);
}); });
afterEach(function() { afterEach(function() {
@ -129,27 +135,19 @@ describe("Utils", function() {
}); });
describe("#subclass", function() { describe("#subclass", function() {
var BaseClass = (function() { var BaseClass = function BaseClass(opts) {
function BaseClass(opts) { this.greeting = opts.greeting;
this.greeting = opts.greeting; };
};
BaseClass.prototype.sayHi = function() { BaseClass.prototype.sayHi = function() {
return "Hi!"; return "Hi!";
}; };
return BaseClass var SubClass = function SubClass() {
})(); SubClass.__super__.constructor.apply(this, arguments);
};
var SubClass = (function(klass) { utils.subclass(SubClass, BaseClass);
utils.subclass(SubClass, klass);
function SubClass(opts) {
SubClass.__super__.constructor.apply(this, arguments);
};
return SubClass;
})(BaseClass);
it("adds inheritance to Javascript classes", function() { it("adds inheritance to Javascript classes", function() {
var sub = new SubClass({greeting: "Hello World"}); var sub = new SubClass({greeting: "Hello World"});
@ -159,7 +157,7 @@ describe("Utils", function() {
}); });
describe("#proxyFunctionsToObject", function() { describe("#proxyFunctionsToObject", function() {
var methods = ['asString', 'toString', 'returnString']; var methods = ["asString", "toString", "returnString"];
var ProxyClass = (function() { var ProxyClass = (function() {
function ProxyClass() {} function ProxyClass() {}
@ -181,7 +179,7 @@ describe("Utils", function() {
var TestClass = (function() { var TestClass = (function() {
function TestClass() { function TestClass() {
this.testInstance = new ProxyClass; this.testInstance = new ProxyClass();
utils.proxyFunctionsToObject(methods, this.testInstance, this, true); utils.proxyFunctionsToObject(methods, this.testInstance, this, true);
} }
@ -190,28 +188,28 @@ describe("Utils", function() {
var testclass = new TestClass(); var testclass = new TestClass();
it('can alias methods', function() { it("can alias methods", function() {
expect(testclass.asString()).to.be.eql("[object ProxyClass]"); expect(testclass.asString()).to.be.eql("[object ProxyClass]");
}); });
it('can alias existing methods if forced to', function() { it("can alias existing methods if forced to", function() {
expect(testclass.toString()).to.be.eql("[object ProxyClass]"); expect(testclass.toString()).to.be.eql("[object ProxyClass]");
}); });
it('can alias methods with arguments', function() { it("can alias methods with arguments", function() {
expect(testclass.returnString).to.be.a('function'); expect(testclass.returnString).to.be.a("function");
}); });
}); });
describe("#fetch", function() { describe("#fetch", function() {
var fetch = utils.fetch, var fetch = utils.fetch,
obj = { property: 'hello world', 'false': false, 'null': null }; obj = { property: "hello world", "false": false, "null": null };
context("if the property exists on the object", function() { context("if the property exists on the object", function() {
it("returns the value", function() { it("returns the value", function() {
expect(fetch(obj, 'property')).to.be.eql('hello world'); expect(fetch(obj, "property")).to.be.eql("hello world");
expect(fetch(obj, 'false')).to.be.eql(false); expect(fetch(obj, "false")).to.be.eql(false);
expect(fetch(obj, 'null')).to.be.eql(null); expect(fetch(obj, "null")).to.be.eql(null);
}); });
}); });
@ -219,21 +217,21 @@ describe("Utils", function() {
context("and no fallback value has been provided", function() { context("and no fallback value has been provided", function() {
it("throws an Error", function() { it("throws an Error", function() {
var fn = function() { return fetch(obj, "notaproperty"); }; var fn = function() { return fetch(obj, "notaproperty"); };
expect(fn).to.throw(Error, 'key not found: "notaproperty"'); expect(fn).to.throw(Error, "key not found: \"notaproperty\"");
}); });
}); });
context("and a fallback value has been provided", function() { context("and a fallback value has been provided", function() {
it('returns the fallback value', function() { it("returns the fallback value", function() {
expect(fetch(obj, 'notakey', 'fallback')).to.be.eql('fallback'); expect(fetch(obj, "notakey", "fallback")).to.be.eql("fallback");
}); });
}); });
context("and a fallback function has been provided", function() { context("and a fallback function has been provided", function() {
context("if the function has no return value", function() { context("if the function has no return value", function() {
it("throws an Error", function() { it("throws an Error", function() {
var fn = function() { fetch(obj, 'notakey', function() {}); }, var fn = function() { fetch(obj, "notakey", function() {}); },
str = 'no return value from provided fallback function'; str = "no return value from provided fallback function";
expect(fn).to.throw(Error, str); expect(fn).to.throw(Error, str);
}); });
@ -241,10 +239,10 @@ describe("Utils", function() {
context("if the function returns a value", function() { context("if the function returns a value", function() {
it("returns the value returned by the fallback function", function() { it("returns the value returned by the fallback function", function() {
var fn = function(key) { return "Couldn't find " + key }, var fn = function(key) { return "Couldn't find " + key; },
value = "Couldn't find notakey"; value = "Couldn't find notakey";
expect(fetch(obj, 'notakey', fn)).to.be.eql(value); expect(fetch(obj, "notakey", fn)).to.be.eql(value);
}); });
}); });
}); });