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"
|
2014-07-31 23:39:52 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2014-07-31 23:39:52 -07:00
|
|
|
func initTestMavlinkAdaptor() *MavlinkAdaptor {
|
2014-08-04 12:06:54 -07:00
|
|
|
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
|
2014-12-17 16:31:49 -08:00
|
|
|
m.sp = nullReadWriteCloser{}
|
2014-12-18 15:27:23 -08:00
|
|
|
m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil }
|
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()
|
|
|
|
gobot.Assert(t, a.Name(), "myAdaptor")
|
|
|
|
gobot.Assert(t, a.Port(), "/dev/null")
|
|
|
|
}
|
2014-07-31 23:39:52 -07:00
|
|
|
func TestMavlinkAdaptorConnect(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Connect()), 0)
|
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") }
|
2014-12-17 16:31:49 -08:00
|
|
|
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMavlinkAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestMavlinkAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Finalize()), 0)
|
2014-12-17 16:31:49 -08:00
|
|
|
|
|
|
|
testAdaptorClose = func() error {
|
|
|
|
return errors.New("close error")
|
|
|
|
}
|
|
|
|
gobot.Assert(t, a.Finalize()[0], errors.New("close error"))
|
2014-07-31 23:39:52 -07:00
|
|
|
}
|