forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
57 lines (48 loc) · 1.11 KB
/
driver_test.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
package gobot
import (
"fmt"
"testing"
"time"
)
func TestDriver(t *testing.T) {
a := NewTestAdaptor("testAdaptor")
d := NewDriver("",
"testDriver",
a,
"1",
5*time.Second,
)
Refute(t, d.Name(), "")
Assert(t, d.Type(), "testDriver")
Assert(t, d.Interval(), 5*time.Second)
Assert(t, d.Pin(), "1")
Assert(t, d.Adaptor(), a)
d.SetPin("10")
Assert(t, d.Pin(), "10")
d.SetName("myDriver")
Assert(t, d.Name(), "myDriver")
d.SetInterval(100 * time.Second)
Assert(t, d.Interval(), 100*time.Second)
Assert(t, len(d.Commands()), 0)
d.AddCommand("cmd1", func(params map[string]interface{}) interface{} {
return fmt.Sprintf("hello from %v", params["name"])
})
Assert(t, len(d.Commands()), 1)
Assert(t,
d.Command("cmd1")(map[string]interface{}{"name": d.Name()}).(string),
"hello from "+d.Name(),
)
Assert(t, len(d.Events()), 0)
d.AddEvent("event1")
Assert(t, len(d.Events()), 1)
Refute(t, d.Event("event1"), nil)
defer func() {
r := recover()
if r != nil {
Assert(t, "Unknown Driver Event: event2", r)
} else {
t.Errorf("Did not return Unknown Event error")
}
}()
d.Event("event2")
}