2014-07-31 23:39:52 -07:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-12-17 16:31:49 -08:00
|
|
|
"errors"
|
2014-12-18 15:27:23 -08:00
|
|
|
"io"
|
2017-04-06 10:52:59 +02:00
|
|
|
"strings"
|
2014-07-31 23:39:52 -07:00
|
|
|
"testing"
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 14:17:02 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-15 20:51:52 +01:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-07-31 23:39:52 -07:00
|
|
|
)
|
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-08-27 11:56:01 +02:00
|
|
|
|
2014-12-17 16:31:49 -08:00
|
|
|
type nullReadWriteCloser struct{}
|
|
|
|
|
2023-11-15 20:51:52 +01:00
|
|
|
var payload = []byte{
|
|
|
|
0xFE, 0x09, 0x4E, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F,
|
|
|
|
}
|
2014-12-17 16:31:49 -08:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:44:40 +02:00
|
|
|
func initTestMavlinkAdaptor() *Adaptor {
|
|
|
|
m := NewAdaptor("/dev/null")
|
2014-12-17 16:31:49 -08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2024-11-01 12:54:20 +01:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil } //nolint:nilnil // ok for tests
|
2014-08-04 12:06:54 -07:00
|
|
|
return m
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|
|
|
|
|
2014-12-17 16:31:49 -08:00
|
|
|
func TestMavlinkAdaptor(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "/dev/null", a.Port())
|
2014-12-17 16:31:49 -08:00
|
|
|
}
|
2017-04-06 10:52:59 +02:00
|
|
|
|
|
|
|
func TestMavlinkAdaptorName(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(a.Name(), "Mavlink"))
|
2017-04-06 10:52:59 +02:00
|
|
|
a.SetName("NewName")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "NewName", a.Name())
|
2017-04-06 10:52:59 +02:00
|
|
|
}
|
|
|
|
|
2014-07-31 23:39:52 -07:00
|
|
|
func TestMavlinkAdaptorConnect(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2023-11-12 14:17:02 +01:00
|
|
|
require.NoError(t, a.Connect())
|
2014-12-17 16:31:49 -08:00
|
|
|
|
2014-12-18 15:27:23 -08:00
|
|
|
a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") }
|
2023-11-12 14:17:02 +01:00
|
|
|
require.ErrorContains(t, a.Connect(), "connect error")
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2023-11-12 14:17:02 +01:00
|
|
|
require.NoError(t, a.Finalize())
|
2014-12-17 16:31:49 -08:00
|
|
|
|
|
|
|
testAdaptorClose = func() error {
|
|
|
|
return errors.New("close error")
|
|
|
|
}
|
2023-11-12 14:17:02 +01:00
|
|
|
require.ErrorContains(t, a.Finalize(), "close error")
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|