diff --git a/state/tracker.go b/state/tracker.go index 80de101..91dd9b8 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -88,6 +88,10 @@ func (st *stateTracker) DelNick(n string) { // Creates a new Channel, initialises it, and stores it so it // can be properly tracked for state management purposes. func (st *stateTracker) NewChannel(c string) *Channel { + if _, ok := st.chans[c]; ok { + logging.Warn("StateTracker.NewChannel(): %s already tracked", c) + return nil + } ch := &Channel{Name: c, st: st} ch.initialise() st.chans[c] = ch diff --git a/state/tracker_test.go b/state/tracker_test.go index 9bc6368..9c83f0e 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -99,3 +99,80 @@ func TestDelNick(t *testing.T) { t.Errorf("DelNick had unexpected side-effects.") } } + +func TestNewChannel(t *testing.T) { + st := NewTracker() + + if len(st.chans) != 0 { + t.Errorf("Channel list of new tracker is non-zero length.") + } + + test1 := st.NewChannel("#test1") + + if test1 == nil || test1.Name != "#test1" || test1.st != st { + 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.") + } + + if fail := st.NewChannel("#test1"); fail != nil { + t.Errorf("Creating duplicate chan did not produce nil return.") + } +} + +func TestGetChannel(t *testing.T) { + st := NewTracker() + + test1 := &Channel{Name: "#test1", st: st} + st.chans["#test1"] = test1 + + if c := st.GetChannel("#test1"); c != test1 { + t.Errorf("Incorrect Channel returned by GetChannel.") + } + if c := st.GetChannel("#test2"); c != nil { + t.Errorf("Channel unexpectedly returned by GetChannel.") + } + if len(st.chans) != 1 { + t.Errorf("Channel list changed size during GetChannel.") + } +} + +func TestDelChannel(t *testing.T) { + st := NewTracker() + + test1 := &Channel{Name: "#test1", st: st} + st.chans["#test1"] = test1 + + st.DelChannel("#test1") + + if _, ok := st.chans["#test1"]; ok { + t.Errorf("Channel test1 still exists after DelChannel.") + } + if len(st.chans) != 0 { + t.Errorf("Channel list still contains chans after DelChannel.") + } + + st.chans["#test1"] = test1 + + st.DelChannel("test2") + + if len(st.chans) != 1 { + t.Errorf("DelChannel had unexpected side-effects.") + } +} + +func TestIsOn(t *testing.T) { + st := NewTracker() + + nick1 := st.NewNick("test1") + chan1 := st.NewChannel("#test1") + + if st.IsOn("#test1", "test1") { + t.Errorf("test1 is not on #test1 (yet)") + } + chan1.AddNick(nick1) + if !st.IsOn("#test1", "test1") { + t.Errorf("test1 is on #test1 (now)") + } +}