forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pir_motion_driver_test.go
101 lines (80 loc) · 2.1 KB
/
pir_motion_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
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
package gpio
import (
"errors"
"strings"
"testing"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*PIRMotionDriver)(nil)
const motionTestDelay = 150
func initTestPIRMotionDriver() *PIRMotionDriver {
return NewPIRMotionDriver(newGpioTestAdaptor(), "1")
}
func TestPIRMotionDriverHalt(t *testing.T) {
d := initTestPIRMotionDriver()
go func() {
<-d.halt
}()
gobottest.Assert(t, d.Halt(), nil)
}
func TestPIRMotionDriver(t *testing.T) {
d := NewPIRMotionDriver(newGpioTestAdaptor(), "1")
gobottest.Refute(t, d.Connection(), nil)
d = NewPIRMotionDriver(newGpioTestAdaptor(), "1", 30*time.Second)
gobottest.Assert(t, d.interval, 30*time.Second)
}
func TestPIRMotionDriverStart(t *testing.T) {
sem := make(chan bool, 0)
a := newGpioTestAdaptor()
d := NewPIRMotionDriver(a, "1")
gobottest.Assert(t, d.Start(), nil)
d.Once(MotionDetected, func(data interface{}) {
gobottest.Assert(t, d.Active, true)
sem <- true
})
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
})
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"MotionDetected\" was not published")
}
d.Once(MotionStopped, func(data interface{}) {
gobottest.Assert(t, d.Active, false)
sem <- true
})
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 0
return
})
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
}
d.Once(Error, func(data interface{}) {
sem <- true
})
a.TestAdaptorDigitalRead(func() (val int, err error) {
err = errors.New("digital read error")
return
})
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"Error\" was not published")
}
}
func TestPIRDriverDefaultName(t *testing.T) {
d := initTestPIRMotionDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "PIR"), true)
}
func TestPIRDriverSetName(t *testing.T) {
d := initTestPIRMotionDriver()
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}