Add tests for Cylon#findRobotDevice()

This commit is contained in:
Andrew Stewart 2013-11-08 17:16:17 -07:00
parent b2f2d11a85
commit b7add10cb4
4 changed files with 94 additions and 4 deletions

3
dist/cylon.js vendored
View File

@ -120,12 +120,13 @@
if (err) {
callback(err, robot);
}
device = null;
if (robot.devices[deviceid]) {
device = robot.devices[deviceid];
}
if (device == null) {
error = {
error: "No device found with the name " + device + "."
error: "No device found with the name " + deviceid + "."
};
}
if (callback) {

View File

@ -117,9 +117,10 @@ class Cylon
@findRobot robotid, (err, robot) ->
callback(err, robot) if err
device = null
device = robot.devices[deviceid] if robot.devices[deviceid]
unless device?
error = { error: "No device found with the name #{device}." }
error = { error: "No device found with the name #{deviceid}." }
if callback then callback(error, device) else device

View File

@ -1,12 +1,18 @@
(function() {
'use strict';
var Cylon, Robot,
var Connection, Cylon, Device, Driver, Robot,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Cylon = source("cylon");
Robot = source('robot');
Device = source('device');
Driver = source('driver');
Connection = source('connection');
describe("Cylon", function() {
it("should create a robot", function() {
var robot;
@ -50,7 +56,7 @@
return _results;
});
});
return describe("#findRobot", function() {
describe("#findRobot", function() {
describe("synchronous", function() {
describe("with a valid robot name", function() {
return it("returns the robot", function() {
@ -89,6 +95,53 @@
});
});
});
return describe("#findRobotDevice", function() {
var crow;
crow = Cylon.robot({
name: "Crow",
device: {
name: 'testDevice',
driver: 'ping'
}
});
describe("synchronous", function() {
describe("with a valid robot and device name", function() {
return it("returns the device", function() {
var device;
device = Cylon.findRobotDevice("Crow", "testDevice");
assert(device instanceof Device);
return device.name.should.be.equal("testDevice");
});
});
return describe("with an invalid device name", function() {
return it("returns null", function() {
var device;
device = Cylon.findRobotDevice("Crow", "madethisup");
return assert(device === null);
});
});
});
return describe("async", function() {
describe("with a valid robot and device name", function() {
return it("passes the device and an empty error to the callback", function() {
return Cylon.findRobotDevice("Crow", "testDevice", function(error, device) {
assert(error === void 0);
assert(device instanceof Device);
return device.name.should.be.equal("testDevice");
});
});
});
return describe("with an invalid device name", function() {
return it("passes no device and an error message to the callback", function() {
return Cylon.findRobotDevice("Crow", "madethisup", function(err, device) {
assert(device === null);
assert(typeof err === 'object');
return err.error.should.be.eql("No device found with the name madethisup.");
});
});
});
});
});
});
}).call(this);

View File

@ -2,6 +2,9 @@
Cylon = source("cylon")
Robot = source('robot')
Device = source('device')
Driver = source('driver')
Connection = source('connection')
describe "Cylon", ->
it "should create a robot", ->
@ -56,3 +59,35 @@ describe "Cylon", ->
assert typeof error is 'object'
error.error.should.be.eql "No Robot found with the name Tom Servo"
describe "#findRobotDevice", ->
crow = Cylon.robot({
name: "Crow"
device: { name: 'testDevice', driver: 'ping' }
})
describe "synchronous", ->
describe "with a valid robot and device name", ->
it "returns the device", ->
device = Cylon.findRobotDevice("Crow", "testDevice")
assert device instanceof Device
device.name.should.be.equal "testDevice"
describe "with an invalid device name", ->
it "returns null", ->
device = Cylon.findRobotDevice("Crow", "madethisup")
assert device is null
describe "async", ->
describe "with a valid robot and device name", ->
it "passes the device and an empty error to the callback", ->
Cylon.findRobotDevice "Crow", "testDevice", (error, device) ->
assert error is undefined
assert device instanceof Device
device.name.should.be.equal "testDevice"
describe "with an invalid device name", ->
it "passes no device and an error message to the callback", ->
Cylon.findRobotDevice "Crow", "madethisup", (err, device) ->
assert device is null
assert typeof err is 'object'
err.error.should.be.eql "No device found with the name madethisup."