From 69d52270b66eb7231566361e5491d7c9f7735d44 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Sat, 5 Nov 2011 12:22:04 +0000 Subject: [PATCH] Convert state tracker to use gomock logging for tests. --- state/channel_test.go | 74 +++++++------- state/mock_tracker.go | 150 +++++++++++++++++++++++++++ state/nick_test.go | 49 +++++---- state/tracker_test.go | 232 +++++++++++++++++++----------------------- 4 files changed, 326 insertions(+), 179 deletions(-) create mode 100644 state/mock_tracker.go diff --git a/state/channel_test.go b/state/channel_test.go index 37e29ce..48c8124 100644 --- a/state/channel_test.go +++ b/state/channel_test.go @@ -1,15 +1,16 @@ package state import ( - "github.com/fluffle/goirc/logging" "testing" ) func TestNewChannel(t *testing.T) { - l, _ := logging.NewMock(t) - ch := NewChannel("#test1", l) + _, s := setUp(t) + defer s.tearDown() - if ch.Name != "#test1" || ch.l != l { + ch := NewChannel("#test1", s.log) + + if ch.Name != "#test1" || ch.l != s.log { t.Errorf("Channel not created correctly by NewChannel()") } if len(ch.nicks) != 0 || len(ch.lookup) != 0 { @@ -18,13 +19,14 @@ func TestNewChannel(t *testing.T) { } func TestAddNick(t *testing.T) { - l, m := logging.NewMock(t) - ch := NewChannel("#test1", l) - nk := NewNick("test1", l) + _, s := setUp(t) + defer s.tearDown() + + ch := NewChannel("#test1", s.log) + nk := NewNick("test1", s.log) cp := new(ChanPrivs) ch.addNick(nk, cp) - m.ExpectNothing() if len(ch.nicks) != 1 || len(ch.lookup) != 1 { t.Errorf("Nick lists not updated correctly for add.") @@ -36,19 +38,23 @@ func TestAddNick(t *testing.T) { t.Errorf("Nick test1 not properly stored in lookup map.") } + s.log.EXPECT().Warn("Channel.addNick(): %s already on %s.", + "test1", "#test1") ch.addNick(nk, cp) - m.ExpectAt(logging.Warn, "Channel.addNick(): test1 already on #test1.") } func TestDelNick(t *testing.T) { - l, m := logging.NewMock(t) - ch := NewChannel("#test1", l) - nk := NewNick("test1", l) + _, s := setUp(t) + defer s.tearDown() + + ch := NewChannel("#test1", s.log) + nk := NewNick("test1", s.log) cp := new(ChanPrivs) // Testing the error state first is easier + s.log.EXPECT().Warn("Channel.delNick(): %s not on %s.", + "test1", "#test1") ch.delNick(nk) - m.ExpectAt(logging.Warn, "Channel.delNick(): test1 not on #test1.") ch.addNick(nk, cp) ch.delNick(nk) @@ -64,12 +70,14 @@ func TestDelNick(t *testing.T) { } func TestChannelParseModes(t *testing.T) { - l, m := logging.NewMock(t) - ch := NewChannel("#test1", l) + _, s := setUp(t) + defer s.tearDown() + + ch := NewChannel("#test1", s.log) md := ch.Modes // Channel modes can adjust channel privs too, so we need a Nick - nk := NewNick("test1", l) + nk := NewNick("test1", s.log) cp := new(ChanPrivs) ch.addNick(nk, cp) @@ -86,7 +94,6 @@ func TestChannelParseModes(t *testing.T) { // Flip some MOAR bits. ch.ParseModes("+s-p+tm-i") - m.ExpectNothing() if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg || !md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { @@ -100,22 +107,20 @@ func TestChannelParseModes(t *testing.T) { // enable limit correctly ch.ParseModes("+l", "256") - m.ExpectNothing() if md.Limit != 256 { t.Errorf("Limit for channel not set correctly") } - // enable limit incorrectly + // enable limit incorrectly. see nick_test.go for why the byte() cast. + s.log.EXPECT().Warn("Channel.ParseModes(): not enough arguments to "+ + "process MODE %s %s%c", "#test1", "+", byte('l')) ch.ParseModes("+l") - m.ExpectAt(logging.Warn, - "Channel.ParseModes(): not enough arguments to process MODE #test1 +l") if md.Limit != 256 { t.Errorf("Bad limit value caused limit to be unset.") } // disable limit correctly ch.ParseModes("-l") - m.ExpectNothing() if md.Limit != 0 { t.Errorf("Limit for channel not unset correctly") } @@ -127,22 +132,20 @@ func TestChannelParseModes(t *testing.T) { // enable key correctly ch.ParseModes("+k", "foobar") - m.ExpectNothing() if md.Key != "foobar" { t.Errorf("Key for channel not set correctly") } // enable key incorrectly + s.log.EXPECT().Warn("Channel.ParseModes(): not enough arguments to "+ + "process MODE %s %s%c", "#test1", "+", byte('k')) ch.ParseModes("+k") - m.ExpectAt(logging.Warn, - "Channel.ParseModes(): not enough arguments to process MODE #test1 +k") if md.Key != "foobar" { t.Errorf("Bad key value caused key to be unset.") } // disable key correctly ch.ParseModes("-k") - m.ExpectNothing() if md.Key != "" { t.Errorf("Key for channel not unset correctly") } @@ -151,25 +154,24 @@ func TestChannelParseModes(t *testing.T) { cp.Op = true cp.HalfOp = true ch.ParseModes("+aq-o", "test1", "test1", "test1") - m.ExpectNothing() if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice { t.Errorf("Channel privileges not flipped correctly by ParseModes.") } + s.log.EXPECT().Warn("Channel.ParseModes(): untracked nick %s "+ + "received MODE on channel %s", "test2", "#test1") ch.ParseModes("+v", "test2") - m.ExpectAt(logging.Warn, - "Channel.ParseModes(): untracked nick test2 received MODE on channel #test1") + s.log.EXPECT().Warn("Channel.ParseModes(): not enough arguments to "+ + "process MODE %s %s%c", "#test1", "-", byte('v')) ch.ParseModes("-v") - m.ExpectAt(logging.Warn, - "Channel.ParseModes(): not enough arguments to process MODE #test1 -v") - + // Test a random mix of modes, just to be sure md.Limit = 256 + s.log.EXPECT().Warn("Channel.ParseModes(): not enough arguments to "+ + "process MODE %s %s%c", "#test1", "-", byte('h')) ch.ParseModes("+zpt-qsl+kv-h", "test1", "foobar", "test1") - m.ExpectAt(logging.Warn, - "Channel.ParseModes(): not enough arguments to process MODE #test1 -h") if !md.Private || md.Secret || !md.ProtectedTopic || !md.NoExternalMsg || !md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly { @@ -182,8 +184,8 @@ func TestChannelParseModes(t *testing.T) { // NOTE: HalfOp not actually unset above thanks to deliberate error. t.Errorf("Channel privileges not flipped correctly by ParseModes (2).") } - + // Finally, check we get an info log for an unrecognised mode character + s.log.EXPECT().Info("Channel.ParseModes(): unknown mode char %c", byte('d')) ch.ParseModes("+d") - m.ExpectAt(logging.Info, "Channel.ParseModes(): unknown mode char d") } diff --git a/state/mock_tracker.go b/state/mock_tracker.go new file mode 100644 index 0000000..b223432 --- /dev/null +++ b/state/mock_tracker.go @@ -0,0 +1,150 @@ +// Automatically generated by MockGen. DO NOT EDIT! +// Source: tracker.go + +package state + +import ( + gomock "gomock.googlecode.com/hg/gomock" +) + +// Mock of StateTracker interface +type MockStateTracker struct { + ctrl *gomock.Controller + recorder *_MockStateTrackerRecorder +} + +// Recorder for MockStateTracker (not exported) +type _MockStateTrackerRecorder struct { + mock *MockStateTracker +} + +func NewMockStateTracker(ctrl *gomock.Controller) *MockStateTracker { + mock := &MockStateTracker{ctrl: ctrl} + mock.recorder = &_MockStateTrackerRecorder{mock} + return mock +} + +func (m *MockStateTracker) EXPECT() *_MockStateTrackerRecorder { + return m.recorder +} + +func (m *MockStateTracker) NewNick(nick string) *Nick { + ret := m.ctrl.Call(m, "NewNick", nick) + ret0, _ := ret[0].(*Nick) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) NewNick(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "NewNick", arg0) +} + +func (m *MockStateTracker) GetNick(nick string) *Nick { + ret := m.ctrl.Call(m, "GetNick", nick) + ret0, _ := ret[0].(*Nick) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) GetNick(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "GetNick", arg0) +} + +func (m *MockStateTracker) ReNick(old string, neu string) { + m.ctrl.Call(m, "ReNick", old, neu) +} + +func (mr *_MockStateTrackerRecorder) ReNick(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "ReNick", arg0, arg1) +} + +func (m *MockStateTracker) DelNick(nick string) { + m.ctrl.Call(m, "DelNick", nick) +} + +func (mr *_MockStateTrackerRecorder) DelNick(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "DelNick", arg0) +} + +func (m *MockStateTracker) NewChannel(channel string) *Channel { + ret := m.ctrl.Call(m, "NewChannel", channel) + ret0, _ := ret[0].(*Channel) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) NewChannel(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "NewChannel", arg0) +} + +func (m *MockStateTracker) GetChannel(channel string) *Channel { + ret := m.ctrl.Call(m, "GetChannel", channel) + ret0, _ := ret[0].(*Channel) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) GetChannel(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "GetChannel", arg0) +} + +func (m *MockStateTracker) DelChannel(channel string) { + m.ctrl.Call(m, "DelChannel", channel) +} + +func (mr *_MockStateTrackerRecorder) DelChannel(arg0 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "DelChannel", arg0) +} + +func (m *MockStateTracker) Me() *Nick { + ret := m.ctrl.Call(m, "Me") + ret0, _ := ret[0].(*Nick) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) Me() *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "Me") +} + +func (m *MockStateTracker) IsOn(channel string, nick string) (*ChanPrivs, bool) { + ret := m.ctrl.Call(m, "IsOn", channel, nick) + ret0, _ := ret[0].(*ChanPrivs) + ret1, _ := ret[1].(bool) + return ret0, ret1 +} + +func (mr *_MockStateTrackerRecorder) IsOn(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "IsOn", arg0, arg1) +} + +func (m *MockStateTracker) Associate(channel *Channel, nick *Nick) *ChanPrivs { + ret := m.ctrl.Call(m, "Associate", channel, nick) + ret0, _ := ret[0].(*ChanPrivs) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) Associate(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "Associate", arg0, arg1) +} + +func (m *MockStateTracker) Dissociate(channel *Channel, nick *Nick) { + m.ctrl.Call(m, "Dissociate", channel, nick) +} + +func (mr *_MockStateTrackerRecorder) Dissociate(arg0, arg1 interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "Dissociate", arg0, arg1) +} + +func (m *MockStateTracker) Wipe() { + m.ctrl.Call(m, "Wipe") +} + +func (mr *_MockStateTrackerRecorder) Wipe() *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "Wipe") +} + +func (m *MockStateTracker) String() string { + ret := m.ctrl.Call(m, "String") + ret0, _ := ret[0].(string) + return ret0 +} + +func (mr *_MockStateTrackerRecorder) String() *gomock.Call { + return mr.mock.ctrl.RecordCall(mr.mock, "String") +} diff --git a/state/nick_test.go b/state/nick_test.go index 1f0e672..283f908 100644 --- a/state/nick_test.go +++ b/state/nick_test.go @@ -1,15 +1,16 @@ package state import ( - "github.com/fluffle/goirc/logging" "testing" ) func TestNewNick(t *testing.T) { - l, _ := logging.NewMock(t) - nk := NewNick("test1", l) + _, s := setUp(t) + defer s.tearDown() - if nk.Nick != "test1" || nk.l != l { + nk := NewNick("test1", s.log) + + if nk.Nick != "test1" || nk.l != s.log { t.Errorf("Nick not created correctly by NewNick()") } if len(nk.chans) != 0 || len(nk.lookup) != 0 { @@ -18,13 +19,14 @@ func TestNewNick(t *testing.T) { } func TestAddChannel(t *testing.T) { - l, m := logging.NewMock(t) - nk := NewNick("test1", l) - ch := NewChannel("#test1", l) + _, s := setUp(t) + defer s.tearDown() + + nk := NewNick("test1", s.log) + ch := NewChannel("#test1", s.log) cp := new(ChanPrivs) nk.addChannel(ch, cp) - m.ExpectNothing() if len(nk.chans) != 1 || len(nk.lookup) != 1 { t.Errorf("Channel lists not updated correctly for add.") @@ -36,19 +38,22 @@ func TestAddChannel(t *testing.T) { t.Errorf("Channel #test1 not properly stored in lookup map.") } + s.log.EXPECT().Warn("Nick.addChannel(): %s already on %s.", + "test1", "#test1") nk.addChannel(ch, cp) - m.ExpectAt(logging.Warn, "Nick.addChannel(): test1 already on #test1.") } func TestDelChannel(t *testing.T) { - l, m := logging.NewMock(t) - nk := NewNick("test1", l) - ch := NewChannel("#test1", l) + _, s := setUp(t) + defer s.tearDown() + + nk := NewNick("test1", s.log) + ch := NewChannel("#test1", s.log) cp := new(ChanPrivs) // Testing the error state first is easier + s.log.EXPECT().Warn("Nick.delChannel(): %s not on %s.", "test1", "#test1") nk.delChannel(ch) - m.ExpectAt(logging.Warn, "Nick.delChannel(): test1 not on #test1.") nk.addChannel(ch, cp) nk.delChannel(ch) @@ -64,8 +69,10 @@ func TestDelChannel(t *testing.T) { } func TestNickParseModes(t *testing.T) { - l, m := logging.NewMock(t) - nk := NewNick("test1", l) + _, s := setUp(t) + defer s.tearDown() + + nk := NewNick("test1", s.log) md := nk.Modes // Modes should all be false for a new nick @@ -79,13 +86,21 @@ func TestNickParseModes(t *testing.T) { // Parse a mode line that flips one true to false and two false to true nk.ParseModes("+z-x+w") - m.ExpectNothing() if !md.Invisible || md.Oper || !md.WallOps || md.HiddenHost || !md.SSL { t.Errorf("Modes not flipped correctly by ParseModes.") } // Check that passing an unknown mode char results in an info log + // The cast to byte here is needed to pass; gomock uses reflect.DeepEqual + // to examine argument equality, but 'd' (when not implicitly coerced to a + // uint8 by the type system) is an int, whereas string("+d")[1] is not. + // This type difference (despite the values being nominally the same) + // causes the test to fail with the following confusing error. + // + // no matching expected call: *logging.MockLogger.Info([Nick.ParseModes(): unknown mode char %c [100]]) + // missing call(s) to *logging.MockLogger.Info(is equal to Nick.ParseModes(): unknown mode char %c, is equal to [100]) + + s.log.EXPECT().Info("Nick.ParseModes(): unknown mode char %c", byte('d')) nk.ParseModes("+d") - m.ExpectAt(logging.Info, "Nick.ParseModes(): unknown mode char d") } diff --git a/state/tracker_test.go b/state/tracker_test.go index 8591e4b..d60da5f 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -2,16 +2,30 @@ package state import ( "github.com/fluffle/goirc/logging" + "gomock.googlecode.com/hg/gomock" "testing" ) +type testState struct { + ctrl *gomock.Controller + log *logging.MockLogger +} + +func setUp(t *testing.T) (*stateTracker, *testState) { + ctrl := gomock.NewController(t) + log := logging.NewMockLogger(ctrl) + return NewTracker("mynick", log), &testState{ctrl, log} +} + +func (s *testState) tearDown() { + s.ctrl.Finish() +} + func TestSTNewTracker(t *testing.T) { - l, m := logging.NewMock(t) + st, s := setUp(t) + defer s.tearDown() - st := NewTracker("mynick", l) - m.ExpectNothing() - - if st.l != l { + if st.l != s.log { t.Errorf("State tracker's logger not set correctly.") } if len(st.nicks) != 1 { @@ -26,31 +40,29 @@ func TestSTNewTracker(t *testing.T) { } func TestSTNewNick(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() test1 := st.NewNick("test1") - m.ExpectNothing() - if test1 == nil || test1.Nick != "test1" || test1.l != l { + if test1 == nil || test1.Nick != "test1" || test1.l != s.log { t.Errorf("Nick object created incorrectly by NewNick.") } if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 { t.Errorf("Nick object stored incorrectly by NewNick.") } + s.log.EXPECT().Warn("StateTracker.NewNick(): %s already tracked.", "test1") if fail := st.NewNick("test1"); fail != nil { t.Errorf("Creating duplicate nick did not produce nil return.") } - m.ExpectAt(logging.Warn, "StateTracker.NewNick(): test1 already tracked.") } func TestSTGetNick(t *testing.T) { - l, _ := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() - test1 := NewNick("test1", l) - st.nicks["test1"] = test1 + test1 := st.NewNick("test1") if n := st.GetNick("test1"); n != test1 { t.Errorf("Incorrect nick returned by GetNick.") @@ -64,20 +76,16 @@ func TestSTGetNick(t *testing.T) { } func TestSTReNick(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() - test1 := NewNick("test1", l) - st.nicks["test1"] = test1 + test1 := st.NewNick("test1") // This channel is here to ensure that its lookup map gets updated - cp := new(ChanPrivs) - chan1 := NewChannel("#chan1", l) - test1.addChannel(chan1, cp) - chan1.addNick(test1, cp) + chan1 := st.NewChannel("#chan1") + st.Associate(chan1, test1) st.ReNick("test1", "test2") - m.ExpectNothing() if _, ok := st.nicks["test1"]; ok { t.Errorf("Nick test1 still exists after ReNick.") @@ -98,11 +106,10 @@ func TestSTReNick(t *testing.T) { t.Errorf("Nick list changed size during ReNick.") } - test2 := NewNick("test1", l) - st.nicks["test1"] = test2 + test2 := st.NewNick("test1") + s.log.EXPECT().Warn("StateTracker.ReNick(): %s already exists.", "test2") st.ReNick("test1", "test2") - m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test2 already exists.") if n, ok := st.nicks["test2"]; !ok || n != test1 { t.Errorf("Nick test2 overwritten/deleted by ReNick.") @@ -114,19 +121,16 @@ func TestSTReNick(t *testing.T) { t.Errorf("Nick list changed size during ReNick.") } + s.log.EXPECT().Warn("StateTracker.ReNick(): %s not tracked.", "test3") st.ReNick("test3", "test2") - m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test3 not tracked.") } func TestSTDelNick(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) - - nick1 := NewNick("test1", l) - st.nicks["test1"] = nick1 + st, s := setUp(t) + defer s.tearDown() + st.NewNick("test1") st.DelNick("test1") - m.ExpectNothing() if _, ok := st.nicks["test1"]; ok { t.Errorf("Nick test1 still exists after DelNick.") @@ -135,18 +139,19 @@ func TestSTDelNick(t *testing.T) { t.Errorf("Nick list still contains nicks after DelNick.") } - st.nicks["test1"] = nick1 + // Deleting unknown nick shouldn't work, but let's make sure we have a + // known nick first to catch any possible accidental removals. + nick1 := st.NewNick("test1") + s.log.EXPECT().Warn("StateTracker.DelNick(): %s not tracked.", "test2") st.DelNick("test2") - m.ExpectAt(logging.Warn, "StateTracker.DelNick(): test2 not tracked.") - if len(st.nicks) != 2 { t.Errorf("Deleting unknown nick had unexpected side-effects.") } // Deleting my nick shouldn't work + s.log.EXPECT().Warn("StateTracker.DelNick(): won't delete myself.") st.DelNick("mynick") - m.ExpectAt(logging.Warn, "StateTracker.DelNick(): won't delete myself.") if len(st.nicks) != 2 { t.Errorf("Deleting myself had unexpected side-effects.") @@ -155,19 +160,14 @@ func TestSTDelNick(t *testing.T) { // Test that deletion correctly dissociates nick from channels. // NOTE: the two error states in delNick (as opposed to DelNick) // are not tested for here, as they will only arise from programming - // errors in other methods. Using m.CheckNothingWritten() while - // testing these methods will catch those errors should they occur. + // errors in other methods. The mock logger should catch these. - // Create a new channel for testing purposes - chan1 := NewChannel("#test1", l) - st.chans["#test1"] = chan1 + // Create a new channel for testing purposes. + chan1 := st.NewChannel("#test1") // Associate both "my" nick and test1 with the channel - p := new(ChanPrivs) - chan1.addNick(st.me, p) - st.me.addChannel(chan1, p) - chan1.addNick(nick1, p) - nick1.addChannel(chan1, p) + st.Associate(chan1, st.me) + st.Associate(chan1, nick1) // Test we have the expected starting state (at least vaguely) if len(chan1.nicks) != 2 || len(st.nicks) != 2 || @@ -176,7 +176,6 @@ func TestSTDelNick(t *testing.T) { } st.DelNick("test1") - m.ExpectNothing() // Actual deletion tested above... if len(chan1.nicks) != 1 || len(st.chans) != 1 || @@ -193,35 +192,33 @@ func TestSTDelNick(t *testing.T) { } func TestSTNewChannel(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() if len(st.chans) != 0 { t.Errorf("Channel list of new tracker is non-zero length.") } test1 := st.NewChannel("#test1") - m.ExpectNothing() - if test1 == nil || test1.Name != "#test1" || test1.l != l { + if test1 == nil || test1.Name != "#test1" || test1.l != s.log { t.Errorf("Channel object created incorrectly by NewChannel.") } if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 { t.Errorf("Channel object stored incorrectly by NewChannel.") } + s.log.EXPECT().Warn("StateTracker.NewChannel(): %s already tracked.", "#test1") if fail := st.NewChannel("#test1"); fail != nil { t.Errorf("Creating duplicate chan did not produce nil return.") } - m.ExpectAt(logging.Warn, "StateTracker.NewChannel(): #test1 already tracked.") } func TestSTGetChannel(t *testing.T) { - l, _ := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() - test1 := NewChannel("#test1", l) - st.chans["#test1"] = test1 + test1 := st.NewChannel("#test1") if c := st.GetChannel("#test1"); c != test1 { t.Errorf("Incorrect Channel returned by GetChannel.") @@ -235,14 +232,11 @@ func TestSTGetChannel(t *testing.T) { } func TestSTDelChannel(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) - - chan1 := NewChannel("#test1", l) - st.chans["#test1"] = chan1 + st, s := setUp(t) + defer s.tearDown() + st.NewChannel("#test1") st.DelChannel("#test1") - m.ExpectNothing() if _, ok := st.chans["#test1"]; ok { t.Errorf("Channel test1 still exists after DelChannel.") @@ -251,34 +245,28 @@ func TestSTDelChannel(t *testing.T) { t.Errorf("Channel list still contains chans after DelChannel.") } - st.chans["#test1"] = chan1 + // Deleting unknown nick shouldn't work, but let's make sure we have a + // known nick first to catch any possible accidental removals. + chan1 := st.NewChannel("#test1") + s.log.EXPECT().Warn("StateTracker.DelChannel(): %s not tracked.", "#test2") st.DelChannel("#test2") - m.ExpectAt(logging.Warn, "StateTracker.DelChannel(): #test2 not tracked.") if len(st.chans) != 1 { t.Errorf("DelChannel had unexpected side-effects.") } // Test that deletion correctly dissociates channel from tracked nicks. - // In order to test this thoroughly we need two channels (so that delNick() // is not called internally in delChannel() when len(nick1.chans) == 0. - chan2 := NewChannel("#test2", l) - st.chans["#test2"] = chan2 - nick1 := NewNick("test1", l) - st.nicks["test1"] = nick1 + chan2 := st.NewChannel("#test2") + nick1 := st.NewNick("test1") // Associate both "my" nick and test1 with the channels - p := new(ChanPrivs) - chan1.addNick(st.me, p) - st.me.addChannel(chan1, p) - chan1.addNick(nick1, p) - nick1.addChannel(chan1, p) - chan2.addNick(st.me, p) - st.me.addChannel(chan2, p) - chan2.addNick(nick1, p) - nick1.addChannel(chan2, p) + st.Associate(chan1, st.me) + st.Associate(chan1, nick1) + st.Associate(chan2, st.me) + st.Associate(chan2, nick1) // Test we have the expected starting state (at least vaguely) if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || @@ -287,7 +275,6 @@ func TestSTDelChannel(t *testing.T) { } st.DelChannel("#test1") - m.ExpectNothing() // Test intermediate state. We're still on #test2 with test1, so test1 // shouldn't be deleted from state tracking itself just yet. @@ -295,7 +282,6 @@ func TestSTDelChannel(t *testing.T) { len(st.me.chans) != 1 || len(nick1.chans) != 1 || len(st.chans) != 1 { t.Errorf("Deleting channel didn't dissociate correctly from nicks.") } - if _, ok := nick1.chans[chan1]; ok { t.Errorf("Channel not removed from nick's chans map.") } @@ -304,7 +290,6 @@ func TestSTDelChannel(t *testing.T) { } st.DelChannel("#test2") - m.ExpectNothing() // Test final state. Deleting #test2 means that we're no longer on any // common channels with test1, and thus it should be removed from tracking. @@ -312,7 +297,6 @@ func TestSTDelChannel(t *testing.T) { len(st.me.chans) != 0 || len(nick1.chans) != 0 || len(st.chans) != 0 { t.Errorf("Deleting last channel didn't dissociate correctly from nicks.") } - if _, ok := st.nicks["test1"]; ok { t.Errorf("Nick not deleted correctly when on no channels.") } @@ -322,63 +306,63 @@ func TestSTDelChannel(t *testing.T) { } func TestSTIsOn(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() - nick1 := NewNick("test1", l) - st.nicks["test1"] = nick1 - chan1 := NewChannel("#test1", l) - st.chans["#test1"] = chan1 + nick1 := st.NewNick("test1") + chan1 := st.NewChannel("#test1") if priv, ok := st.IsOn("#test1", "test1"); ok || priv != nil { t.Errorf("test1 is not on #test1 (yet)") } - cp := new(ChanPrivs) - chan1.addNick(nick1, cp) - nick1.addChannel(chan1, cp) + cp := st.Associate(chan1, nick1) if priv, ok := st.IsOn("#test1", "test1"); !ok || priv != cp { t.Errorf("test1 is on #test1 (now)") } - m.ExpectNothing() } func TestSTAssociate(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() nick1 := st.NewNick("test1") chan1 := st.NewChannel("#test1") cp := st.Associate(chan1, nick1) - m.ExpectNothing() - if priv, ok := st.IsOn("#test1", "test1"); !ok || cp != priv { + if priv, ok := nick1.chans[chan1]; !ok || cp != priv { + t.Errorf("#test1 was not associated with test1.") + } + if priv, ok := chan1.nicks[nick1]; !ok || cp != priv { t.Errorf("test1 was not associated with #test1.") } // Test error cases + s.log.EXPECT().Error("StateTracker.Associate(): passed nil values :-(") st.Associate(nil, nick1) - m.ExpectAt(logging.Error, "StateTracker.Associate(): passed nil values :-(") + s.log.EXPECT().Error("StateTracker.Associate(): passed nil values :-(") st.Associate(chan1, nil) - m.ExpectAt(logging.Error, "StateTracker.Associate(): passed nil values :-(") + s.log.EXPECT().Warn("StateTracker.Associate(): %s already on %s.", + "test1", "#test1") st.Associate(chan1, nick1) - m.ExpectAt(logging.Warn, "StateTracker.Associate(): test1 already on #test1.") - nick2 := NewNick("test2", l) + // nick2 deliberately created directly here. + nick2 := NewNick("test2", s.log) + s.log.EXPECT().Error("StateTracker.Associate(): nick %s not found in "+ + "(or differs from) internal state.", "test2") st.Associate(chan1, nick2) - m.ExpectAt(logging.Error, "StateTracker.Associate(): nick test2 not found "+ - "in (or differs from) internal state.") - chan2 := NewChannel("#test2", l) + // chan2 deliberately created directly here. + chan2 := NewChannel("#test2", s.log) + s.log.EXPECT().Error("StateTracker.Associate(): channel %s not found in "+ + "(or differs from) internal state.", "#test2") st.Associate(chan2, nick1) - m.ExpectAt(logging.Error, "StateTracker.Associate(): channel #test2 not "+ - "found in (or differs from) internal state.") } func TestSTDissociate(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() nick1 := st.NewNick("test1") chan1 := st.NewChannel("#test1") @@ -398,7 +382,6 @@ func TestSTDissociate(t *testing.T) { // First, test the case of me leaving #test2 st.Dissociate(chan2, st.me) - m.ExpectNothing() // This should have resulted in the complete deletion of the channel. if len(chan1.nicks) != 2 || len(chan2.nicks) != 0 || len(st.nicks) != 2 || @@ -410,7 +393,6 @@ func TestSTDissociate(t *testing.T) { chan2 = st.NewChannel("#test2") st.Associate(chan2, st.me) st.Associate(chan2, nick1) - m.ExpectNothing() // Check state once moar. if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || @@ -421,7 +403,6 @@ func TestSTDissociate(t *testing.T) { // Now, lets dissociate test1 from #test1 then #test2. // This first one should only result in a change in associations. st.Dissociate(chan1, nick1) - m.ExpectNothing() if len(chan1.nicks) != 1 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || len(st.me.chans) != 2 || len(nick1.chans) != 1 || len(st.chans) != 2 { @@ -431,7 +412,6 @@ func TestSTDissociate(t *testing.T) { // This second one should also delete test1 // as it's no longer on any common channels with us st.Dissociate(chan2, nick1) - m.ExpectNothing() if len(chan1.nicks) != 1 || len(chan2.nicks) != 1 || len(st.nicks) != 1 || len(st.me.chans) != 2 || len(nick1.chans) != 0 || len(st.chans) != 2 { @@ -441,29 +421,32 @@ func TestSTDissociate(t *testing.T) { // Check error cases // test1 was deleted above, so "re-track" it for this test. nick1 = st.NewNick("test1") + s.log.EXPECT().Warn("StateTracker.Dissociate(): %s not on %s.", + "test1", "#test1") st.Dissociate(chan1, nick1) - m.ExpectAt(logging.Warn, "StateTracker.Dissociate(): test1 not on #test1.") + s.log.EXPECT().Error("StateTracker.Dissociate(): passed nil values :-(") st.Dissociate(chan1, nil) - m.ExpectAt(logging.Error, "StateTracker.Dissociate(): passed nil values :-(") + s.log.EXPECT().Error("StateTracker.Dissociate(): passed nil values :-(") st.Dissociate(nil, nick1) - m.ExpectAt(logging.Error, "StateTracker.Dissociate(): passed nil values :-(") - nick3 := NewNick("test3", l) + // nick3 deliberately created directly here. + nick3 := NewNick("test3", s.log) + s.log.EXPECT().Error("StateTracker.Dissociate(): nick %s not found in "+ + "(or differs from) internal state.", "test3") st.Dissociate(chan1, nick3) - m.ExpectAt(logging.Error, "StateTracker.Dissociate(): nick test3 not "+ - "found in (or differs from) internal state.") - chan3 := NewChannel("#test3", l) + // chan3 deliberately created directly here. + chan3 := NewChannel("#test3", s.log) + s.log.EXPECT().Error("StateTracker.Dissociate(): channel %s not found in "+ + "(or differs from) internal state.", "#test3") st.Dissociate(chan3, nick1) - m.ExpectAt(logging.Error, "StateTracker.Dissociate(): channel #test3 not "+ - "found in (or differs from) internal state.") } func TestSTWipe(t *testing.T) { - l, m := logging.NewMock(t) - st := NewTracker("mynick", l) + st, s := setUp(t) + defer s.tearDown() nick1 := st.NewNick("test1") nick2 := st.NewNick("test2") @@ -487,8 +470,6 @@ func TestSTWipe(t *testing.T) { st.Associate(chan1, nick3) - m.ExpectNothing() - // Check the state we have at this point is what we would expect. if len(st.nicks) != 4 || len(st.chans) != 3 || len(st.me.chans) != 3 { t.Errorf("Tracker nick/channel lists wrong length before wipe.") @@ -502,7 +483,6 @@ func TestSTWipe(t *testing.T) { // Nuke *all* the state! st.Wipe() - m.ExpectNothing() // Check the state we have at this point is what we would expect. if len(st.nicks) != 1 || len(st.chans) != 0 || len(st.me.chans) != 0 {