Skip to content

Commit

Permalink
Add master and robot test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 31, 2013
1 parent 5deff8d commit 411702d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 23 deletions.
3 changes: 1 addition & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func NewDevice(driver DriverInterface, r *Robot) *device {

func (d *device) Start() bool {
fmt.Println("Device " + d.Name + " started")
d.Driver.Start()
return true
return d.Driver.Start()
}

func (d *device) Commands() interface{} {
Expand Down
49 changes: 49 additions & 0 deletions master_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package gobot

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

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

var (
myMaster Master
)

BeforeEach(func() {
myMaster = Master{
Robots: []Robot{
Robot{
Name: "Robot 1",
Connections: []Connection{newTestAdaptor("Connection 1")},
Devices: []Device{newTestDriver("Device 1")},
},
Robot{
Name: "Robot 2",
Connections: []Connection{newTestAdaptor("Connection 2")},
Devices: []Device{newTestDriver("Device 2")},
},
Robot{
Name: "Robot 3",
Connections: []Connection{newTestAdaptor("Connection 3")},
Devices: []Device{newTestDriver("Device 3")},
},
},
}
myMaster.Robots[0].initDevices()
myMaster.Robots[0].initConnections()
})

Context("when valid", func() {
It("should Find the specific robot", func() {
Expect(myMaster.FindRobot("Robot 1").Name).To(Equal("Robot 1"))
})
It("should Find the specific robot device", func() {
Expect(myMaster.FindRobotDevice("Robot 1", "Device 1").Name).To(Equal("Device 1"))
})
It("should Find the specific robot connection", func() {
Expect(myMaster.FindRobotConnection("Robot 1", "Connection 1").Name).To(Equal("Connection 1"))
})
})
})
26 changes: 20 additions & 6 deletions robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ func (r *Robot) startRobot() {
r.initCommands()
r.initConnections()
r.initDevices()
r.startConnections()
r.startDevices()
if r.startConnections() != true {
panic("Could not start connections")
}
if r.startDevices() != true {
panic("Could not start devices")
}
if r.Work != nil {
r.Work()
}
Expand Down Expand Up @@ -67,20 +71,30 @@ func (r *Robot) initDevices() {
}
}

func (r *Robot) startConnections() {
func (r *Robot) startConnections() bool {
fmt.Println("Starting connections...")
success := true
for i := range r.connections {
fmt.Println("Starting connection " + r.connections[i].Name + "...")
r.connections[i].Connect()
if r.connections[i].Connect() == false {
success = false
break
}
}
return success
}

func (r *Robot) startDevices() {
func (r *Robot) startDevices() bool {
fmt.Println("Starting devices...")
success := true
for i := range r.devices {
fmt.Println("Starting device " + r.devices[i].Name + "...")
r.devices[i].Start()
if r.devices[i].Start() == false {
success = false
break
}
}
return success
}

func (r *Robot) GetDevices() []*device {
Expand Down
41 changes: 26 additions & 15 deletions robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var _ = Describe("Robot", func() {

BeforeEach(func() {
someRobot = Robot{
Work: func() {
Connections: []Connection{newTestAdaptor("Connection 1"), newTestAdaptor("Connection 2"), newTestAdaptor("Connection 3")},
Devices: []Device{newTestDriver("Device 1"), newTestDriver("Device 2"), newTestDriver("Device 3")},
Commands: map[string]interface{}{
"Command1": func() {},
"Command2": func() {},
},
}
})
Expand All @@ -29,26 +33,33 @@ var _ = Describe("Robot", func() {
Expect(someRobot.Name).NotTo(BeNil())
Expect(someRobot.Name).NotTo(Equal("Bumblebee"))
})
PIt("should Start", func() {
Expect(true)
It("initCommands should set RobotCommands equal to Commands Key", func() {
someRobot.initCommands()
Expect(someRobot.RobotCommands).To(Equal([]string{"Command1", "Command2"}))
})
PIt("should initConnections", func() {
Expect(true)
It("GetDevices should return robot devices", func() {
someRobot.initDevices()
Expect(someRobot.GetDevices).NotTo(BeNil())
})
PIt("should initDevices", func() {
Expect(true)
It("GetDevice should return a robot device", func() {
someRobot.initDevices()
Expect(someRobot.GetDevice("Device 1").Name).To(Equal("Device 1"))
})
PIt("should startConnections", func() {
Expect(true)
It("initConnections should initialize connections", func() {
someRobot.initConnections()
Expect(len(someRobot.connections)).To(Equal(3))
})
PIt("should startDevices", func() {
Expect(true)
It("initDevices should initialize devices", func() {
someRobot.initDevices()
Expect(len(someRobot.devices)).To(Equal(3))
})
PIt("should GetDevices", func() {
Expect(true)
It("startConnections should connect all connections", func() {
someRobot.initConnections()
Expect(someRobot.startConnections()).To(Equal(true))
})
PIt("should GetDevice", func() {
Expect(true)
It("startDevices should start all devices", func() {
someRobot.initDevices()
Expect(someRobot.startDevices()).To(Equal(true))
})
})
})
28 changes: 28 additions & 0 deletions test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gobot

type testDriver struct {
Driver
}

//func (me *testDriver) Start() bool { return true }
func (me *testDriver) Start() bool { return true }

type testAdaptor struct {
Adaptor
}

func (me *testAdaptor) Finalize() bool { return true }
func (me *testAdaptor) Connect() bool { return true }
func (me *testAdaptor) Disconnect() bool { return true }
func (me *testAdaptor) Reconnect() bool { return true }

func newTestDriver(name string) *testDriver {
d := new(testDriver)
d.Name = name
return d
}
func newTestAdaptor(name string) *testAdaptor {
a := new(testAdaptor)
a.Name = name
return a
}

0 comments on commit 411702d

Please sign in to comment.