diff --git a/connection_test.go b/connection_test.go index 013a6aad0..f10837430 100644 --- a/connection_test.go +++ b/connection_test.go @@ -13,7 +13,10 @@ var _ = Describe("Connection", func() { BeforeEach(func() { someRobot = newTestRobot("") - someRobot.startRobot() + start = func(r *Robot) { + r.startRobot() + } + someRobot.Start() }) Context("when valid", func() { diff --git a/device_test.go b/device_test.go index 0f8d21092..7396ba4ac 100644 --- a/device_test.go +++ b/device_test.go @@ -13,7 +13,10 @@ var _ = Describe("Device", func() { BeforeEach(func() { someRobot = newTestRobot("") - someRobot.startRobot() + start = func(r *Robot) { + r.startRobot() + } + someRobot.Start() }) Context("when valid", func() { diff --git a/gobot_suite_test.go b/gobot_suite_test.go index 337362460..0c8e34601 100644 --- a/gobot_suite_test.go +++ b/gobot_suite_test.go @@ -7,12 +7,6 @@ import ( "testing" ) -type null struct{} - -func (null) Write(p []byte) (int, error) { - return len(p), nil -} - func TestGobot(t *testing.T) { log.SetOutput(new(null)) RegisterFailHandler(Fail) diff --git a/master.go b/master.go index d3f5fc9ef..2c3f54b94 100644 --- a/master.go +++ b/master.go @@ -17,7 +17,7 @@ func GobotMaster() *Master { return m } -func (m *Master) Start() { +var startRobots = func(m *Master) { runtime.GOMAXPROCS(m.NumCPU) for s := range m.Robots { @@ -36,6 +36,10 @@ func (m *Master) Start() { } } +func (m *Master) Start() { + startRobots(m) +} + func (m *Master) FindRobot(name string) *Robot { for _, robot := range m.Robots { if robot.Name == name { diff --git a/master_test.go b/master_test.go index 2be924524..86b9865bc 100644 --- a/master_test.go +++ b/master_test.go @@ -6,7 +6,6 @@ import ( ) var _ = Describe("Master", func() { - var ( myMaster *Master ) @@ -18,9 +17,12 @@ var _ = Describe("Master", func() { newTestRobot("Robot 2"), newTestRobot("Robot 3"), } - for s := range myMaster.Robots { - myMaster.Robots[s].startRobot() + startRobots = func(m *Master) { + for s := range m.Robots { + m.Robots[s].startRobot() + } } + myMaster.Start() }) Context("when valid", func() { diff --git a/robot.go b/robot.go index c8ab34fb3..1523d53b4 100644 --- a/robot.go +++ b/robot.go @@ -18,12 +18,16 @@ type Robot struct { devices []*device `json:"-"` } -func (r *Robot) Start() { +var start = func(r *Robot) { m := GobotMaster() m.Robots = []Robot{*r} m.Start() } +func (r *Robot) Start() { + start(r) +} + func (r *Robot) startRobot() { r.initName() r.initCommands() diff --git a/robot_test.go b/robot_test.go index ff91fba7e..efebe773b 100644 --- a/robot_test.go +++ b/robot_test.go @@ -13,46 +13,35 @@ var _ = Describe("Robot", func() { BeforeEach(func() { someRobot = newTestRobot("") + start = func(r *Robot) { + r.startRobot() + } + someRobot.Start() }) Context("when valid", func() { It("initName should not change name when already set", func() { someRobot.Name = "Bumblebee" - someRobot.initName() Expect(someRobot.Name).To(Equal("Bumblebee")) }) It("initName should set random name when not set", func() { - someRobot.initName() Expect(someRobot.Name).NotTo(BeNil()) Expect(someRobot.Name).NotTo(Equal("Bumblebee")) }) It("initCommands should set RobotCommands equal to Commands Key", func() { - someRobot.initCommands() Expect(someRobot.RobotCommands).To(Equal([]string{"Command1", "Command2"})) }) It("GetDevices should return robot devices", func() { - someRobot.initDevices() Expect(someRobot.GetDevices).NotTo(BeNil()) }) It("GetDevice should return a robot device", func() { - someRobot.initDevices() Expect(someRobot.GetDevice("Device 1").Name).To(Equal("Device 1")) }) It("initConnections should initialize connections", func() { - someRobot.initConnections() Expect(len(someRobot.connections)).To(Equal(3)) }) It("initDevices should initialize devices", func() { - someRobot.initDevices() Expect(len(someRobot.devices)).To(Equal(3)) }) - It("startConnections should connect all connections", func() { - someRobot.initConnections() - Expect(someRobot.startConnections()).To(Equal(true)) - }) - It("startDevices should start all devices", func() { - someRobot.initDevices() - Expect(someRobot.startDevices()).To(Equal(true)) - }) }) }) diff --git a/test_helper.go b/test_helper.go index d73ed1220..ad9c34751 100644 --- a/test_helper.go +++ b/test_helper.go @@ -1,5 +1,11 @@ package gobot +type null struct{} + +func (null) Write(p []byte) (int, error) { + return len(p), nil +} + type testDriver struct { Driver }