-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
direct_pin_driver_test.go
187 lines (157 loc) · 5.13 KB
/
direct_pin_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
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
//nolint:forcetypeassert // ok here
package gpio
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
)
var _ gobot.Driver = (*DirectPinDriver)(nil)
func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.digitalReadFunc = func(string) (int, error) {
return 1, nil
}
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
a.pwmWriteFunc = func(string, byte) error {
return errors.New("write error")
}
a.servoWriteFunc = func(string, byte) error {
return errors.New("write error")
}
return NewDirectPinDriver(a, "1")
}
func TestNewDirectPinDriver(t *testing.T) {
// arrange
a := newGpioTestAdaptor()
// act
d := NewDirectPinDriver(a, "10")
// assert
assert.IsType(t, &DirectPinDriver{}, d)
// assert: gpio.driver attributes
require.NotNil(t, d.driver)
assert.True(t, strings.HasPrefix(d.driverCfg.name, "DirectPin"))
assert.Equal(t, "10", d.driverCfg.pin)
assert.Equal(t, a, d.connection)
assert.NotNil(t, d.afterStart)
assert.NotNil(t, d.beforeHalt)
assert.NotNil(t, d.Commander)
assert.NotNil(t, d.mutex)
}
func TestNewDirectPinDriver_options(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithName() option, least one
// option of this driver and one of another driver (which should lead to panic). Further tests for options can also
// be done by call of "WithOption(val).apply(cfg)".
// arrange
const (
myName = "count up"
)
panicFunc := func() {
NewDirectPinDriver(newGpioTestAdaptor(), "1", WithName("crazy"),
aio.WithActuatorScaler(func(float64) int { return 0 }))
}
// act
d := NewDirectPinDriver(newGpioTestAdaptor(), "1", WithName(myName))
// assert
assert.Equal(t, myName, d.Name())
assert.PanicsWithValue(t, "'scaler option for analog actuators' can not be applied on 'crazy'", panicFunc)
}
func TestDirectPin_Commands(t *testing.T) {
var ret map[string]interface{}
var err interface{}
d := initTestDirectPinDriver()
ret = d.Command("DigitalRead")(nil).(map[string]interface{})
assert.Equal(t, 1, ret["val"].(int))
assert.Nil(t, ret["err"])
err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
require.EqualError(t, err.(error), "write error")
err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
require.EqualError(t, err.(error), "write error")
err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
require.EqualError(t, err.(error), "write error")
}
func TestDirectPinOff(t *testing.T) {
d := initTestDirectPinDriver()
require.Error(t, d.Off())
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
require.NoError(t, d.Off())
}
func TestDirectPinOffNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
require.EqualError(t, d.Off(), "DigitalWrite is not supported by this platform")
}
func TestDirectPinOn(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
require.NoError(t, d.On())
}
func TestDirectPinOnError(t *testing.T) {
d := initTestDirectPinDriver()
require.Error(t, d.On())
}
func TestDirectPinOnNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
require.EqualError(t, d.On(), "DigitalWrite is not supported by this platform")
}
func TestDirectPinDigitalWrite(t *testing.T) {
adaptor := newGpioTestAdaptor()
d := NewDirectPinDriver(adaptor, "1")
require.NoError(t, d.DigitalWrite(1))
}
func TestDirectPinDigitalWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
require.EqualError(t, d.DigitalWrite(1), "DigitalWrite is not supported by this platform")
}
func TestDirectPinDigitalWriteError(t *testing.T) {
d := initTestDirectPinDriver()
require.Error(t, d.DigitalWrite(1))
}
func TestDirectPinDigitalRead(t *testing.T) {
d := initTestDirectPinDriver()
ret, err := d.DigitalRead()
assert.Equal(t, 1, ret)
require.NoError(t, err)
}
func TestDirectPinDigitalReadNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
_, e := d.DigitalRead()
require.EqualError(t, e, "DigitalRead is not supported by this platform")
}
func TestDirectPinPwmWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
require.NoError(t, d.PwmWrite(1))
}
func TestDirectPinPwmWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
require.EqualError(t, d.PwmWrite(1), "PwmWrite is not supported by this platform")
}
func TestDirectPinPwmWriteError(t *testing.T) {
d := initTestDirectPinDriver()
require.Error(t, d.PwmWrite(1))
}
func TestDirectPinServoWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
require.NoError(t, d.ServoWrite(1))
}
func TestDirectPinServoWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
require.EqualError(t, d.ServoWrite(1), "ServoWrite is not supported by this platform")
}
func TestDirectPinServoWriteError(t *testing.T) {
d := initTestDirectPinDriver()
require.Error(t, d.ServoWrite(1))
}