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