Skip to content

Commit

Permalink
Increase gobot package test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 30, 2014
1 parent e7694b0 commit be963f9
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 126 deletions.
6 changes: 3 additions & 3 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func initTestAPI() *api {
a.Start()
a.Debug()

g.AddRobot(gobot.NewTestRobot("Robot1"))
g.AddRobot(gobot.NewTestRobot("Robot2"))
g.AddRobot(gobot.NewTestRobot("Robot3"))
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot("Robot3"))
g.AddCommand("TestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string)
return fmt.Sprintf("hey %v", message)
Expand Down
82 changes: 82 additions & 0 deletions api/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package api

import (
"fmt"

"github.com/hybridgroup/gobot"
)

type testDriver struct {
name string
pin string
connection gobot.Connection
gobot.Commander
}

func (t *testDriver) Start() (errs []error) { return }
func (t *testDriver) Halt() (errs []error) { return }
func (t *testDriver) Name() string { return t.name }
func (t *testDriver) Pin() string { return t.pin }
func (t *testDriver) Connection() gobot.Connection { return t.connection }

func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
t := &testDriver{
name: name,
connection: adaptor,
pin: pin,
Commander: gobot.NewCommander(),
}

t.AddCommand("TestDriverCommand", func(params map[string]interface{}) interface{} {
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})

t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} {
name := params["name"].(string)
return fmt.Sprintf("hello %v", name)
})

return t
}

type testAdaptor struct {
name string
port string
}

var testAdaptorConnect = func() (errs []error) { return }
var testAdaptorFinalize = func() (errs []error) { return }

func (t *testAdaptor) Finalize() (errs []error) { return testAdaptorFinalize() }
func (t *testAdaptor) Connect() (errs []error) { return testAdaptorConnect() }
func (t *testAdaptor) Name() string { return t.name }
func (t *testAdaptor) Port() string { return t.port }

func newTestAdaptor(name string, port string) *testAdaptor {
return &testAdaptor{
name: name,
port: port,
}
}

func newTestRobot(name string) *gobot.Robot {
adaptor1 := newTestAdaptor("Connection1", "/dev/null")
adaptor2 := newTestAdaptor("Connection2", "/dev/null")
adaptor3 := newTestAdaptor("", "/dev/null")
driver1 := newTestDriver(adaptor1, "Device1", "0")
driver2 := newTestDriver(adaptor2, "Device2", "2")
driver3 := newTestDriver(adaptor3, "", "1")
work := func() {}
r := gobot.NewRobot(name,
[]gobot.Connection{adaptor1, adaptor2, adaptor3},
[]gobot.Device{driver1, driver2, driver3},
work,
)
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
message := params["message"].(string)
robot := params["robot"].(string)
return fmt.Sprintf("hey %v, %v", robot, message)
})
return r
}
20 changes: 20 additions & 0 deletions commander_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gobot

import "testing"

func TestCommaner(t *testing.T) {
c := NewCommander()
c.AddCommand("test", func(map[string]interface{}) interface{} {
return "hi"
})

if _, ok := c.Commands()["test"]; !ok {
t.Errorf("Could not add command to list of Commands")
}

command := c.Command("test")
Refute(t, command, nil)

command = c.Command("booyeah")
Assert(t, command, (func(map[string]interface{}) interface{})(nil))
}
18 changes: 18 additions & 0 deletions eventer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gobot

import "testing"

func TestEventer(t *testing.T) {
e := NewEventer()
e.AddEvent("test")

if _, ok := e.Events()["test"]; !ok {
t.Errorf("Could not add event to list of Events")
}

event := e.Event("test")
Refute(t, event, nil)

event = e.Event("booyeah")
Assert(t, event, (*Event)(nil))
}
62 changes: 60 additions & 2 deletions examples/batty.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {
return params["a"]
})

loopback := gobot.NewLoopbackAdaptor("loopback")
ping := gobot.NewPingDriver(loopback, "ping")
loopback := NewLoopbackAdaptor("loopback", "/dev/null")
ping := NewPingDriver(loopback, "ping", "1")

