Skip to content

Commit

Permalink
Increase leap test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 7, 2014
1 parent ce71817 commit 9793dd6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
21 changes: 21 additions & 0 deletions platforms/leap/coverage.out
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions platforms/leap/leap_motion_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions platforms/leap/leap_motion_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 9 additions & 7 deletions platforms/leap/leap_motion_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package leap

import (
"encoding/json"
"io"

"code.google.com/p/go.net/websocket"
"github.com/hybridgroup/gobot"
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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 }
30 changes: 18 additions & 12 deletions platforms/leap/leap_motion_driver_test.go
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
}
7 changes: 0 additions & 7 deletions platforms/leap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package leap

import (
"encoding/json"
"regexp"
)

type Gesture struct {
Expand Down Expand Up @@ -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
}

0 comments on commit 9793dd6

Please sign in to comment.