-
Notifications
You must be signed in to change notification settings - Fork 156
/
gpiodriver_test.go
187 lines (161 loc) · 4.07 KB
/
gpiodriver_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
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
180
181
182
183
184
185
186
187
package embd
import (
"testing"
"time"
)
type fakeDigitalPin struct {
id string
n int
drv GPIODriver
}
func (p *fakeDigitalPin) N() int {
return p.n
}
func (*fakeDigitalPin) SetDirection(dir Direction) error {
return nil
}
func (*fakeDigitalPin) Read() (int, error) {
return 0, nil
}
func (*fakeDigitalPin) Write(val int) error {
return nil
}
func (*fakeDigitalPin) TimePulse(state int) (time.Duration, error) {
return 0, nil
}
func (*fakeDigitalPin) ActiveLow(b bool) error {
return nil
}
func (*fakeDigitalPin) PullUp() error {
return nil
}
func (*fakeDigitalPin) PullDown() error {
return nil
}
func (p *fakeDigitalPin) Close() error {
return p.drv.Unregister(p.id)
}
func (p *fakeDigitalPin) Watch(edge Edge, handler func(DigitalPin)) error {
return nil
}
func (p *fakeDigitalPin) StopWatching() error {
return nil
}
func newFakeDigitalPin(pd *PinDesc, drv GPIODriver) DigitalPin {
return &fakeDigitalPin{id: pd.ID, n: pd.DigitalLogical, drv: drv}
}
func TestGpioDriverDigitalPin(t *testing.T) {
tests := []struct {
key interface{}
n int
}{
{1, 1},
}
pinMap := PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
}
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
for _, test := range tests {
pin, err := driver.DigitalPin(test.key)
if err != nil {
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
continue
}
if pin.N() != test.n {
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.N(), test.n)
}
}
}
type fakeAnalogPin struct {
id string
n int
drv GPIODriver
}
func (p *fakeAnalogPin) N() int {
return p.n
}
func (*fakeAnalogPin) Read() (int, error) {
return 0, nil
}
func (*fakeAnalogPin) Write(val int) error {
return nil
}
func (*fakeAnalogPin) Close() error {
return nil
}
func newFakeAnalogPin(pd *PinDesc, drv GPIODriver) AnalogPin {
return &fakeAnalogPin{id: pd.ID, n: pd.AnalogLogical, drv: drv}
}
func TestGpioDriverAnalogPin(t *testing.T) {
tests := []struct {
key interface{}
n int
}{
{1, 1},
}
pinMap := PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
}
driver := NewGPIODriver(pinMap, nil, newFakeAnalogPin, nil)
for _, test := range tests {
pin, err := driver.AnalogPin(test.key)
if err != nil {
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
continue
}
if pin.N() != test.n {
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.N(), test.n)
}
}
}
func TestGpioDriverUnavailablePinType(t *testing.T) {
pinMap := PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
&PinDesc{ID: "P1_2", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
}
driver := NewGPIODriver(pinMap, nil, nil, nil)
_, err := driver.DigitalPin(1)
if err == nil {
t.Fatal("Looking up digital pin 1: did not get error")
}
expected := "gpio: digital io not supported on this host"
if err.Error() != expected {
t.Fatalf("Looking up digital pin 1: got error %q, expected %q", err, expected)
}
_, err = driver.AnalogPin(1)
if err == nil {
t.Fatal("Looking up analog pin 1: did not get error")
}
expected = "gpio: analog io not supported on this host"
if err.Error() != expected {
t.Fatalf("Looking up analog pin 1: got error %q, expected %q", err, expected)
}
}
func TestGpioPinCaching(t *testing.T) {
pinMap := PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital},
}
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
pin, err := driver.DigitalPin(1)
if err != nil {
t.Fatalf("Looking up digital pin 1: got %v", err)
}
// Lookup the same pin again
pin2, err := driver.DigitalPin(1)
if err != nil {
t.Fatalf("Looking up digital pin 1: got %v", err)
}
if pin != pin2 {
t.Fatalf("Looking up digital pin 1 for the second time: got %v, want %v", &pin2, &pin)
}
// Looking up a closed pin
pin.Close()
pin3, err := driver.DigitalPin(1)
if err != nil {
t.Fatalf("Looking up digital pin 1: got %v", err)
return
}
if pin == pin3 {
t.Fatal("Looking up a closed pin, but got the same old instance")
}
}