-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
adaptor_test.go
155 lines (142 loc) · 3.4 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
package serialport
import (
"errors"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
func initTestAdaptor() (*Adaptor, *nullReadWriteCloser) {
a := NewAdaptor("/dev/null")
rwc := newNullReadWriteCloser()
a.connectFunc = func(string, int) (io.ReadWriteCloser, error) {
return rwc, nil
}
if err := a.Connect(); err != nil {
panic(err)
}
return a, rwc
}
func TestNewAdaptor(t *testing.T) {
// arrange
a := NewAdaptor("/dev/null")
assert.Equal(t, "/dev/null", a.Port())
require.NotNil(t, a.cfg)
assert.Equal(t, 115200, a.cfg.baudRate)
assert.True(t, strings.HasPrefix(a.Name(), "Serial"))
}
func TestSerialRead(t *testing.T) {
tests := map[string]struct {
readDataBuffer []byte
simReadErr bool
wantCount int
wantErr string
}{
"read_ok": {
readDataBuffer: []byte{0, 0},
wantCount: 2,
},
"error_read": {
readDataBuffer: []byte{},
simReadErr: true,
wantErr: "read error",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a, rwc := initTestAdaptor()
rwc.simulateReadErr = tc.simReadErr
// act
gotCount, err := a.SerialRead(tc.readDataBuffer)
// assert
if tc.wantErr == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.wantErr)
}
assert.Equal(t, tc.wantCount, gotCount)
})
}
}
func TestSerialWrite(t *testing.T) {
tests := map[string]struct {
writeDataBuffer []byte
simWriteErr bool
wantCount int
wantWritten []byte
wantErr string
}{
"write_ok": {
writeDataBuffer: []byte{1, 3, 6},
wantWritten: []byte{1, 3, 6},
wantCount: 3,
},
"error_write": {
writeDataBuffer: []byte{},
simWriteErr: true,
wantErr: "write error",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
a, rwc := initTestAdaptor()
rwc.simulateWriteErr = tc.simWriteErr
// act
gotCount, err := a.SerialWrite(tc.writeDataBuffer)
// assert
if tc.wantErr == "" {
require.NoError(t, err)
assert.Equal(t, tc.wantWritten, rwc.written)
} else {
require.EqualError(t, err, tc.wantErr)
}
assert.Equal(t, tc.wantCount, gotCount)
})
}
}
func TestConnect(t *testing.T) {
// arrange
a, _ := initTestAdaptor()
require.True(t, a.IsConnected())
// act & assert
require.EqualError(t, a.Connect(), "serial port is already connected, try reconnect or run disconnect first")
// re-arrange error
a.sp = nil
a.connectFunc = func(string, int) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error")
}
// act & assert
require.ErrorContains(t, a.Connect(), "connect error")
assert.False(t, a.IsConnected())
}
func TestReconnect(t *testing.T) {
// arrange
a, _ := initTestAdaptor()
require.NotNil(t, a.sp)
// act & assert
require.NoError(t, a.Reconnect())
require.NotNil(t, a.sp)
// act & assert
require.NoError(t, a.Disconnect())
require.Nil(t, a.sp)
// act & assert
require.NoError(t, a.Reconnect())
require.NotNil(t, a.sp)
}
func TestFinalize(t *testing.T) {
// arrange
a, rwc := initTestAdaptor()
// act & assert
require.NoError(t, a.Finalize())
assert.False(t, a.IsConnected())
// re-arrange error
rwc.simulateCloseErr = true
require.NoError(t, a.Connect())
// act & assert
require.ErrorContains(t, a.Finalize(), "close error")
}