2011-10-12 22:06:46 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2011-10-22 22:58:06 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2011-10-12 22:06:46 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTNewTracker(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-22 22:58:06 +00:00
|
|
|
|
|
|
|
if st.l != l {
|
|
|
|
t.Errorf("State tracker's logger not set correctly.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 1 {
|
|
|
|
t.Errorf("Nick list of new tracker is not 1 (me!).")
|
2011-10-12 22:06:46 +00:00
|
|
|
}
|
2011-10-22 22:58:06 +00:00
|
|
|
if len(st.chans) != 0 {
|
|
|
|
t.Errorf("Channel list of new tracker is not empty.")
|
|
|
|
}
|
|
|
|
if nk, ok := st.nicks["mynick"]; !ok || nk.Nick != "mynick" || nk != st.me {
|
|
|
|
t.Errorf("My nick not stored correctly in tracker.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTNewNick(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
test1 := st.NewNick("test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
if test1 == nil || test1.Nick != "test1" || test1.l != l {
|
2011-10-12 22:06:46 +00:00
|
|
|
t.Errorf("Nick object created incorrectly by NewNick.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 {
|
2011-10-12 22:06:46 +00:00
|
|
|
t.Errorf("Nick object stored incorrectly by NewNick.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail := st.NewNick("test1"); fail != nil {
|
|
|
|
t.Errorf("Creating duplicate nick did not produce nil return.")
|
|
|
|
}
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.NewNick(): test1 already tracked.")
|
2011-10-12 22:06:46 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTGetNick(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, _ := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
test1 := NewNick("test1", l)
|
2011-10-12 22:06:46 +00:00
|
|
|
st.nicks["test1"] = test1
|
|
|
|
|
|
|
|
if n := st.GetNick("test1"); n != test1 {
|
|
|
|
t.Errorf("Incorrect nick returned by GetNick.")
|
|
|
|
}
|
|
|
|
if n := st.GetNick("test2"); n != nil {
|
|
|
|
t.Errorf("Nick unexpectedly returned by GetNick.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 2 {
|
2011-10-12 22:06:46 +00:00
|
|
|
t.Errorf("Nick list changed size during GetNick.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTReNick(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
test1 := NewNick("test1", l)
|
2011-10-12 22:06:46 +00:00
|
|
|
st.nicks["test1"] = test1
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
// 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)
|
|
|
|
|
2011-10-12 22:06:46 +00:00
|
|
|
st.ReNick("test1", "test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-12 22:06:46 +00:00
|
|
|
|
|
|
|
if _, ok := st.nicks["test1"]; ok {
|
|
|
|
t.Errorf("Nick test1 still exists after ReNick.")
|
|
|
|
}
|
|
|
|
if n, ok := st.nicks["test2"]; !ok || n != test1 {
|
|
|
|
t.Errorf("Nick test2 doesn't exist after ReNick.")
|
|
|
|
}
|
2011-10-22 22:58:06 +00:00
|
|
|
if _, ok := chan1.lookup["test1"]; ok {
|
|
|
|
t.Errorf("Channel #chan1 still knows about test1 after ReNick.")
|
|
|
|
}
|
|
|
|
if n, ok := chan1.lookup["test2"]; !ok || n != test1 {
|
|
|
|
t.Errorf("Channel #chan1 doesn't know about test2 after ReNick.")
|
|
|
|
}
|
2011-10-13 22:19:31 +00:00
|
|
|
if test1.Nick != "test2" {
|
|
|
|
t.Errorf("Nick test1 not changed correctly.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 2 {
|
2011-10-13 19:44:44 +00:00
|
|
|
t.Errorf("Nick list changed size during ReNick.")
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
test2 := NewNick("test1", l)
|
2011-10-13 19:44:44 +00:00
|
|
|
st.nicks["test1"] = test2
|
|
|
|
|
|
|
|
st.ReNick("test1", "test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test2 already exists.")
|
2011-10-22 22:58:06 +00:00
|
|
|
|
2011-10-13 19:44:44 +00:00
|
|
|
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.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 3 {
|
2011-10-13 19:44:44 +00:00
|
|
|
t.Errorf("Nick list changed size during ReNick.")
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
st.ReNick("test3", "test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.ReNick(): test3 not tracked.")
|
2011-10-13 19:44:44 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTDelNick(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-13 19:44:44 +00:00
|
|
|
|
2011-10-27 15:04:45 +00:00
|
|
|
nick1 := NewNick("test1", l)
|
|
|
|
st.nicks["test1"] = nick1
|
2011-10-13 19:44:44 +00:00
|
|
|
|
|
|
|
st.DelNick("test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-13 19:44:44 +00:00
|
|
|
|
|
|
|
if _, ok := st.nicks["test1"]; ok {
|
|
|
|
t.Errorf("Nick test1 still exists after DelNick.")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 1 {
|
2011-10-13 19:44:44 +00:00
|
|
|
t.Errorf("Nick list still contains nicks after DelNick.")
|
|
|
|
}
|
|
|
|
|
2011-10-27 15:04:45 +00:00
|
|
|
st.nicks["test1"] = nick1
|
2011-10-13 19:44:44 +00:00
|
|
|
|
|
|
|
st.DelNick("test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.DelNick(): test2 not tracked.")
|
2011-10-22 22:58:06 +00:00
|
|
|
|
|
|
|
if len(st.nicks) != 2 {
|
|
|
|
t.Errorf("Deleting unknown nick had unexpected side-effects.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting my nick shouldn't work
|
|
|
|
st.DelNick("mynick")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.DelNick(): won't delete myself.")
|
2011-10-13 19:44:44 +00:00
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
if len(st.nicks) != 2 {
|
2011-10-22 22:58:06 +00:00
|
|
|
t.Errorf("Deleting myself had unexpected side-effects.")
|
2011-10-12 22:06:46 +00:00
|
|
|
}
|
2011-10-27 15:04:45 +00:00
|
|
|
|
|
|
|
// Test that deletion correctly dissociates nick from channels.
|
2011-10-27 15:41:56 +00:00
|
|
|
// 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.
|
|
|
|
|
2011-10-27 15:04:45 +00:00
|
|
|
// Create a new channel for testing purposes
|
|
|
|
chan1 := NewChannel("#test1", l)
|
|
|
|
st.chans["#test1"] = chan1
|
2011-10-27 15:41:56 +00:00
|
|
|
|
2011-10-27 15:04:45 +00:00
|
|
|
// 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)
|
|
|
|
|
2011-10-27 15:41:56 +00:00
|
|
|
// Test we have the expected starting state (at least vaguely)
|
|
|
|
if len(chan1.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 1 || len(nick1.chans) != 1 || len(st.chans) != 1 {
|
2011-10-27 15:04:45 +00:00
|
|
|
t.Errorf("Bad initial state for test DelNick() channel dissociation.")
|
|
|
|
}
|
|
|
|
|
|
|
|
st.DelNick("test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 15:04:45 +00:00
|
|
|
|
|
|
|
// Actual deletion tested above...
|
2011-10-27 15:41:56 +00:00
|
|
|
if len(chan1.nicks) != 1 || len(st.chans) != 1 ||
|
|
|
|
len(st.me.chans) != 1 || len(nick1.chans) != 0 || len(st.chans) != 1 {
|
2011-10-27 15:04:45 +00:00
|
|
|
t.Errorf("Deleting nick didn't dissociate correctly from channels.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chan1.nicks[nick1]; ok {
|
|
|
|
t.Errorf("Nick not removed from channel's nick map.")
|
|
|
|
}
|
|
|
|
if _, ok := chan1.lookup["test1"]; ok {
|
2011-10-27 15:41:56 +00:00
|
|
|
t.Errorf("Nick not removed from channel's lookup map.")
|
2011-10-27 15:04:45 +00:00
|
|
|
}
|
2011-10-12 22:06:46 +00:00
|
|
|
}
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTNewChannel(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-13 21:31:09 +00:00
|
|
|
|
|
|
|
if len(st.chans) != 0 {
|
|
|
|
t.Errorf("Channel list of new tracker is non-zero length.")
|
|
|
|
}
|
|
|
|
|
|
|
|
test1 := st.NewChannel("#test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
if test1 == nil || test1.Name != "#test1" || test1.l != l {
|
2011-10-13 21:31:09 +00:00
|
|
|
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.")
|
|
|
|
}
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.NewChannel(): #test1 already tracked.")
|
2011-10-13 21:31:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTGetChannel(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, _ := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
test1 := NewChannel("#test1", l)
|
2011-10-13 21:31:09 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTDelChannel(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-27 15:41:56 +00:00
|
|
|
chan1 := NewChannel("#test1", l)
|
|
|
|
st.chans["#test1"] = chan1
|
2011-10-13 21:31:09 +00:00
|
|
|
|
|
|
|
st.DelChannel("#test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-13 21:31:09 +00:00
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2011-10-27 15:41:56 +00:00
|
|
|
st.chans["#test1"] = chan1
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
st.DelChannel("#test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.DelChannel(): #test2 not tracked.")
|
2011-10-13 21:31:09 +00:00
|
|
|
|
|
|
|
if len(st.chans) != 1 {
|
|
|
|
t.Errorf("DelChannel had unexpected side-effects.")
|
|
|
|
}
|
2011-10-27 15:41:56 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Test we have the expected starting state (at least vaguely)
|
|
|
|
if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(nick1.chans) != 2 || len(st.chans) != 2 {
|
|
|
|
t.Errorf("Bad initial state for test DelChannel() nick dissociation.")
|
|
|
|
}
|
|
|
|
|
|
|
|
st.DelChannel("#test1")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 15:41:56 +00:00
|
|
|
|
|
|
|
// Test intermediate state. We're still on #test2 with test1, so test1
|
|
|
|
// shouldn't be deleted from state tracking itself just yet.
|
|
|
|
if len(chan1.nicks) != 0 || len(chan2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
if _, ok := nick1.lookup["#test1"]; ok {
|
|
|
|
t.Errorf("Channel not removed from nick's lookup map.")
|
|
|
|
}
|
|
|
|
|
|
|
|
st.DelChannel("#test2")
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 15:41:56 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if len(chan1.nicks) != 0 || len(chan2.nicks) != 0 || len(st.nicks) != 1 ||
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
if _, ok := st.nicks["mynick"]; !ok {
|
|
|
|
t.Errorf("My nick deleted incorrectly when on no channels.")
|
|
|
|
}
|
2011-10-13 21:31:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTIsOn(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-22 22:58:06 +00:00
|
|
|
st := NewTracker("mynick", l)
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
nick1 := NewNick("test1", l)
|
2011-10-13 22:19:31 +00:00
|
|
|
st.nicks["test1"] = nick1
|
2011-10-22 22:58:06 +00:00
|
|
|
chan1 := NewChannel("#test1", l)
|
2011-10-13 22:19:31 +00:00
|
|
|
st.chans["#test1"] = chan1
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-10-27 16:03:01 +00:00
|
|
|
if priv, ok := st.IsOn("#test1", "test1"); ok || priv != nil {
|
2011-10-13 21:31:09 +00:00
|
|
|
t.Errorf("test1 is not on #test1 (yet)")
|
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
cp := new(ChanPrivs)
|
|
|
|
chan1.addNick(nick1, cp)
|
|
|
|
nick1.addChannel(chan1, cp)
|
2011-10-27 16:03:01 +00:00
|
|
|
if priv, ok := st.IsOn("#test1", "test1"); !ok || priv != cp {
|
2011-10-13 21:31:09 +00:00
|
|
|
t.Errorf("test1 is on #test1 (now)")
|
|
|
|
}
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-13 21:31:09 +00:00
|
|
|
}
|
2011-10-27 15:55:33 +00:00
|
|
|
|
|
|
|
func TestSTAssociate(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-27 15:55:33 +00:00
|
|
|
st := NewTracker("mynick", l)
|
|
|
|
|
|
|
|
nick1 := st.NewNick("test1")
|
|
|
|
chan1 := st.NewChannel("#test1")
|
|
|
|
|
2011-10-27 16:54:23 +00:00
|
|
|
cp := st.Associate(chan1, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 16:54:23 +00:00
|
|
|
if priv, ok := st.IsOn("#test1", "test1"); !ok || cp != priv {
|
2011-10-27 15:55:33 +00:00
|
|
|
t.Errorf("test1 was not associated with #test1.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test error cases
|
|
|
|
st.Associate(nil, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Associate(): passed nil values :-(")
|
2011-10-27 15:55:33 +00:00
|
|
|
|
|
|
|
st.Associate(chan1, nil)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Associate(): passed nil values :-(")
|
2011-10-27 15:55:33 +00:00
|
|
|
|
|
|
|
st.Associate(chan1, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.Associate(): test1 already on #test1.")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
nick2 := NewNick("test2", l)
|
|
|
|
st.Associate(chan1, nick2)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Associate(): nick test2 not found "+
|
|
|
|
"in (or differs from) internal state.")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
chan2 := NewChannel("#test2", l)
|
|
|
|
st.Associate(chan2, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Associate(): channel #test2 not "+
|
|
|
|
"found in (or differs from) internal state.")
|
2011-10-27 16:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTDissociate(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-10-27 16:54:23 +00:00
|
|
|
st := NewTracker("mynick", l)
|
|
|
|
|
|
|
|
nick1 := st.NewNick("test1")
|
|
|
|
chan1 := st.NewChannel("#test1")
|
|
|
|
chan2 := st.NewChannel("#test2")
|
|
|
|
|
|
|
|
// Associate both "my" nick and test1 with the channels
|
|
|
|
st.Associate(chan1, st.me)
|
|
|
|
st.Associate(chan1, nick1)
|
|
|
|
st.Associate(chan2, st.me)
|
|
|
|
st.Associate(chan2, nick1)
|
|
|
|
|
|
|
|
// Check the initial state looks mostly like we expect it to.
|
|
|
|
if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(nick1.chans) != 2 || len(st.chans) != 2 {
|
|
|
|
t.Errorf("Initial state for dissociation tests looks odd.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, test the case of me leaving #test2
|
|
|
|
st.Dissociate(chan2, st.me)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// This should have resulted in the complete deletion of the channel.
|
|
|
|
if len(chan1.nicks) != 2 || len(chan2.nicks) != 0 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 1 || len(nick1.chans) != 1 || len(st.chans) != 1 {
|
|
|
|
t.Errorf("Dissociating myself from channel didn't delete it correctly.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reassociating myself and test1 to #test2 shouldn't cause any errors.
|
|
|
|
chan2 = st.NewChannel("#test2")
|
|
|
|
st.Associate(chan2, st.me)
|
|
|
|
st.Associate(chan2, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// Check state once moar.
|
|
|
|
if len(chan1.nicks) != 2 || len(chan2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(nick1.chans) != 2 || len(st.chans) != 2 {
|
|
|
|
t.Errorf("Reassociating to channel has produced unexpected state.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, lets dissociate test1 from #test1 then #test2.
|
|
|
|
// This first one should only result in a change in associations.
|
|
|
|
st.Dissociate(chan1, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Dissociating a nick from one channel went wrong.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This second one should also delete test1
|
|
|
|
// as it's no longer on any common channels with us
|
|
|
|
st.Dissociate(chan2, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Dissociating a nick from it's last channel went wrong.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check error cases
|
|
|
|
// test1 was deleted above, so "re-track" it for this test.
|
|
|
|
nick1 = st.NewNick("test1")
|
|
|
|
st.Dissociate(chan1, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Warn, "StateTracker.Dissociate(): test1 not on #test1.")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
st.Dissociate(chan1, nil)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Dissociate(): passed nil values :-(")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
st.Dissociate(nil, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Dissociate(): passed nil values :-(")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
nick3 := NewNick("test3", l)
|
|
|
|
st.Dissociate(chan1, nick3)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Dissociate(): nick test3 not "+
|
|
|
|
"found in (or differs from) internal state.")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
chan3 := NewChannel("#test3", l)
|
|
|
|
st.Dissociate(chan3, nick1)
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectAt(logging.Error, "StateTracker.Dissociate(): channel #test3 not "+
|
|
|
|
"found in (or differs from) internal state.")
|
2011-10-27 15:55:33 +00:00
|
|
|
}
|
2011-11-05 02:25:25 +00:00
|
|
|
|
|
|
|
func TestSTWipe(t *testing.T) {
|
2011-11-05 06:06:40 +00:00
|
|
|
l, m := logging.NewMock(t)
|
2011-11-05 02:25:25 +00:00
|
|
|
st := NewTracker("mynick", l)
|
|
|
|
|
|
|
|
nick1 := st.NewNick("test1")
|
|
|
|
nick2 := st.NewNick("test2")
|
|
|
|
nick3 := st.NewNick("test3")
|
|
|
|
|
|
|
|
chan1 := st.NewChannel("#test1")
|
|
|
|
chan2 := st.NewChannel("#test2")
|
|
|
|
chan3 := st.NewChannel("#test3")
|
|
|
|
|
|
|
|
// Some associations
|
|
|
|
st.Associate(chan1, st.me)
|
|
|
|
st.Associate(chan2, st.me)
|
|
|
|
st.Associate(chan3, st.me)
|
|
|
|
|
|
|
|
st.Associate(chan1, nick1)
|
|
|
|
st.Associate(chan2, nick2)
|
|
|
|
st.Associate(chan3, nick3)
|
|
|
|
|
|
|
|
st.Associate(chan1, nick2)
|
|
|
|
st.Associate(chan2, nick3)
|
|
|
|
|
|
|
|
st.Associate(chan1, nick3)
|
|
|
|
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-11-05 02:25:25 +00:00
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
if len(chan1.nicks) != 4 || len(chan2.nicks) != 3 || len(chan3.nicks) != 2 {
|
|
|
|
t.Errorf("Channel nick lists wrong length before wipe.")
|
|
|
|
}
|
|
|
|
if len(nick1.chans) != 1 || len(nick2.chans) != 2 || len(nick3.chans) != 3 {
|
|
|
|
t.Errorf("Nick chan lists wrong length before wipe.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke *all* the state!
|
|
|
|
st.Wipe()
|
2011-11-05 06:06:40 +00:00
|
|
|
m.ExpectNothing()
|
2011-11-05 02:25:25 +00:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
t.Errorf("Tracker nick/channel lists wrong length after wipe.")
|
|
|
|
}
|
|
|
|
if len(chan1.nicks) != 0 || len(chan2.nicks) != 0 || len(chan3.nicks) != 0 {
|
|
|
|
t.Errorf("Channel nick lists wrong length after wipe.")
|
|
|
|
}
|
|
|
|
if len(nick1.chans) != 0 || len(nick2.chans) != 0 || len(nick3.chans) != 0 {
|
|
|
|
t.Errorf("Nick chan lists wrong length after wipe.")
|
|
|
|
}
|
|
|
|
}
|