1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-01 13:48:57 +08:00

Update gobot tests

This commit is contained in:
Adrian Zankich 2014-07-10 11:14:08 -07:00
parent 99ac20c03a
commit bccc355a19
2 changed files with 33 additions and 13 deletions

View File

@ -33,3 +33,13 @@ func TestGobotRobot(t *testing.T) {
Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (Connection)(nil)) Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (Connection)(nil))
Expect(t, g.Robot("Robot 1").Connections().Len(), 3) Expect(t, g.Robot("Robot 1").Connections().Len(), 3)
} }
func TestGobotToJSON(t *testing.T) {
g := initTestGobot()
g.AddCommand("test_function", func(params map[string]interface{}) interface{} {
return nil
})
json := g.ToJSON()
Expect(t, len(json.Robots), g.Robots().Len())
Expect(t, len(json.Commands), len(g.Commands()))
}

View File

@ -6,13 +6,15 @@ import (
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"time"
) )
func Expect(t *testing.T, a interface{}, b interface{}) { func Expect(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) { if !reflect.DeepEqual(a, b) {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
s := strings.Split(file, "/") s := strings.Split(file, "/")
t.Errorf("%v:%v Got %v - type %v, Expected %v - type %v", s[len(s)-1], line, a, reflect.TypeOf(a), b, reflect.TypeOf(b)) t.Errorf("%v:%v Got %v - type %v, Expected %v - type %v",
s[len(s)-1], line, a, reflect.TypeOf(a), b, reflect.TypeOf(b))
} }
} }
@ -56,11 +58,13 @@ func (t *testDriver) Halt() bool { return true }
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver { func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{ t := &testDriver{
Driver: Driver{ Driver: *NewDriver(
commands: make(map[string]func(map[string]interface{}) interface{}), name,
name: name, "TestDriver",
adaptor: adaptor, adaptor,
}, "1",
100*time.Millisecond,
),
} }
t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} { t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
@ -85,25 +89,31 @@ func (t *testAdaptor) Connect() bool { return true }
func NewTestAdaptor(name string) *testAdaptor { func NewTestAdaptor(name string) *testAdaptor {
return &testAdaptor{ return &testAdaptor{
Adaptor: Adaptor{ Adaptor: *NewAdaptor(
name: name, name,
params: map[string]interface{}{ "TestAdaptor",
"/dev/null",
map[string]interface{}{
"param1": "1", "param1": "1",
"param2": 2, "param2": 2,
}, },
}, ),
} }
} }
func NewTestRobot(name string) *Robot { func NewTestRobot(name string) *Robot {
adaptor1 := NewTestAdaptor("Connection 1") adaptor1 := NewTestAdaptor("Connection 1")
adaptor2 := NewTestAdaptor("Connection 2") adaptor2 := NewTestAdaptor("Connection 2")
adaptor3 := NewTestAdaptor("Connection 3") adaptor3 := NewTestAdaptor("")
driver1 := NewTestDriver("Device 1", adaptor1) driver1 := NewTestDriver("Device 1", adaptor1)
driver2 := NewTestDriver("Device 2", adaptor2) driver2 := NewTestDriver("Device 2", adaptor2)
driver3 := NewTestDriver("Device 3", adaptor3) driver3 := NewTestDriver("", adaptor3)
work := func() {} work := func() {}
r := NewRobot(name, []Connection{adaptor1, adaptor2, adaptor3}, []Device{driver1, driver2, driver3}, work) r := NewRobot(name,
[]Connection{adaptor1, adaptor2, adaptor3},
[]Device{driver1, driver2, driver3},
work,
)
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} { r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string) message := params["message"].(string)
robot := params["robot"].(string) robot := params["robot"].(string)