work := func() {
gobot.Every(5*time.Second, func() {
Expand All @@ -38,3 +38,61 @@ func main() {
gbot.AddRobot(r)
gbot.Start()
}

var _ gobot.Adaptor = (*loopbackAdaptor)(nil)

type loopbackAdaptor struct {
name string
port string
}

func (t *loopbackAdaptor) Finalize() (errs []error) { return }
func (t *loopbackAdaptor) Connect() (errs []error) { return }
func (t *loopbackAdaptor) Name() string { return t.name }
func (t *loopbackAdaptor) Port() string { return t.port }

func NewLoopbackAdaptor(name, port string) *loopbackAdaptor {
return &loopbackAdaptor{
name: name,
port: port,
}
}

var _ gobot.Driver = (*pingDriver)(nil)

type pingDriver struct {
name string
pin string
connection gobot.Connection
gobot.Eventer
gobot.Commander
}

func (t *pingDriver) Start() (errs []error) { return }
func (t *pingDriver) Halt() (errs []error) { return }
func (t *pingDriver) Name() string { return t.name }
func (t *pingDriver) Pin() string { return t.pin }
func (t *pingDriver) Connection() gobot.Connection { return t.connection }

func NewPingDriver(adaptor *loopbackAdaptor, name string, pin string) *pingDriver {
t := &pingDriver{
name: name,
connection: adaptor,
pin: pin,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
}

t.AddEvent("ping")

t.AddCommand("ping", func(params map[string]interface{}) interface{} {
return t.Ping()
})

return t
}

func (t *pingDriver) Ping() string {
gobot.Publish(t.Event("ping"), "ping")
return "pong"
}
90 changes: 84 additions & 6 deletions gobot_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
package gobot

import (
"errors"
"log"
"os"
"testing"
)

func TestConnectionEach(t *testing.T) {
r := newTestRobot("Robot1")

i := 0
r.Connections().Each(func(conn Connection) {
i++
})
Assert(t, r.Connections().Len(), i)
}

func initTestGobot() *Gobot {
log.SetOutput(&NullReadWriteCloser{})
g := NewGobot()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.AddRobot(NewTestRobot("Robot1"))
g.AddRobot(NewTestRobot("Robot2"))
g.AddRobot(NewTestRobot("Robot3"))
g.AddRobot(newTestRobot("Robot1"))
g.AddRobot(newTestRobot("Robot2"))
g.AddRobot(newTestRobot(""))
return g
}

func TestGobotStart(t *testing.T) {
g := initTestGobot()
g.Start()
func TestVersion(t *testing.T) {
Assert(t, version, Version())
}

func TestNullReadWriteCloser(t *testing.T) {
n := &NullReadWriteCloser{}
i, _ := n.Write([]byte{1, 2, 3})
Assert(t, i, 3)
i, _ = n.Read(make([]byte, 10))
Assert(t, i, 10)
Assert(t, n.Close(), nil)
}

func TestGobotRobot(t *testing.T) {
g := initTestGobot()
Assert(t, g.Robot("Robot1").Name, "Robot1")
Assert(t, g.Robot("Robot4"), (*Robot)(nil))
Assert(t, g.Robot("Robot4").Device("Device1"), (Device)(nil))
Assert(t, g.Robot("Robot4").Connection("Connection1"), (Connection)(nil))
Assert(t, g.Robot("Robot1").Device("Device4"), (Device)(nil))
Assert(t, g.Robot("Robot1").Device("Device1").Name(), "Device1")
Assert(t, g.Robot("Robot1").Devices().Len(), 3)
Expand All @@ -43,3 +64,60 @@ func TestGobotToJSON(t *testing.T) {
Assert(t, len(json.Robots), g.Robots().Len())
Assert(t, len(json.Commands), len(g.Commands()))
}

func TestGobotStart(t *testing.T) {
g := initTestGobot()
Assert(t, len(g.Start()), 0)
}

func TestGobotStartErrors(t *testing.T) {
log.SetOutput(&NullReadWriteCloser{})
g := NewGobot()

adaptor1 := newTestAdaptor("Connection1", "/dev/null")
driver1 := newTestDriver(adaptor1, "Device1", "0")
r := NewRobot("Robot1",
[]Connection{adaptor1},
[]Device{driver1},
)

g.AddRobot(r)

testDriverStart = func() (errs []error) {
return []error{
errors.New("driver start error 1"),
}
}

Assert(t, len(g.Start()), 1)

testDriverStart = func() (errs []error) { return }
testAdaptorConnect = func() (errs []error) {
return []error{
errors.New("adaptor start error 1"),
}
}

Assert(t, len(g.Start()), 1)

testDriverStart = func() (errs []error) { return }
testAdaptorConnect = func() (errs []error) { return }

g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}

testDriverHalt = func() (errs []error) {
return []error{
errors.New("driver halt error 1"),
}
}

testAdaptorFinalize = func() (errs []error) {
return []error{
errors.New("adaptor finalize error 1"),
}
}

Assert(t, len(g.Start()), 2)
}
20 changes: 0 additions & 20 deletions loopback.go

This file was deleted.

40 changes: 0 additions & 40 deletions pinger.go

This file was deleted.

Loading

0 comments on commit be963f9

Please sign in to comment.