From 2019cd5b6c7acc86c6387ee84c1f1d893b23169b Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 20 Oct 2013 13:29:41 -0700 Subject: [PATCH] All robots have a name --- src/robot.coffee | 17 +++++++++++------ test/src/specs/robot.spec.coffee | 13 +++++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/robot.coffee b/src/robot.coffee index 2d09c76..e01dcf4 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -9,16 +9,23 @@ 'use strict'; Connection = source("connection") +Device = source("device") module.exports = class Robot constructor: (opts) -> + opts ?= {} @_connections = {} @_devices = {} - @name = opts.name + @name = opts.name or @constructor.randomName() @connections = initConnections(opts.connection or opts.connections or {}) @devices = initDevices(opts.device or opts.devices or {}) @work = opts.work or -> (console.log "No work yet") + start: -> + startConnections() + startDevices() + (@work) + initConnections = (connections) -> console.log "Initializing connections..." initConnection connection for connection in connections @@ -35,13 +42,11 @@ module.exports = class Robot console.log "Initializing device '#{ device.name }'..." @_devices[device.name] = new Device(device) - start: -> - startConnections() - startDevices() - (@work) - startConnections = -> console.log "Starting connections..." startDevices = -> console.log "Starting devices..." + + @randomName: -> + "Robot #{ Math.floor(Math.random() * 100000) }" diff --git a/test/src/specs/robot.spec.coffee b/test/src/specs/robot.spec.coffee index 0627eeb..24c7e6a 100644 --- a/test/src/specs/robot.spec.coffee +++ b/test/src/specs/robot.spec.coffee @@ -6,10 +6,15 @@ describe "basic tests", -> testWork = -> console.log "hi" - r = new Robot(name: "irobot", work: testWork) + robot = new Robot(name: "irobot", work: testWork) - it "robot should have a name", -> - r.name.should.be.equal 'irobot' + it "robot should have a name, if given", -> + robot.name.should.be.equal 'irobot' + + it "robot should have a random name, if not given", -> + sinon.stub(Robot, 'randomName').returns('Electra') + r = new Robot + r.name.should.be.equal 'Electra' it "robot should have work", -> - r.work.should.be.equal testWork + robot.work.should.be.equal testWork