1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-05-13 19:13:20 +00:00

The great state tracker privatisation 1/3: channels.

This commit is contained in:
Alex Bramley 2014-12-31 13:17:11 +00:00
parent 8231942086
commit bffe946388
2 changed files with 141 additions and 88 deletions

View file

@ -1,23 +1,35 @@
package state
import (
"testing"
)
import "testing"
func compareChannel(t *testing.T, ch *channel) {
c := ch.Channel()
if c.Name != ch.name || c.Topic != ch.topic ||
!c.Modes.Equals(ch.modes) || len(c.Nicks) != len(ch.nicks) {
t.Errorf("Channel not duped correctly from internal state.")
}
for nk, cp := range ch.nicks {
if other, ok := c.Nicks[nk.nick]; !ok || !cp.Equals(other) {
t.Errorf("Nick not duped correctly from internal state.")
}
}
}
func TestNewChannel(t *testing.T) {
ch := NewChannel("#test1")
ch := newChannel("#test1")
if ch.Name != "#test1" {
if ch.name != "#test1" {
t.Errorf("Channel not created correctly by NewChannel()")
}
if len(ch.nicks) != 0 || len(ch.lookup) != 0 {
t.Errorf("Channel maps contain data after NewChannel()")
}
compareChannel(t, ch)
}
func TestAddNick(t *testing.T) {
ch := NewChannel("#test1")
nk := NewNick("test1")
ch := newChannel("#test1")
nk := newNick("test1")
cp := new(ChanPrivs)
ch.addNick(nk, cp)
@ -31,11 +43,12 @@ func TestAddNick(t *testing.T) {
if n, ok := ch.lookup["test1"]; !ok || n != nk {
t.Errorf("Nick test1 not properly stored in lookup map.")
}
compareChannel(t, ch)
}
func TestDelNick(t *testing.T) {
ch := NewChannel("#test1")
nk := NewNick("test1")
ch := newChannel("#test1")
nk := newNick("test1")
cp := new(ChanPrivs)
ch.addNick(nk, cp)
@ -49,18 +62,20 @@ func TestDelNick(t *testing.T) {
if n, ok := ch.lookup["#test1"]; ok || n != nil {
t.Errorf("Nick test1 not properly removed from lookup map.")
}
compareChannel(t, ch)
}
func TestChannelParseModes(t *testing.T) {
ch := NewChannel("#test1")
md := ch.Modes
ch := newChannel("#test1")
md := ch.modes
// Channel modes can adjust channel privs too, so we need a Nick
nk := NewNick("test1")
nk := newNick("test1")
cp := new(ChanPrivs)
ch.addNick(nk, cp)
// Test bools first.
compareChannel(t, ch)
if md.Private || md.Secret || md.ProtectedTopic || md.NoExternalMsg ||
md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly {
t.Errorf("Modes for new channel set to true.")
@ -72,8 +87,9 @@ func TestChannelParseModes(t *testing.T) {
md.InviteOnly = true
// Flip some MOAR bits.
ch.ParseModes("+s-p+tm-i")
ch.parseModes("+s-p+tm-i")
compareChannel(t, ch)
if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly {
t.Errorf("Modes not flipped correctly by ParseModes.")
@ -85,19 +101,22 @@ func TestChannelParseModes(t *testing.T) {
}
// enable limit correctly
ch.ParseModes("+l", "256")
ch.parseModes("+l", "256")
compareChannel(t, ch)
if md.Limit != 256 {
t.Errorf("Limit for channel not set correctly")
}
// enable limit incorrectly
ch.ParseModes("+l")
ch.parseModes("+l")
compareChannel(t, ch)
if md.Limit != 256 {
t.Errorf("Bad limit value caused limit to be unset.")
}
// disable limit correctly
ch.ParseModes("-l")
ch.parseModes("-l")
compareChannel(t, ch)
if md.Limit != 0 {
t.Errorf("Limit for channel not unset correctly")
}
@ -108,19 +127,22 @@ func TestChannelParseModes(t *testing.T) {
}
// enable key correctly
ch.ParseModes("+k", "foobar")
ch.parseModes("+k", "foobar")
compareChannel(t, ch)
if md.Key != "foobar" {
t.Errorf("Key for channel not set correctly")
}
// enable key incorrectly
ch.ParseModes("+k")
ch.parseModes("+k")
compareChannel(t, ch)
if md.Key != "foobar" {
t.Errorf("Bad key value caused key to be unset.")
}
// disable key correctly
ch.ParseModes("-k")
ch.parseModes("-k")
compareChannel(t, ch)
if md.Key != "" {
t.Errorf("Key for channel not unset correctly")
}
@ -128,16 +150,18 @@ func TestChannelParseModes(t *testing.T) {
// Test chan privs parsing.
cp.Op = true
cp.HalfOp = true
ch.ParseModes("+aq-o", "test1", "test1", "test1")
ch.parseModes("+aq-o", "test1", "test1", "test1")
compareChannel(t, ch)
if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice {
t.Errorf("Channel privileges not flipped correctly by ParseModes.")
}
// Test a random mix of modes, just to be sure
md.Limit = 256
ch.ParseModes("+zpt-qsl+kv-h", "test1", "foobar", "test1")
ch.parseModes("+zpt-qsl+kv-h", "test1", "foobar", "test1")
compareChannel(t, ch)
if !md.Private || md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly {
t.Errorf("Modes not flipped correctly by ParseModes (2).")