Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 11, 2014
1 parent c75894c commit e318d70
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 28 deletions.
5 changes: 4 additions & 1 deletion connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 0 additions & 6 deletions gobot_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions master_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

var _ = Describe("Master", func() {

var (
myMaster *Master
)
Expand All @@ -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() {
Expand Down
6 changes: 5 additions & 1 deletion robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 4 additions & 15 deletions robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
})
6 changes: 6 additions & 0 deletions test_helper.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gobot

type null struct{}

func (null) Write(p []byte) (int, error) {
return len(p), nil
}

type testDriver struct {
Driver
}
Expand Down

0 comments on commit e318d70

Please sign in to comment.