hybridgroup.gobot/test_helper.go

124 lines
2.6 KiB
Go
Raw Normal View History

2013-12-31 08:51:21 +08:00
package gobot
2014-04-30 04:20:32 +08:00
import (
"fmt"
2014-06-13 05:38:03 +08:00
"reflect"
2014-06-14 03:39:02 +08:00
"runtime"
"strings"
2014-06-13 05:38:03 +08:00
"testing"
2014-07-11 02:14:08 +08:00
"time"
2014-04-30 04:20:32 +08:00
)
2014-04-16 08:59:44 +08:00
2014-06-13 05:38:03 +08:00
func Expect(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
2014-06-14 03:39:02 +08:00
_, file, line, _ := runtime.Caller(1)
s := strings.Split(file, "/")
2014-07-11 02:14:08 +08:00
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))
2014-06-13 05:38:03 +08:00
}
}
2014-06-14 05:46:07 +08:00
type testStruct struct {
i int
f float64
}
func NewTestStruct() *testStruct {
return &testStruct{
i: 10,
f: 0.2,
}
}
2014-06-11 06:16:11 +08:00
func (t *testStruct) Hello(name string, message string) string {
2014-04-16 12:10:32 +08:00
return fmt.Sprintf("Hello %v! %v", name, message)
}
2014-06-14 07:01:39 +08:00
type NullReadWriteCloser struct{}
2014-04-11 21:02:21 +08:00
2014-06-14 07:01:39 +08:00
func (NullReadWriteCloser) Write(p []byte) (int, error) {
2014-04-11 21:02:21 +08:00
return len(p), nil
}
2014-06-14 07:01:39 +08:00
func (NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}
2014-07-10 00:38:43 +08:00
2014-06-14 07:01:39 +08:00
func (NullReadWriteCloser) Close() error {
return nil
}
2013-12-31 08:51:21 +08:00
type testDriver struct {
Driver
}
2014-06-11 06:16:11 +08:00
func (t *testDriver) Init() bool { return true }
func (t *testDriver) Start() bool { return true }
func (t *testDriver) Halt() bool { return true }
2013-12-31 08:51:21 +08:00
2014-06-14 05:46:07 +08:00
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
2014-06-12 08:41:04 +08:00
t := &testDriver{
2014-07-11 02:14:08 +08:00
Driver: *NewDriver(
name,
"TestDriver",
adaptor,
"1",
100*time.Millisecond,
),
2013-12-31 09:22:16 +08:00
}
2014-06-12 08:41:04 +08:00
2014-07-10 09:32:27 +08:00
t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})
2014-07-10 09:32:27 +08:00
t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
2014-06-12 08:41:04 +08:00
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})
return t
2013-12-31 08:51:21 +08:00
}
2014-04-15 14:16:35 +08:00
2014-06-14 05:46:07 +08:00
type testAdaptor struct {
Adaptor
}
func (t *testAdaptor) Finalize() bool { return true }
func (t *testAdaptor) Connect() bool { return true }
func NewTestAdaptor(name string) *testAdaptor {
2014-06-11 06:16:11 +08:00
return &testAdaptor{
2014-07-11 02:14:08 +08:00
Adaptor: *NewAdaptor(
name,
"TestAdaptor",
"/dev/null",
map[string]interface{}{
2014-06-11 06:16:11 +08:00
"param1": "1",
"param2": 2,
},
2014-07-11 02:14:08 +08:00
),
2014-04-15 14:16:35 +08:00
}
2013-12-31 08:51:21 +08:00
}
2014-01-01 05:16:25 +08:00
2014-05-16 02:50:45 +08:00
func NewTestRobot(name string) *Robot {
2014-06-14 05:46:07 +08:00
adaptor1 := NewTestAdaptor("Connection 1")
adaptor2 := NewTestAdaptor("Connection 2")
2014-07-11 02:14:08 +08:00
adaptor3 := NewTestAdaptor("")
2014-06-14 05:46:07 +08:00
driver1 := NewTestDriver("Device 1", adaptor1)
driver2 := NewTestDriver("Device 2", adaptor2)
2014-07-11 02:14:08 +08:00
driver3 := NewTestDriver("", adaptor3)
2014-05-03 18:22:22 +08:00
work := func() {}
2014-07-11 02:14:08 +08:00
r := NewRobot(name,
[]Connection{adaptor1, adaptor2, adaptor3},
[]Device{driver1, driver2, driver3},
work,
)
2014-06-12 08:41:04 +08:00
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string)
robot := params["robot"].(string)
return fmt.Sprintf("hey %v, %v", robot, message)
})
return r
2014-01-01 05:16:25 +08:00
}