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

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'),
MockResponse = require('../../../support/mock_response');
var MockRequest = require("../../../support/mock_request"),
MockResponse = require("../../../support/mock_response");
describe("Basic Auth", function() {
var opts = { user: 'user', pass: 'pass' },
var opts = { user: "user", pass: "pass" },
req,
res,
next;
@ -18,7 +19,7 @@ describe("Basic Auth", function() {
res = new MockResponse();
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 };
});
@ -69,7 +70,7 @@ describe("Basic Auth", function() {
context("if the user/pass don't match", 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 };
});
@ -80,7 +81,7 @@ describe("Basic Auth", function() {
var result;
beforeEach(function() {
req.user = 'user';
req.user = "user";
result = basic(req, res, next);
});
@ -103,7 +104,7 @@ describe("Basic Auth", function() {
context("the authorization type isn't Basic", 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 };
});
@ -120,7 +121,7 @@ describe("Basic Auth", function() {
context("if the authorization header isn't formatted correctly", 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 };
});

View File

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

View File

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

View File

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

View File

@ -1,14 +1,15 @@
/* jshint expr:true */
"use strict";
var Cylon = source("cylon"),
Robot = source("robot"),
Utils = source('utils');
Utils = source("utils");
var API = source('api'),
Logger = source('logger'),
Adaptor = source('adaptor'),
Driver = source('driver'),
Config = source('config');
var API = source("api"),
Logger = source("logger"),
Adaptor = source("adaptor"),
Driver = source("driver"),
Config = source("config");
describe("Cylon", function() {
describe("exports", function() {
@ -47,7 +48,7 @@ describe("Cylon", function() {
var robot = Cylon.robot(opts);
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() {
@ -61,22 +62,22 @@ describe("Cylon", function() {
describe("#api", function() {
beforeEach(function() {
stub(API.prototype, 'listen');
stub(API.prototype, "listen");
});
afterEach(function() {
API.prototype.listen.restore();
});
it('creates a new API instance', function() {
it("creates a new API instance", function() {
Cylon.api();
expect(Cylon.api_instance).to.be.an.instanceOf(API);
});
it('passes configuration to the API constructor', function() {
Cylon.config({ api: { port: '1234' }});
it("passes configuration to the API constructor", function() {
Cylon.config({ api: { port: "1234" }});
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() };
Cylon.robots = {
'bot1': bot1,
'bot2': bot2
"bot1": bot1,
"bot2": bot2
};
Cylon.start();
@ -103,7 +104,7 @@ describe("Cylon", function() {
delete Config[c];
}
stub(Logger, 'setup');
stub(Logger, "setup");
});
afterEach(function() {
@ -150,8 +151,8 @@ describe("Cylon", function() {
bot2 = { halt: spy() };
Cylon.robots = {
'bot1': bot1,
'bot2': bot2
"bot1": bot1,
"bot2": bot2
};
Cylon.halt();
@ -168,7 +169,7 @@ describe("Cylon", function() {
bot1 = 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; };
json = Cylon.toJSON();
@ -179,7 +180,7 @@ describe("Cylon", 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";
var Ping = source('test/ping'),
var Ping = source("test/ping"),
Device = source("device");
describe("Device", function() {
it("returns a Driver instance", function() {
var driver = Device({
name: 'test',
driver: 'ping'
name: "test",
driver: "ping"
});
expect(driver).to.be.an.instanceOf(Ping);

View File

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

View File

@ -1,6 +1,7 @@
/* jshint expr:true */
"use strict";
var Utils = source('io/utils.js');
var Utils = source("io/utils.js");
describe("IOUtils", function() {
describe("#periodAndDuty", function() {
@ -12,12 +13,12 @@ describe("IOUtils", 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 })
});
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 })
});
});

View File

@ -1,13 +1,14 @@
'use strict';
/* jshint expr:true */
"use strict";
var Logger = source('logger'),
Config = source('config'),
Utils = source('utils');
var Logger = source("logger"),
Config = source("config"),
Utils = source("utils");
describe('Logger', function() {
describe("Logger", function() {
afterEach(function() {
// to be friendly to other specs
Config.logging = { logger: false, level: 'debug' };
Config.logging = { logger: false, level: "debug" };
Logger.setup();
});
@ -112,7 +113,7 @@ describe('Logger', function() {
Config.logging = {
logger: logger,
level: 'warn'
level: "warn"
}
Logger.setup();
@ -131,9 +132,9 @@ describe('Logger', function() {
Logger.error("error message");
Logger.fatal("fatal message");
expect(logger.warn).to.be.calledWith('warn message');
expect(logger.error).to.be.calledWith('error message');
expect(logger.fatal).to.be.calledWith('fatal message');
expect(logger.warn).to.be.calledWith("warn message");
expect(logger.error).to.be.calledWith("error message");
expect(logger.fatal).to.be.calledWith("fatal message");
});
it("defaults to 'info' level", function() {
@ -144,7 +145,7 @@ describe('Logger', function() {
Logger.info("info message");
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();
describe('BasicLogger', function() {
describe("BasicLogger", function() {
var clock;
beforeEach(function() {
stub(console, 'log');
stub(console, "log");
clock = sinon.useFakeTimers(0);
});
@ -19,7 +20,7 @@ describe('BasicLogger', function() {
describe("#toString", 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";
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() {
var original;
@ -27,8 +28,8 @@ describe("Registry", function() {
expect(Registry.data).to.be.eql({
"./../spec/support/mock_module.js": {
module: module,
drivers: ['test-driver'],
adaptors: ['test-adaptor'],
drivers: ["test-driver"],
adaptors: ["test-adaptor"],
dependencies: []
}
});
@ -41,7 +42,7 @@ describe("Registry", 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() {
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";
var Driver = source('driver'),
Adaptor = source('adaptor'),
var Driver = source("driver"),
Adaptor = source("adaptor"),
Robot = source("robot"),
Utils = source('utils');
Utils = source("utils");
describe("Robot", function() {
var work, extraFunction, robot;
@ -33,7 +34,7 @@ describe("Robot", function() {
context("if not provided", function() {
beforeEach(function() {
stub(Robot, 'randomName').returns("New Robot");
stub(Robot, "randomName").returns("New Robot");
});
afterEach(function() {
@ -69,12 +70,12 @@ describe("Robot", function() {
});
context("if there are devices but no connections", function() {
it('throws an error', function() {
it("throws an error", function() {
var fn = function() {
return new Robot({
name: 'BrokenBot',
name: "BrokenBot",
devices: {
ping: { driver: 'ping' }
ping: { driver: "ping" }
}
});
};
@ -88,8 +89,8 @@ describe("Robot", function() {
beforeEach(function() {
robot = new Robot({
name: 'NewBot',
otherThings: { more: 'details' },
name: "NewBot",
otherThings: { more: "details" },
sayHello: function() { return "Hello!" }
});
});
@ -104,7 +105,7 @@ describe("Robot", function() {
beforeEach(function() {
robot = new Robot({
name: 'NewBot',
name: "NewBot",
sayHello: function() { return this.name + " says hello" },
@ -125,7 +126,7 @@ describe("Robot", function() {
beforeEach(function() {
fn = function() {
new Robot({
name: 'NewBot',
name: "NewBot",
commands: function() {
return [];
@ -145,7 +146,7 @@ describe("Robot", function() {
beforeEach(function() {
robot = new Robot({
name: 'NewBot',
name: "NewBot",
sayHello: function() { return this.name + " says hello" },
@ -156,19 +157,19 @@ describe("Robot", 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() {
beforeEach(function() {
robot = new Robot({
name: 'NewBot',
name: "NewBot",
hiThere: 'hi there',
hiThere: "hi there",
sayHi: function() {
return 'hi';
return "hi";
},
start: "start"
@ -181,7 +182,7 @@ describe("Robot", 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
});
it('makes Jack a dull boy', function() {
it("makes Jack a dull boy", function() {
expect(playBot.work).to.be.eql(play);
})
});
@ -201,18 +202,18 @@ describe("Robot", function() {
describe("#toJSON", function() {
var bot = new Robot({
connections: {
loopback: { adaptor: 'loopback' }
loopback: { adaptor: "loopback" }
},
devices: {
ping: { driver: 'ping' }
ping: { driver: "ping" }
}
});
var json = bot.toJSON();
it("returns an object", function() {
expect(json).to.be.a('object');
expect(json).to.be.a("object");
});
it("contains the robot's name", function() {
@ -237,24 +238,24 @@ describe("Robot", function() {
beforeEach(function() {
bot = new Robot();
opts = { adaptor: 'loopback' };
opts = { adaptor: "loopback" };
});
it("creates and adds a new Connection", function() {
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);
})
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);
})
it("avoids name collisions", function() {
bot.connection('loopback', opts);
bot.connection('loopback', opts);
expect(Object.keys(bot.connections)).to.be.eql(['loopback', 'loopback-1']);
bot.connection("loopback", opts);
bot.connection("loopback", opts);
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() {
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 });
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() {
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 });
expect(bot.connections['loopback']).to.be.instanceOf(Adaptor);
expect(bot.connections["loopback"]).to.be.instanceOf(Adaptor);
});
it("avoids name collisions", function() {
var opts = {
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() {
bot = new Robot();
opts = { driver: 'ping' };
opts = { driver: "ping" };
});
it("creates and adds a new Device", function() {
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);
})
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);
})
it("avoids name collisions", function() {
bot.device('ping', opts);
bot.device('ping', opts);
expect(Object.keys(bot.devices)).to.be.eql(['ping', 'ping-1']);
bot.device("ping", opts);
bot.device("ping", opts);
expect(Object.keys(bot.devices)).to.be.eql(["ping", "ping-1"]);
});
});
@ -335,7 +336,7 @@ describe("Robot", function() {
beforeEach(function() {
bot = new Robot({
connections: {
loopback: { adaptor: 'loopback' }
loopback: { adaptor: "loopback" }
}
});
});
@ -349,25 +350,25 @@ describe("Robot", function() {
context("when passed a device 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 });
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() {
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});
expect(bot.devices['ping']).to.be.instanceOf(Driver);
expect(bot.devices["ping"]).to.be.instanceOf(Driver);
});
it("avoids name collisions collisions", function() {
bot.initDevices({
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() {
beforeEach(function() {
stub(robot, 'startConnections').callsArg(0);
stub(robot, 'startDevices').callsArg(0);
stub(robot, 'emit').returns(null);
stub(robot, "startConnections").callsArg(0);
stub(robot, "startDevices").callsArg(0);
stub(robot, "emit").returns(null);
robot.start();
});
@ -419,13 +420,13 @@ describe("Robot", function() {
beforeEach(function() {
bot = new Robot({
connections: {
alpha: { adaptor: 'loopback' },
bravo: { adaptor: 'loopback' }
alpha: { adaptor: "loopback" },
bravo: { adaptor: "loopback" }
}
});
stub(bot.connections.alpha, 'connect').returns(true);
stub(bot.connections.bravo, 'connect').returns(true);
stub(bot.connections.alpha, "connect").returns(true);
stub(bot.connections.bravo, "connect").returns(true);
});
it("runs #connect on each connection", function() {
@ -442,17 +443,17 @@ describe("Robot", function() {
beforeEach(function() {
bot = new Robot({
connections: {
loopback: { adaptor: 'loopback' }
loopback: { adaptor: "loopback" }
},
devices: {
alpha: { driver: 'ping' },
bravo: { driver: 'ping' }
alpha: { driver: "ping" },
bravo: { driver: "ping" }
}
});
stub(bot.devices.alpha, 'start').returns(true);
stub(bot.devices.bravo, 'start').returns(true);
stub(bot.devices.alpha, "start").returns(true);
stub(bot.devices.bravo, "start").returns(true);
});
it("runs #start on each device", function() {
@ -469,19 +470,19 @@ describe("Robot", function() {
beforeEach(function() {
bot = new Robot({
devices: {
ping: { driver: 'ping' }
ping: { driver: "ping" }
},
connections: {
loopback: { adaptor: 'loopback' }
loopback: { adaptor: "loopback" }
}
});
device = bot.devices.ping;
connection = bot.connections.loopback;
stub(device, 'halt').yields(true);
stub(connection, 'disconnect').yields(true);
stub(device, "halt").yields(true);
stub(connection, "disconnect").yields(true);
});
afterEach(function() {

View File

@ -1,3 +1,4 @@
/* jshint expr:true */
"use strict";
var utils = source("utils");
@ -23,17 +24,22 @@ describe("Utils", 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() {
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() {
it("should return 0", function() {
expect((15).fromScale(0, 10)).to.be.eql(1);
expect((5).fromScale(10, 20)).to.be.eql(0);
});
});
});
describe("#toScale", function() {
it("converts a value from 0-1 scale to another", function() {
@ -49,7 +55,7 @@ describe("Utils", 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() {
@ -113,7 +119,7 @@ describe("Utils", function() {
describe("constantly", function() {
beforeEach(function() {
stub(global, 'every').returns(0);
stub(global, "every").returns(0);
});
afterEach(function() {
@ -129,8 +135,7 @@ describe("Utils", function() {
});
describe("#subclass", function() {
var BaseClass = (function() {
function BaseClass(opts) {
var BaseClass = function BaseClass(opts) {
this.greeting = opts.greeting;
};
@ -138,18 +143,11 @@ describe("Utils", function() {
return "Hi!";
};
return BaseClass
})();
var SubClass = (function(klass) {
utils.subclass(SubClass, klass);
function SubClass(opts) {
var SubClass = function SubClass() {
SubClass.__super__.constructor.apply(this, arguments);
};
return SubClass;
})(BaseClass);
utils.subclass(SubClass, BaseClass);
it("adds inheritance to Javascript classes", function() {
var sub = new SubClass({greeting: "Hello World"});
@ -159,7 +157,7 @@ describe("Utils", function() {
});
describe("#proxyFunctionsToObject", function() {
var methods = ['asString', 'toString', 'returnString'];
var methods = ["asString", "toString", "returnString"];
var ProxyClass = (function() {
function ProxyClass() {}
@ -181,7 +179,7 @@ describe("Utils", function() {
var TestClass = (function() {
function TestClass() {
this.testInstance = new ProxyClass;
this.testInstance = new ProxyClass();
utils.proxyFunctionsToObject(methods, this.testInstance, this, true);
}
@ -190,28 +188,28 @@ describe("Utils", function() {
var testclass = new TestClass();
it('can alias methods', function() {
it("can alias methods", function() {
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]");
});
it('can alias methods with arguments', function() {
expect(testclass.returnString).to.be.a('function');
it("can alias methods with arguments", function() {
expect(testclass.returnString).to.be.a("function");
});
});
describe("#fetch", function() {
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() {
it("returns the value", function() {
expect(fetch(obj, 'property')).to.be.eql('hello world');
expect(fetch(obj, 'false')).to.be.eql(false);
expect(fetch(obj, 'null')).to.be.eql(null);
expect(fetch(obj, "property")).to.be.eql("hello world");
expect(fetch(obj, "false")).to.be.eql(false);
expect(fetch(obj, "null")).to.be.eql(null);
});
});
@ -219,21 +217,21 @@ describe("Utils", function() {
context("and no fallback value has been provided", function() {
it("throws an Error", function() {
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() {
it('returns the fallback value', function() {
expect(fetch(obj, 'notakey', 'fallback')).to.be.eql('fallback');
it("returns the fallback value", function() {
expect(fetch(obj, "notakey", "fallback")).to.be.eql("fallback");
});
});
context("and a fallback function has been provided", function() {
context("if the function has no return value", function() {
it("throws an Error", function() {
var fn = function() { fetch(obj, 'notakey', function() {}); },
str = 'no return value from provided fallback function';
var fn = function() { fetch(obj, "notakey", function() {}); },
str = "no return value from provided fallback function";
expect(fn).to.throw(Error, str);
});
@ -241,10 +239,10 @@ describe("Utils", function() {
context("if the function returns a value", 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";
expect(fetch(obj, 'notakey', fn)).to.be.eql(value);
expect(fetch(obj, "notakey", fn)).to.be.eql(value);
});
});
});