Increase leap test coverage

This commit is contained in:
Adrian Zankich 2014-11-07 13:15:45 -08:00
parent ce7181741f
commit 9793dd6db3
6 changed files with 58 additions and 33 deletions

View File

@ -0,0 +1,21 @@
mode: set
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:15.50,19.2 3 0
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:25.79,36.2 3 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:39.57,41.2 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:48.41,52.16 4 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:56.2,56.12 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:62.2,62.13 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:52.16,54.3 1 0
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:56.12,57.7 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:57.7,59.4 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_driver.go:66.40,66.55 1 1
github.com/hybridgroup/gobot/platforms/leap/parser.go:69.28,71.2 1 1
github.com/hybridgroup/gobot/platforms/leap/parser.go:74.28,76.2 1 1
github.com/hybridgroup/gobot/platforms/leap/parser.go:79.28,81.2 1 1
github.com/hybridgroup/gobot/platforms/leap/parser.go:84.58,88.2 3 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:17.72,24.39 1 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:24.39,30.18 2 0
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:33.4,33.13 1 0
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:30.18,32.5 1 0
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:39.44,43.2 3 1
github.com/hybridgroup/gobot/platforms/leap/leap_motion_adaptor.go:46.45,46.60 1 1

View File

@ -4,11 +4,12 @@ import (
"code.google.com/p/go.net/websocket"
"fmt"
"github.com/hybridgroup/gobot"
"io"
)
type LeapMotionAdaptor struct {
gobot.Adaptor
ws *websocket.Conn
ws io.ReadWriteCloser
connect func(*LeapMotionAdaptor)
}
@ -21,9 +22,11 @@ func NewLeapMotionAdaptor(name string, port string) *LeapMotionAdaptor {
port,
),
connect: func(l *LeapMotionAdaptor) {
origin := fmt.Sprintf("http://%v", l.Port())
url := fmt.Sprintf("ws://%v/v3.json", l.Port())
ws, err := websocket.Dial(url, "", origin)
ws, err := websocket.Dial(
fmt.Sprintf("ws://%v/v3.json", l.Port()),
"",
fmt.Sprintf("http://%v", l.Port()),
)
if err != nil {
panic(err)
}

View File

@ -6,17 +6,17 @@ import (
)
func initTestLeapMotionAdaptor() *LeapMotionAdaptor {
return NewLeapMotionAdaptor("bot", "/dev/null")
a := NewLeapMotionAdaptor("bot", "")
a.connect = func(l *LeapMotionAdaptor) {}
return a
}
func TestLeapMotionAdaptorConnect(t *testing.T) {
t.SkipNow()
a := initTestLeapMotionAdaptor()
gobot.Assert(t, a.Connect(), true)
}
func TestLeapMotionAdaptorFinalize(t *testing.T) {
t.SkipNow()
a := initTestLeapMotionAdaptor()
gobot.Assert(t, a.Finalize(), true)
}

View File

@ -2,6 +2,7 @@ package leap
import (
"encoding/json"
"io"
"code.google.com/p/go.net/websocket"
"github.com/hybridgroup/gobot"
@ -11,6 +12,12 @@ type LeapMotionDriver struct {
gobot.Driver
}
var receive = func(ws io.ReadWriteCloser) []byte {
var msg []byte
websocket.Message.Receive(ws.(*websocket.Conn), &msg)
return msg
}
// NewLeapMotionDriver creates a new leap motion driver with specified name
//
// Adds the following events:
@ -37,7 +44,7 @@ func (l *LeapMotionDriver) adaptor() *LeapMotionAdaptor {
// and listening from incoming messages.
//
// Publishes the following events:
// "message" - Sends parsed data of received frames.
// "message" - Emits Frame on new message received from Leap.
func (l *LeapMotionDriver) Start() bool {
enableGestures := map[string]bool{"enableGestures": true}
b, _ := json.Marshal(enableGestures)
@ -48,17 +55,12 @@ func (l *LeapMotionDriver) Start() bool {
go func() {
for {
var msg []byte
websocket.Message.Receive(l.adaptor().ws, &msg)
gobot.Publish(l.Event("message"), l.ParseFrame(msg))
gobot.Publish(l.Event("message"), l.ParseFrame(receive(l.adaptor().ws)))
}
}()
return true
}
// Init returns true if driver is initialized correctly
func (l *LeapMotionDriver) Init() bool { return true }
// Halt returns true if driver is halted succesfully
func (l *LeapMotionDriver) Halt() bool { return true }

View File

@ -1,33 +1,37 @@
package leap
import (
"github.com/hybridgroup/gobot"
"io"
"io/ioutil"
"testing"
"github.com/hybridgroup/gobot"
)
func initTestLeapMotionDriver() *LeapMotionDriver {
return NewLeapMotionDriver(NewLeapMotionAdaptor("bot", "/dev/null"), "bot")
a := NewLeapMotionAdaptor("bot", "")
a.connect = func(l *LeapMotionAdaptor) {
l.ws = new(gobot.NullReadWriteCloser)
}
a.Connect()
receive = func(ws io.ReadWriteCloser) []byte {
file, _ := ioutil.ReadFile("./test/support/example_frame.json")
return file
}
return NewLeapMotionDriver(a, "bot")
}
func TestLeapMotionDriverStart(t *testing.T) {
t.SkipNow()
//t.SkipNow()
d := initTestLeapMotionDriver()
gobot.Assert(t, d.Start(), true)
}
func TestLeapMotionDriverHalt(t *testing.T) {
t.SkipNow()
d := initTestLeapMotionDriver()
gobot.Assert(t, d.Halt(), true)
}
func TestLeapMotionDriverInit(t *testing.T) {
t.SkipNow()
d := initTestLeapMotionDriver()
gobot.Assert(t, d.Init(), true)
}
func TestLeapMotionDriverParser(t *testing.T) {
d := initTestLeapMotionDriver()
file, _ := ioutil.ReadFile("./test/support/example_frame.json")
@ -37,6 +41,8 @@ func TestLeapMotionDriverParser(t *testing.T) {
t.Errorf("ParseFrame incorrectly parsed frame")
}
parsedFrame = d.ParseFrame([]byte{})
gobot.Assert(t, parsedFrame.Timestamp, 0)
gobot.Assert(t, parsedFrame.Timestamp, 4729292670)
gobot.Assert(t, parsedFrame.Hands[0].X(), 117.546)
gobot.Assert(t, parsedFrame.Hands[0].Y(), 236.007)
gobot.Assert(t, parsedFrame.Hands[0].Z(), 76.3394)
}

View File

@ -2,7 +2,6 @@ package leap
import (
"encoding/json"
"regexp"
)
type Gesture struct {
@ -87,9 +86,3 @@ func (l *LeapMotionDriver) ParseFrame(data []byte) Frame {
json.Unmarshal(data, &frame)
return frame
}
// isAFrame returns true if data contains a frame representation
func (l *LeapMotionDriver) isAFrame(data []byte) bool {
match, _ := regexp.Match("currentFrameRate", data)
return match
}