Permalink
Browse files

Convert state tracker to use gomock logging for tests.

  • Loading branch information...
1 parent d0f0175 commit 69d52270b66eb7231566361e5491d7c9f7735d44 @fluffle committed Nov 5, 2011
Showing with 326 additions and 179 deletions.
  1. +38 −36 state/channel_test.go
  2. +150 −0 state/mock_tracker.go
  3. +32 −17 state/nick_test.go
  4. +106 −126 state/tracker_test.go
View
@@ -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")
}
View
@@ -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")
+}
Oops, something went wrong.

0 comments on commit 69d5227

Please sign in to comment.