diff options
Diffstat (limited to 'goctl_test.go')
-rw-r--r-- | goctl_test.go | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/goctl_test.go b/goctl_test.go index a1e4711..a4a1041 100644 --- a/goctl_test.go +++ b/goctl_test.go @@ -9,6 +9,23 @@ import ( "testing" ) +type testHandler struct { + name string + fn func([]string) string +} + +func makeHandler(name string, fn func(_ []string) string) testHandler { + return testHandler{name: name, fn: fn} +} + +func (th testHandler) Name() string { + return th.name +} + +func (th testHandler) Run(args []string) string { + return th.fn(args) +} + var sockpath string func init() { @@ -98,12 +115,12 @@ func TestAddHandler(t *testing.T) { gc := start(t) defer gc.Stop() - gc.AddHandler("foo", func(args []string) string { + gc.AddHandler(makeHandler("foo", func(args []string) string { if !reflect.DeepEqual(args, []string{"bar", "baz"}) { t.Errorf("Got %v, expected ['bar', 'baz']", args) } return "bar baz" - }) + })) c := dial(t) defer c.Close() @@ -121,19 +138,20 @@ func TestAddHandlers(t *testing.T) { gc := start(t) defer gc.Stop() - gc.AddHandlers([]*Handler{ - {"foo", func(args []string) string { + gc.AddHandlers([]Handler{ + makeHandler("foo", func(args []string) string { if !reflect.DeepEqual(args, []string{"bar", "baz"}) { t.Errorf("Got %v, expected ['bar', 'baz']", args) } return "" - }}, - {"bar", func(args []string) string { + }), + makeHandler("bar", func(args []string) string { if !reflect.DeepEqual(args, []string{"baz", "pham"}) { t.Errorf("Got %v, expected ['baz', 'pham']", args) } return "wauug" - }}}) + }), + }) c := dial(t) defer c.Close() @@ -158,15 +176,15 @@ func TestCannotOverrideExtantHandlers(t *testing.T) { gc := start(t) defer gc.Stop() - err := gc.AddHandler("ping", func(args []string) string { + err := gc.AddHandler(makeHandler("ping", func(args []string) string { return "gnip" - }) + })) if err != HandlerExists { t.Error("Was able to override built-in ping handler.") } - err = gc.AddHandlers([]*Handler{ - {"foo", func(args []string) string { return "foo" }}, - {"foo", func(args []string) string { return "foo" }}, + err = gc.AddHandlers([]Handler{ + makeHandler("foo", func(args []string) string { return "foo" }), + makeHandler("foo", func(args []string) string { return "foo" }), }) if err != HandlerExists { t.Error("Was able to assign two handlers for 'foo'.") |