2017-09-07 01:26:16 +02:00
|
|
|
// Copyright (c) 2017 Daniel Oaks
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2022-05-06 04:34:43 +02:00
|
|
|
"fmt"
|
2017-09-07 01:26:16 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2018-02-03 11:28:02 +01:00
|
|
|
|
2021-05-25 06:34:38 +02:00
|
|
|
"github.com/ergochat/ergo/irc/modes"
|
2017-09-07 01:26:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseDefaultChannelModes(t *testing.T) {
|
|
|
|
nt := "+nt"
|
|
|
|
n := "+n"
|
|
|
|
empty := ""
|
|
|
|
tminusi := "+t -i"
|
|
|
|
|
|
|
|
var parseTests = []struct {
|
|
|
|
raw *string
|
2018-02-03 11:28:02 +01:00
|
|
|
expected modes.Modes
|
2017-09-07 01:26:16 +02:00
|
|
|
}{
|
2018-02-03 11:28:02 +01:00
|
|
|
{&nt, modes.Modes{modes.NoOutside, modes.OpOnlyTopic}},
|
|
|
|
{&n, modes.Modes{modes.NoOutside}},
|
|
|
|
{&empty, modes.Modes{}},
|
|
|
|
{&tminusi, modes.Modes{modes.OpOnlyTopic}},
|
|
|
|
{nil, modes.Modes{modes.NoOutside, modes.OpOnlyTopic}},
|
2017-09-07 01:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range parseTests {
|
2018-07-16 09:46:40 +02:00
|
|
|
result := ParseDefaultChannelModes(testcase.raw)
|
2017-09-07 01:26:16 +02:00
|
|
|
if !reflect.DeepEqual(result, testcase.expected) {
|
|
|
|
t.Errorf("expected modes %s, got %s", testcase.expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 06:38:20 +02:00
|
|
|
|
2020-04-30 05:53:33 +02:00
|
|
|
func TestParseDefaultUserModes(t *testing.T) {
|
|
|
|
iR := "+iR"
|
|
|
|
i := "+i"
|
|
|
|
empty := ""
|
|
|
|
rminusi := "+R -i"
|
|
|
|
|
|
|
|
var parseTests = []struct {
|
|
|
|
raw *string
|
2020-05-28 17:53:14 +02:00
|
|
|
expected modes.Modes
|
2020-04-30 05:53:33 +02:00
|
|
|
}{
|
2020-05-28 17:53:14 +02:00
|
|
|
{&iR, modes.Modes{modes.Invisible, modes.RegisteredOnly}},
|
|
|
|
{&i, modes.Modes{modes.Invisible}},
|
|
|
|
{&empty, modes.Modes{}},
|
|
|
|
{&rminusi, modes.Modes{modes.RegisteredOnly}},
|
|
|
|
{nil, modes.Modes{}},
|
2020-04-30 05:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range parseTests {
|
|
|
|
result := ParseDefaultUserModes(testcase.raw)
|
|
|
|
if !reflect.DeepEqual(result, testcase.expected) {
|
2020-05-28 17:53:14 +02:00
|
|
|
t.Errorf("expected modes %s, got %s", testcase.expected, result)
|
2020-04-30 05:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 06:38:20 +02:00
|
|
|
func TestUmodeGreaterThan(t *testing.T) {
|
|
|
|
if !umodeGreaterThan(modes.Halfop, modes.Voice) {
|
|
|
|
t.Errorf("expected Halfop > Voice")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !umodeGreaterThan(modes.Voice, modes.Mode(0)) {
|
|
|
|
t.Errorf("expected Voice > 0 (the zero value of modes.Mode)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if umodeGreaterThan(modes.ChannelAdmin, modes.ChannelAdmin) {
|
|
|
|
t.Errorf("modes should not be greater than themselves")
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 06:05:12 +02:00
|
|
|
|
2022-05-06 04:34:43 +02:00
|
|
|
func assertEqual(found, expected interface{}) {
|
|
|
|
if !reflect.DeepEqual(found, expected) {
|
|
|
|
panic(fmt.Sprintf("found %#v, expected %#v", found, expected))
|
2019-04-23 06:05:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChannelUserModeHasPrivsOver(t *testing.T) {
|
2022-05-06 04:34:43 +02:00
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Voice, modes.Halfop), false)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Mode(0), modes.Halfop), false)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Voice, modes.Mode(0)), false)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.ChannelAdmin, modes.ChannelAdmin), false)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Halfop, modes.Halfop), false)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Voice, modes.Voice), false)
|
2019-04-23 06:05:12 +02:00
|
|
|
|
2022-05-06 04:34:43 +02:00
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.Halfop, modes.Voice), true)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.ChannelFounder, modes.ChannelAdmin), true)
|
|
|
|
assertEqual(channelUserModeHasPrivsOver(modes.ChannelOperator, modes.ChannelOperator), true)
|
2019-04-23 06:05:12 +02:00
|
|
|
}
|