From b7add10cb4696c1beacab6417306d214cfe210e9 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Fri, 8 Nov 2013 17:16:17 -0700 Subject: [PATCH] Add tests for Cylon#findRobotDevice() --- dist/cylon.js | 3 +- src/cylon.coffee | 3 +- test/dist/specs/cylon.spec.js | 57 ++++++++++++++++++++++++++++++-- test/src/specs/cylon.spec.coffee | 35 ++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/dist/cylon.js b/dist/cylon.js index 2902c09..c5f3ff7 100644 --- a/dist/cylon.js +++ b/dist/cylon.js @@ -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) { diff --git a/src/cylon.coffee b/src/cylon.coffee index f390864..a2ff15d 100644 --- a/src/cylon.coffee +++ b/src/cylon.coffee @@ -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 diff --git a/test/dist/specs/cylon.spec.js b/test/dist/specs/cylon.spec.js index e4f2278..e59a893 100644 --- a/test/dist/specs/cylon.spec.js +++ b/test/dist/specs/cylon.spec.js @@ -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); diff --git a/test/src/specs/cylon.spec.coffee b/test/src/specs/cylon.spec.coffee index 4fffb71..b037853 100644 --- a/test/src/specs/cylon.spec.coffee +++ b/test/src/specs/cylon.spec.coffee @@ -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."