mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Update gobot tests
This commit is contained in:
parent
99ac20c03a
commit
bccc355a19
@ -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").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()))
|
||||
}
|
||||
|
@ -6,13 +6,15 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if !reflect.DeepEqual(a, b) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
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 {
|
||||
t := &testDriver{
|
||||
Driver: Driver{
|
||||
commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
name: name,
|
||||
adaptor: adaptor,
|
||||
},
|
||||
Driver: *NewDriver(
|
||||
name,
|
||||
"TestDriver",
|
||||
adaptor,
|
||||
"1",
|
||||
100*time.Millisecond,
|
||||
),
|
||||
}
|
||||
|
||||
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 {
|
||||
return &testAdaptor{
|
||||
Adaptor: Adaptor{
|
||||
name: name,
|
||||
params: map[string]interface{}{
|
||||
Adaptor: *NewAdaptor(
|
||||
name,
|
||||
"TestAdaptor",
|
||||
"/dev/null",
|
||||
map[string]interface{}{
|
||||
"param1": "1",
|
||||
"param2": 2,
|
||||
},
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func NewTestRobot(name string) *Robot {
|
||||
adaptor1 := NewTestAdaptor("Connection 1")
|
||||
adaptor2 := NewTestAdaptor("Connection 2")
|
||||
adaptor3 := NewTestAdaptor("Connection 3")
|
||||
adaptor3 := NewTestAdaptor("")
|
||||
driver1 := NewTestDriver("Device 1", adaptor1)
|
||||
driver2 := NewTestDriver("Device 2", adaptor2)
|
||||
driver3 := NewTestDriver("Device 3", adaptor3)
|
||||
driver3 := NewTestDriver("", adaptor3)
|
||||
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{} {
|
||||
message := params["message"].(string)
|
||||
robot := params["robot"].(string)
|
||||
|
Loading…
x
Reference in New Issue
Block a user