forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digispark_adaptor_test.go
143 lines (120 loc) · 4.18 KB
/
digispark_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
137
138
139
140
141
142
143
package digispark
import (
"errors"
"strings"
"testing"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
var _ gpio.DigitalWriter = (*Adaptor)(nil)
var _ gpio.PwmWriter = (*Adaptor)(nil)
var _ gpio.ServoWriter = (*Adaptor)(nil)
type mock struct {
locationA uint8
locationB uint8
pwmChannelA uint8
pwmChannelB uint8
pwmPrescalerValue uint
pin uint8
mode uint8
state uint8
}
// setup mock for GPIO, PWM and servo tests
func (l *mock) digitalWrite(pin uint8, state uint8) error {
l.pin = pin
l.state = state
return l.error()
}
func (l *mock) pinMode(pin uint8, mode uint8) error {
l.pin = pin
l.mode = mode
return l.error()
}
var pwmInitErrorFunc = func() error { return nil }
func (l *mock) pwmInit() error { return pwmInitErrorFunc() }
func (l *mock) pwmStop() error { return l.error() }
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error {
l.pwmChannelA = channelA
l.pwmChannelB = channelB
return l.error()
}
func (l *mock) pwmUpdatePrescaler(value uint) error {
l.pwmPrescalerValue = value
return l.error()
}
func (l *mock) servoInit() error { return l.error() }
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error {
l.locationA = locationA
l.locationB = locationB
return l.error()
}
var errorFunc = func() error { return nil }
func (l *mock) error() error { return errorFunc() }
// i2c functions unused in this test scenarios
func (l *mock) i2cInit() error { return nil }
func (l *mock) i2cStart(address7bit uint8, direction uint8) error { return nil }
func (l *mock) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error { return nil }
func (l *mock) i2cRead(readBuffer []byte, length int, endWithStop uint8) error { return nil }
func (l *mock) i2cUpdateDelay(duration uint) error { return nil }
func initTestAdaptor() *Adaptor {
a := NewAdaptor()
a.connect = func(a *Adaptor) (err error) { return nil }
a.littleWire = new(mock)
errorFunc = func() error { return nil }
pwmInitErrorFunc = func() error { return nil }
return a
}
func TestDigisparkAdaptorName(t *testing.T) {
a := NewAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Digispark"), true)
a.SetName("NewName")
gobottest.Assert(t, a.Name(), "NewName")
}
func TestDigisparkAdaptorConnect(t *testing.T) {
a := initTestAdaptor()
gobottest.Assert(t, a.Connect(), nil)
}
func TestDigisparkAdaptorFinalize(t *testing.T) {
a := initTestAdaptor()
gobottest.Assert(t, a.Finalize(), nil)
}
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
a := initTestAdaptor()
err := a.DigitalWrite("0", uint8(1))
gobottest.Assert(t, err, nil)
gobottest.Assert(t, a.littleWire.(*mock).pin, uint8(0))
gobottest.Assert(t, a.littleWire.(*mock).state, uint8(1))
err = a.DigitalWrite("?", uint8(1))
gobottest.Refute(t, err, nil)
errorFunc = func() error { return errors.New("pin mode error") }
err = a.DigitalWrite("0", uint8(1))
gobottest.Assert(t, err, errors.New("pin mode error"))
}
func TestDigisparkAdaptorServoWrite(t *testing.T) {
a := initTestAdaptor()
err := a.ServoWrite("2", uint8(80))
gobottest.Assert(t, err, nil)
gobottest.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
gobottest.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
a = initTestAdaptor()
errorFunc = func() error { return errors.New("servo error") }
err = a.ServoWrite("2", uint8(80))
gobottest.Assert(t, err, errors.New("servo error"))
}
func TestDigisparkAdaptorPwmWrite(t *testing.T) {
a := initTestAdaptor()
err := a.PwmWrite("1", uint8(100))
gobottest.Assert(t, err, nil)
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
gobottest.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
a = initTestAdaptor()
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
err = a.PwmWrite("1", uint8(100))
gobottest.Assert(t, err, errors.New("pwminit error"))
a = initTestAdaptor()
errorFunc = func() error { return errors.New("pwm error") }
err = a.PwmWrite("1", uint8(100))
gobottest.Assert(t, err, errors.New("pwm error"))
}