forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digitalpin_gpiod_test.go
315 lines (299 loc) · 7.64 KB
/
digitalpin_gpiod_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package system
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var (
_ gobot.DigitalPinner = (*digitalPinGpiod)(nil)
_ gobot.DigitalPinValuer = (*digitalPinGpiod)(nil)
_ gobot.DigitalPinOptioner = (*digitalPinGpiod)(nil)
_ gobot.DigitalPinOptionApplier = (*digitalPinGpiod)(nil)
)
func Test_newDigitalPinGpiod(t *testing.T) {
// arrange
const (
chip = "gpiochip0"
pin = 17
label = "gobotio17"
)
// act
d := newDigitalPinGpiod(chip, pin)
// assert
assert.NotNil(t, d)
assert.Equal(t, chip, d.chipName)
assert.Equal(t, pin, d.pin)
assert.Equal(t, label, d.label)
assert.Equal(t, IN, d.direction)
assert.Equal(t, 0, d.outInitialState)
}
func Test_newDigitalPinGpiodWithOptions(t *testing.T) {
// This is a general test, that options are applied by using "newDigitalPinGpiod" with the WithPinLabel() option.
// All other configuration options will be tested in tests for "digitalPinConfig".
//
// arrange
const label = "my own label"
// act
dp := newDigitalPinGpiod("", 9, WithPinLabel(label))
// assert
assert.Equal(t, label, dp.label)
}
func TestApplyOptions(t *testing.T) {
tests := map[string]struct {
changed []bool
simErr error
wantReconfigured int
wantErr error
}{
"both_changed": {
changed: []bool{true, true},
wantReconfigured: 1,
},
"first_changed": {
changed: []bool{true, false},
wantReconfigured: 1,
},
"second_changed": {
changed: []bool{false, true},
wantReconfigured: 1,
},
"none_changed": {
changed: []bool{false, false},
simErr: fmt.Errorf("error not raised"),
wantReconfigured: 0,
},
"error_on_change": {
changed: []bool{false, true},
simErr: fmt.Errorf("error raised"),
wantReconfigured: 1,
wantErr: fmt.Errorf("error raised"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// currently the gpiod.Chip has no interface for RequestLine(),
// so we can only test without trigger of real reconfigure
// arrange
orgReconf := digitalPinGpiodReconfigure
defer func() { digitalPinGpiodReconfigure = orgReconf }()
inputForced := true
reconfigured := 0
digitalPinGpiodReconfigure = func(d *digitalPinGpiod, forceInput bool) error {
inputForced = forceInput
reconfigured++
return tc.simErr
}
d := &digitalPinGpiod{digitalPinConfig: &digitalPinConfig{direction: "in"}}
optionFunction1 := func(gobot.DigitalPinOptioner) bool {
d.digitalPinConfig.direction = "test"
return tc.changed[0]
}
optionFunction2 := func(gobot.DigitalPinOptioner) bool {
d.digitalPinConfig.drive = 15
return tc.changed[1]
}
// act
err := d.ApplyOptions(optionFunction1, optionFunction2)
// assert
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, "test", d.digitalPinConfig.direction)
assert.Equal(t, 15, d.digitalPinConfig.drive)
assert.Equal(t, tc.wantReconfigured, reconfigured)
if reconfigured > 0 {
assert.False(t, inputForced)
}
})
}
}
func TestExportGpiod(t *testing.T) {
tests := map[string]struct {
simErr error
wantReconfigured int
wantErr error
}{
"no_err": {
wantReconfigured: 1,
},
"error": {
wantReconfigured: 1,
simErr: fmt.Errorf("reconfigure error"),
wantErr: fmt.Errorf("gpiod.Export(): reconfigure error"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// currently the gpiod.Chip has no interface for RequestLine(),
// so we can only test without trigger of real reconfigure
// arrange
orgReconf := digitalPinGpiodReconfigure
defer func() { digitalPinGpiodReconfigure = orgReconf }()
inputForced := true
reconfigured := 0
digitalPinGpiodReconfigure = func(d *digitalPinGpiod, forceInput bool) error {
inputForced = forceInput
reconfigured++
return tc.simErr
}
d := &digitalPinGpiod{}
// act
err := d.Export()
// assert
assert.Equal(t, tc.wantErr, err)
assert.False(t, inputForced)
assert.Equal(t, tc.wantReconfigured, reconfigured)
})
}
}
func TestUnexportGpiod(t *testing.T) {
tests := map[string]struct {
simNoLine bool
simReconfErr error
simCloseErr error
wantReconfigured int
wantErr error
}{
"no_line_no_err": {
simNoLine: true,
wantReconfigured: 0,
},
"no_line_with_err": {
simNoLine: true,
simReconfErr: fmt.Errorf("reconfigure error"),
wantReconfigured: 0,
},
"no_err": {
wantReconfigured: 1,
},
"error_reconfigure": {
wantReconfigured: 1,
simReconfErr: fmt.Errorf("reconfigure error"),
wantErr: fmt.Errorf("reconfigure error"),
},
"error_close": {
wantReconfigured: 1,
simCloseErr: fmt.Errorf("close error"),
wantErr: fmt.Errorf("gpiod.Unexport()-line.Close(): close error"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// currently the gpiod.Chip has no interface for RequestLine(),
// so we can only test without trigger of real reconfigure
// arrange
orgReconf := digitalPinGpiodReconfigure
defer func() { digitalPinGpiodReconfigure = orgReconf }()
inputForced := false
reconfigured := 0
digitalPinGpiodReconfigure = func(d *digitalPinGpiod, forceInput bool) error {
inputForced = forceInput
reconfigured++
return tc.simReconfErr
}
dp := newDigitalPinGpiod("", 4)
if !tc.simNoLine {
dp.line = &lineMock{simCloseErr: tc.simCloseErr}
}
// act
err := dp.Unexport()
// assert
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantReconfigured, reconfigured)
if reconfigured > 0 {
assert.True(t, inputForced)
}
})
}
}
func TestWriteGpiod(t *testing.T) {
tests := map[string]struct {
val int
simErr error
want int
wantErr []string
}{
"write_zero": {
val: 0,
want: 0,
},
"write_one": {
val: 1,
want: 1,
},
"write_minus_one": {
val: -1,
want: 0,
},
"write_two": {
val: 2,
want: 1,
},
"write_with_err": {
simErr: fmt.Errorf("a write err"),
wantErr: []string{"a write err", "gpiod.Write"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
dp := newDigitalPinGpiod("", 4)
lm := &lineMock{lastVal: 10, simSetErr: tc.simErr}
dp.line = lm
// act
err := dp.Write(tc.val)
// assert
if tc.wantErr != nil {
for _, want := range tc.wantErr {
require.ErrorContains(t, err, want)
}
} else {
require.NoError(t, err)
}
assert.Equal(t, tc.want, lm.lastVal)
})
}
}
func TestReadGpiod(t *testing.T) {
tests := map[string]struct {
simVal int
simErr error
wantErr []string
}{
"read_ok": {
simVal: 3,
},
"write_with_err": {
simErr: fmt.Errorf("a read err"),
wantErr: []string{"a read err", "gpiod.Read"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
dp := newDigitalPinGpiod("", 4)
lm := &lineMock{lastVal: tc.simVal, simValueErr: tc.simErr}
dp.line = lm
// act
got, err := dp.Read()
// assert
if tc.wantErr != nil {
for _, want := range tc.wantErr {
require.ErrorContains(t, err, want)
}
} else {
require.NoError(t, err)
}
assert.Equal(t, got, tc.simVal)
})
}
}
type lineMock struct {
lastVal int
simSetErr error
simValueErr error
simCloseErr error
}
func (lm *lineMock) SetValue(value int) error { lm.lastVal = value; return lm.simSetErr }
func (lm *lineMock) Value() (int, error) { return lm.lastVal, lm.simValueErr }
func (lm *lineMock) Close() error { return lm.simCloseErr }