goirc/state/nick_test.go

107 lines
3.0 KiB
Go
Raw Normal View History

2011-10-22 22:58:06 +00:00
package state
import (
"testing"
)
func TestNewNick(t *testing.T) {
_, s := setUp(t)
defer s.tearDown()
2011-10-22 22:58:06 +00:00
nk := NewNick("test1", s.log)
if nk.Nick != "test1" || nk.l != s.log {
2011-10-22 22:58:06 +00:00
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) {
_, s := setUp(t)
defer s.tearDown()
nk := NewNick("test1", s.log)
ch := NewChannel("#test1", s.log)
2011-10-22 22:58:06 +00:00
cp := new(ChanPrivs)
nk.addChannel(ch, cp)
if len(nk.chans) != 1 || len(nk.lookup) != 1 {
2011-10-27 17:43:34 +00:00
t.Errorf("Channel lists not updated correctly for add.")
2011-10-22 22:58:06 +00:00
}
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.")
}
2011-10-27 17:43:34 +00:00
s.log.EXPECT().Warn("Nick.addChannel(): %s already on %s.",
"test1", "#test1")
2011-10-27 17:43:34 +00:00
nk.addChannel(ch, cp)
}
func TestDelChannel(t *testing.T) {
_, s := setUp(t)
defer s.tearDown()
nk := NewNick("test1", s.log)
ch := NewChannel("#test1", s.log)
2011-10-27 17:43:34 +00:00
cp := new(ChanPrivs)
// Testing the error state first is easier
s.log.EXPECT().Warn("Nick.delChannel(): %s not on %s.", "test1", "#test1")
2011-10-27 17:43:34 +00:00
nk.delChannel(ch)
nk.addChannel(ch, cp)
nk.delChannel(ch)
if len(nk.chans) != 0 || len(nk.lookup) != 0 {
t.Errorf("Channel lists not updated correctly for del.")
}
if c, ok := nk.chans[ch]; ok || c != nil {
t.Errorf("Channel #test1 not properly removed from chans map.")
}
if c, ok := nk.lookup["#test1"]; ok || c != nil {
t.Errorf("Channel #test1 not properly removed from lookup map.")
}
2011-10-22 22:58:06 +00:00
}
2011-11-03 03:01:50 +00:00
2011-11-03 04:15:12 +00:00
func TestNickParseModes(t *testing.T) {
_, s := setUp(t)
defer s.tearDown()
nk := NewNick("test1", s.log)
2011-11-03 03:01:50 +00:00
md := nk.Modes
// Modes should all be false for a new nick
if md.Invisible || md.Oper || md.WallOps || md.HiddenHost || md.SSL {
t.Errorf("Modes for new nick set to true.")
}
// Set a couple of modes, for testing.
md.Invisible = true
md.HiddenHost = true
// Parse a mode line that flips one true to false and two false to true
nk.ParseModes("+z-x+w")
if !md.Invisible || md.Oper || !md.WallOps || md.HiddenHost || !md.SSL {
t.Errorf("Modes not flipped correctly by ParseModes.")
}
// Check that passing an unknown mode char results in an info log
// The cast to byte here is needed to pass; gomock uses reflect.DeepEqual
// to examine argument equality, but 'd' (when not implicitly coerced to a
// uint8 by the type system) is an int, whereas string("+d")[1] is not.
// This type difference (despite the values being nominally the same)
// causes the test to fail with the following confusing error.
//
// no matching expected call: *logging.MockLogger.Info([Nick.ParseModes(): unknown mode char %c [100]])
// missing call(s) to *logging.MockLogger.Info(is equal to Nick.ParseModes(): unknown mode char %c, is equal to [100])
s.log.EXPECT().Info("Nick.ParseModes(): unknown mode char %c", byte('d'))
2011-11-03 03:01:50 +00:00
nk.ParseModes("+d")
}