Skip to content

Commit

Permalink
Increase sphero test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 17, 2014
1 parent 4cc5788 commit f3e3dea
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion platforms/sphero/sphero_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
if err != nil {
return err
}
a.connected = true
a.sp = s
return
},
Expand All @@ -43,6 +42,7 @@ func (a *SpheroAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil {
return []error{err}
}
a.connected = true
return
}

Expand Down
60 changes: 59 additions & 1 deletion platforms/sphero/sphero_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,81 @@
package sphero

import (
"errors"
"testing"

"github.com/hybridgroup/gobot"
)

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()
}

func initTestSpheroAdaptor() *SpheroAdaptor {
a := NewSpheroAdaptor("bot", "/dev/null")
a.sp = gobot.NullReadWriteCloser{}
a.sp = nullReadWriteCloser{}
a.connect = func(a *SpheroAdaptor) (err error) { return nil }
return a
}

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)
}

func TestSpheroAdaptorFinalize(t *testing.T) {
a := initTestSpheroAdaptor()
gobot.Assert(t, len(a.Finalize()), 0)

testAdaptorClose = func() error {
return errors.New("close error")
}

gobot.Assert(t, a.Finalize()[0], errors.New("close error"))
}

func TestSpheroAdaptorConnect(t *testing.T) {
a := initTestSpheroAdaptor()
gobot.Assert(t, len(a.Connect()), 0)

a.connect = func(a *SpheroAdaptor) (err error) {
return errors.New("connect error")
}

gobot.Assert(t, a.Connect()[0], errors.New("connect error"))
}
2 changes: 1 addition & 1 deletion platforms/sphero/sphero_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
return nil
})
s.AddCommand("SetStabilization", func(params map[string]interface{}) interface{} {
on := params["heading"].(bool)
on := params["enable"].(bool)
s.SetStabilization(on)
return nil
})
Expand Down
41 changes: 40 additions & 1 deletion platforms/sphero/sphero_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,49 @@ import (

func initTestSpheroDriver() *SpheroDriver {
a := NewSpheroAdaptor("bot", "/dev/null")
a.sp = gobot.NullReadWriteCloser{}
a.sp = nullReadWriteCloser{}
return NewSpheroDriver(a, "bot")
}

func TestSpheroDriver(t *testing.T) {
d := initTestSpheroDriver()
var ret interface{}

ret = d.Command("SetRGB")(
map[string]interface{}{"r": 100.0, "g": 100.0, "b": 100.0},
)
gobot.Assert(t, ret, nil)

ret = d.Command("Roll")(
map[string]interface{}{"speed": 100.0, "heading": 100.0},
)
gobot.Assert(t, ret, nil)

ret = d.Command("SetBackLED")(
map[string]interface{}{"level": 100.0},
)
gobot.Assert(t, ret, nil)

ret = d.Command("SetHeading")(
map[string]interface{}{"heading": 100.0},
)
gobot.Assert(t, ret, nil)

ret = d.Command("SetStabilization")(
map[string]interface{}{"enable": true},
)
gobot.Assert(t, ret, nil)

ret = d.Command("Stop")(nil)
gobot.Assert(t, ret, nil)

ret = d.Command("GetRGB")(nil)
gobot.Assert(t, ret.([]byte), []byte{})

gobot.Assert(t, d.Name(), "bot")
gobot.Assert(t, d.Connection().Name(), "bot")
}

func TestSpheroDriverStart(t *testing.T) {
d := initTestSpheroDriver()
gobot.Assert(t, len(d.Start()), 0)
Expand Down

0 comments on commit f3e3dea

Please sign in to comment.