gobot.io/x/gobot/v2@v2.1.0/platforms/mavlink/mavlink_adaptor_test.go (about) 1 package mavlink 2 3 import ( 4 "errors" 5 "io" 6 "strings" 7 "testing" 8 9 "gobot.io/x/gobot/v2" 10 "gobot.io/x/gobot/v2/gobottest" 11 ) 12 13 var _ gobot.Adaptor = (*Adaptor)(nil) 14 15 type nullReadWriteCloser struct{} 16 17 var payload = []byte{0xFE, 0x09, 0x4E, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03, 0x1C, 0x7F} 18 19 var testAdaptorRead = func(p []byte) (int, error) { 20 return len(p), nil 21 } 22 23 func (nullReadWriteCloser) Write(p []byte) (int, error) { 24 return testAdaptorRead(p) 25 } 26 27 var testAdaptorWrite = func(b []byte) (int, error) { 28 if len(payload) > 0 { 29 copy(b, payload[:len(b)]) 30 payload = payload[len(b):] 31 return len(b), nil 32 } 33 return 0, errors.New("out of bytes") 34 } 35 36 func (nullReadWriteCloser) Read(b []byte) (int, error) { 37 return testAdaptorWrite(b) 38 } 39 40 var testAdaptorClose = func() error { 41 return nil 42 } 43 44 func (nullReadWriteCloser) Close() error { 45 return testAdaptorClose() 46 } 47 48 func initTestMavlinkAdaptor() *Adaptor { 49 m := NewAdaptor("/dev/null") 50 m.sp = nullReadWriteCloser{} 51 m.connect = func(port string) (io.ReadWriteCloser, error) { return nil, nil } 52 return m 53 } 54 55 func TestMavlinkAdaptor(t *testing.T) { 56 a := initTestMavlinkAdaptor() 57 gobottest.Assert(t, a.Port(), "/dev/null") 58 } 59 60 func TestMavlinkAdaptorName(t *testing.T) { 61 a := initTestMavlinkAdaptor() 62 gobottest.Assert(t, strings.HasPrefix(a.Name(), "Mavlink"), true) 63 a.SetName("NewName") 64 gobottest.Assert(t, a.Name(), "NewName") 65 } 66 67 func TestMavlinkAdaptorConnect(t *testing.T) { 68 a := initTestMavlinkAdaptor() 69 gobottest.Assert(t, a.Connect(), nil) 70 71 a.connect = func(port string) (io.ReadWriteCloser, error) { return nil, errors.New("connect error") } 72 gobottest.Assert(t, a.Connect(), errors.New("connect error")) 73 } 74 75 func TestMavlinkAdaptorFinalize(t *testing.T) { 76 a := initTestMavlinkAdaptor() 77 gobottest.Assert(t, a.Finalize(), nil) 78 79 testAdaptorClose = func() error { 80 return errors.New("close error") 81 } 82 gobottest.Assert(t, a.Finalize(), errors.New("close error")) 83 }