mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-29 13:49:14 +08:00
Add master and robot test coverage
This commit is contained in:
parent
5deff8dfed
commit
411702d284
@ -28,8 +28,7 @@ func NewDevice(driver DriverInterface, r *Robot) *device {
|
|||||||
|
|
||||||
func (d *device) Start() bool {
|
func (d *device) Start() bool {
|
||||||
fmt.Println("Device " + d.Name + " started")
|
fmt.Println("Device " + d.Name + " started")
|
||||||
d.Driver.Start()
|
return d.Driver.Start()
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *device) Commands() interface{} {
|
func (d *device) Commands() interface{} {
|
||||||
|
49
master_test.go
Normal file
49
master_test.go
Normal 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"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
26
robot.go
26
robot.go
@ -28,8 +28,12 @@ func (r *Robot) startRobot() {
|
|||||||
r.initCommands()
|
r.initCommands()
|
||||||
r.initConnections()
|
r.initConnections()
|
||||||
r.initDevices()
|
r.initDevices()
|
||||||
r.startConnections()
|
if r.startConnections() != true {
|
||||||
r.startDevices()
|
panic("Could not start connections")
|
||||||
|
}
|
||||||
|
if r.startDevices() != true {
|
||||||
|
panic("Could not start devices")
|
||||||
|
}
|
||||||
if r.Work != nil {
|
if r.Work != nil {
|
||||||
r.Work()
|
r.Work()
|
||||||
}
|
}
|
||||||
@ -67,20 +71,30 @@ func (r *Robot) initDevices() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Robot) startConnections() {
|
func (r *Robot) startConnections() bool {
|
||||||
fmt.Println("Starting connections...")
|
fmt.Println("Starting connections...")
|
||||||
|
success := true
|
||||||
for i := range r.connections {
|
for i := range r.connections {
|
||||||
fmt.Println("Starting connection " + r.connections[i].Name + "...")
|
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...")
|
fmt.Println("Starting devices...")
|
||||||
|
success := true
|
||||||
for i := range r.devices {
|
for i := range r.devices {
|
||||||
fmt.Println("Starting device " + r.devices[i].Name + "...")
|
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 {
|
func (r *Robot) GetDevices() []*device {
|
||||||
|
@ -13,7 +13,11 @@ var _ = Describe("Robot", func() {
|
|||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
someRobot = Robot{
|
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(BeNil())
|
||||||
Expect(someRobot.Name).NotTo(Equal("Bumblebee"))
|
Expect(someRobot.Name).NotTo(Equal("Bumblebee"))
|
||||||
})
|
})
|
||||||
PIt("should Start", func() {
|
It("initCommands should set RobotCommands equal to Commands Key", func() {
|
||||||
Expect(true)
|
someRobot.initCommands()
|
||||||
|
Expect(someRobot.RobotCommands).To(Equal([]string{"Command1", "Command2"}))
|
||||||
})
|
})
|
||||||
PIt("should initConnections", func() {
|
It("GetDevices should return robot devices", func() {
|
||||||
Expect(true)
|
someRobot.initDevices()
|
||||||
|
Expect(someRobot.GetDevices).NotTo(BeNil())
|
||||||
})
|
})
|
||||||
PIt("should initDevices", func() {
|
It("GetDevice should return a robot device", func() {
|
||||||
Expect(true)
|
someRobot.initDevices()
|
||||||
|
Expect(someRobot.GetDevice("Device 1").Name).To(Equal("Device 1"))
|
||||||
})
|
})
|
||||||
PIt("should startConnections", func() {
|
It("initConnections should initialize connections", func() {
|
||||||
Expect(true)
|
someRobot.initConnections()
|
||||||
|
Expect(len(someRobot.connections)).To(Equal(3))
|
||||||
})
|
})
|
||||||
PIt("should startDevices", func() {
|
It("initDevices should initialize devices", func() {
|
||||||
Expect(true)
|
someRobot.initDevices()
|
||||||
|
Expect(len(someRobot.devices)).To(Equal(3))
|
||||||
})
|
})
|
||||||
PIt("should GetDevices", func() {
|
It("startConnections should connect all connections", func() {
|
||||||
Expect(true)
|
someRobot.initConnections()
|
||||||
|
Expect(someRobot.startConnections()).To(Equal(true))
|
||||||
})
|
})
|
||||||
PIt("should GetDevice", func() {
|
It("startDevices should start all devices", func() {
|
||||||
Expect(true)
|
someRobot.initDevices()
|
||||||
|
Expect(someRobot.startDevices()).To(Equal(true))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
28
test_helper.go
Normal file
28
test_helper.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user