forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_driver_test.go
132 lines (111 loc) · 3.23 KB
/
relay_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
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
package gpio
import (
"strings"
"testing"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Driver = (*RelayDriver)(nil)
// Helper to return low/high value for testing
func (l *RelayDriver) High() bool { return l.high }
func initTestRelayDriver() (*RelayDriver, *gpioTestAdaptor) {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return nil
}
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return nil
}
return NewRelayDriver(a, "1"), a
}
func TestRelayDriverDefaultName(t *testing.T) {
g, _ := initTestRelayDriver()
gobottest.Refute(t, g.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Relay"), true)
}
func TestRelayDriverSetName(t *testing.T) {
g, _ := initTestRelayDriver()
g.SetName("mybot")
gobottest.Assert(t, g.Name(), "mybot")
}
func TestRelayDriverStart(t *testing.T) {
d, _ := initTestRelayDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestRelayDriverHalt(t *testing.T) {
d, _ := initTestRelayDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestRelayDriverToggle(t *testing.T) {
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})
d.Off()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
d.Toggle()
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(1))
d.Toggle()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
}
func TestRelayDriverToggleInverted(t *testing.T) {
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})
d.Inverted = true
d.Off()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
d.Toggle()
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(0))
d.Toggle()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
}
func TestRelayDriverCommands(t *testing.T) {
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})
gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
gobottest.Assert(t, d.Command("On")(nil), nil)
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(1))
gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
}
func TestRelayDriverCommandsInverted(t *testing.T) {
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})
d.Inverted = true
gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
gobottest.Assert(t, d.Command("On")(nil), nil)
gobottest.Assert(t, d.High(), false)
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(0))
gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
}