Moar testing and updates.

This commit is contained in:
Alex Bramley 2011-10-22 23:58:06 +01:00
parent 1fbd0a8b17
commit 6ded98b9e5
2 changed files with 141 additions and 28 deletions

38
state/nick_test.go Normal file
View File

@ -0,0 +1,38 @@
package state
import (
"github.com/fluffle/goirc/logging"
"testing"
)
func TestNewNick(t *testing.T) {
l, _ := logging.NewMock()
nk := NewNick("test1", l)
if nk.Nick != "test1" || nk.l != l {
t.Errorf("Nick not created correctly by NewNick()")
}
if len(nk.chans) != 0 || len(nk.lookup) != 0 {
t.Errorf("Nick maps contain data after NewNick()")
}
}
func TestAddChannel(t *testing.T) {
l, m := logging.NewMock()
nk := NewNick("test1", l)
ch := NewChannel("#test1", l)
cp := new(ChanPrivs)
nk.addChannel(ch, cp)
m.CheckNothingWritten(t)
if len(nk.chans) != 1 || len(nk.lookup) != 1 {
t.Errorf("Channel lists not updated correctly.")
}
if c, ok := nk.chans[ch]; !ok || c != cp {
t.Errorf("Channel #test1 not properly stored in chans map.")
}
if c, ok := nk.lookup["#test1"]; !ok || c != ch {
t.Errorf("Channel #test1 not properly stored in lookup map.")
}
}

View File

