-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
mavlink_adaptor_test.go
87 lines (68 loc) · 1.96 KB
/
mavlink_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
package mavlink
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)
type nullReadWriteCloser struct{}
var payload = []byte{
0xFE, 0x09, 0x4E, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F,
}
var testAdaptorRead = func(p []byte) (int, error) {
return len(p), nil
}
func (nullReadWriteCloser) Write(p []byte) (int, error) {
return testAdaptorRead(p)
}
var testAdaptorWrite = func(b []byte) (int, error) {
if len(payload) > 0 {
copy(b, payload[:len(b)])
payload = payload[len(b):]
return len(b), nil
}
return 0, errors.New("out of bytes")
}
func (nullReadWriteCloser) Read(b []byte) (int, error) {
return testAdaptorWrite(b)
}
var testAdaptorClose = func() error {
return nil
}
func (nullReadWriteCloser) Close() error {
return testAdaptorClose()
}
func initTestMavlinkAdaptor() *Adaptor {
m := NewAdaptor("/dev/null")
m.sp = nullReadWriteCloser{}
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil } //nolint:nilnil // ok for tests
return m
}
func TestMavlinkAdaptor(t *testing.T) {
a := initTestMavlinkAdaptor()
assert.Equal(t, "/dev/null", a.Port())
}
func TestMavlinkAdaptorName(t *testing.T) {
a := initTestMavlinkAdaptor()
assert.True(t, strings.HasPrefix(a.Name(), "Mavlink"))
a.SetName("NewName")
assert.Equal(t, "NewName", a.Name())
}
func TestMavlinkAdaptorConnect(t *testing.T) {
a := initTestMavlinkAdaptor()
require.NoError(t, a.Connect())
a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") }
require.ErrorContains(t, a.Connect(), "connect error")
}
func TestMavlinkAdaptorFinalize(t *testing.T) {
a := initTestMavlinkAdaptor()
require.NoError(t, a.Finalize())
testAdaptorClose = func() error {
return errors.New("close error")
}
require.ErrorContains(t, a.Finalize(), "close error")
}