1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/test_helper.go

169 lines
3.5 KiB
Go
Raw Normal View History

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