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 {
name string
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
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
return &ArdroneAdaptor{
a := &ArdroneAdaptor{
name: name,
connect: func(a *ArdroneAdaptor) (err error) {
config := client.DefaultConfig()
if len(v) > 0 {
config.Ip = v[0]
}
d, err := client.Connect(config)
if err != nil {
return
}
a.drone = d
return
connect: func(a *ArdroneAdaptor) (drone, error) {
return client.Connect(a.config)
},
}
a.config = client.DefaultConfig()
if len(v) > 0 {
a.config.Ip = v[0]
}
return a
}
func (a *ArdroneAdaptor) Name() string { return a.name }
// Connect returns true when connection to ardrone is established correclty
func (a *ArdroneAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil {
if d, err := a.connect(a); err != nil {
return []error{err}
} else {
a.drone = d
}
return
}

View File

@ -1,6 +1,7 @@
package ardrone
import (
"errors"
"testing"
"github.com/hybridgroup/gobot"
@ -8,19 +9,32 @@ import (
func initTestArdroneAdaptor() *ArdroneAdaptor {
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) error {
a.drone = &testDrone{}
return nil
a.connect = func(a *ArdroneAdaptor) (drone, error) {
return &testDrone{}, nil
}
return a
}
func TestConnect(t *testing.T) {
a := initTestArdroneAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
func TestArdroneAdaptor(t *testing.T) {
a := NewArdroneAdaptor("drone")
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()
gobot.Assert(t, len(a.Finalize()), 0)
}

View File

@ -8,9 +8,8 @@ import (
func initTestArdroneDriver() *ArdroneDriver {
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) (err error) {
a.drone = &testDrone{}
return
a.connect = func(a *ArdroneAdaptor) (drone, error) {
return &testDrone{}, nil
}
d := NewArdroneDriver(a, "drone")
a.Connect()
@ -19,7 +18,8 @@ func initTestArdroneDriver() *ArdroneDriver {
func TestArdroneDriverStart(t *testing.T) {
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) {