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-06-13 14:26:18 -07:00
|
|
|
"testing"
|
2014-11-28 18:21:02 -08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2014-12-17 14: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-13 16:01:39 -07:00
|
|
|
func initTestSpheroAdaptor() *SpheroAdaptor {
|
|
|
|
a := NewSpheroAdaptor("bot", "/dev/null")
|
2014-12-17 14:32:12 -08:00
|
|
|
a.sp = nullReadWriteCloser{}
|
2014-11-19 16:17:14 -08:00
|
|
|
a.connect = func(a *SpheroAdaptor) (err error) { return nil }
|
2014-06-13 16:01:39 -07:00
|
|
|
return a
|
2014-06-13 14:26:18 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-12-17 14:32:12 -08:00
|
|
|
func TestSpheroAdaptor(t *testing.T) {
|
|
|
|
a := initTestSpheroAdaptor()
|
|
|
|
gobot.Assert(t, a.Name(), "bot")
|
|
|
|
gobot.Assert(t, a.Port(), "/dev/null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpheroAdaptorReconnect(t *testing.T) {
|
|
|
|
a := initTestSpheroAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
gobot.Assert(t, a.connected, true)
|
|
|
|
a.Reconnect()
|
|
|
|
gobot.Assert(t, a.connected, true)
|
|
|
|
a.Disconnect()
|
|
|
|
gobot.Assert(t, a.connected, false)
|
|
|
|
a.Reconnect()
|
|
|
|
gobot.Assert(t, a.connected, true)
|
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestSpheroAdaptorFinalize(t *testing.T) {
|
|
|
|
a := initTestSpheroAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Finalize()), 0)
|
2014-12-17 14:32:12 -08:00
|
|
|
|
|
|
|
testAdaptorClose = func() error {
|
|
|
|
return errors.New("close error")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobot.Assert(t, a.Finalize()[0], 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) {
|
|
|
|
a := initTestSpheroAdaptor()
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(a.Connect()), 0)
|
2014-12-17 14:32:12 -08:00
|
|
|
|
|
|
|
a.connect = func(a *SpheroAdaptor) (err error) {
|
|
|
|
return errors.New("connect error")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))
|
2014-06-13 14:26:18 -07:00
|
|
|
}
|