Skip to content

Commit

Permalink
Increase mavlink test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 18, 2014
1 parent f3e3dea commit 5569fd3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
49 changes: 48 additions & 1 deletion platforms/mavlink/mavlink_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,71 @@
package mavlink

import (
"errors"
"testing"

"github.com/hybridgroup/gobot"
)

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() *MavlinkAdaptor {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = gobot.NullReadWriteCloser{}
m.sp = nullReadWriteCloser{}
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
return m
}

func TestMavlinkAdaptor(t *testing.T) {
a := initTestMavlinkAdaptor()
gobot.Assert(t, a.Name(), "myAdaptor")
gobot.Assert(t, a.Port(), "/dev/null")
}
func TestMavlinkAdaptorConnect(t *testing.T) {
a := initTestMavlinkAdaptor()
gobot.Assert(t, len(a.Connect()), 0)

a.connect = func(a *MavlinkAdaptor) (err error) { return errors.New("connect error") }
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))
}

func TestMavlinkAdaptorFinalize(t *testing.T) {
a := initTestMavlinkAdaptor()
gobot.Assert(t, len(a.Finalize()), 0)

testAdaptorClose = func() error {
return errors.New("close error")
}
gobot.Assert(t, a.Finalize()[0], errors.New("close error"))
}
61 changes: 60 additions & 1 deletion platforms/mavlink/mavlink_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,80 @@
package mavlink

import (
"errors"
"testing"
"time"

"github.com/hybridgroup/gobot"
common "github.com/hybridgroup/gobot/platforms/mavlink/common"
)

func initTestMavlinkDriver() *MavlinkDriver {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = gobot.NullReadWriteCloser{}
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }
m.sp = nullReadWriteCloser{}
return NewMavlinkDriver(m, "myDriver")
}

func TestMavlinkDriver(t *testing.T) {
m := NewMavlinkAdaptor("myAdaptor", "/dev/null")
m.sp = nullReadWriteCloser{}
m.connect = func(a *MavlinkAdaptor) (err error) { return nil }

d := NewMavlinkDriver(m, "myDriver")
gobot.Assert(t, d.Name(), "myDriver")
gobot.Assert(t, d.Connection().Name(), "myAdaptor")
gobot.Assert(t, d.interval, 10*time.Millisecond)

d = NewMavlinkDriver(m, "myDriver", 100*time.Millisecond)
gobot.Assert(t, d.interval, 100*time.Millisecond)

}
func TestMavlinkDriverStart(t *testing.T) {
d := initTestMavlinkDriver()
err := make(chan error, 0)
packet := make(chan *common.MAVLinkPacket, 0)
message := make(chan common.MAVLinkMessage, 0)

gobot.Once(d.Event("packet"), func(data interface{}) {
packet <- data.(*common.MAVLinkPacket)
})

gobot.Once(d.Event("message"), func(data interface{}) {
message <- data.(common.MAVLinkMessage)
})

gobot.On(d.Event("error"), func(data interface{}) {
err <- data.(error)
})
gobot.Assert(t, len(d.Start()), 0)

select {
case p := <-packet:
gobot.Assert(t, d.SendPacket(p), nil)

case <-time.After(100 * time.Millisecond):
t.Errorf("packet was not emitted")
}
select {
case <-message:
case <-time.After(100 * time.Millisecond):
t.Errorf("message was not emitted")
}
select {
case <-err:
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}

payload = []byte{0xFE, 0x09, 0x4E, 0x01, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
select {
case e := <-err:
gobot.Assert(t, e, errors.New("Unknown Message ID: 255"))
case <-time.After(100 * time.Millisecond):
t.Errorf("error was not emitted")
}

}

func TestMavlinkDriverHalt(t *testing.T) {
Expand Down

0 comments on commit 5569fd3

Please sign in to comment.