2011-10-12 22:06:46 +00:00
|
|
|
package state
|
|
|
|
|
2014-05-15 11:28:57 +00:00
|
|
|
import "testing"
|
2011-11-05 12:22:04 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// There is some awkwardness in these tests. Items retrieved directly from the
|
|
|
|
// state trackers internal maps are private and only have private,
|
|
|
|
// uncaptialised members. Items retrieved from state tracker public interface
|
|
|
|
// methods are public and only have public, capitalised members. Comparisons of
|
|
|
|
// the two are done on the basis of nick or channel name.
|
|
|
|
|
2011-11-05 12:22:04 +00:00
|
|
|
func TestSTNewTracker(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-22 22:58:06 +00:00
|
|
|
|
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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if nk, ok := st.nicks["mynick"]; !ok || nk.nick != "mynick" || nk != st.me {
|
2011-10-22 22:58:06 +00:00
|
|
|
t.Errorf("My nick not stored correctly in tracker.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTNewNick(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-19 23:10:33 +00:00
|
|
|
test1 := st.NewNick("test1")
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2013-01-23 22:33:01 +00:00
|
|
|
if test1 == nil || test1.Nick != "test1" {
|
2011-10-12 22:06:46 +00:00
|
|
|
t.Errorf("Nick object created incorrectly by NewNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if n, ok := st.nicks["test1"]; !ok || !test1.Equals(n.Nick()) || 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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if fail := st.NewNick(""); fail != nil {
|
|
|
|
t.Errorf("Creating empty nick did not produce nil return.")
|
|
|
|
}
|
2011-10-12 22:06:46 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTGetNick(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-11-05 12:22:04 +00:00
|
|
|
test1 := st.NewNick("test1")
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if n := st.GetNick("test1"); !test1.Equals(n) {
|
2011-10-12 22:06:46 +00:00
|
|
|
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) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-11-05 12:22:04 +00:00
|
|
|
test1 := st.NewNick("test1")
|
2011-10-12 22:06:46 +00:00
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
// This channel is here to ensure that its lookup map gets updated
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewChannel("#chan1")
|
|
|
|
st.Associate("#chan1", "test1")
|
|
|
|
|
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
n1 := st.nicks["test1"]
|
|
|
|
c1 := st.chans["#chan1"]
|
2011-10-22 22:58:06 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
test2 := st.ReNick("test1", "test2")
|
2011-10-12 22:06:46 +00:00
|
|
|
|
|
|
|
if _, ok := st.nicks["test1"]; ok {
|
|
|
|
t.Errorf("Nick test1 still exists after ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if n, ok := st.nicks["test2"]; !ok || n != n1 {
|
2011-10-12 22:06:46 +00:00
|
|
|
t.Errorf("Nick test2 doesn't exist after ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if _, ok := c1.lookup["test1"]; ok {
|
2011-10-22 22:58:06 +00:00
|
|
|
t.Errorf("Channel #chan1 still knows about test1 after ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if n, ok := c1.lookup["test2"]; !ok || n != n1 {
|
2011-10-22 22:58:06 +00:00
|
|
|
t.Errorf("Channel #chan1 doesn't know about test2 after ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if test1.Nick != "test1" {
|
|
|
|
t.Errorf("Nick test1 changed unexpectedly.")
|
|
|
|
}
|
|
|
|
if !test2.Equals(n1.Nick()) {
|
|
|
|
t.Errorf("Nick test2 did not change.")
|
2011-10-13 22:19:31 +00:00
|
|
|
}
|
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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.lookup) != 1 {
|
|
|
|
t.Errorf("Channel lookup list changed size during ReNick.")
|
|
|
|
}
|
2011-10-13 19:44:44 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
n2 := st.nicks["test1"]
|
|
|
|
fail := st.ReNick("test1", "test2")
|
2011-10-22 22:58:06 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if n, ok := st.nicks["test2"]; !ok || n != n1 {
|
2011-10-13 19:44:44 +00:00
|
|
|
t.Errorf("Nick test2 overwritten/deleted by ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if n, ok := st.nicks["test1"]; !ok || n != n2 {
|
2011-10-13 19:44:44 +00:00
|
|
|
t.Errorf("Nick test1 overwritten/deleted by ReNick.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if fail != nil {
|
|
|
|
t.Errorf("ReNick returned Nick on failure.")
|
|
|
|
}
|
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
|
|
|
func TestSTDelNick(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-13 19:44:44 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
add := st.NewNick("test1")
|
|
|
|
del := st.DelNick("test1")
|
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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if !add.Equals(del) {
|
|
|
|
t.Errorf("DelNick returned different nick.")
|
|
|
|
}
|
2011-10-13 19:44:44 +00:00
|
|
|
|
2011-11-05 12:22:04 +00:00
|
|
|
// Deleting unknown nick shouldn't work, but let's make sure we have a
|
|
|
|
// known nick first to catch any possible accidental removals.
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
fail := st.DelNick("test2")
|
|
|
|
if fail != nil || len(st.nicks) != 2 {
|
2011-10-22 22:58:06 +00:00
|
|
|
t.Errorf("Deleting unknown nick had unexpected side-effects.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting my nick shouldn't work
|
2014-12-31 13:17:46 +00:00
|
|
|
fail = st.DelNick("mynick")
|
|
|
|
if fail != nil || 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
|
2014-12-31 13:17:46 +00:00
|
|
|
// errors in other methods.
|
2011-10-27 15:41:56 +00:00
|
|
|
|
2011-11-05 12:22:04 +00:00
|
|
|
// Create a new channel for testing purposes.
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewChannel("#test1")
|
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
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Associate("#test1", "mynick")
|
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
|
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
n1 := st.nicks["test1"]
|
|
|
|
c1 := st.chans["#test1"]
|
2011-10-27 15:04:45 +00:00
|
|
|
|
2011-10-27 15:41:56 +00:00
|
|
|
// Test we have the expected starting state (at least vaguely)
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 1 || len(n1.chans) != 1 || len(st.chans) != 1 {
|
2011-10-27 15:04:45 +00:00
|
|
|
t.Errorf("Bad initial state for test DelNick() channel dissociation.")
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// Actual deletion tested above...
|
2011-10-27 15:04:45 +00:00
|
|
|
st.DelNick("test1")
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 1 || len(st.nicks) != 1 ||
|
|
|
|
len(st.me.chans) != 1 || len(n1.chans) != 0 || len(st.chans) != 1 {
|
2011-10-27 15:04:45 +00:00
|
|
|
t.Errorf("Deleting nick didn't dissociate correctly from channels.")
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if _, ok := c1.nicks[n1]; ok {
|
2011-10-27 15:04:45 +00:00
|
|
|
t.Errorf("Nick not removed from channel's nick map.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if _, ok := c1.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
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
func TestSTNickInfo(t *testing.T) {
|
|
|
|
st := NewTracker("mynick")
|
|
|
|
test1 := st.NewNick("test1")
|
|
|
|
test2 := st.NickInfo("test1", "foo", "bar", "baz")
|
|
|
|
test3 := st.GetNick("test1")
|
|
|
|
|
|
|
|
if test1.Equals(test2) {
|
|
|
|
t.Errorf("NickInfo did not return modified nick.")
|
|
|
|
}
|
|
|
|
if !test3.Equals(test2) {
|
|
|
|
t.Errorf("Getting nick after NickInfo returned different nick.")
|
|
|
|
}
|
|
|
|
test1.Ident, test1.Host, test1.Name = "foo", "bar", "baz"
|
|
|
|
if !test1.Equals(test2) {
|
|
|
|
t.Errorf("NickInfo did not set nick info correctly.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail := st.NickInfo("test2", "foo", "bar", "baz"); fail != nil {
|
|
|
|
t.Errorf("NickInfo for nonexistent nick did not return nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTNickModes(t *testing.T) {
|
|
|
|
st := NewTracker("mynick")
|
|
|
|
test1 := st.NewNick("test1")
|
|
|
|
test2 := st.NickModes("test1", "+iB")
|
|
|
|
test3 := st.GetNick("test1")
|
|
|
|
|
|
|
|
if test1.Equals(test2) {
|
|
|
|
t.Errorf("NickModes did not return modified nick.")
|
|
|
|
}
|
|
|
|
if !test3.Equals(test2) {
|
|
|
|
t.Errorf("Getting nick after NickModes returned different nick.")
|
|
|
|
}
|
|
|
|
test1.Modes.Invisible, test1.Modes.Bot = true, true
|
|
|
|
if !test1.Equals(test2) {
|
|
|
|
t.Errorf("NickModes did not set nick modes correctly.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail := st.NickModes("test2", "whatevs"); fail != nil {
|
|
|
|
t.Errorf("NickModes for nonexistent nick did not return nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTNewChannel(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
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")
|
|
|
|
|
2013-01-23 22:33:01 +00:00
|
|
|
if test1 == nil || test1.Name != "#test1" {
|
2011-10-13 21:31:09 +00:00
|
|
|
t.Errorf("Channel object created incorrectly by NewChannel.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if c, ok := st.chans["#test1"]; !ok || !test1.Equals(c.Channel()) || len(st.chans) != 1 {
|
2011-10-13 21:31:09 +00:00
|
|
|
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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if fail := st.NewChannel(""); fail != nil {
|
|
|
|
t.Errorf("Creating empty chan did not produce nil return.")
|
|
|
|
}
|
2011-10-13 21:31:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTGetChannel(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2011-11-05 12:22:04 +00:00
|
|
|
test1 := st.NewChannel("#test1")
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if c := st.GetChannel("#test1"); !test1.Equals(c) {
|
2011-10-13 21:31:09 +00:00
|
|
|
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) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
add := st.NewChannel("#test1")
|
|
|
|
del := st.DelChannel("#test1")
|
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.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if !add.Equals(del) {
|
|
|
|
t.Errorf("DelChannel returned different channel.")
|
|
|
|
}
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// Deleting unknown channel shouldn't work, but let's make sure we have a
|
|
|
|
// known channel first to catch any possible accidental removals.
|
|
|
|
st.NewChannel("#test1")
|
|
|
|
fail := st.DelChannel("#test2")
|
|
|
|
if fail != nil || len(st.chans) != 1 {
|
2011-10-13 21:31:09 +00:00
|
|
|
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.
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewChannel("#test2")
|
|
|
|
st.NewNick("test1")
|
2011-10-27 15:41:56 +00:00
|
|
|
|
|
|
|
// Associate both "my" nick and test1 with the channels
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Associate("#test1", "mynick")
|
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
st.Associate("#test2", "mynick")
|
|
|
|
st.Associate("#test2", "test1")
|
|
|
|
|
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
n1 := st.nicks["test1"]
|
|
|
|
c1 := st.chans["#test1"]
|
|
|
|
c2 := st.chans["#test2"]
|
2011-10-27 15:41:56 +00:00
|
|
|
|
|
|
|
// Test we have the expected starting state (at least vaguely)
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 2 || len(c2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(n1.chans) != 2 || len(st.chans) != 2 {
|
2011-10-27 15:41:56 +00:00
|
|
|
t.Errorf("Bad initial state for test DelChannel() nick dissociation.")
|
|
|
|
}
|
|
|
|
|
|
|
|
st.DelChannel("#test1")
|
|
|
|
|
|
|
|
// Test intermediate state. We're still on #test2 with test1, so test1
|
|
|
|
// shouldn't be deleted from state tracking itself just yet.
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 0 || len(c2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 1 || len(n1.chans) != 1 || len(st.chans) != 1 {
|
2011-10-27 15:41:56 +00:00
|
|
|
t.Errorf("Deleting channel didn't dissociate correctly from nicks.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if _, ok := n1.chans[c1]; ok {
|
2011-10-27 15:41:56 +00:00
|
|
|
t.Errorf("Channel not removed from nick's chans map.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if _, ok := n1.lookup["#test1"]; ok {
|
2011-10-27 15:41:56 +00:00
|
|
|
t.Errorf("Channel not removed from nick's lookup map.")
|
|
|
|
}
|
|
|
|
|
|
|
|
st.DelChannel("#test2")
|
|
|
|
|
|
|
|
// 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.
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 0 || len(c2.nicks) != 0 || len(st.nicks) != 1 ||
|
|
|
|
len(st.me.chans) != 0 || len(n1.chans) != 0 || len(st.chans) != 0 {
|
2011-10-27 15:41:56 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
func TestSTTopic(t *testing.T) {
|
|
|
|
st := NewTracker("mynick")
|
|
|
|
test1 := st.NewChannel("#test1")
|
|
|
|
test2 := st.Topic("#test1", "foo bar")
|
|
|
|
test3 := st.GetChannel("#test1")
|
|
|
|
|
|
|
|
if test1.Equals(test2) {
|
|
|
|
t.Errorf("Topic did not return modified channel.")
|
|
|
|
}
|
|
|
|
if !test3.Equals(test2) {
|
|
|
|
t.Errorf("Getting channel after Topic returned different channel.")
|
|
|
|
}
|
|
|
|
test1.Topic = "foo bar"
|
|
|
|
if !test1.Equals(test2) {
|
|
|
|
t.Errorf("Topic did not set channel topic correctly.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail := st.Topic("#test2", "foo baz"); fail != nil {
|
|
|
|
t.Errorf("Topic for nonexistent channel did not return nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTChannelModes(t *testing.T) {
|
|
|
|
st := NewTracker("mynick")
|
|
|
|
test1 := st.NewChannel("#test1")
|
|
|
|
test2 := st.ChannelModes("#test1", "+sk", "foo")
|
|
|
|
test3 := st.GetChannel("#test1")
|
|
|
|
|
|
|
|
if test1.Equals(test2) {
|
|
|
|
t.Errorf("ChannelModes did not return modified channel.")
|
|
|
|
}
|
|
|
|
if !test3.Equals(test2) {
|
|
|
|
t.Errorf("Getting channel after ChannelModes returned different channel.")
|
|
|
|
}
|
|
|
|
test1.Modes.Secret, test1.Modes.Key = true, "foo"
|
|
|
|
if !test1.Equals(test2) {
|
|
|
|
t.Errorf("ChannelModes did not set channel modes correctly.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fail := st.ChannelModes("test2", "whatevs"); fail != nil {
|
|
|
|
t.Errorf("ChannelModes for nonexistent channel did not return nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 22:58:06 +00:00
|
|
|
func TestSTIsOn(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-13 21:31:09 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
st.NewChannel("#test1")
|
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)")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
if priv, ok := st.IsOn("#test1", "test1"); !ok || priv == nil {
|
2011-10-13 21:31:09 +00:00
|
|
|
t.Errorf("test1 is on #test1 (now)")
|
|
|
|
}
|
|
|
|
}
|
2011-10-27 15:55:33 +00:00
|
|
|
|
|
|
|
func TestSTAssociate(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-27 15:55:33 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
st.NewChannel("#test1")
|
2011-10-27 15:55:33 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
n1 := st.nicks["test1"]
|
|
|
|
c1 := st.chans["#test1"]
|
|
|
|
|
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
npriv, nok := n1.chans[c1]
|
|
|
|
cpriv, cok := c1.nicks[n1]
|
|
|
|
if !nok || !cok || npriv != cpriv {
|
2011-11-05 12:22:04 +00:00
|
|
|
t.Errorf("#test1 was not associated with test1.")
|
|
|
|
}
|
2011-10-27 15:55:33 +00:00
|
|
|
|
|
|
|
// Test error cases
|
2014-12-31 13:17:46 +00:00
|
|
|
if st.Associate("", "test1") != nil {
|
|
|
|
t.Errorf("Associating unknown channel did not return nil.")
|
2013-01-23 22:33:01 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if st.Associate("#test1", "") != nil {
|
|
|
|
t.Errorf("Associating unknown nick did not return nil.")
|
2013-01-23 22:33:01 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if st.Associate("#test1", "test1") != nil {
|
2013-01-23 22:33:01 +00:00
|
|
|
t.Errorf("Associating already-associated things did not return nil.")
|
|
|
|
}
|
2011-10-27 16:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSTDissociate(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
st.NewChannel("#test1")
|
|
|
|
st.NewChannel("#test2")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// Associate both "my" nick and test1 with the channels
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Associate("#test1", "mynick")
|
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
st.Associate("#test2", "mynick")
|
|
|
|
st.Associate("#test2", "test1")
|
|
|
|
|
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
n1 := st.nicks["test1"]
|
|
|
|
c1 := st.chans["#test1"]
|
|
|
|
c2 := st.chans["#test2"]
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// Check the initial state looks mostly like we expect it to.
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 2 || len(c2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(n1.chans) != 2 || len(st.chans) != 2 {
|
2011-10-27 16:54:23 +00:00
|
|
|
t.Errorf("Initial state for dissociation tests looks odd.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, test the case of me leaving #test2
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Dissociate("#test2", "mynick")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// This should have resulted in the complete deletion of the channel.
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 2 || len(c2.nicks) != 0 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 1 || len(n1.chans) != 1 || len(st.chans) != 1 {
|
2011-10-27 16:54:23 +00:00
|
|
|
t.Errorf("Dissociating myself from channel didn't delete it correctly.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if st.GetChannel("#test2") != nil {
|
|
|
|
t.Errorf("Able to get channel after dissociating myself.")
|
|
|
|
}
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// Reassociating myself and test1 to #test2 shouldn't cause any errors.
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewChannel("#test2")
|
|
|
|
st.Associate("#test2", "mynick")
|
|
|
|
st.Associate("#test2", "test1")
|
|
|
|
|
|
|
|
// c2 is out of date with the complete deletion of the channel
|
|
|
|
c2 = st.chans["#test2"]
|
2011-10-27 16:54:23 +00:00
|
|
|
|
|
|
|
// Check state once moar.
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 2 || len(c2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(n1.chans) != 2 || len(st.chans) != 2 {
|
2011-10-27 16:54:23 +00:00
|
|
|
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.
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Dissociate("#test1", "test1")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 1 || len(c2.nicks) != 2 || len(st.nicks) != 2 ||
|
|
|
|
len(st.me.chans) != 2 || len(n1.chans) != 1 || len(st.chans) != 2 {
|
2011-10-27 16:54:23 +00:00
|
|
|
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
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Dissociate("#test2", "test1")
|
2011-10-27 16:54:23 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
if len(c1.nicks) != 1 || len(c2.nicks) != 1 || len(st.nicks) != 1 ||
|
|
|
|
len(st.me.chans) != 2 || len(n1.chans) != 0 || len(st.chans) != 2 {
|
2011-10-27 16:54:23 +00:00
|
|
|
t.Errorf("Dissociating a nick from it's last channel went wrong.")
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
if st.GetNick("test1") != nil {
|
|
|
|
t.Errorf("Able to get nick after dissociating from all channels.")
|
|
|
|
}
|
2011-10-27 15:55:33 +00:00
|
|
|
}
|
2011-11-05 02:25:25 +00:00
|
|
|
|
|
|
|
func TestSTWipe(t *testing.T) {
|
2013-01-23 22:33:01 +00:00
|
|
|
st := NewTracker("mynick")
|
2011-11-05 02:25:25 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
st.NewNick("test1")
|
|
|
|
st.NewNick("test2")
|
|
|
|
st.NewNick("test3")
|
|
|
|
st.NewChannel("#test1")
|
|
|
|
st.NewChannel("#test2")
|
|
|
|
st.NewChannel("#test3")
|
2011-11-05 02:25:25 +00:00
|
|
|
|
|
|
|
// Some associations
|
2014-12-31 13:17:46 +00:00
|
|
|
st.Associate("#test1", "mynick")
|
|
|
|
st.Associate("#test2", "mynick")
|
|
|
|
st.Associate("#test3", "mynick")
|
|
|
|
|
|
|
|
st.Associate("#test1", "test1")
|
|
|
|
st.Associate("#test2", "test2")
|
|
|
|
st.Associate("#test3", "test3")
|
|
|
|
|
|
|
|
st.Associate("#test1", "test2")
|
|
|
|
st.Associate("#test2", "test3")
|
|
|
|
|
|
|
|
st.Associate("#test1", "test3")
|
|
|
|
|
|
|
|
// We need to check out the manipulation of the internals.
|
|
|
|
nick1 := st.nicks["test1"]
|
|
|
|
nick2 := st.nicks["test2"]
|
|
|
|
nick3 := st.nicks["test3"]
|
|
|
|
chan1 := st.chans["#test1"]
|
|
|
|
chan2 := st.chans["#test2"]
|
|
|
|
chan3 := st.chans["#test3"]
|
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()
|
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
}
|