forked from lrstanley/girc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap_test.go
107 lines (87 loc) · 2.76 KB
/
cap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc
import "testing"
import "reflect"
func TestCapList(t *testing.T) {
c := New(Config{
Server: "irc.example.com",
Nick: "test",
User: "user",
SASL: &SASLPlain{User: "test", Pass: "example"},
SupportedCaps: map[string][]string{"example": nil},
})
var ok bool
ls := possibleCapList(c)
if _, ok = ls["batch"]; !ok {
t.Fatal("possibleCapList() missing std cap")
}
if _, ok = ls["example"]; !ok {
t.Fatal("possibleCapList() missing user provided cap")
}
if _, ok = ls["sasl"]; !ok {
t.Fatal("possibleCapList() missing sasl cap even though auth provided")
}
}
func TestParseCap(t *testing.T) {
tests := []struct {
in string
want map[string][]string
}{
{in: "userhost-in-names", want: map[string][]string{"userhost-in-names": nil}},
{in: "userhost-in-names test2", want: map[string][]string{"userhost-in-names": nil, "test2": nil}},
{in: "example/name=test", want: map[string][]string{"example/name": {"test"}}},
{
in: "userhost-in-names example/name example/name2=test,test2",
want: map[string][]string{
"userhost-in-names": nil,
"example/name": nil,
"example/name2": {"test", "test2"},
},
},
}
for _, tt := range tests {
got := parseCap(tt.in)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("parseCap(%q) == %#v :: want %#v", tt.in, got, tt.want)
}
}
}
func TestTagGetSetCount(t *testing.T) {
e := ParseEvent("@aaa=bbb :nick!user@host TEST :test1")
if e == nil || e.Tags == nil {
t.Fatal("event for get/set tests didn't parse successfully")
}
if e.Tags.Count() != 1 {
t.Fatalf("Event.Tag.Count() == %d, wanted 1", e.Tags.Count())
}
if v, _ := e.Tags.Get("aaa"); v != "bbb" {
t.Fatalf("Event.Tag.Get('aaa') == %s, wanted bbb", v)
}
if err := e.Tags.Set("bbb/test", "test1"); err != nil {
t.Fatal(err)
}
if e.Tags.Count() != 2 {
t.Fatal("tag count didn't increase after set")
}
if v, _ := e.Tags.Get("bbb/test"); v != "test1" {
t.Fatalf("Event.Tag.Get('bbb/test') == %s, wanted test1", v)
}
if ok := e.Tags.Remove("bbb/test"); !ok {
t.Fatal("tag removal of bbb/test failed")
}
if e.Tags.Count() != 1 {
t.Fatal("tag removal didn't decrease count")
}
if err := e.Tags.Set("invalid!key", ""); err == nil {
t.Fatal("tag set of invalid key should have returned error")
}
if err := e.Tags.Set("", ""); err == nil {
t.Fatal("tag set of empty key should have returned error")
}
// Add a hidden ascii value at the end to make it invalid.
if err := e.Tags.Set("key", "invalid-value"+string(0x08)); err == nil {
t.Fatal("tag set of invalid value should have returned error")
}
}