3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00
ergo/irc/caps/set_test.go

94 lines
2.6 KiB
Go
Raw Normal View History

2017-10-07 14:19:37 +02:00
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package caps
import "testing"
import "reflect"
func TestSets(t *testing.T) {
s1 := NewSet()
s1.Enable(AccountTag, EchoMessage, UserhostInNames)
if !(s1.Has(AccountTag) && s1.Has(EchoMessage) && s1.Has(UserhostInNames)) {
2017-10-07 14:19:37 +02:00
t.Error("Did not have the tags we expected")
}
if s1.Has(STS) {
t.Error("Has() returned true when we don't have the given capability")
2017-10-07 14:19:37 +02:00
}
s1.Disable(AccountTag)
if s1.Has(AccountTag) {
t.Error("Disable() did not correctly disable the given capability")
}
enabledCaps := NewSet()
enabledCaps.Union(s1)
expectedCaps := NewSet(EchoMessage, UserhostInNames)
2017-10-07 14:19:37 +02:00
if !reflect.DeepEqual(enabledCaps, expectedCaps) {
t.Errorf("Enabled and expected capability lists do not match: %v, %v", enabledCaps, expectedCaps)
}
// make sure re-enabling doesn't add to the count or something weird like that
s1.Enable(EchoMessage)
2017-10-08 01:26:01 +02:00
// make sure add and remove work fine
s1.Add(InviteNotify)
s1.Remove(EchoMessage)
if !s1.Has(InviteNotify) || s1.Has(EchoMessage) {
t.Error("Add/Remove don't work")
2017-10-08 01:26:01 +02:00
}
2019-09-08 12:22:34 +02:00
// test Strings()
2019-08-27 06:51:09 +02:00
values := make(Values)
values[InviteNotify] = "invitemepls"
2017-10-08 01:26:01 +02:00
actualCap301ValuesString := s1.Strings(Cap301, values, 0)
2019-08-27 06:51:09 +02:00
expectedCap301ValuesString := []string{"invite-notify userhost-in-names"}
if !reflect.DeepEqual(actualCap301ValuesString, expectedCap301ValuesString) {
t.Errorf("Generated Cap301 values string [%v] did not match expected values string [%v]", actualCap301ValuesString, expectedCap301ValuesString)
2017-10-08 01:26:01 +02:00
}
actualCap302ValuesString := s1.Strings(Cap302, values, 0)
2019-08-27 06:51:09 +02:00
expectedCap302ValuesString := []string{"invite-notify=invitemepls userhost-in-names"}
if !reflect.DeepEqual(actualCap302ValuesString, expectedCap302ValuesString) {
2017-10-08 01:26:01 +02:00
t.Errorf("Generated Cap302 values string [%s] did not match expected values string [%s]", actualCap302ValuesString, expectedCap302ValuesString)
}
2017-10-07 14:19:37 +02:00
}
2018-06-25 12:50:19 +02:00
func TestSubtract(t *testing.T) {
s1 := NewSet(AccountTag, EchoMessage, UserhostInNames, ServerTime)
toRemove := NewSet(UserhostInNames, EchoMessage)
s1.Subtract(toRemove)
if !reflect.DeepEqual(s1, NewSet(AccountTag, ServerTime)) {
t.Errorf("subtract doesn't work")
}
}
2018-06-25 12:50:19 +02:00
func BenchmarkSetReads(b *testing.B) {
set := NewSet(UserhostInNames, EchoMessage)
b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Has(UserhostInNames)
set.Has(LabeledResponse)
set.Has(EchoMessage)
2020-09-09 17:46:05 +02:00
set.Has(Nope)
2018-06-25 12:50:19 +02:00
}
}
func BenchmarkSetWrites(b *testing.B) {
for i := 0; i < b.N; i++ {
set := NewSet(UserhostInNames, EchoMessage)
2020-09-09 17:46:05 +02:00
set.Add(Nope)
2018-06-25 12:50:19 +02:00
set.Add(ExtendedJoin)
set.Remove(UserhostInNames)
set.Remove(LabeledResponse)
}
}