forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct_pin_driver_test.go
171 lines (139 loc) · 4.68 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
package gpio
import (
"errors"
"strings"
"testing"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Driver = (*DirectPinDriver)(nil)
func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalRead = func(string) (val int, err error) {
val = 1
return
}
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return errors.New("write error")
}
a.testAdaptorServoWrite = func(string, byte) (err error) {
return errors.New("write error")
}
return NewDirectPinDriver(a, "1")
}
func TestDirectPinDriver(t *testing.T) {
var ret map[string]interface{}
var err interface{}
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
ret = d.Command("DigitalRead")(nil).(map[string]interface{})
gobottest.Assert(t, ret["val"].(int), 1)
gobottest.Assert(t, ret["err"], nil)
err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
}
func TestDirectPinDriverStart(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestDirectPinDriverHalt(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestDirectPinDriverOff(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.Off(), nil)
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.Off(), nil)
}
func TestDirectPinDriverOffNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.Off(), errors.New("DigitalWrite is not supported by this platform"))
}
func TestDirectPinDriverOn(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.On(), nil)
}
func TestDirectPinDriverOnError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.On(), nil)
}
func TestDirectPinDriverOnNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.On(), errors.New("DigitalWrite is not supported by this platform"))
}
func TestDirectPinDriverDigitalWrite(t *testing.T) {
adaptor := newGpioTestAdaptor()
d := NewDirectPinDriver(adaptor, "1")
gobottest.Assert(t, d.DigitalWrite(1), nil)
}
func TestDirectPinDriverDigitalWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.DigitalWrite(1), errors.New("DigitalWrite is not supported by this platform"))
}
func TestDirectPinDriverDigitalWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.DigitalWrite(1), nil)
}
func TestDirectPinDriverDigitalRead(t *testing.T) {
d := initTestDirectPinDriver()
ret, err := d.DigitalRead()
gobottest.Assert(t, ret, 1)
gobottest.Assert(t, err, nil)
}
func TestDirectPinDriverDigitalReadNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
_, e := d.DigitalRead()
gobottest.Assert(t, e, errors.New("DigitalRead is not supported by this platform"))
}
func TestDirectPinDriverPwmWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.PwmWrite(1), nil)
}
func TestDirectPinDriverPwmWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.PwmWrite(1), errors.New("PwmWrite is not supported by this platform"))
}
func TestDirectPinDriverPwmWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.PwmWrite(1), nil)
}
func TestDirectPinDriverServoWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.ServoWrite(1), nil)
}
func TestDirectPinDriverServoWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.ServoWrite(1), errors.New("ServoWrite is not supported by this platform"))
}
func TestDirectPinDriverServoWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.ServoWrite(1), nil)
}
func TestDirectPinDriverDefaultName(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Direct"), true)
}
func TestDirectPinDriverSetName(t *testing.T) {
d := initTestDirectPinDriver()
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}