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

127 lines
3.4 KiB
Go
Raw Normal View History

2013-12-30 16:51:21 -08:00
package gobot
import (
2014-11-30 00:19:53 -08:00
"errors"
2014-06-12 14:38:03 -07:00
"log"
"os"
2014-06-12 14:38:03 -07:00
"testing"
"github.com/hybridgroup/gobot/gobottest"
2013-12-30 16:51:21 -08:00
)
2014-11-30 00:19:53 -08:00
func TestConnectionEach(t *testing.T) {
r := newTestRobot("Robot1")
i := 0
r.Connections().Each(func(conn Connection) {
i++
})
gobottest.Assert(t, r.Connections().Len(), i)
2014-11-30 00:19:53 -08:00
}
func initTestMaster() *Master {
2014-06-13 16:01:39 -07:00
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
2014-11-30 00:19:53 -08:00
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot(""))
2014-06-13 16:01:39 -07:00
return g
2014-06-12 14:38:03 -07:00
}
2013-12-30 16:51:21 -08:00
func initTestMaster1Robot() *Master {
log.SetOutput(&NullReadWriteCloser{})
g := NewMaster()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(newTestRobot("Robot99"))
return g
}
2014-11-30 00:19:53 -08:00
func TestVersion(t *testing.T) {
gobottest.Assert(t, version, Version())
2014-11-30 00:19:53 -08:00
}
func TestNullReadWriteCloser(t *testing.T) {
n := &NullReadWriteCloser{}
i, _ := n.Write([]byte{1, 2, 3})
gobottest.Assert(t, i, 3)
2014-11-30 00:19:53 -08:00
i, _ = n.Read(make([]byte, 10))
gobottest.Assert(t, i, 10)
gobottest.Assert(t, n.Close(), nil)
2014-06-12 14:38:03 -07:00
}
2014-05-03 03:22:22 -07:00
2014-06-13 16:01:39 -07:00
func TestGobotRobot(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, g.Robot("Robot1").Name, "Robot1")
gobottest.Assert(t, g.Robot("Robot4"), (*Robot)(nil))
gobottest.Assert(t, g.Robot("Robot4").Device("Device1"), (Device)(nil))
gobottest.Assert(t, g.Robot("Robot4").Connection("Connection1"), (Connection)(nil))
gobottest.Assert(t, g.Robot("Robot1").Device("Device4"), (Device)(nil))
gobottest.Assert(t, g.Robot("Robot1").Device("Device1").Name(), "Device1")
gobottest.Assert(t, g.Robot("Robot1").Devices().Len(), 3)
gobottest.Assert(t, g.Robot("Robot1").Connection("Connection4"), (Connection)(nil))
gobottest.Assert(t, g.Robot("Robot1").Connections().Len(), 3)
2014-06-12 14:38:03 -07:00
}
2014-07-10 11:14:08 -07:00
func TestGobotToJSON(t *testing.T) {
g := initTestMaster()
2014-07-10 11:14:08 -07:00
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := NewJSONMaster(g)
gobottest.Assert(t, len(json.Robots), g.Robots().Len())
gobottest.Assert(t, len(json.Commands), len(g.Commands()))
2014-07-10 11:14:08 -07:00
}
2014-11-30 00:19:53 -08:00
func TestMasterStart(t *testing.T) {
g := initTestMaster()
gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop(), nil)
2014-11-30 00:19:53 -08:00
}
func TestMasterStartDriverErrors(t *testing.T) {
g := initTestMaster1Robot()
2014-11-30 00:19:53 -08:00
testDriverStart = func() (err error) {
return errors.New("driver start error 1")
2014-11-30 00:19:53 -08:00
}
gobottest.Assert(t, g.Start().Error(), "3 error(s) occurred:\n\n* driver start error 1\n* driver start error 1\n* driver start error 1")
gobottest.Assert(t, g.Stop(), nil)
2014-11-30 00:19:53 -08:00
testDriverStart = func() (err error) { return }
}
func TestMasterStartAdaptorErrors(t *testing.T) {
g := initTestMaster1Robot()
testAdaptorConnect = func() (err error) {
return errors.New("adaptor start error 1")
2014-11-30 00:19:53 -08:00
}
gobottest.Assert(t, g.Start().Error(), "3 error(s) occurred:\n\n* adaptor start error 1\n* adaptor start error 1\n* adaptor start error 1")
gobottest.Assert(t, g.Stop(), nil)
2014-11-30 00:19:53 -08:00
testAdaptorConnect = func() (err error) { return }
}
2014-11-30 00:19:53 -08:00
func TestMasterHaltErrors(t *testing.T) {
g := initTestMaster1Robot()
testDriverHalt = func() (err error) {
return errors.New("driver halt error 2")
2014-11-30 00:19:53 -08:00
}
testAdaptorFinalize = func() (err error) {
return errors.New("adaptor finalize error 2")
2014-11-30 00:19:53 -08:00
}
gobottest.Assert(t, g.Start(), nil)
gobottest.Assert(t, g.Stop().Error(), "3 error(s) occurred:\n\n* adaptor finalize error 2\n* adaptor finalize error 2\n* adaptor finalize error 2")
2014-11-30 00:19:53 -08:00
}