forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitalpin_access_test.go
102 lines (94 loc) · 2.36 KB
/
digitalpin_access_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
package system
import (
"testing"
"gobot.io/x/gobot/v2/gobottest"
)
func Test_isSupportedSysfs(t *testing.T) {
// arrange
dpa := sysfsDigitalPinAccess{}
// act
got := dpa.isSupported()
// assert
gobottest.Assert(t, got, true)
}
func Test_isSupportedGpiod(t *testing.T) {
var tests = map[string]struct {
mockPaths []string
want bool
}{
"supported": {
mockPaths: []string{"/sys/class/gpio/", "/dev/gpiochip3"},
want: true,
},
"not_supported": {
mockPaths: []string{"/sys/class/gpio/"},
want: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
fs := newMockFilesystem(tc.mockPaths)
dpa := gpiodDigitalPinAccess{fs: fs}
// act
got := dpa.isSupported()
// assert
gobottest.Assert(t, got, tc.want)
})
}
}
func Test_createAsSysfs(t *testing.T) {
// arrange
dpa := sysfsDigitalPinAccess{}
// act
dp := dpa.createPin("chip", 8)
// assert
gobottest.Refute(t, dp, nil)
dps := dp.(*digitalPinSysfs)
// chip is dropped
gobottest.Assert(t, dps.label, "gpio8")
}
func Test_createAsGpiod(t *testing.T) {
// arrange
const (
pin = 18
label = "gobotio18"
chip = "gpiochip1"
)
dpa := gpiodDigitalPinAccess{}
// act
dp := dpa.createPin(chip, 18)
// assert
gobottest.Refute(t, dp, nil)
dpg := dp.(*digitalPinGpiod)
gobottest.Assert(t, dpg.label, label)
gobottest.Assert(t, dpg.chipName, chip)
}
func Test_createPinWithOptionsSysfs(t *testing.T) {
// This is a general test, that options are applied by using "create" with the WithPinLabel() option.
// All other configuration options will be tested in tests for "digitalPinConfig".
//
// arrange
const label = "my sysfs label"
dpa := sysfsDigitalPinAccess{}
// act
dp := dpa.createPin("", 9, WithPinLabel(label))
dps := dp.(*digitalPinSysfs)
// assert
gobottest.Assert(t, dps.label, label)
}
func Test_createPinWithOptionsGpiod(t *testing.T) {
// This is a general test, that options are applied by using "create" with the WithPinLabel() option.
// All other configuration options will be tested in tests for "digitalPinConfig".
//
// arrange
const label = "my gpiod label"
dpa := gpiodDigitalPinAccess{}
// act
dp := dpa.createPin("", 19, WithPinLabel(label))
dpg := dp.(*digitalPinGpiod)
// assert
gobottest.Assert(t, dpg.label, label)
// test fallback for empty chip
gobottest.Assert(t, dpg.chipName, "gpiochip0")
}