2014-04-26 18:07:04 -07:00
|
|
|
package sphero
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-12-17 14:32:12 -08:00
|
|
|
"errors"
|
2014-12-23 07:00:23 -08:00
|
|
|
"io"
|
2017-02-02 16:46:25 +01:00
|
|
|
"strings"
|
2014-06-13 14:26:18 -07:00
|
|
|
"testing"
|
2014-11-28 18:21:02 -08:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2016-10-01 18:52:58 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-08-27 11:56:01 +02:00
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
type nullReadWriteCloser struct {
|
|
|
|
testAdaptorRead func(p []byte) (int, error)
|
|
|
|
testAdaptorWrite func(b []byte) (int, error)
|
|
|
|
testAdaptorClose func() error
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
func (n *nullReadWriteCloser) Write(p []byte) (int, error) {
|
|
|
|
return n.testAdaptorWrite(p)
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
func (n *nullReadWriteCloser) Read(b []byte) (int, error) {
|
|
|
|
return n.testAdaptorRead(b)
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
func (n *nullReadWriteCloser) Close() error {
|
|
|
|
return n.testAdaptorClose()
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
func NewNullReadWriteCloser() *nullReadWriteCloser {
|
|
|
|
return &nullReadWriteCloser{
|
|
|
|
testAdaptorRead: func(p []byte) (int, error) {
|
|
|
|
return len(p), nil
|
|
|
|
},
|
|
|
|
testAdaptorWrite: func(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
},
|
|
|
|
testAdaptorClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
func initTestSpheroAdaptor() (*Adaptor, *nullReadWriteCloser) {
|
2016-10-01 18:52:58 +02:00
|
|
|
a := NewAdaptor("/dev/null")
|
2017-04-02 23:50:07 +02:00
|
|
|
rwc := NewNullReadWriteCloser()
|
|
|
|
|
2014-12-23 07:00:23 -08:00
|
|
|
a.connect = func(string) (io.ReadWriteCloser, error) {
|
2017-04-02 23:50:07 +02:00
|
|
|
return rwc, nil
|
2014-12-23 07:00:23 -08:00
|
|
|
}
|
2017-04-02 23:50:07 +02:00
|
|
|
return a, rwc
|
2014-06-13 14:26:18 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2017-04-06 11:16:48 +02:00
|
|
|
func TestSpheroAdaptorName(t *testing.T) {
|
|
|
|
a, _ := initTestSpheroAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:32:12 -08:00
|
|
|
func TestSpheroAdaptor(t *testing.T) {
|
2017-04-02 23:50:07 +02:00
|
|
|
a, _ := initTestSpheroAdaptor()
|
2017-02-02 16:46:25 +01:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.Port(), "/dev/null")
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpheroAdaptorReconnect(t *testing.T) {
|
2017-04-02 23:50:07 +02:00
|
|
|
a, _ := initTestSpheroAdaptor()
|
2014-12-17 14:32:12 -08:00
|
|
|
a.Connect()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.connected, true)
|
2014-12-17 14:32:12 -08:00
|
|
|
a.Reconnect()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.connected, true)
|
2014-12-17 14:32:12 -08:00
|
|
|
a.Disconnect()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.connected, false)
|
2014-12-17 14:32:12 -08:00
|
|
|
a.Reconnect()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.connected, true)
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestSpheroAdaptorFinalize(t *testing.T) {
|
2017-04-02 23:50:07 +02:00
|
|
|
a, rwc := initTestSpheroAdaptor()
|
2014-12-23 07:00:23 -08:00
|
|
|
a.Connect()
|
2016-11-07 20:15:07 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-12-17 14:32:12 -08:00
|
|
|
|
2017-04-02 23:50:07 +02:00
|
|
|
rwc.testAdaptorClose = func() error {
|
2014-12-17 14:32:12 -08:00
|
|
|
return errors.New("close error")
|
|
|
|
}
|
|
|
|
|
2014-12-23 07:00:23 -08:00
|
|
|
a.connected = true
|
2016-11-07 20:15:07 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), errors.New("close error"))
|
2014-06-13 14:26:18 -07:00
|
|
|
}
|
2014-12-17 14:32:12 -08:00
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestSpheroAdaptorConnect(t *testing.T) {
|
2017-04-02 23:50:07 +02:00
|
|
|
a, _ := initTestSpheroAdaptor()
|
2016-11-07 20:15:07 +01:00
|
|
|
gobottest.Assert(t, a.Connect(), nil)
|
2014-12-17 14:32:12 -08:00
|
|
|
|
2014-12-23 07:00:23 -08:00
|
|
|
a.connect = func(string) (io.ReadWriteCloser, error) {
|
|
|
|
return nil, errors.New("connect error")
|
2014-12-17 14:32:12 -08:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:15:07 +01:00
|
|
|
gobottest.Assert(t, a.Connect(), errors.New("connect error"))
|
2014-06-13 14:26:18 -07:00
|
|
|
}
|