forked from lrstanley/girc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
191 lines (161 loc) · 7.62 KB
/
event_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) Liam Stanley <[email protected]>. 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 (
"reflect"
"testing"
)
func mockEvent() *Event {
return &Event{
Source: &Source{Name: "nick", Ident: "user", Host: "host.com"},
Command: "PRIVMSG",
Params: []string{"#channel"},
Trailing: "1 2 3",
}
}
func TestParseSource(t *testing.T) {
type args struct {
raw string
}
tests := []struct {
name string
args args
wantSrc *Source
}{
{name: "full", args: args{raw: "[email protected]"}, wantSrc: &Source{
Name: "nick", Ident: "user", Host: "hostname.com",
}},
{name: "special chars", args: args{raw: "^[][email protected]"}, wantSrc: &Source{
Name: "^[]nick", Ident: "~user", Host: "test.host---name.com",
}},
{name: "short", args: args{raw: "a!b@c"}, wantSrc: &Source{
Name: "a", Ident: "b", Host: "c",
}},
{name: "short", args: args{raw: "a!b"}, wantSrc: &Source{
Name: "a", Ident: "b", Host: "",
}},
{name: "short", args: args{raw: "a@b"}, wantSrc: &Source{
Name: "a", Ident: "", Host: "b",
}},
{name: "short", args: args{raw: "test"}, wantSrc: &Source{
Name: "test", Ident: "", Host: "",
}},
}
for _, tt := range tests {
gotSrc := ParseSource(tt.args.raw)
if !reflect.DeepEqual(gotSrc, tt.wantSrc) {
t.Errorf("ParseSource() = %v, want %v", gotSrc, tt.wantSrc)
}
if gotSrc.Len() != tt.wantSrc.Len() {
t.Errorf("ParseSource().Len() = %v, want %v", gotSrc.Len(), tt.wantSrc.Len())
}
if gotSrc.String() != tt.wantSrc.String() {
t.Errorf("ParseSource().String() = %v, want %v", gotSrc.String(), tt.wantSrc.String())
}
if gotSrc.IsServer() != tt.wantSrc.IsServer() {
t.Errorf("ParseSource().IsServer() = %v, want %v", gotSrc.IsServer(), tt.wantSrc.IsServer())
}
if gotSrc.IsHostmask() != tt.wantSrc.IsHostmask() {
t.Errorf("ParseSource().IsHostmask() = %v, want %v", gotSrc.IsHostmask(), tt.wantSrc.IsHostmask())
}
if !reflect.DeepEqual(gotSrc.Bytes(), tt.wantSrc.Bytes()) {
t.Errorf("ParseSource().Bytes() = %v, want %v", gotSrc, tt.wantSrc)
}
}
}
func TestParseEvent(t *testing.T) {
tests := []struct {
in string
want string
}{
{in: "", want: ""},
{in: ":host.domain.com TEST", want: ":host.domain.com TEST"},
{in: ":host.domain.com TEST\r\n", want: ":host.domain.com TEST"},
{in: ":host.domain.com TEST arg1 arg2", want: ":host.domain.com TEST arg1 arg2"},
{in: ":host.domain.com TEST :", want: ":host.domain.com TEST :"},
{in: ":host.domain.com TEST :test1", want: ":host.domain.com TEST :test1"},
{in: ":host.domain.com TEST arg1 arg2 :test1", want: ":host.domain.com TEST arg1 arg2 :test1"},
{in: ":host.domain.com TEST arg1 arg=:10 :test1", want: ":host.domain.com TEST arg1 arg=:10 :test1"},
{in: ":nick!user@host TEST :test1", want: ":nick!user@host TEST :test1"},
{in: ":nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000LONG TEXT TRUNCATED HERE", want: ":nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
{in: "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000LONG TEXT TRUNCATED HERE", want: "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST :test0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
{in: "@aaa=bbb :nick!user@host TEST :test1", want: "@aaa=bbb :nick!user@host TEST :test1"},
{in: "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST :test1", want: "@aaa=bbb;+ccc;example.com/ddd=eee :nick!user@host TEST :test1"},
{in: "@bbb=aaa;aaa :nick!user@host TEST :test1", want: "@aaa;bbb=aaa :nick!user@host TEST :test1"},
}
for _, tt := range tests {
got := ParseEvent(tt.in)
if got == nil && tt.want == "" {
continue
}
if got == nil {
t.Errorf("ParseEvent: got nil, want: %s", tt.want)
}
if got.String() != tt.want {
if got.Tags != nil {
if len(got.String()) != len(tt.want) {
t.Fatalf("ParseEvent: length exception in tag parse: got %q, want %q", got.String(), tt.want)
}
} else {
t.Fatalf("ParseEvent: got %q, want %q", got.String(), tt.want)
}
}
if got.Len() != len(tt.want) {
if got.Len() > 510 {
continue
}
t.Fatalf("Event.Len: got %d from %q, want %d", got.Len(), got.String(), len(tt.want))
}
}
}
func TestEventCopy(t *testing.T) {
var nilEvent *Event
if event := nilEvent.Copy(); event != nil {
t.Fatalf("Event.Copy: returned non-nil on nil event: %#v", event)
}
msg := "@aaa=bbb;ccc;example.com/ddd=eee :nick!user@host TEST arg1 arg2 :test1"
event := ParseEvent(msg)
eventCopy := event.Copy()
if !reflect.DeepEqual(event, eventCopy) {
t.Fatalf("Event.Copy: want %#v, got %#v", event, eventCopy)
}
// Since Event.Copy() calls Source.Copy()...
if !reflect.DeepEqual(event.Source, eventCopy.Source) {
t.Fatalf("Source.Copy: want %#v, got %#v", event.Source, eventCopy.Source)
}
event.Source = nil
if src := event.Source.Copy(); src != nil {
t.Fatalf("Source.Copy: returned non-nil on nil source: %#v", src)
}
}
func TestEventIs(t *testing.T) {
event := ParseEvent(":nick!user@host PRIVMSG #test :\x01ACTION this is a test\x01")
if !event.IsAction() {
t.Fatalf("Event.IsAction: returned false on %#v", event)
}
event.Command = "TEST"
if event.IsAction() {
t.Fatalf("Event.IsAction: returned true though not privmsg; %#v", event)
}
event.Command = "PRIVMSG"
event.Trailing = event.StripAction()
if event.IsAction() || event.Trailing != "this is a test" {
t.Fatalf("Event.IsAction: returned true on %#v", event)
}
if !event.IsFromChannel() {
t.Fatalf("Event.IsFromChannel: returned false on %#v", event)
}
event.Command = "TEST"
if event.IsFromChannel() {
t.Fatalf("Event.IsFromChannel: returned true though not privmsg; %#v", event)
}
event.Params[0] = "user1"
if event.IsFromUser() {
t.Fatalf("Event.IsFromUser: returned true when not privmsg; %#v", event)
}
event.Command = "PRIVMSG"
if !event.IsFromUser() {
t.Fatalf("Event.IsFromUser: returned false on %#v", event)
}
}