hybridgroup.gobot/platforms/sphero/sphero_adaptor_test.go

89 lines
1.9 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"
2014-06-14 05:26:18 +08:00
"testing"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Adaptor = (*SpheroAdaptor)(nil)
2014-12-18 06:32:12 +08:00
type nullReadWriteCloser struct{}
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) {
return len(b), nil
}
func (nullReadWriteCloser) Read(b []byte) (int, error) {
return testAdaptorWrite(b)
}
var testAdaptorClose = func() error {
return nil
}
func (nullReadWriteCloser) Close() error {
return testAdaptorClose()
}
2014-06-14 07:01:39 +08:00
func initTestSpheroAdaptor() *SpheroAdaptor {
a := NewSpheroAdaptor("bot", "/dev/null")
a.connect = func(string) (io.ReadWriteCloser, error) {
return &nullReadWriteCloser{}, nil
}
2014-06-14 07:01:39 +08:00
return a
2014-06-14 05:26:18 +08:00
}
2014-12-18 06:32:12 +08:00
func TestSpheroAdaptor(t *testing.T) {
a := initTestSpheroAdaptor()
gobottest.Assert(t, a.Name(), "bot")
gobottest.Assert(t, a.Port(), "/dev/null")
2014-12-18 06:32:12 +08:00
}
func TestSpheroAdaptorReconnect(t *testing.T) {
a := initTestSpheroAdaptor()
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 := initTestSpheroAdaptor()
a.Connect()
gobottest.Assert(t, len(a.Finalize()), 0)
2014-12-18 06:32:12 +08:00
testAdaptorClose = func() error {
return errors.New("close error")
}
a.connected = true
gobottest.Assert(t, a.Finalize()[0], 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, len(a.Connect()), 0)
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()[0], errors.New("connect error"))
2014-06-14 05:26:18 +08:00
}