1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00
hybridgroup.gobot/platforms/mavlink/mavlink_adaptor_test.go

88 lines
2.0 KiB
Go
Raw Normal View History

2014-07-31 23:39:52 -07:00
package mavlink
import (
2014-12-17 16:31:49 -08:00
"errors"
"io"
"strings"
2014-07-31 23:39:52 -07:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
2014-07-31 23:39:52 -07:00
)
var _ gobot.Adaptor = (*Adaptor)(nil)
2014-12-17 16:31:49 -08:00
type nullReadWriteCloser struct{}
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()
}
func initTestMavlinkAdaptor() *Adaptor {
m := NewAdaptor("/dev/null")
2014-12-17 16:31:49 -08:00
m.sp = nullReadWriteCloser{}
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()
assert.Equal(t, "/dev/null", a.Port())
2014-12-17 16:31:49 -08:00
}
func TestMavlinkAdaptorName(t *testing.T) {
a := initTestMavlinkAdaptor()
assert.True(t, strings.HasPrefix(a.Name(), "Mavlink"))
a.SetName("NewName")
assert.Equal(t, "NewName", a.Name())
}
2014-07-31 23:39:52 -07:00
func TestMavlinkAdaptorConnect(t *testing.T) {
a := initTestMavlinkAdaptor()
require.NoError(t, a.Connect())
2014-12-17 16:31:49 -08:00
a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") }
require.ErrorContains(t, a.Connect(), "connect error")
2014-07-31 23:39:52 -07:00
}
func TestMavlinkAdaptorFinalize(t *testing.T) {
a := initTestMavlinkAdaptor()
require.NoError(t, a.Finalize())
2014-12-17 16:31:49 -08:00
testAdaptorClose = func() error {
return errors.New("close error")
}
require.ErrorContains(t, a.Finalize(), "close error")
2014-07-31 23:39:52 -07:00
}