forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidarlite_driver_test.go
167 lines (130 loc) · 4.09 KB
/
lidarlite_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
package i2c
import (
"bytes"
"errors"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*LIDARLiteDriver)(nil)
// --------- HELPERS
func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
return
}
func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewLIDARLiteDriver(adaptor), adaptor
}
// --------- TESTS
func TestNewLIDARLiteDriver(t *testing.T) {
// Does it return a pointer to an instance of LIDARLiteDriver?
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
_, ok := bm.(*LIDARLiteDriver)
if !ok {
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
}
b := NewLIDARLiteDriver(newI2cTestAdaptor())
gobottest.Refute(t, b.Connection(), nil)
}
// Methods
func TestLIDARLiteDriverStart(t *testing.T) {
hmc, _ := initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
}
func TestLIDARLiteStartConnectError(t *testing.T) {
d, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestLIDARLiteDriverHalt(t *testing.T) {
hmc := initTestLIDARLiteDriver()
gobottest.Assert(t, hmc.Halt(), nil)
}
func TestLIDARLiteDriverDistance(t *testing.T) {
// when everything is happy
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
first := true
adaptor.i2cReadImpl = func(b []byte) (int, error) {
if first {
first = false
copy(b, []byte{99})
return 1, nil
}
copy(b, []byte{1})
return 1, nil
}
distance, err := hmc.Distance()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, distance, int(25345))
// when insufficient bytes have been read
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, nil
}
distance, err = hmc.Distance()
gobottest.Assert(t, distance, int(0))
gobottest.Assert(t, err, ErrNotEnoughBytes)
// when read error
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
distance, err = hmc.Distance()
gobottest.Assert(t, distance, int(0))
gobottest.Assert(t, err, errors.New("read error"))
}
func TestLIDARLiteDriverDistanceError1(t *testing.T) {
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
distance, err := hmc.Distance()
gobottest.Assert(t, distance, int(0))
gobottest.Assert(t, err, errors.New("write error"))
}
func TestLIDARLiteDriverDistanceError2(t *testing.T) {
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
if b[0] == 0x0f {
return 0, errors.New("write error")
}
return len(b), nil
}
distance, err := hmc.Distance()
gobottest.Assert(t, distance, int(0))
gobottest.Assert(t, err, errors.New("write error"))
}
func TestLIDARLiteDriverDistanceError3(t *testing.T) {
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
if b[0] == 0x10 {
return 0, errors.New("write error")
}
return len(b), nil
}
adaptor.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
buf.Write([]byte{0x03})
copy(b, buf.Bytes())
return buf.Len(), nil
}
distance, err := hmc.Distance()
gobottest.Assert(t, distance, int(0))
gobottest.Assert(t, err, errors.New("write error"))
}
func TestLIDARLiteDriverSetName(t *testing.T) {
l := initTestLIDARLiteDriver()
l.SetName("TESTME")
gobottest.Assert(t, l.Name(), "TESTME")
}
func TestLIDARLiteDriverOptions(t *testing.T) {
l := NewLIDARLiteDriver(newI2cTestAdaptor(), WithBus(2))
gobottest.Assert(t, l.GetBusOrDefault(1), 2)
}