1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00

Add master and robot test coverage

This commit is contained in:
Adrian Zankich 2013-12-30 16:51:21 -08:00
parent 5deff8dfed
commit 411702d284
5 changed files with 124 additions and 23 deletions

View File

@ -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{} {

49
master_test.go Normal file
View File

@ -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"))
})
})
})

View File

@ -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()
}
@ -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 {

View File

@ -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() {},
},
}
})
@ -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
test_helper.go Normal file
View File

@ -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
}