You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

set_test.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package caps
  4. import "testing"
  5. import "reflect"
  6. func TestSets(t *testing.T) {
  7. s1 := NewSet()
  8. s1.Enable(AccountTag, EchoMessage, UserhostInNames)
  9. if !(s1.Has(AccountTag) && s1.Has(EchoMessage) && s1.Has(UserhostInNames)) {
  10. t.Error("Did not have the tags we expected")
  11. }
  12. if s1.Has(STS) {
  13. t.Error("Has() returned true when we don't have the given capability")
  14. }
  15. s1.Disable(AccountTag)
  16. if s1.Has(AccountTag) {
  17. t.Error("Disable() did not correctly disable the given capability")
  18. }
  19. enabledCaps := NewSet()
  20. enabledCaps.Union(s1)
  21. expectedCaps := NewSet(EchoMessage, UserhostInNames)
  22. if !reflect.DeepEqual(enabledCaps, expectedCaps) {
  23. t.Errorf("Enabled and expected capability lists do not match: %v, %v", enabledCaps, expectedCaps)
  24. }
  25. // make sure re-enabling doesn't add to the count or something weird like that
  26. s1.Enable(EchoMessage)
  27. // make sure add and remove work fine
  28. s1.Add(InviteNotify)
  29. s1.Remove(EchoMessage)
  30. if !s1.Has(InviteNotify) || s1.Has(EchoMessage) {
  31. t.Error("Add/Remove don't work")
  32. }
  33. // test String()
  34. values := NewValues()
  35. values.Set(InviteNotify, "invitemepls")
  36. actualCap301ValuesString := s1.String(Cap301, values)
  37. expectedCap301ValuesString := "invite-notify userhost-in-names"
  38. if actualCap301ValuesString != expectedCap301ValuesString {
  39. t.Errorf("Generated Cap301 values string [%s] did not match expected values string [%s]", actualCap301ValuesString, expectedCap301ValuesString)
  40. }
  41. actualCap302ValuesString := s1.String(Cap302, values)
  42. expectedCap302ValuesString := "invite-notify=invitemepls userhost-in-names"
  43. if actualCap302ValuesString != expectedCap302ValuesString {
  44. t.Errorf("Generated Cap302 values string [%s] did not match expected values string [%s]", actualCap302ValuesString, expectedCap302ValuesString)
  45. }
  46. }
  47. func BenchmarkSetReads(b *testing.B) {
  48. set := NewSet(UserhostInNames, EchoMessage)
  49. b.ResetTimer()
  50. for i := 0; i < b.N; i++ {
  51. set.Has(UserhostInNames)
  52. set.Has(LabeledResponse)
  53. set.Has(EchoMessage)
  54. set.Has(Rename)
  55. }
  56. }
  57. func BenchmarkSetWrites(b *testing.B) {
  58. for i := 0; i < b.N; i++ {
  59. set := NewSet(UserhostInNames, EchoMessage)
  60. set.Add(Rename)
  61. set.Add(ExtendedJoin)
  62. set.Remove(UserhostInNames)
  63. set.Remove(LabeledResponse)
  64. }
  65. }