hybridgroup.gobot/platforms/sphero/sphero_adaptor_test.go

105 lines
2.3 KiB
Go
Raw Normal View History

2014-04-27 09:07:04 +08:00
package sphero
import (
2014-12-18 06:32:12 +08:00
"errors"
"io"
"strings"
2014-06-14 05:26:18 +08:00
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
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
}
func (n *nullReadWriteCloser) Write(p []byte) (int, error) {
return n.testAdaptorWrite(p)
2014-12-18 06:32:12 +08:00
}
func (n *nullReadWriteCloser) Read(b []byte) (int, error) {
return n.testAdaptorRead(b)
2014-12-18 06:32:12 +08:00
}
func (n *nullReadWriteCloser) Close() error {
return n.testAdaptorClose()
2014-12-18 06:32:12 +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
}
func initTestSpheroAdaptor() (*Adaptor, *nullReadWriteCloser) {
a := NewAdaptor("/dev/null")
rwc := NewNullReadWriteCloser()
a.connect = func(string) (io.ReadWriteCloser, error) {
return rwc, nil
}
return a, rwc
2014-06-14 05:26:18 +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) {
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, strings.HasPrefix(a.Name(), "Sphero"), true)
gobottest.Assert(t, a.Port(), "/dev/null")
2014-12-18 06:32:12 +08:00
}
func TestSpheroAdaptorReconnect(t *testing.T) {
a, _ := initTestSpheroAdaptor()
2014-12-18 06:32:12 +08:00
a.Connect()
gobottest.Assert(t, a.connected, true)
2014-12-18 06:32:12 +08:00
a.Reconnect()
gobottest.Assert(t, a.connected, true)
2014-12-18 06:32:12 +08:00
a.Disconnect()
gobottest.Assert(t, a.connected, false)
2014-12-18 06:32:12 +08:00
a.Reconnect()
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) {
a, rwc := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, a.Finalize(), nil)
2014-12-18 06:32:12 +08:00
rwc.testAdaptorClose = func() error {
2014-12-18 06:32:12 +08:00
return errors.New("close error")
}
a.connected = true
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) {
a, _ := initTestSpheroAdaptor()
gobottest.Assert(t, a.Connect(), nil)
2014-12-18 06:32:12 +08:00
a.connect = func(string) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error")
2014-12-18 06:32:12 +08:00
}
gobottest.Assert(t, a.Connect(), errors.New("connect error"))
2014-06-14 05:26:18 +08:00
}