mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
Clean up test_helper
This commit is contained in:
parent
1a63727776
commit
56848d5a81
@ -9,15 +9,15 @@ import (
|
||||
var g *Gobot
|
||||
|
||||
func init() {
|
||||
log.SetOutput(new(null))
|
||||
log.SetOutput(new(Null))
|
||||
g = NewGobot()
|
||||
g.trap = func(c chan os.Signal) {
|
||||
c <- os.Interrupt
|
||||
}
|
||||
g.Robots = []*Robot{
|
||||
newTestRobot("Robot 1"),
|
||||
newTestRobot("Robot 2"),
|
||||
newTestRobot("Robot 3"),
|
||||
NewTestRobot("Robot 1"),
|
||||
NewTestRobot("Robot 2"),
|
||||
NewTestRobot("Robot 3"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,6 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testStruct struct {
|
||||
i int
|
||||
f float64
|
||||
}
|
||||
|
||||
func Expect(t *testing.T, a interface{}, b interface{}) {
|
||||
if !reflect.DeepEqual(a, b) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
@ -21,13 +16,25 @@ func Expect(t *testing.T, a interface{}, b interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
type testStruct struct {
|
||||
i int
|
||||
f float64
|
||||
}
|
||||
|
||||
func NewTestStruct() *testStruct {
|
||||
return &testStruct{
|
||||
i: 10,
|
||||
f: 0.2,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *testStruct) Hello(name string, message string) string {
|
||||
return fmt.Sprintf("Hello %v! %v", name, message)
|
||||
}
|
||||
|
||||
type null struct{}
|
||||
type Null struct{}
|
||||
|
||||
func (null) Write(p []byte) (int, error) {
|
||||
func (Null) Write(p []byte) (int, error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
@ -40,14 +47,7 @@ func (t *testDriver) Init() bool { return true }
|
||||
func (t *testDriver) Start() bool { return true }
|
||||
func (t *testDriver) Halt() bool { return true }
|
||||
|
||||
type testAdaptor struct {
|
||||
Adaptor
|
||||
}
|
||||
|
||||
func (t *testAdaptor) Finalize() bool { return true }
|
||||
func (t *testAdaptor) Connect() bool { return true }
|
||||
|
||||
func newTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
||||
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
||||
t := &testDriver{
|
||||
Driver: Driver{
|
||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||
@ -69,7 +69,14 @@ func newTestDriver(name string, adaptor *testAdaptor) *testDriver {
|
||||
return t
|
||||
}
|
||||
|
||||
func newTestAdaptor(name string) *testAdaptor {
|
||||
type testAdaptor struct {
|
||||
Adaptor
|
||||
}
|
||||
|
||||
func (t *testAdaptor) Finalize() bool { return true }
|
||||
func (t *testAdaptor) Connect() bool { return true }
|
||||
|
||||
func NewTestAdaptor(name string) *testAdaptor {
|
||||
return &testAdaptor{
|
||||
Adaptor: Adaptor{
|
||||
Name: name,
|
||||
@ -82,15 +89,12 @@ func newTestAdaptor(name string) *testAdaptor {
|
||||
}
|
||||
|
||||
func NewTestRobot(name string) *Robot {
|
||||
return newTestRobot(name)
|
||||
}
|
||||
func newTestRobot(name string) *Robot {
|
||||
adaptor1 := newTestAdaptor("Connection 1")
|
||||
adaptor2 := newTestAdaptor("Connection 2")
|
||||
adaptor3 := newTestAdaptor("Connection 3")
|
||||
driver1 := newTestDriver("Device 1", adaptor1)
|
||||
driver2 := newTestDriver("Device 2", adaptor2)
|
||||
driver3 := newTestDriver("Device 3", adaptor3)
|
||||
adaptor1 := NewTestAdaptor("Connection 1")
|
||||
adaptor2 := NewTestAdaptor("Connection 2")
|
||||
adaptor3 := NewTestAdaptor("Connection 3")
|
||||
driver1 := NewTestDriver("Device 1", adaptor1)
|
||||
driver2 := NewTestDriver("Device 2", adaptor2)
|
||||
driver3 := NewTestDriver("Device 3", adaptor3)
|
||||
work := func() {}
|
||||
r := NewRobot(name, []Connection{adaptor1, adaptor2, adaptor3}, []Device{driver1, driver2, driver3}, work)
|
||||
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
|
||||
@ -100,10 +104,3 @@ func newTestRobot(name string) *Robot {
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func newTestStruct() *testStruct {
|
||||
return &testStruct{
|
||||
i: 10,
|
||||
f: 0.2,
|
||||
}
|
||||
}
|
||||
|
@ -64,16 +64,16 @@ func TestRand(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFieldByName(t *testing.T) {
|
||||
testInterface := *newTestStruct()
|
||||
testInterface := *NewTestStruct()
|
||||
Expect(t, FieldByName(testInterface, "i").Int(), int64(10))
|
||||
}
|
||||
|
||||
func TestFieldByNamePtr(t *testing.T) {
|
||||
testInterface := newTestStruct()
|
||||
testInterface := NewTestStruct()
|
||||
Expect(t, FieldByNamePtr(testInterface, "f").Float(), 0.2)
|
||||
}
|
||||
|
||||
func TestCall(t *testing.T) {
|
||||
testInterface := newTestStruct()
|
||||
testInterface := NewTestStruct()
|
||||
Expect(t, Call(testInterface, "Hello", "Human", "How are you?")[0].String(), "Hello Human! How are you?")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user