Skip to content

Commit

Permalink
Increase firmata test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 19, 2014
1 parent 6e3bb51 commit 4a104fd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
4 changes: 3 additions & 1 deletion platforms/firmata/firmata.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
i2CModeStopReading byte = 0x03
)

var defaultInitTimeInterval time.Duration = 1 * time.Second

type board struct {
serial io.ReadWriteCloser
pins []pin
Expand Down Expand Up @@ -84,7 +86,7 @@ func newBoard(sp io.ReadWriteCloser) *board {
analogPins: []byte{},
connected: false,
events: make(map[string]*gobot.Event),
initTimeInterval: 1 * time.Second,
initTimeInterval: defaultInitTimeInterval,
}

for _, s := range []string{
Expand Down
44 changes: 22 additions & 22 deletions platforms/firmata/firmata_adaptor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package firmata

import (
"errors"
"fmt"
"io"
"strconv"
Expand All @@ -27,7 +28,8 @@ type FirmataAdaptor struct {
port string
board *board
i2cAddress byte
connect func(*FirmataAdaptor) (err error)
conn io.ReadWriteCloser
connect func(string) (io.ReadWriteCloser, error)
}

// NewFirmataAdaptor returns a new firmata adaptor with specified name and optionally accepts:
Expand All @@ -40,39 +42,37 @@ type FirmataAdaptor struct {
// is supplied, then the FirmataAdaptor will use the provided io.ReadWriteCloser and use the
// string port as a label to be displayed in the log and api.
func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
var conn io.ReadWriteCloser
var port string = ""
f := &FirmataAdaptor{
name: name,
port: "",
conn: nil,
connect: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
},
}

for _, arg := range args {
switch arg.(type) {
case string:
port = arg.(string)
f.port = arg.(string)
case io.ReadWriteCloser:
conn = arg.(io.ReadWriteCloser)
f.conn = arg.(io.ReadWriteCloser)
}
}

return &FirmataAdaptor{
name: name,
port: port,
connect: func(f *FirmataAdaptor) (err error) {
if conn == nil {
conn, err = serial.OpenPort(&serial.Config{Name: f.Port(), Baud: 57600})
if err != nil {
return err
}
}
f.board = newBoard(conn)
return
},
}
return f
}

// Connect returns true if connection to board is succesfull
func (f *FirmataAdaptor) Connect() (errs []error) {
if err := f.connect(f); err != nil {
return []error{err}
if f.conn == nil {
if sp, err := f.connect(f.Port()); err != nil {
return []error{err}
} else {
f.conn = sp
}
}
f.board = newBoard(f.conn)
f.board.connect()
return
}
Expand All @@ -83,7 +83,7 @@ func (f *FirmataAdaptor) Disconnect() (err error) {
if f.board != nil {
return f.board.serial.Close()
}
return
return errors.New("no board connected")
}

// Finalize disconnects firmata adaptor
Expand Down
58 changes: 44 additions & 14 deletions platforms/firmata/firmata_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package firmata

import (
"errors"
"fmt"
"io"
"testing"
"time"

"github.com/hybridgroup/gobot"
)

func initTestFirmataAdaptor() *FirmataAdaptor {
a := NewFirmataAdaptor("board", "/dev/null")
a.connect = func(f *FirmataAdaptor) (err error) {
f.board = newBoard(NullReadWriteCloser{})
f.board.initTimeInterval = 0 * time.Second
var connect = func(a *FirmataAdaptor) []error {
defaultInitTimeInterval = 0 * time.Second
gobot.After(1*time.Millisecond, func() {
// arduino uno r3 firmware response "StandardFirmata.ino"
f.board.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100,
a.board.process([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100,
0, 97, 0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0,
97, 0, 46, 0, 105, 0, 110, 0, 111, 0, 247})
// arduino uno r3 capabilities response
f.board.process([]byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1,
a.board.process([]byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1,
1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14,
127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1,
4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127,
Expand All @@ -27,25 +27,55 @@ func initTestFirmataAdaptor() *FirmataAdaptor {
127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1,
2, 10, 6, 1, 127, 247})
// arduino uno r3 analog mapping response
f.board.process([]byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127,
a.board.process([]byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247})
return nil
})
return a.Connect()
}

func initTestFirmataAdaptor() *FirmataAdaptor {
a := NewFirmataAdaptor("board", "/dev/null")
a.connect = func(port string) (io.ReadWriteCloser, error) {
return &NullReadWriteCloser{}, nil
}
a.Connect()
connect(a)
return a
}
func TestFirmataAdaptor(t *testing.T) {
a := initTestFirmataAdaptor()
gobot.Assert(t, a.Name(), "board")
gobot.Assert(t, a.Port(), "/dev/null")
}

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

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

func TestFirmataAdaptorDisconnect(t *testing.T) {
a := NewFirmataAdaptor("board", "/dev/null")
gobot.Assert(t, a.Disconnect(), errors.New("no board connected"))
}

func TestFirmataAdaptorConnect(t *testing.T) {
a := initTestFirmataAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
a := NewFirmataAdaptor("board", "/dev/null")
a.connect = func(port string) (io.ReadWriteCloser, error) {
return &NullReadWriteCloser{}, nil
}
gobot.Assert(t, len(connect(a)), 0)

a = NewFirmataAdaptor("board", "/dev/null")
a.connect = func(port string) (io.ReadWriteCloser, error) {
return nil, errors.New("connect error")
}
gobot.Assert(t, a.Connect()[0], errors.New("connect error"))

a = NewFirmataAdaptor("board", NullReadWriteCloser{})
gobot.Assert(t, a.connect(a), nil)
a = NewFirmataAdaptor("board", &NullReadWriteCloser{})
gobot.Assert(t, len(connect(a)), 0)
}

func TestFirmataAdaptorServoWrite(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion platforms/firmata/firmata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ func (NullReadWriteCloser) Read(b []byte) (int, error) {
return len(b), nil
}

var closeErr error = nil

func (NullReadWriteCloser) Close() error {
return nil
return closeErr
}

func initTestFirmata() *board {
Expand Down

0 comments on commit 4a104fd

Please sign in to comment.