-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
parser_test.go
277 lines (229 loc) · 6.07 KB
/
parser_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package irc_test
import (
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
"gopkg.in/irc.v4"
)
func BenchmarkParseMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
irc.MustParseMessage("@tag1=something :nick!user@host PRIVMSG #channel :some message")
}
}
func TestParseMessage(t *testing.T) {
t.Parallel()
var messageTests = []struct { //nolint:gofumpt
Input string
Err error
}{
{
Input: "",
Err: irc.ErrZeroLengthMessage,
},
{
Input: "@asdf",
Err: irc.ErrMissingDataAfterTags,
},
{
Input: ":asdf",
Err: irc.ErrMissingDataAfterPrefix,
},
{
Input: " :",
Err: irc.ErrMissingCommand,
},
{
Input: "PING :asdf",
},
}
for i, test := range messageTests {
m, err := irc.ParseMessage(test.Input)
assert.Equal(t, test.Err, err, "%d. Error didn't match expected", i)
if test.Err != nil {
assert.Nil(t, m, "%d. Didn't get nil message", i)
} else {
assert.NotNil(t, m, "%d. Got nil message", i)
}
}
}
func TestMustParseMessage(t *testing.T) {
t.Parallel()
assert.Panics(t, func() {
irc.MustParseMessage("")
}, "Didn't get expected panic")
assert.NotPanics(t, func() {
irc.MustParseMessage("PING :asdf")
}, "Got unexpected panic")
}
func TestMessageParam(t *testing.T) {
t.Parallel()
m := irc.MustParseMessage("PING :test")
assert.Equal(t, m.Param(0), "test")
assert.Equal(t, m.Param(-1), "")
assert.Equal(t, m.Param(2), "")
}
func TestMessageTrailing(t *testing.T) {
t.Parallel()
m := irc.MustParseMessage("PING :helloworld")
assert.Equal(t, "helloworld", m.Trailing())
m = irc.MustParseMessage("PING")
assert.Equal(t, "", m.Trailing())
}
func TestMessageCopy(t *testing.T) {
t.Parallel()
m := irc.MustParseMessage("@tag=val :user@host PING :helloworld")
// Ensure copied messages are equal
c := m.Copy()
assert.EqualValues(t, m, c, "Copied values are not equal")
// Ensure messages with modified tags don't match
c = m.Copy()
for k := range c.Tags {
c.Tags[k] += "junk"
}
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with modified tags should not match")
// Ensure messages with modified prefix don't match
c = m.Copy()
c.Prefix.Name += "junk"
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with modified identity should not match")
// Ensure messages with modified params don't match
c = m.Copy()
c.Params = append(c.Params, "junk")
assert.False(t, assert.ObjectsAreEqualValues(m, c), "Copied with additional params should not match")
// The message itself doesn't matter, we just need to make sure we
// don't error if the user does something crazy and makes Params
// nil.
m = irc.MustParseMessage("PING :hello world")
m.Prefix = nil
c = m.Copy()
assert.EqualValues(t, m, c, "nil prefix copy failed")
// Ensure an empty Params is copied as nil
m = irc.MustParseMessage("PING")
m.Params = []string{}
c = m.Copy()
assert.Nil(t, c.Params, "Expected nil for empty params")
}
// Everything beyond here comes from the testcases repo
type MsgSplitTests struct {
Tests []struct {
Desc string
Input string
Atoms struct {
Source *string
Verb string
Params []string
Tags map[string]interface{}
}
}
}
func TestMsgSplit(t *testing.T) {
t.Parallel()
data, err := ioutil.ReadFile("./_testcases/tests/msg-split.yaml")
require.NoError(t, err)
var splitTests MsgSplitTests
err = yaml.Unmarshal(data, &splitTests)
require.NoError(t, err)
for _, test := range splitTests.Tests {
msg, err := irc.ParseMessage(test.Input)
assert.NoError(t, err, "%s: Failed to parse: %s (%s)", test.Desc, test.Input, err)
assert.Equal(t,
strings.ToUpper(test.Atoms.Verb), msg.Command,
"%s: Wrong command for input: %s", test.Desc, test.Input,
)
assert.Equal(t,
test.Atoms.Params, msg.Params,
"%s: Wrong params for input: %s", test.Desc, test.Input,
)
if test.Atoms.Source != nil {
assert.Equal(t, *test.Atoms.Source, msg.Prefix.String())
}
assert.Equal(t,
len(test.Atoms.Tags), len(msg.Tags),
"%s: Wrong number of tags",
test.Desc,
)
for k, v := range test.Atoms.Tags {
tag, ok := msg.Tags[k]
assert.True(t, ok, "Missing tag")
if v == nil {
assert.EqualValues(t, "", tag, "%s: Tag %q differs: %s != \"\"", test.Desc, k, tag)
} else {
assert.EqualValues(t, v, tag, "%s: Tag %q differs: %s != %s", test.Desc, k, v, tag)
}
}
}
}
type MsgJoinTests struct {
Tests []struct {
Desc string
Atoms struct {
Source string
Verb string
Params []string
Tags map[string]interface{}
}
Matches []string
}
}
func TestMsgJoin(t *testing.T) {
var ok bool
t.Parallel()
data, err := ioutil.ReadFile("./_testcases/tests/msg-join.yaml")
require.NoError(t, err)
var splitTests MsgJoinTests
err = yaml.Unmarshal(data, &splitTests)
require.NoError(t, err)
for _, test := range splitTests.Tests {
msg := &irc.Message{
Prefix: irc.ParsePrefix(test.Atoms.Source),
Command: test.Atoms.Verb,
Params: test.Atoms.Params,
Tags: make(map[string]string),
}
for k, v := range test.Atoms.Tags {
if v == nil {
msg.Tags[k] = ""
} else {
msg.Tags[k], ok = v.(string)
assert.True(t, ok)
}
}
assert.Contains(t, test.Matches, msg.String())
}
}
type UserhostSplitTests struct {
Tests []struct {
Desc string
Source string
Atoms struct {
Nick string
User string
Host string
}
}
}
func TestUserhostSplit(t *testing.T) {
t.Parallel()
data, err := ioutil.ReadFile("./_testcases/tests/userhost-split.yaml")
require.NoError(t, err)
var userhostTests UserhostSplitTests
err = yaml.Unmarshal(data, &userhostTests)
require.NoError(t, err)
for _, test := range userhostTests.Tests {
prefix := irc.ParsePrefix(test.Source)
assert.Equal(t,
test.Atoms.Nick, prefix.Name,
"%s: Name did not match for input: %q", test.Desc, test.Source,
)
assert.Equal(t,
test.Atoms.User, prefix.User,
"%s: User did not match for input: %q", test.Desc, test.Source,
)
assert.Equal(t,
test.Atoms.Host, prefix.Host,
"%s: Host did not match for input: %q", test.Desc, test.Source,
)
}
}