@@ -2,7 +2,11 @@ package mbserver
2
2
3
3
import (
4
4
"encoding/json"
5
+ "errors"
5
6
"testing"
7
+
8
+ "github.com/stretchr/testify/assert"
9
+ "github.com/stretchr/testify/require"
6
10
)
7
11
8
12
func isEqual (a interface {}, b interface {}) bool {
@@ -43,9 +47,8 @@ func TestReadCoils(t *testing.T) {
43
47
// 2 bytes, 0b1000011, 0b00000001
44
48
expect := []byte {2 , 131 , 1 }
45
49
got := response .GetData ()
46
- if ! isEqual (expect , got ) {
47
- t .Errorf ("expected %v, got %v" , expect , got )
48
- }
50
+
51
+ assert .Equal (t , expect , got )
49
52
}
50
53
51
54
// Function 2
@@ -76,9 +79,8 @@ func TestReadDiscreteInputs(t *testing.T) {
76
79
}
77
80
expect := []byte {2 , 129 , 3 }
78
81
got := response .GetData ()
79
- if ! isEqual (expect , got ) {
80
- t .Errorf ("expected %v, got %v" , expect , got )
81
- }
82
+
83
+ assert .Equal (t , expect , got )
82
84
}
83
85
84
86
// Function 3
@@ -106,9 +108,7 @@ func TestReadHoldingRegisters(t *testing.T) {
106
108
}
107
109
expect := []byte {6 , 0 , 1 , 0 , 2 , 255 , 255 }
108
110
got := response .GetData ()
109
- if ! isEqual (expect , got ) {
110
- t .Errorf ("expected %v, got %v" , expect , got )
111
- }
111
+ assert .Equal (t , expect , got )
112
112
}
113
113
114
114
// Function 4
@@ -136,9 +136,7 @@ func TestReadInputRegisters(t *testing.T) {
136
136
}
137
137
expect := []byte {6 , 0 , 1 , 0 , 2 , 255 , 255 }
138
138
got := response .GetData ()
139
- if ! isEqual (expect , got ) {
140
- t .Errorf ("expected %v, got %v" , expect , got )
141
- }
139
+ assert .Equal (t , expect , got )
142
140
}
143
141
144
142
// Function 5
@@ -157,15 +155,11 @@ func TestWriteSingleCoil(t *testing.T) {
157
155
req .frame = & frame
158
156
response := s .handle (& req )
159
157
exception := GetException (response )
160
- if exception != Success {
161
- t .Errorf ("expected Success, got %v" , exception .String ())
162
- t .FailNow ()
163
- }
164
- expect := 1
158
+ require .Equalf (t , exception , Success , "expected Success, got %v" , exception .String ())
159
+
160
+ expect := uint8 (1 )
165
161
got := s .Coils [65535 ]
166
- if ! isEqual (expect , got ) {
167
- t .Errorf ("expected %v, got %v\n " , expect , got )
168
- }
162
+ assert .Equal (t , expect , got )
169
163
}
170
164
171
165
// Function 6
@@ -184,15 +178,11 @@ func TestWriteHoldingRegister(t *testing.T) {
184
178
req .frame = & frame
185
179
response := s .handle (& req )
186
180
exception := GetException (response )
187
- if exception != Success {
188
- t .Errorf ("expected Success, got %v" , exception .String ())
189
- t .FailNow ()
190
- }
191
- expect := 6
181
+ require .Equalf (t , exception , Success , "expected Success, got %v" , exception .String ())
182
+
183
+ expect := uint16 (6 )
192
184
got := s .HoldingRegisters [5 ]
193
- if ! isEqual (expect , got ) {
194
- t .Errorf ("expected %v, got %v\n " , expect , got )
195
- }
185
+ assert .Equal (t , expect , got )
196
186
}
197
187
198
188
// Function 15
@@ -217,9 +207,7 @@ func TestWriteMultipleCoils(t *testing.T) {
217
207
}
218
208
expect := []byte {1 , 1 }
219
209
got := s .Coils [1 :3 ]
220
- if ! isEqual (expect , got ) {
221
- t .Errorf ("expected %v, got %v\n " , expect , got )
222
- }
210
+ assert .Equal (t , expect , got )
223
211
}
224
212
225
213
// Function 16
@@ -244,27 +232,21 @@ func TestWriteHoldingRegisters(t *testing.T) {
244
232
}
245
233
expect := []uint16 {3 , 4 }
246
234
got := s .HoldingRegisters [1 :3 ]
247
- if ! isEqual (expect , got ) {
248
- t .Errorf ("expected %v, got %v\n " , expect , got )
249
- }
235
+ assert .Equal (t , expect , got )
250
236
}
251
237
252
238
func TestBytesToUint16 (t * testing.T ) {
253
239
bytes := []byte {1 , 2 , 3 , 4 }
254
240
got := BytesToUint16 (bytes )
255
241
expect := []uint16 {258 , 772 }
256
- if ! isEqual (expect , got ) {
257
- t .Errorf ("expected %v, got %v\n " , expect , got )
258
- }
242
+ assert .Equal (t , expect , got )
259
243
}
260
244
261
245
func TestUint16ToBytes (t * testing.T ) {
262
246
values := []uint16 {1 , 2 , 3 }
263
247
got := Uint16ToBytes (values )
264
248
expect := []byte {0 , 1 , 0 , 2 , 0 , 3 }
265
- if ! isEqual (expect , got ) {
266
- t .Errorf ("expected %v, got %v\n " , expect , got )
267
- }
249
+ assert .Equal (t , expect , got )
268
250
}
269
251
270
252
func TestOutOfBounds (t * testing.T ) {
@@ -285,22 +267,22 @@ func TestOutOfBounds(t *testing.T) {
285
267
frame .Function = 1
286
268
response := s .handle (& req )
287
269
exception := GetException (response )
288
- if exception != IllegalDataAddress {
270
+ if ! errors . Is ( exception , IllegalDataAddress ) {
289
271
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
290
272
}
291
273
292
274
frame .Function = 2
293
275
response = s .handle (& req )
294
276
exception = GetException (response )
295
- if exception != IllegalDataAddress {
277
+ if ! errors . Is ( exception , IllegalDataAddress ) {
296
278
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
297
279
}
298
280
299
281
SetDataWithRegisterAndNumberAndBytes (& frame , 65535 , 2 , []byte {3 })
300
282
frame .Function = 15
301
283
response = s .handle (& req )
302
284
exception = GetException (response )
303
- if exception != IllegalDataAddress {
285
+ if ! errors . Is ( exception , IllegalDataAddress ) {
304
286
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
305
287
}
306
288
@@ -310,22 +292,22 @@ func TestOutOfBounds(t *testing.T) {
310
292
frame .Function = 3
311
293
response = s .handle (& req )
312
294
exception = GetException (response )
313
- if exception != IllegalDataAddress {
295
+ if ! errors . Is ( exception , IllegalDataAddress ) {
314
296
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
315
297
}
316
298
317
299
frame .Function = 4
318
300
response = s .handle (& req )
319
301
exception = GetException (response )
320
- if exception != IllegalDataAddress {
302
+ if ! errors . Is ( exception , IllegalDataAddress ) {
321
303
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
322
304
}
323
305
324
306
SetDataWithRegisterAndNumberAndValues (& frame , 65535 , 2 , []uint16 {0 , 0 })
325
307
frame .Function = 16
326
308
response = s .handle (& req )
327
309
exception = GetException (response )
328
- if exception != IllegalDataAddress {
310
+ if ! errors . Is ( exception , IllegalDataAddress ) {
329
311
t .Errorf ("expected IllegalDataAddress, got %v" , exception .String ())
330
312
}
331
313
}
0 commit comments