-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathjhd1313m1_driver_test.go
229 lines (184 loc) · 6.17 KB
/
jhd1313m1_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package i2c
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var _ gobot.Driver = (*JHD1313M1Driver)(nil)
// --------- HELPERS
func initTestJHD1313M1Driver() *JHD1313M1Driver {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
return d
}
func initTestJHD1313M1DriverWithStubbedAdaptor() (*JHD1313M1Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewJHD1313M1Driver(adaptor), adaptor
}
// --------- TESTS
func TestNewJHD1313M1Driver(t *testing.T) {
// Does it return a pointer to an instance of JHD1313M1Driver?
var mpl interface{} = NewJHD1313M1Driver(newI2cTestAdaptor())
_, ok := mpl.(*JHD1313M1Driver)
if !ok {
require.Fail(t, "NewJHD1313M1Driver() should have returned a *JHD1313M1Driver")
}
}
// Methods
func TestJHD1313M1Driver(t *testing.T) {
jhd := initTestJHD1313M1Driver()
assert.NotNil(t, jhd.Connection())
assert.True(t, strings.HasPrefix(jhd.Name(), "JHD1313M1"))
}
func TestJHD1313MDriverSetName(t *testing.T) {
d := initTestJHD1313M1Driver()
d.SetName("TESTME")
assert.Equal(t, "TESTME", d.Name())
}
func TestJHD1313MDriverOptions(t *testing.T) {
d := NewJHD1313M1Driver(newI2cTestAdaptor(), WithBus(2))
assert.Equal(t, 2, d.GetBusOrDefault(1))
}
func TestJHD1313MDriverStart(t *testing.T) {
d := initTestJHD1313M1Driver()
require.NoError(t, d.Start())
}
func TestJHD1313MStartConnectError(t *testing.T) {
d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
require.ErrorContains(t, d.Start(), "Invalid i2c connection")
}
func TestJHD1313MDriverStartWriteError(t *testing.T) {
d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
require.ErrorContains(t, d.Start(), "write error")
}
func TestJHD1313MDriverHalt(t *testing.T) {
d := initTestJHD1313M1Driver()
_ = d.Start()
require.NoError(t, d.Halt())
}
func TestJHD1313MDriverSetRgb(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.SetRGB(0x00, 0x00, 0x00))
}
func TestJHD1313MDriverSetRgbError(t *testing.T) {
d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
require.ErrorContains(t, d.SetRGB(0x00, 0x00, 0x00), "write error")
}
func TestJHD1313MDriverClear(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Clear())
}
func TestJHD1313MDriverClearError(t *testing.T) {
d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
require.ErrorContains(t, d.Clear(), "write error")
}
func TestJHD1313MDriverHome(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Home())
}
func TestJHD1313MDriverWrite(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("Hello"))
}
func TestJHD1313MDriverWriteError(t *testing.T) {
d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
require.ErrorContains(t, d.Write("Hello"), "write error")
}
func TestJHD1313MDriverWriteTwoLines(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("Hello\nthere"))
}
func TestJHD1313MDriverWriteTwoLinesError(t *testing.T) {
d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
require.ErrorContains(t, d.Write("Hello\nthere"), "write error")
}
func TestJHD1313MDriverSetPosition(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.SetPosition(2))
}
func TestJHD1313MDriverSetSecondLinePosition(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.SetPosition(18))
}
func TestJHD1313MDriverSetPositionInvalid(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
assert.Equal(t, jhd1313m1ErrInvalidPosition, d.SetPosition(-1))
assert.Equal(t, jhd1313m1ErrInvalidPosition, d.SetPosition(32))
}
func TestJHD1313MDriverScroll(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Scroll(true))
}
func TestJHD1313MDriverReverseScroll(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Scroll(false))
}
func TestJHD1313MDriverSetCustomChar(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
_ = d.Start()
require.NoError(t, d.SetCustomChar(0, data))
}
func TestJHD1313MDriverSetCustomCharError(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
_ = d.Start()
require.ErrorContains(t, d.SetCustomChar(10, data), "can't set a custom character at a position greater than 7")
}
func TestJHD1313MDriverSetCustomCharWriteError(t *testing.T) {
d, a := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
data := [8]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
require.ErrorContains(t, d.SetCustomChar(0, data), "write error")
}
func TestJHD1313MDriverCommands(t *testing.T) {
d, _ := initTestJHD1313M1DriverWithStubbedAdaptor()
_ = d.Start()
err := d.Command("SetRGB")(map[string]interface{}{"r": "1", "g": "1", "b": "1"})
assert.Nil(t, err)
err = d.Command("Clear")(map[string]interface{}{})
assert.Nil(t, err)
err = d.Command("Home")(map[string]interface{}{})
assert.Nil(t, err)
err = d.Command("Write")(map[string]interface{}{"msg": "Hello"})
assert.Nil(t, err)
err = d.Command("SetPosition")(map[string]interface{}{"pos": "1"})
assert.Nil(t, err)
err = d.Command("Scroll")(map[string]interface{}{"lr": "true"})
assert.Nil(t, err)
}