@ -1,19 +1,40 @@
package state package state
import ( import (
"github.com/fluffle/goirc/logging"
"testing" "testing"
) )
func TestNewNick(t *testing.T) { func TestSTNewTracker(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
m.CheckNothingWritten(t)
if st.l != l {
t.Errorf("State tracker's logger not set correctly.")
}
if len(st.nicks) != 1 { if len(st.nicks) != 1 {
t.Errorf("Nick list of new tracker is not 1 (me!).") t.Errorf("Nick list of new tracker is not 1 (me!).")
} }
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) {
l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := st.NewNick("test1") test1 := st.NewNick("test1")
m.CheckNothingWritten(t)
if test1 == nil || test1.Nick != "test1" { if test1 == nil || test1.Nick != "test1" || test1.l != l {
t.Errorf("Nick object created incorrectly by NewNick.") t.Errorf("Nick object created incorrectly by NewNick.")
} }
if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 { if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 {
@ -23,12 +44,16 @@ func TestNewNick(t *testing.T) {
if fail := st.NewNick("test1"); fail != nil { if fail := st.NewNick("test1"); fail != nil {
t.Errorf("Creating duplicate nick did not produce nil return.") t.Errorf("Creating duplicate nick did not produce nil return.")
} }
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.NewNick(): test1 already tracked.")
} }
func TestGetNick(t *testing.T) { func TestSTGetNick(t *testing.T) {
st := NewTracker("mynick") l, _ := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := NewNick("test1") test1 := NewNick("test1", l)
st.nicks["test1"] = test1 st.nicks["test1"] = test1
if n := st.GetNick("test1"); n != test1 { if n := st.GetNick("test1"); n != test1 {
@ -42,13 +67,22 @@ func TestGetNick(t *testing.T) {
} }
} }
func TestReNick(t *testing.T) { func TestSTReNick(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := NewNick("test1") test1 := NewNick("test1", l)
st.nicks["test1"] = test1 st.nicks["test1"] = test1
// 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)
st.ReNick("test1", "test2") st.ReNick("test1", "test2")
m.CheckNothingWritten(t)
if _, ok := st.nicks["test1"]; ok { if _, ok := st.nicks["test1"]; ok {
t.Errorf("Nick test1 still exists after ReNick.") t.Errorf("Nick test1 still exists after ReNick.")
@ -56,6 +90,12 @@ func TestReNick(t *testing.T) {
if n, ok := st.nicks["test2"]; !ok || n != test1 { if n, ok := st.nicks["test2"]; !ok || n != test1 {
t.Errorf("Nick test2 doesn't exist after ReNick.") t.Errorf("Nick test2 doesn't exist after ReNick.")
} }
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.")
}
if test1.Nick != "test2" { if test1.Nick != "test2" {
t.Errorf("Nick test1 not changed correctly.") t.Errorf("Nick test1 not changed correctly.")
} }
@ -63,10 +103,13 @@ func TestReNick(t *testing.T) {
t.Errorf("Nick list changed size during ReNick.") t.Errorf("Nick list changed size during ReNick.")
} }
test2 := NewNick("test1") test2 := NewNick("test1", l)
st.nicks["test1"] = test2 st.nicks["test1"] = test2
st.ReNick("test1", "test2") st.ReNick("test1", "test2")
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.ReNick(): test2 already exists.")
if n, ok := st.nicks["test2"]; !ok || n != test1 { if n, ok := st.nicks["test2"]; !ok || n != test1 {
t.Errorf("Nick test2 overwritten/deleted by ReNick.") t.Errorf("Nick test2 overwritten/deleted by ReNick.")
} }
@ -77,15 +120,21 @@ func TestReNick(t *testing.T) {
t.Errorf("Nick list changed size during ReNick.") t.Errorf("Nick list changed size during ReNick.")
} }
st.ReNick("test3", "test2")
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.ReNick(): test3 not tracked.")
} }
func TestDelNick(t *testing.T) { func TestSTDelNick(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := NewNick("test1") test1 := NewNick("test1", l)
st.nicks["test1"] = test1 st.nicks["test1"] = test1
st.DelNick("test1") st.DelNick("test1")
m.CheckNothingWritten(t)
if _, ok := st.nicks["test1"]; ok { if _, ok := st.nicks["test1"]; ok {
t.Errorf("Nick test1 still exists after DelNick.") t.Errorf("Nick test1 still exists after DelNick.")
@ -97,22 +146,36 @@ func TestDelNick(t *testing.T) {
st.nicks["test1"] = test1 st.nicks["test1"] = test1
st.DelNick("test2") st.DelNick("test2")
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.DelNick(): test2 not tracked.")
if len(st.nicks) != 2 { if len(st.nicks) != 2 {
t.Errorf("DelNick had unexpected side-effects.") t.Errorf("Deleting unknown nick had unexpected side-effects.")
}
// Deleting my nick shouldn't work
st.DelNick("mynick")
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.DelNick(): won't delete myself.")
if len(st.nicks) != 2 {
t.Errorf("Deleting myself had unexpected side-effects.")
} }
} }
func TestNewChannel(t *testing.T) { func TestSTNewChannel(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
if len(st.chans) != 0 { if len(st.chans) != 0 {
t.Errorf("Channel list of new tracker is non-zero length.") t.Errorf("Channel list of new tracker is non-zero length.")
} }
test1 := st.NewChannel("#test1") test1 := st.NewChannel("#test1")
m.CheckNothingWritten(t)
if test1 == nil || test1.Name != "#test1" { if test1 == nil || test1.Name != "#test1" || test1.l != l {
t.Errorf("Channel object created incorrectly by NewChannel.") t.Errorf("Channel object created incorrectly by NewChannel.")
} }
if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 { if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 {
@ -122,12 +185,16 @@ func TestNewChannel(t *testing.T) {
if fail := st.NewChannel("#test1"); fail != nil { if fail := st.NewChannel("#test1"); fail != nil {
t.Errorf("Creating duplicate chan did not produce nil return.") t.Errorf("Creating duplicate chan did not produce nil return.")
} }
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.NewChannel(): #test1 already tracked.")
} }
func TestGetChannel(t *testing.T) { func TestSTGetChannel(t *testing.T) {
st := NewTracker("mynick") l, _ := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := NewChannel("#test1") test1 := NewChannel("#test1", l)
st.chans["#test1"] = test1 st.chans["#test1"] = test1
if c := st.GetChannel("#test1"); c != test1 { if c := st.GetChannel("#test1"); c != test1 {
@ -141,13 +208,16 @@ func TestGetChannel(t *testing.T) {
} }
} }
func TestDelChannel(t *testing.T) { func TestSTDelChannel(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
test1 := NewChannel("#test1") test1 := NewChannel("#test1", l)
st.chans["#test1"] = test1 st.chans["#test1"] = test1
st.DelChannel("#test1") st.DelChannel("#test1")
m.CheckNothingWritten(t)
if _, ok := st.chans["#test1"]; ok { if _, ok := st.chans["#test1"]; ok {
t.Errorf("Channel test1 still exists after DelChannel.") t.Errorf("Channel test1 still exists after DelChannel.")
@ -158,19 +228,23 @@ func TestDelChannel(t *testing.T) {
st.chans["#test1"] = test1 st.chans["#test1"] = test1
st.DelChannel("test2") st.DelChannel("#test2")
m.CheckWrittenAtLevel(t, logging.Warn,
"StateTracker.DelChannel(): #test2 not tracked.")
if len(st.chans) != 1 { if len(st.chans) != 1 {
t.Errorf("DelChannel had unexpected side-effects.") t.Errorf("DelChannel had unexpected side-effects.")
} }
} }
func TestIsOn(t *testing.T) { func TestSTIsOn(t *testing.T) {
st := NewTracker("mynick") l, m := logging.NewMock()
l.SetLogLevel(logging.Debug)
st := NewTracker("mynick", l)
nick1 := NewNick("test1") nick1 := NewNick("test1", l)
st.nicks["test1"] = nick1 st.nicks["test1"] = nick1
chan1 := NewChannel("#test1") chan1 := NewChannel("#test1", l)
st.chans["#test1"] = chan1 st.chans["#test1"] = chan1
if st.IsOn("#test1", "test1") { if st.IsOn("#test1", "test1") {
@ -182,4 +256,5 @@ func TestIsOn(t *testing.T) {
if !st.IsOn("#test1", "test1") { if !st.IsOn("#test1", "test1") {
t.Errorf("test1 is on #test1 (now)") t.Errorf("test1 is on #test1 (now)")
} }
m.CheckNothingWritten(t)
} }