summaryrefslogtreecommitdiffstats
path: root/goctl_test.go
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2015-07-10 19:42:00 +0000
committerBrian Cully <bjc@kublai.com>2015-07-10 19:42:00 +0000
commitf38c11939e83895411dfc0d1c79bca172c56f3fc (patch)
treefdbf97491ced825befcaed7c824cd8d3f5331f2d /goctl_test.go
parent2fd292d5f480fd51cdbbff4821b6f09940a005a9 (diff)
downloadgoctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.tar.gz
goctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.zip
Pass Goctl object into handlers.
Diffstat (limited to 'goctl_test.go')
-rw-r--r--goctl_test.go27
1 files changed, 15 insertions, 12 deletions
diff --git a/goctl_test.go b/goctl_test.go
index a4a1041..7e30f7e 100644
--- a/goctl_test.go
+++ b/goctl_test.go
@@ -11,10 +11,10 @@ import (
type testHandler struct {
name string
- fn func([]string) string
+ fn func(*Goctl, []string) string
}
-func makeHandler(name string, fn func(_ []string) string) testHandler {
+func makeHandler(name string, fn func(_ *Goctl, _ []string) string) testHandler {
return testHandler{name: name, fn: fn}
}
@@ -22,8 +22,8 @@ func (th testHandler) Name() string {
return th.name
}
-func (th testHandler) Run(args []string) string {
- return th.fn(args)
+func (th testHandler) Run(gc *Goctl, args []string) string {
+ return th.fn(gc, args)
}
var sockpath string
@@ -39,12 +39,12 @@ func init() {
}
}
-func start(t testing.TB) Goctl {
+func start(t testing.TB) *Goctl {
gc := NewGoctl(sockpath)
if err := gc.Start(); err != nil {
t.Fatalf("Couldn't start: %s.", err)
}
- return gc
+ return &gc
}
func dial(t testing.TB) net.Conn {
@@ -115,7 +115,10 @@ func TestAddHandler(t *testing.T) {
gc := start(t)
defer gc.Stop()
- gc.AddHandler(makeHandler("foo", func(args []string) string {
+ gc.AddHandler(makeHandler("foo", func(innerGC *Goctl, args []string) string {
+ if innerGC != gc {
+ t.Errorf("Goctl object not passed into handler properly (got: %p, want: %p).", innerGC, gc)
+ }
if !reflect.DeepEqual(args, []string{"bar", "baz"}) {
t.Errorf("Got %v, expected ['bar', 'baz']", args)
}
@@ -139,13 +142,13 @@ func TestAddHandlers(t *testing.T) {
defer gc.Stop()
gc.AddHandlers([]Handler{
- makeHandler("foo", func(args []string) string {
+ makeHandler("foo", func(_ *Goctl, args []string) string {
if !reflect.DeepEqual(args, []string{"bar", "baz"}) {
t.Errorf("Got %v, expected ['bar', 'baz']", args)
}
return ""
}),
- makeHandler("bar", func(args []string) string {
+ makeHandler("bar", func(_ *Goctl, args []string) string {
if !reflect.DeepEqual(args, []string{"baz", "pham"}) {
t.Errorf("Got %v, expected ['baz', 'pham']", args)
}
@@ -176,15 +179,15 @@ func TestCannotOverrideExtantHandlers(t *testing.T) {
gc := start(t)
defer gc.Stop()
- err := gc.AddHandler(makeHandler("ping", func(args []string) string {
+ err := gc.AddHandler(makeHandler("ping", func(_ *Goctl, args []string) string {
return "gnip"
}))
if err != HandlerExists {
t.Error("Was able to override built-in ping handler.")
}
err = gc.AddHandlers([]Handler{
- makeHandler("foo", func(args []string) string { return "foo" }),
- makeHandler("foo", func(args []string) string { return "foo" }),
+ makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
+ makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
})
if err != HandlerExists {
t.Error("Was able to assign two handlers for 'foo'.")