diff --git a/state/tracker.go b/state/tracker.go index 978be2a..80de101 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -64,9 +64,13 @@ func (st *stateTracker) GetNick(n string) *Nick { // under a "neu" nick rather than the old one. func (st *stateTracker) ReNick(old, neu string) { if n, ok := st.nicks[old]; ok { - st.nicks[old] = nil, false - n.Nick = neu - st.nicks[neu] = n + if _, ok := st.nicks[neu]; !ok { + st.nicks[old] = nil, false + n.Nick = neu + st.nicks[neu] = n + } else { + logging.Warn("StateTracker.ReNick(): %s already exists.", neu) + } } else { logging.Warn("StateTracker.ReNick(): %s not tracked.", old) } diff --git a/state/tracker_test.go b/state/tracker_test.go index 9834144..9bc6368 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -57,6 +57,45 @@ func TestReNick(t *testing.T) { t.Errorf("Nick test2 doesn't exist after ReNick.") } if len(st.nicks) != 1 { - t.Errorf("Nick list changed size during GetNick.") + t.Errorf("Nick list changed size during ReNick.") + } + + test2 := &Nick{Nick: "test2", st: st} + st.nicks["test1"] = test2 + + st.ReNick("test1", "test2") + if n, ok := st.nicks["test2"]; !ok || n != test1 { + t.Errorf("Nick test2 overwritten/deleted by ReNick.") + } + if n, ok := st.nicks["test1"]; !ok || n != test2 { + t.Errorf("Nick test1 overwritten/deleted by ReNick.") + } + if len(st.nicks) != 2 { + t.Errorf("Nick list changed size during ReNick.") + } + +} + +func TestDelNick(t *testing.T) { + st := NewTracker() + + test1 := &Nick{Nick: "test1", st: st} + st.nicks["test1"] = test1 + + st.DelNick("test1") + + if _, ok := st.nicks["test1"]; ok { + t.Errorf("Nick test1 still exists after DelNick.") + } + if len(st.nicks) != 0 { + t.Errorf("Nick list still contains nicks after DelNick.") + } + + st.nicks["test1"] = test1 + + st.DelNick("test2") + + if len(st.nicks) != 1 { + t.Errorf("DelNick had unexpected side-effects.") } }