Convert state tracker to use gomock logging for tests.

This commit is contained in:
Alex Bramley 2011-11-05 12:22:04 +00:00
parent d0f0175d7f
commit 69d52270b6
4 changed files with 326 additions and 179 deletions

View File

@ -1,15 +1,16 @@
package state package state
import ( import (
"github.com/fluffle/goirc/logging"
"testing" "testing"
) )
func TestNewChannel(t *testing.T) { func TestNewChannel(t *testing.T) {
l, _ := logging.NewMock(t) _, s := setUp(t)
ch := NewChannel("#test1", l) 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()") t.Errorf("Channel not created correctly by NewChannel()")
} }
if len(ch.nicks) != 0 || len(ch.lookup) != 0 { if len(ch.nicks) != 0 || len(ch.lookup) != 0 {
@ -18,13 +19,14 @@ func TestNewChannel(t *testing.T) {
} }
func TestAddNick(t *testing.T) { func TestAddNick(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
ch := NewChannel("#test1", l) defer s.tearDown()
nk := NewNick("test1", l)
ch := NewChannel("#test1", s.log)
nk := NewNick("test1", s.log)
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
m.ExpectNothing()
if len(ch.nicks) != 1 || len(ch.lookup) != 1 { if len(ch.nicks) != 1 || len(ch.lookup) != 1 {
t.Errorf("Nick lists not updated correctly for add.") 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.") 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) ch.addNick(nk, cp)
m.ExpectAt(logging.Warn, "Channel.addNick(): test1 already on #test1.")
} }
func TestDelNick(t *testing.T) { func TestDelNick(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
ch := NewChannel("#test1", l) defer s.tearDown()
nk := NewNick("test1", l)
ch := NewChannel("#test1", s.log)
nk := NewNick("test1", s.log)
cp := new(ChanPrivs) cp := new(ChanPrivs)
// Testing the error state first is easier // Testing the error state first is easier
s.log.EXPECT().Warn("Channel.delNick(): %s not on %s.",
"test1", "#test1")
ch.delNick(nk) ch.delNick(nk)
m.ExpectAt(logging.Warn, "Channel.delNick(): test1 not on #test1.")
ch.addNick(nk, cp) ch.addNick(nk, cp)
ch.delNick(nk) ch.delNick(nk)
@ -64,12 +70,14 @@ func TestDelNick(t *testing.T) {
} }
func TestChannelParseModes(t *testing.T) { func TestChannelParseModes(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
ch := NewChannel("#test1", l) defer s.tearDown()
ch := NewChannel("#test1", s.log)
md := ch.Modes md := ch.Modes
// Channel modes can adjust channel privs too, so we need a Nick // Channel modes can adjust channel privs too, so we need a Nick
nk := NewNick("test1", l) nk := NewNick("test1", s.log)
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
@ -86,7 +94,6 @@ func TestChannelParseModes(t *testing.T) {
// Flip some MOAR bits. // Flip some MOAR bits.
ch.ParseModes("+s-p+tm-i") ch.ParseModes("+s-p+tm-i")
m.ExpectNothing()
if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg || if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { !md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly {
@ -100,22 +107,20 @@ func TestChannelParseModes(t *testing.T) {
// enable limit correctly // enable limit correctly
ch.ParseModes("+l", "256") ch.ParseModes("+l", "256")
m.ExpectNothing()
if md.Limit != 256 { if md.Limit != 256 {
t.Errorf("Limit for channel not set correctly") 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") ch.ParseModes("+l")
m.ExpectAt(logging.Warn,
"Channel.ParseModes(): not enough arguments to process MODE #test1 +l")
if md.Limit != 256 { if md.Limit != 256 {
t.Errorf("Bad limit value caused limit to be unset.") t.Errorf("Bad limit value caused limit to be unset.")
} }
// disable limit correctly // disable limit correctly
ch.ParseModes("-l") ch.ParseModes("-l")
m.ExpectNothing()
if md.Limit != 0 { if md.Limit != 0 {
t.Errorf("Limit for channel not unset correctly") t.Errorf("Limit for channel not unset correctly")
} }
@ -127,22 +132,20 @@ func TestChannelParseModes(t *testing.T) {
// enable key correctly // enable key correctly
ch.ParseModes("+k", "foobar") ch.ParseModes("+k", "foobar")
m.ExpectNothing()
if md.Key != "foobar" { if md.Key != "foobar" {
t.Errorf("Key for channel not set correctly") t.Errorf("Key for channel not set correctly")
} }
// enable key incorrectly // enable key incorrectly
s.log.EXPECT().Warn("Channel.ParseModes(): not enough arguments to "+
"process MODE %s %s%c", "#test1", "+", byte('k'))
ch.ParseModes("+k") ch.ParseModes("+k")
m.ExpectAt(logging.Warn,
"Channel.ParseModes(): not enough arguments to process MODE #test1 +k")
if md.Key != "foobar" { if md.Key != "foobar" {
t.Errorf("Bad key value caused key to be unset.") t.Errorf("Bad key value caused key to be unset.")
} }
// disable key correctly // disable key correctly
ch.ParseModes("-k") ch.ParseModes("-k")
m.ExpectNothing()
if md.Key != "" { if md.Key != "" {
t.Errorf("Key for channel not unset correctly") t.Errorf("Key for channel not unset correctly")
} }
@ -151,25 +154,24 @@ func TestChannelParseModes(t *testing.T) {
cp.Op = true cp.Op = true
cp.HalfOp = true cp.HalfOp = true
ch.ParseModes("+aq-o", "test1", "test1", "test1") ch.ParseModes("+aq-o", "test1", "test1", "test1")
m.ExpectNothing()
if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice { if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice {
t.Errorf("Channel privileges not flipped correctly by ParseModes.") 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") 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") 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 // Test a random mix of modes, just to be sure
md.Limit = 256 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") 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 || if !md.Private || md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly { !md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly {
@ -184,6 +186,6 @@ func TestChannelParseModes(t *testing.T) {
} }
// Finally, check we get an info log for an unrecognised mode character // 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") ch.ParseModes("+d")
m.ExpectAt(logging.Info, "Channel.ParseModes(): unknown mode char d")
} }

150
state/mock_tracker.go Normal file
View File

@ -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")
}

View File

@ -1,15 +1,16 @@
package state package state
import ( import (
"github.com/fluffle/goirc/logging"
"testing" "testing"
) )
func TestNewNick(t *testing.T) { func TestNewNick(t *testing.T) {
l, _ := logging.NewMock(t) _, s := setUp(t)
nk := NewNick("test1", l) 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()") t.Errorf("Nick not created correctly by NewNick()")
} }
if len(nk.chans) != 0 || len(nk.lookup) != 0 { if len(nk.chans) != 0 || len(nk.lookup) != 0 {
@ -18,13 +19,14 @@ func TestNewNick(t *testing.T) {
} }
func TestAddChannel(t *testing.T) { func TestAddChannel(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
nk := NewNick("test1", l) defer s.tearDown()
ch := NewChannel("#test1", l)
nk := NewNick("test1", s.log)
ch := NewChannel("#test1", s.log)
cp := new(ChanPrivs) cp := new(ChanPrivs)
nk.addChannel(ch, cp) nk.addChannel(ch, cp)
m.ExpectNothing()
if len(nk.chans) != 1 || len(nk.lookup) != 1 { if len(nk.chans) != 1 || len(nk.lookup) != 1 {
t.Errorf("Channel lists not updated correctly for add.") 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.") 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) nk.addChannel(ch, cp)
m.ExpectAt(logging.Warn, "Nick.addChannel(): test1 already on #test1.")
} }
func TestDelChannel(t *testing.T) { func TestDelChannel(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
nk := NewNick("test1", l) defer s.tearDown()
ch := NewChannel("#test1", l)
nk := NewNick("test1", s.log)
ch := NewChannel("#test1", s.log)
cp := new(ChanPrivs) cp := new(ChanPrivs)
// Testing the error state first is easier // Testing the error state first is easier
s.log.EXPECT().Warn("Nick.delChannel(): %s not on %s.", "test1", "#test1")
nk.delChannel(ch) nk.delChannel(ch)
m.ExpectAt(logging.Warn, "Nick.delChannel(): test1 not on #test1.")
nk.addChannel(ch, cp) nk.addChannel(ch, cp)
nk.delChannel(ch) nk.delChannel(ch)
@ -64,8 +69,10 @@ func TestDelChannel(t *testing.T) {
} }
func TestNickParseModes(t *testing.T) { func TestNickParseModes(t *testing.T) {
l, m := logging.NewMock(t) _, s := setUp(t)
nk := NewNick("test1", l) defer s.tearDown()
nk := NewNick("test1", s.log)
md := nk.Modes md := nk.Modes
// Modes should all be false for a new nick // 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 // Parse a mode line that flips one true to false and two false to true
nk.ParseModes("+z-x+w") nk.ParseModes("+z-x+w")
m.ExpectNothing()
if !md.Invisible || md.Oper || !md.WallOps || md.HiddenHost || !md.SSL { if !md.Invisible || md.Oper || !md.WallOps || md.HiddenHost || !md.SSL {
t.Errorf("Modes not flipped correctly by ParseModes.") t.Errorf("Modes not flipped correctly by ParseModes.")
} }
// Check that passing an unknown mode char results in an info log // 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") nk.ParseModes("+d")
m.ExpectAt(logging.Info, "Nick.ParseModes(): unknown mode char d")
} }

View File

@ -2,16 +2,30 @@ package state
import ( import (
"github.com/fluffle/goirc/logging" "github.com/fluffle/goirc/logging"
"gomock.googlecode.com/hg/gomock"
"testing" "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) { func TestSTNewTracker(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
defer s.tearDown()
st := NewTracker("mynick", l) if st.l != s.log {
m.ExpectNothing()
if st.l != l {
t.Errorf("State tracker's logger not set correctly.") t.Errorf("State tracker's logger not set correctly.")
} }
if len(st.nicks) != 1 { if len(st.nicks) != 1 {
@ -26,31 +40,29 @@ func TestSTNewTracker(t *testing.T) {
} }
func TestSTNewNick(t *testing.T) { func TestSTNewNick(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
test1 := st.NewNick("test1") 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.") t.Errorf("Nick object created incorrectly by NewNick.")
} }
if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 { if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 {
t.Errorf("Nick object stored incorrectly by NewNick.") 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 { if fail := st.NewNick("test1"); fail != nil {
t.Errorf("Creating duplicate nick did not produce nil return.") t.Errorf("Creating duplicate nick did not produce nil return.")
} }
m.ExpectAt(logging.Warn, "StateTracker.NewNick(): test1 already tracked.")
} }
func TestSTGetNick(t *testing.T) { func TestSTGetNick(t *testing.T) {
l, _ := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
test1 := NewNick("test1", l) test1 := st.NewNick("test1")
st.nicks["test1"] = test1
if n := st.GetNick("test1"); n != test1 { if n := st.GetNick("test1"); n != test1 {
t.Errorf("Incorrect nick returned by GetNick.") t.Errorf("Incorrect nick returned by GetNick.")
@ -64,20 +76,16 @@ func TestSTGetNick(t *testing.T) {
} }
func TestSTReNick(t *testing.T) { func TestSTReNick(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
test1 := NewNick("test1", l) test1 := st.NewNick("test1")
st.nicks["test1"] = test1
// This channel is here to ensure that its lookup map gets updated // This channel is here to ensure that its lookup map gets updated
cp := new(ChanPrivs) chan1 := st.NewChannel("#chan1")
chan1 := NewChannel("#chan1", l) st.Associate(chan1, test1)
test1.addChannel(chan1, cp)
chan1.addNick(test1, cp)
st.ReNick("test1", "test2") st.ReNick("test1", "test2")
m.ExpectNothing()
if _, ok := st.nicks["test1"]; ok { if _, ok := st.nicks["test1"]; ok {
t.Errorf("Nick test1 still exists after ReNick.") 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.") t.Errorf("Nick list changed size during ReNick.")
} }
test2 := NewNick("test1", l) test2 := st.NewNick("test1")
st.nicks["test1"] = test2
s.log.EXPECT().Warn("StateTracker.ReNick(): %s already exists.", "test2")
st.ReNick("test1", "test2") st.ReNick("test1", "test2")
m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test2 already exists.")
if n, ok := st.nicks["test2"]; !ok || n != test1 { if n, ok := st.nicks["test2"]; !ok || n != test1 {
t.Errorf("Nick test2 overwritten/deleted by ReNick.") 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.") t.Errorf("Nick list changed size during ReNick.")
} }
s.log.EXPECT().Warn("StateTracker.ReNick(): %s not tracked.", "test3")
st.ReNick("test3", "test2") st.ReNick("test3", "test2")
m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test3 not tracked.")
} }
func TestSTDelNick(t *testing.T) { func TestSTDelNick(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
nick1 := NewNick("test1", l)
st.nicks["test1"] = nick1
st.NewNick("test1")
st.DelNick("test1") st.DelNick("test1")
m.ExpectNothing()
if _, ok := st.nicks["test1"]; ok { if _, ok := st.nicks["test1"]; ok {
t.Errorf("Nick test1 still exists after DelNick.") 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.") 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") st.DelNick("test2")
m.ExpectAt(logging.Warn, "StateTracker.DelNick(): test2 not tracked.")
if len(st.nicks) != 2 { if len(st.nicks) != 2 {
t.Errorf("Deleting unknown nick had unexpected side-effects.") t.Errorf("Deleting unknown nick had unexpected side-effects.")
} }
// Deleting my nick shouldn't work // Deleting my nick shouldn't work
s.log.EXPECT().Warn("StateTracker.DelNick(): won't delete myself.")
st.DelNick("mynick") st.DelNick("mynick")
m.ExpectAt(logging.Warn, "StateTracker.DelNick(): won't delete myself.")
if len(st.nicks) != 2 { if len(st.nicks) != 2 {
t.Errorf("Deleting myself had unexpected side-effects.") 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. // Test that deletion correctly dissociates nick from channels.
// NOTE: the two error states in delNick (as opposed to DelNick) // NOTE: the two error states in delNick (as opposed to DelNick)
// are not tested for here, as they will only arise from programming // are not tested for here, as they will only arise from programming
// errors in other methods. Using m.CheckNothingWritten() while // errors in other methods. The mock logger should catch these.
// testing these methods will catch those errors should they occur.
// Create a new channel for testing purposes // Create a new channel for testing purposes.
chan1 := NewChannel("#test1", l) chan1 := st.NewChannel("#test1")
st.chans["#test1"] = chan1
// Associate both "my" nick and test1 with the channel // Associate both "my" nick and test1 with the channel
p := new(ChanPrivs) st.Associate(chan1, st.me)
chan1.addNick(st.me, p) st.Associate(chan1, nick1)
st.me.addChannel(chan1, p)
chan1.addNick(nick1, p)
nick1.addChannel(chan1, p)
// Test we have the expected starting state (at least vaguely) // Test we have the expected starting state (at least vaguely)
if len(chan1.nicks) != 2 || len(st.nicks) != 2 || if len(chan1.nicks) != 2 || len(st.nicks) != 2 ||
@ -176,7 +176,6 @@ func TestSTDelNick(t *testing.T) {
} }
st.DelNick("test1") st.DelNick("test1")
m.ExpectNothing()
// Actual deletion tested above... // Actual deletion tested above...
if len(chan1.nicks) != 1 || len(st.chans) != 1 || if len(chan1.nicks) != 1 || len(st.chans) != 1 ||
@ -193,35 +192,33 @@ func TestSTDelNick(t *testing.T) {
} }
func TestSTNewChannel(t *testing.T) { func TestSTNewChannel(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
if len(st.chans) != 0 { if len(st.chans) != 0 {
t.Errorf("Channel list of new tracker is non-zero length.") t.Errorf("Channel list of new tracker is non-zero length.")
} }
test1 := st.NewChannel("#test1") 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.") t.Errorf("Channel object created incorrectly by NewChannel.")
} }
if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 { if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 {
t.Errorf("Channel object stored incorrectly by NewChannel.") 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 { if fail := st.NewChannel("#test1"); fail != nil {
t.Errorf("Creating duplicate chan did not produce nil return.") t.Errorf("Creating duplicate chan did not produce nil return.")
} }
m.ExpectAt(logging.Warn, "StateTracker.NewChannel(): #test1 already tracked.")
} }
func TestSTGetChannel(t *testing.T) { func TestSTGetChannel(t *testing.T) {
l, _ := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
test1 := NewChannel("#test1", l) test1 := st.NewChannel("#test1")
st.chans["#test1"] = test1
if c := st.GetChannel("#test1"); c != test1 { if c := st.GetChannel("#test1"); c != test1 {
t.Errorf("Incorrect Channel returned by GetChannel.") t.Errorf("Incorrect Channel returned by GetChannel.")
@ -235,14 +232,11 @@ func TestSTGetChannel(t *testing.T) {
} }
func TestSTDelChannel(t *testing.T) { func TestSTDelChannel(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
chan1 := NewChannel("#test1", l)
st.chans["#test1"] = chan1
st.NewChannel("#test1")
st.DelChannel("#test1") st.DelChannel("#test1")
m.ExpectNothing()
if _, ok := st.chans["#test1"]; ok { if _, ok := st.chans["#test1"]; ok {
t.Errorf("Channel test1 still exists after DelChannel.") 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.") 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") st.DelChannel("#test2")
m.ExpectAt(logging.Warn, "StateTracker.DelChannel(): #test2 not tracked.")
if len(st.chans) != 1 { if len(st.chans) != 1 {
t.Errorf("DelChannel had unexpected side-effects.") t.Errorf("DelChannel had unexpected side-effects.")
} }
// Test that deletion correctly dissociates channel from tracked nicks. // Test that deletion correctly dissociates channel from tracked nicks.
// In order to test this thoroughly we need two channels (so that delNick() // In order to test this thoroughly we need two channels (so that delNick()
// is not called internally in delChannel() when len(nick1.chans) == 0. // is not called internally in delChannel() when len(nick1.chans) == 0.
chan2 := NewChannel("#test2", l) chan2 := st.NewChannel("#test2")
st.chans["#test2"] = chan2 nick1 := st.NewNick("test1")
nick1 := NewNick("test1", l)
st.nicks["test1"] = nick1
// Associate both "my" nick and test1 with the channels // Associate both "my" nick and test1 with the channels
p := new(ChanPrivs) st.Associate(chan1, st.me)
chan1.addNick(st.me, p) st.Associate(chan1, nick1)
st.me.addChannel(chan1, p) st.Associate(chan2, st.me)
chan1.addNick(nick1, p) st.Associate(chan2, nick1)
nick1.addChannel(chan1, p)
chan2.addNick(st.me, p)
st.me.addChannel(chan2, p)
chan2.addNick(nick1, p)
nick1.addChannel(chan2, p)
// Test we have the expected starting state (at least vaguely) // Test we have the expected starting state (at least vaguely)
if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || 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") st.DelChannel("#test1")
m.ExpectNothing()
// Test intermediate state. We're still on #test2 with test1, so test1 // Test intermediate state. We're still on #test2 with test1, so test1
// shouldn't be deleted from state tracking itself just yet. // 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 { len(st.me.chans) != 1 || len(nick1.chans) != 1 || len(st.chans) != 1 {
t.Errorf("Deleting channel didn't dissociate correctly from nicks.") t.Errorf("Deleting channel didn't dissociate correctly from nicks.")
} }
if _, ok := nick1.chans[chan1]; ok { if _, ok := nick1.chans[chan1]; ok {
t.Errorf("Channel not removed from nick's chans map.") t.Errorf("Channel not removed from nick's chans map.")
} }
@ -304,7 +290,6 @@ func TestSTDelChannel(t *testing.T) {
} }
st.DelChannel("#test2") st.DelChannel("#test2")
m.ExpectNothing()
// Test final state. Deleting #test2 means that we're no longer on any // 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. // 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 { len(st.me.chans) != 0 || len(nick1.chans) != 0 || len(st.chans) != 0 {
t.Errorf("Deleting last channel didn't dissociate correctly from nicks.") t.Errorf("Deleting last channel didn't dissociate correctly from nicks.")
} }
if _, ok := st.nicks["test1"]; ok { if _, ok := st.nicks["test1"]; ok {
t.Errorf("Nick not deleted correctly when on no channels.") t.Errorf("Nick not deleted correctly when on no channels.")
} }
@ -322,63 +306,63 @@ func TestSTDelChannel(t *testing.T) {
} }
func TestSTIsOn(t *testing.T) { func TestSTIsOn(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
nick1 := NewNick("test1", l) nick1 := st.NewNick("test1")
st.nicks["test1"] = nick1 chan1 := st.NewChannel("#test1")
chan1 := NewChannel("#test1", l)
st.chans["#test1"] = chan1
if priv, ok := st.IsOn("#test1", "test1"); ok || priv != nil { if priv, ok := st.IsOn("#test1", "test1"); ok || priv != nil {
t.Errorf("test1 is not on #test1 (yet)") t.Errorf("test1 is not on #test1 (yet)")
} }
cp := new(ChanPrivs) cp := st.Associate(chan1, nick1)
chan1.addNick(nick1, cp)
nick1.addChannel(chan1, cp)
if priv, ok := st.IsOn("#test1", "test1"); !ok || priv != cp { if priv, ok := st.IsOn("#test1", "test1"); !ok || priv != cp {
t.Errorf("test1 is on #test1 (now)") t.Errorf("test1 is on #test1 (now)")
} }
m.ExpectNothing()
} }
func TestSTAssociate(t *testing.T) { func TestSTAssociate(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
nick1 := st.NewNick("test1") nick1 := st.NewNick("test1")
chan1 := st.NewChannel("#test1") chan1 := st.NewChannel("#test1")
cp := st.Associate(chan1, nick1) cp := st.Associate(chan1, nick1)
m.ExpectNothing() if priv, ok := nick1.chans[chan1]; !ok || cp != priv {
if priv, ok := st.IsOn("#test1", "test1"); !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.") t.Errorf("test1 was not associated with #test1.")
} }
// Test error cases // Test error cases
s.log.EXPECT().Error("StateTracker.Associate(): passed nil values :-(")
st.Associate(nil, nick1) 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) 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) 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) 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) 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) { func TestSTDissociate(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
nick1 := st.NewNick("test1") nick1 := st.NewNick("test1")
chan1 := st.NewChannel("#test1") chan1 := st.NewChannel("#test1")
@ -398,7 +382,6 @@ func TestSTDissociate(t *testing.T) {
// First, test the case of me leaving #test2 // First, test the case of me leaving #test2
st.Dissociate(chan2, st.me) st.Dissociate(chan2, st.me)
m.ExpectNothing()
// This should have resulted in the complete deletion of the channel. // This should have resulted in the complete deletion of the channel.
if len(chan1.nicks) != 2 || len(chan2.nicks) != 0 || len(st.nicks) != 2 || 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") chan2 = st.NewChannel("#test2")
st.Associate(chan2, st.me) st.Associate(chan2, st.me)
st.Associate(chan2, nick1) st.Associate(chan2, nick1)
m.ExpectNothing()
// Check state once moar. // Check state once moar.
if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || 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. // Now, lets dissociate test1 from #test1 then #test2.
// This first one should only result in a change in associations. // This first one should only result in a change in associations.
st.Dissociate(chan1, nick1) st.Dissociate(chan1, nick1)
m.ExpectNothing()
if len(chan1.nicks) != 1 || len(chan2.nicks) != 2 || len(st.nicks) != 2 || 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 { 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 // This second one should also delete test1
// as it's no longer on any common channels with us // as it's no longer on any common channels with us
st.Dissociate(chan2, nick1) st.Dissociate(chan2, nick1)
m.ExpectNothing()
if len(chan1.nicks) != 1 || len(chan2.nicks) != 1 || len(st.nicks) != 1 || 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 { 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 // Check error cases
// test1 was deleted above, so "re-track" it for this test. // test1 was deleted above, so "re-track" it for this test.
nick1 = st.NewNick("test1") nick1 = st.NewNick("test1")
s.log.EXPECT().Warn("StateTracker.Dissociate(): %s not on %s.",
"test1", "#test1")
st.Dissociate(chan1, nick1) 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) 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) 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) 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) 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) { func TestSTWipe(t *testing.T) {
l, m := logging.NewMock(t) st, s := setUp(t)
st := NewTracker("mynick", l) defer s.tearDown()
nick1 := st.NewNick("test1") nick1 := st.NewNick("test1")
nick2 := st.NewNick("test2") nick2 := st.NewNick("test2")
@ -487,8 +470,6 @@ func TestSTWipe(t *testing.T) {
st.Associate(chan1, nick3) st.Associate(chan1, nick3)
m.ExpectNothing()
// Check the state we have at this point is what we would expect. // 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 { if len(st.nicks) != 4 || len(st.chans) != 3 || len(st.me.chans) != 3 {
t.Errorf("Tracker nick/channel lists wrong length before wipe.") t.Errorf("Tracker nick/channel lists wrong length before wipe.")
@ -502,7 +483,6 @@ func TestSTWipe(t *testing.T) {
// Nuke *all* the state! // Nuke *all* the state!
st.Wipe() st.Wipe()
m.ExpectNothing()
// Check the state we have at this point is what we would expect. // 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 { if len(st.nicks) != 1 || len(st.chans) != 0 || len(st.me.chans) != 0 {