Skip to content

Commit

Permalink
Add more driver and adaptor test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jul 17, 2014
1 parent 84363c6 commit 988bce8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
1 change: 0 additions & 1 deletion adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewAdaptor(name string, adaptorType string, v ...interface{}) *Adaptor {
switch v[i].(type) {
case string:
a.port = v[i].(string)
default:
}
}

Expand Down
14 changes: 14 additions & 0 deletions adaptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gobot

import "testing"

func TestAdaptor(t *testing.T) {
a := NewAdaptor("", "testBot", "/dev/null")
Refute(t, a.Name(), "")
a.SetPort("/dev/null1")
Assert(t, a.Port(), "/dev/null1")
a.SetName("myAdaptor")
Assert(t, a.Name(), "myAdaptor")
a.SetConnected(true)
Assert(t, a.Connected(), true)
}
2 changes: 0 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func NewDriver(name string, driverType string, v ...interface{}) *Driver {
d.adaptor = v[i].(AdaptorInterface)
case time.Duration:
d.interval = v[i].(time.Duration)
default:
fmt.Println("Unknown argument passed to NewDriver")
}
}

Expand Down
57 changes: 57 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gobot

import (
"fmt"
"testing"
"time"
)

func TestDriver(t *testing.T) {
a := NewTestAdaptor("testAdaptor")
d := NewDriver("",
"testDriver",
a,
"1",
5*time.Second,
)

Refute(t, d.Name(), "")
Assert(t, d.Type(), "testDriver")
Assert(t, d.Interval(), 5*time.Second)
Assert(t, d.Pin(), "1")
Assert(t, d.Adaptor(), a)

d.SetPin("10")
Assert(t, d.Pin(), "10")

d.SetName("myDriver")
Assert(t, d.Name(), "myDriver")

d.SetInterval(100 * time.Second)
Assert(t, d.Interval(), 100*time.Second)

Assert(t, len(d.Commands()), 0)
d.AddCommand("cmd1", func(params map[string]interface{}) interface{} {
return fmt.Sprintf("hello from %v", params["name"])
})
Assert(t, len(d.Commands()), 1)
Assert(t,
d.Command("cmd1")(map[string]interface{}{"name": d.Name()}).(string),
"hello from "+d.Name(),
)

Assert(t, len(d.Events()), 0)
d.AddEvent("event1")
Assert(t, len(d.Events()), 1)
Refute(t, d.Event("event1"), nil)

defer func() {
r := recover()
if r != nil {
Assert(t, "Unknown Driver Event: event2", r)
} else {
t.Errorf("Did not return Unknown Event error")
}
}()
d.Event("event2")
}
15 changes: 6 additions & 9 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"time"
)

func logFailure(t *testing.T, a interface{}, b interface{}) {
func logFailure(t *testing.T, message string) {
_, file, line, _ := runtime.Caller(2)
s := strings.Split(file, "/")
t.Errorf("%v:%v Got %v - type %v, Asserted %v - type %v",
s[len(s)-1], line, a, reflect.TypeOf(a), b, reflect.TypeOf(b))
t.Errorf("%v:%v: %v", s[len(s)-1], line, message)
}
func Assert(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
logFailure(t, a, b)
logFailure(t, fmt.Sprintf("%v - \"%v\", should equal, %v - \"%v\"",
a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
}
}

func Refute(t *testing.T, a interface{}, b interface{}) {
if reflect.DeepEqual(a, b) {
logFailure(t, a, b)
logFailure(t, fmt.Sprintf("%v - \"%v\", should not equal, %v - \"%v\"",
a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
}
}

Expand Down Expand Up @@ -102,10 +103,6 @@ func NewTestAdaptor(name string) *testAdaptor {
name,
"TestAdaptor",
"/dev/null",
map[string]interface{}{
"param1": "1",
"param2": 2,
},
),
}
}
Expand Down

0 comments on commit 988bce8

Please sign in to comment.