-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
dragonboard_adaptor_test.go
136 lines (119 loc) · 3.13 KB
/
dragonboard_adaptor_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
package dragonboard
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/drivers/i2c"
)
// make sure that this Adaptor fulfills all the required interfaces
var (
_ gobot.Adaptor = (*Adaptor)(nil)
_ gobot.DigitalPinnerProvider = (*Adaptor)(nil)
_ gpio.DigitalReader = (*Adaptor)(nil)
_ gpio.DigitalWriter = (*Adaptor)(nil)
_ i2c.Connector = (*Adaptor)(nil)
)
func initTestAdaptor() *Adaptor {
a := NewAdaptor()
if err := a.Connect(); err != nil {
panic(err)
}
return a
}
func TestName(t *testing.T) {
a := initTestAdaptor()
assert.True(t, strings.HasPrefix(a.Name(), "DragonBoard"))
a.SetName("NewName")
assert.Equal(t, "NewName", a.Name())
}
func TestDigitalIO(t *testing.T) {
a := initTestAdaptor()
mockPaths := []string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio36/value",
"/sys/class/gpio/gpio36/direction",
"/sys/class/gpio/gpio12/value",
"/sys/class/gpio/gpio12/direction",
}
fs := a.sys.UseMockFilesystem(mockPaths)
_ = a.DigitalWrite("GPIO_B", 1)
assert.Equal(t, "1", fs.Files["/sys/class/gpio/gpio12/value"].Contents)
fs.Files["/sys/class/gpio/gpio36/value"].Contents = "1"
i, _ := a.DigitalRead("GPIO_A")
assert.Equal(t, 1, i)
require.ErrorContains(t, a.DigitalWrite("GPIO_M", 1), "'GPIO_M' is not a valid id for a digital pin")
require.NoError(t, a.Finalize())
}
func TestFinalizeErrorAfterGPIO(t *testing.T) {
a := initTestAdaptor()
mockPaths := []string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio36/value",
"/sys/class/gpio/gpio36/direction",
"/sys/class/gpio/gpio12/value",
"/sys/class/gpio/gpio12/direction",
}
fs := a.sys.UseMockFilesystem(mockPaths)
require.NoError(t, a.Connect())
require.NoError(t, a.DigitalWrite("GPIO_B", 1))
fs.WithWriteError = true
err := a.Finalize()
require.ErrorContains(t, err, "write error")
}
func TestI2cDefaultBus(t *testing.T) {
a := initTestAdaptor()
assert.Equal(t, 0, a.DefaultI2cBus())
}
func TestI2cFinalizeWithErrors(t *testing.T) {
// arrange
a := NewAdaptor()
a.sys.UseMockSyscall()
fs := a.sys.UseMockFilesystem([]string{"/dev/i2c-1"})
require.NoError(t, a.Connect())
con, err := a.GetI2cConnection(0xff, 1)
require.NoError(t, err)
_, err = con.Write([]byte{0xbf})
require.NoError(t, err)
fs.WithCloseError = true
// act
err = a.Finalize()
// assert
require.ErrorContains(t, err, "close error")
}
func Test_validateI2cBusNumber(t *testing.T) {
tests := map[string]struct {
busNr int
wantErr error
}{
"number_negative_error": {
busNr: -1,
wantErr: fmt.Errorf("Bus number -1 out of range"),
},
"number_0_ok": {
busNr: 0,
},
"number_1_ok": {
busNr: 1,
},
"number_2_error": {
busNr: 2,
wantErr: fmt.Errorf("Bus number 2 out of range"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a := NewAdaptor()
// act
err := a.validateI2cBusNumber(tc.busNr)
// assert
assert.Equal(t, tc.wantErr, err)
})
}
}