2015-06-10 03:48:54 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-06-10 03:48:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseMessage(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
expected *Message
|
|
|
|
}{
|
|
|
|
{
|
2019-01-11 03:53:50 +00:00
|
|
|
":user CMD #chan :some message",
|
2015-06-10 03:48:54 +00:00
|
|
|
&Message{
|
2016-12-12 06:19:22 +00:00
|
|
|
Prefix: "user",
|
|
|
|
Nick: "user",
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#chan", "some message"},
|
2015-06-10 03:48:54 +00:00
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
":nick!user@host.com CMD a b",
|
2015-06-10 03:48:54 +00:00
|
|
|
&Message{
|
|
|
|
Prefix: "nick!user@host.com",
|
|
|
|
Nick: "nick",
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"a", "b"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD a b :",
|
2015-06-10 03:48:54 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
2016-12-12 06:19:22 +00:00
|
|
|
Params: []string{"a", "b", ""},
|
2015-06-10 03:48:54 +00:00
|
|
|
},
|
2015-06-10 20:53:29 +00:00
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD a b",
|
2015-06-10 20:53:29 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"a", "b"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD",
|
2015-06-10 20:53:29 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD :tests and stuff",
|
2015-06-10 20:53:29 +00:00
|
|
|
&Message{
|
2016-12-12 06:19:22 +00:00
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"tests and stuff"},
|
2015-06-10 20:53:29 +00:00
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
":nick@host.com CMD",
|
2015-06-10 20:53:29 +00:00
|
|
|
&Message{
|
|
|
|
Prefix: "nick@host.com",
|
|
|
|
Nick: "nick",
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
":ni@ck!user!name@host!.com CMD",
|
2015-06-10 20:53:29 +00:00
|
|
|
&Message{
|
|
|
|
Prefix: "ni@ck!user!name@host!.com",
|
|
|
|
Nick: "ni@ck",
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
2017-07-03 21:31:14 +00:00
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD #cake pie ",
|
2017-07-03 21:31:14 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", "pie"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
" CMD #cake pie",
|
2017-07-03 21:31:14 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", "pie"},
|
|
|
|
},
|
2018-04-27 00:56:35 +00:00
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD #cake ::pie",
|
2018-04-27 00:56:35 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", ":pie"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD #cake : pie",
|
2018-04-27 00:56:35 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", " pie"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD #cake :pie :P <3",
|
2018-04-27 00:56:35 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", "pie :P <3"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"CMD #cake :pie!",
|
2018-04-27 00:56:35 +00:00
|
|
|
&Message{
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"#cake", "pie!"},
|
|
|
|
},
|
2018-04-29 01:49:02 +00:00
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"@x=y CMD",
|
2018-04-29 01:49:02 +00:00
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
},
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"@x=y :nick!user@host.com CMD",
|
2018-04-29 01:49:02 +00:00
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
},
|
|
|
|
Prefix: "nick!user@host.com",
|
|
|
|
Nick: "nick",
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"@x=y :nick!user@host.com CMD :pie and cake",
|
2018-04-29 01:49:02 +00:00
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
},
|
|
|
|
Prefix: "nick!user@host.com",
|
|
|
|
Nick: "nick",
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"pie and cake"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"@x=y :nick!user@host.com CMD beans rainbows :pie and cake",
|
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
},
|
|
|
|
Prefix: "nick!user@host.com",
|
|
|
|
Nick: "nick",
|
|
|
|
Command: "CMD",
|
|
|
|
Params: []string{"beans", "rainbows", "pie and cake"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"@x=y;a=b CMD",
|
2018-04-29 01:49:02 +00:00
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
"a": "b",
|
|
|
|
},
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
|
|
|
}, {
|
2019-01-11 03:53:50 +00:00
|
|
|
"@x=y;a=\\\\\\:\\s\\r\\n CMD",
|
2018-04-29 01:49:02 +00:00
|
|
|
&Message{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"x": "y",
|
|
|
|
"a": "\\; \r\n",
|
|
|
|
},
|
|
|
|
Command: "CMD",
|
|
|
|
},
|
2015-06-10 03:48:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2019-01-11 03:53:50 +00:00
|
|
|
assert.Equal(t, tc.expected, ParseMessage(tc.input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParseMessage(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ParseMessage("@x=y :nick!user@host.com CMD beans rainbows :pie and cake")
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-16 22:49:37 +00:00
|
|
|
|
2016-12-16 23:11:44 +00:00
|
|
|
func TestLastParam(t *testing.T) {
|
2019-01-11 03:53:50 +00:00
|
|
|
assert.Equal(t, "some message", ParseMessage(":user CMD #chan :some message").LastParam())
|
|
|
|
assert.Equal(t, "", ParseMessage("NO_PARAMS").LastParam())
|
2016-12-16 23:11:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 03:53:50 +00:00
|
|
|
func TestBadMessage(t *testing.T) {
|
|
|
|
assert.Nil(t, ParseMessage("@"))
|
|
|
|
assert.Nil(t, ParseMessage("@ :"))
|
|
|
|
assert.Nil(t, ParseMessage("@ :"))
|
|
|
|
assert.Nil(t, ParseMessage("@ :"))
|
|
|
|
assert.Nil(t, ParseMessage(":user"))
|
|
|
|
assert.Nil(t, ParseMessage(":"))
|
|
|
|
assert.Nil(t, ParseMessage(""))
|
2016-12-16 22:49:37 +00:00
|
|
|
}
|
2018-04-28 18:31:44 +00:00
|
|
|
|
|
|
|
func TestParseISupport(t *testing.T) {
|
|
|
|
s := newISupport()
|
|
|
|
s.parse([]string{"bob", "CAKE=31", "PIE", ":durr"})
|
|
|
|
assert.Equal(t, 31, s.GetInt("CAKE"))
|
|
|
|
assert.Equal(t, "31", s.Get("CAKE"))
|
|
|
|
assert.True(t, s.Has("CAKE"))
|
|
|
|
assert.True(t, s.Has("PIE"))
|
|
|
|
assert.False(t, s.Has("APPLES"))
|
|
|
|
assert.Equal(t, "", s.Get("APPLES"))
|
|
|
|
assert.Equal(t, 0, s.GetInt("APPLES"))
|
|
|
|
|
|
|
|
s.parse([]string{"bob", "-PIE", ":hurr"})
|
|
|
|
assert.False(t, s.Has("PIE"))
|
|
|
|
|
|
|
|
s.parse([]string{"bob", "CAKE=1337", ":durr"})
|
|
|
|
assert.Equal(t, 1337, s.GetInt("CAKE"))
|
|
|
|
|
|
|
|
s.parse([]string{"bob", "CAKE=", ":durr"})
|
|
|
|
assert.Equal(t, "", s.Get("CAKE"))
|
|
|
|
assert.True(t, s.Has("CAKE"))
|
|
|
|
|
|
|
|
s.parse([]string{"bob", "CAKE===", ":durr"})
|
|
|
|
assert.Equal(t, "==", s.Get("CAKE"))
|
|
|
|
|
|
|
|
s.parse([]string{"bob", "-CAKE=31", ":durr"})
|
|
|
|
assert.False(t, s.Has("CAKE"))
|
|
|
|
}
|