From 5ff77fc2f9fff20f473a20174873cde56c115686 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Tue, 23 Aug 2011 10:49:22 +0100 Subject: [PATCH] Add mock dispatcher factory that tests an event fired; use it in Test001(). --- client/connection_test.go | 13 ++++++++++++- client/handlers_test.go | 10 +++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/client/connection_test.go b/client/connection_test.go index 03efd05..3b14be2 100644 --- a/client/connection_test.go +++ b/client/connection_test.go @@ -1,6 +1,9 @@ package client -import "testing" +import ( + "strings" + "testing" +) func setUp(t *testing.T) (*mockNetConn, *Conn) { c := New("test", "test", "Testing IRC") @@ -17,3 +20,11 @@ type mockDispatcher func(string, ...interface{}) func (d mockDispatcher) Dispatch(name string, ev ...interface{}) { d(name, ev...) } + +func WasEventDispatched(name string, flag *bool) mockDispatcher { + return mockDispatcher(func(n string, ev ...interface{}) { + if n == strings.ToLower(name) { + *flag = true + } + }) +} diff --git a/client/handlers_test.go b/client/handlers_test.go index 149d7ae..de5a30a 100644 --- a/client/handlers_test.go +++ b/client/handlers_test.go @@ -20,12 +20,8 @@ func Test001(t *testing.T) { _, c := setUp(t) // Setup a mock event dispatcher to test correct triggering of "connected" - connected := false - c.Dispatcher = mockDispatcher(func(name string, ev ...interface{}) { - if name == "connected" { - connected = true - } - }) + flag := false + c.Dispatcher = WasEventDispatched("connected", &flag) // Assert that the "Host" field of c.Me hasn't been set yet if c.Me.Host != "" { @@ -36,7 +32,7 @@ func Test001(t *testing.T) { c.h_001(parseLine(":irc.server.org 001 test :Welcome to IRC test!ident@somehost.com")) // Check that the event was dispatched correctly - if !connected { + if !flag { t.Errorf("Sending 001 didn't result in dispatch of connected event.") }