forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.go
179 lines (148 loc) · 3.74 KB
/
test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gobot
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"time"
)
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)
}
// Assert ensures a and b are equal and of the same type, or else it
// causes a t.Error
func Assert(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
logFailure(t, fmt.Sprintf("%v - \"%v\", should equal, %v - \"%v\"",
a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
}
}
// Refute ensures a and b are not equal, causes a t.Error if they are equal
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)))
}
}
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 NullReadWriteCloser struct{}
func (NullReadWriteCloser) Write(p []byte) (int, error) {
return len(p), nil
}
func (NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}
func (NullReadWriteCloser) Close() error {
return nil
}
type testDriver struct {
Driver
}
func (t *testDriver) Init() bool { return true }
func (t *testDriver) Start() bool { return true }
func (t *testDriver) Halt() bool { return true }
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{
Driver: *NewDriver(
name,
"TestDriver",
adaptor,
"1",
100*time.Millisecond,
),
}
t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})
t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})
return t
}
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: *NewAdaptor(
name,
"TestAdaptor",
"/dev/null",
),
}
}
func NewTestRobot(name string) *Robot {
adaptor1 := NewTestAdaptor("Connection1")
adaptor2 := NewTestAdaptor("Connection2")
adaptor3 := NewTestAdaptor("")
driver1 := NewTestDriver("Device1", adaptor1)
driver2 := NewTestDriver("Device2", adaptor2)
driver3 := NewTestDriver("", adaptor3)
work := func() {}
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)
return fmt.Sprintf("hey %v, %v", robot, message)
})
return r
}
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.AddEvent("ping")
t.AddCommand("ping", func(params map[string]interface{}) interface{} {
return t.Ping()
})
return t
}
func (t *pingDriver) Ping() string {
Publish(t.Event("ping"), "ping")
return "pong"
}