Increase ardrone test coverage

This commit is contained in:
Adrian Zankich 2014-12-18 13:33:33 -08:00
parent 23890e2d6a
commit ca4e590fd5
3 changed files with 40 additions and 25 deletions

View File

@ -25,34 +25,35 @@ type drone interface {
type ArdroneAdaptor struct { type ArdroneAdaptor struct {
name string name string
drone drone drone drone
connect func(*ArdroneAdaptor) (err error) config client.Config
connect func(*ArdroneAdaptor) (drone, error)
} }
// NewArdroneAdaptor creates a new ardrone and connects with default configuration // NewArdroneAdaptor creates a new ardrone and connects with default configuration
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor { func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
return &ArdroneAdaptor{ a := &ArdroneAdaptor{
name: name, name: name,
connect: func(a *ArdroneAdaptor) (err error) { connect: func(a *ArdroneAdaptor) (drone, error) {
config := client.DefaultConfig() return client.Connect(a.config)
if len(v) > 0 {
config.Ip = v[0]
}
d, err := client.Connect(config)
if err != nil {
return
}
a.drone = d
return
}, },
} }
a.config = client.DefaultConfig()
if len(v) > 0 {
a.config.Ip = v[0]
}
return a
} }
func (a *ArdroneAdaptor) Name() string { return a.name } func (a *ArdroneAdaptor) Name() string { return a.name }
// Connect returns true when connection to ardrone is established correclty // Connect returns true when connection to ardrone is established correclty
func (a *ArdroneAdaptor) Connect() (errs []error) { func (a *ArdroneAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil { if d, err := a.connect(a); err != nil {
return []error{err} return []error{err}
} else {
a.drone = d
} }
return return
} }

View File

@ -1,6 +1,7 @@
package ardrone package ardrone
import ( import (
"errors"
"testing" "testing"
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
@ -8,19 +9,32 @@ import (
func initTestArdroneAdaptor() *ArdroneAdaptor { func initTestArdroneAdaptor() *ArdroneAdaptor {
a := NewArdroneAdaptor("drone") a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) error { a.connect = func(a *ArdroneAdaptor) (drone, error) {
a.drone = &testDrone{} return &testDrone{}, nil
return nil
} }
return a return a
} }
func TestConnect(t *testing.T) { func TestArdroneAdaptor(t *testing.T) {
a := initTestArdroneAdaptor() a := NewArdroneAdaptor("drone")
gobot.Assert(t, len(a.Connect()), 0) gobot.Assert(t, a.Name(), "drone")
gobot.Assert(t, a.config.Ip, "192.168.1.1")
a = NewArdroneAdaptor("drone", "192.168.100.100")
gobot.Assert(t, a.config.Ip, "192.168.100.100")
} }
func TestFinalize(t *testing.T) { func TestArdroneAdaptorConnect(t *testing.T) {
a := initTestArdroneAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
a.connect = func(a *ArdroneAdaptor) (drone, error) {
return nil, errors.New("connection error")
}
gobot.Assert(t, a.Connect()[0], errors.New("connection error"))
}
func TestArdroneAdaptorFinalize(t *testing.T) {
a := initTestArdroneAdaptor() a := initTestArdroneAdaptor()
gobot.Assert(t, len(a.Finalize()), 0) gobot.Assert(t, len(a.Finalize()), 0)
} }

View File

@ -8,9 +8,8 @@ import (
func initTestArdroneDriver() *ArdroneDriver { func initTestArdroneDriver() *ArdroneDriver {
a := NewArdroneAdaptor("drone") a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) (err error) { a.connect = func(a *ArdroneAdaptor) (drone, error) {
a.drone = &testDrone{} return &testDrone{}, nil
return
} }
d := NewArdroneDriver(a, "drone") d := NewArdroneDriver(a, "drone")
a.Connect() a.Connect()
@ -19,7 +18,8 @@ func initTestArdroneDriver() *ArdroneDriver {
func TestArdroneDriverStart(t *testing.T) { func TestArdroneDriverStart(t *testing.T) {
d := initTestArdroneDriver() d := initTestArdroneDriver()
gobot.Assert(t, len(d.Start()), 0) gobot.Assert(t, d.Name(), "drone")
gobot.Assert(t, d.Connection().Name(), "drone")
} }
func TestArdroneDriverHalt(t *testing.T) { func TestArdroneDriverHalt(t *testing.T) {