From 681e197ed94af34adfe5c799692023a386cb2293 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Tue, 9 Jun 2015 11:50:50 -0700 Subject: [PATCH] Stop giving robots random names, use inc. counter --- lib/robot.js | 15 ++++----------- spec/lib/robot.spec.js | 13 ++----------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index 054c26b..28a08c2 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -17,6 +17,9 @@ var initializer = require("./initializer"), var Async = require("async"), EventEmitter = require("events").EventEmitter; +// used when creating default robot names +var ROBOT_ID = 1; + /** * Creates a new Robot instance based on provided options * @@ -100,16 +103,6 @@ var Robot = module.exports = function Robot(opts) { Utils.subclass(Robot, EventEmitter); -/** - * Generates a random name for a Robot. - * - * @todo should perhaps just increment a counter as a global value? - * @returns {String} a robot name - */ -Robot.randomName = function() { - return "Robot " + (Math.floor(Math.random() * 100000)); -}; - /** * Condenses information on a Robot to a JSON-serializable format * @@ -159,7 +152,7 @@ Robot.prototype.connection = function(name, conn) { * @return {void} */ Robot.prototype.initRobot = function(opts) { - this.name = opts.name || Robot.randomName(); + this.name = opts.name || "Robot " + ROBOT_ID++; this.connections = {}; this.devices = {}; this.adaptors = {}; diff --git a/spec/lib/robot.spec.js b/spec/lib/robot.spec.js index 75ae455..c5344be 100644 --- a/spec/lib/robot.spec.js +++ b/spec/lib/robot.spec.js @@ -32,17 +32,8 @@ describe("Robot", function() { }); context("if not provided", function() { - beforeEach(function() { - stub(Robot, "randomName").returns("New Robot"); - }); - - afterEach(function() { - Robot.randomName.restore(); - }); - - it("is set to a random name", function() { - var bot = new Robot({}); - expect(bot.name).to.be.eql("New Robot"); + it("is set to an incrementing name", function() { + expect(new Robot({}).name).to.match(/Robot \d/); }); }); });