111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestParsing(t *testing.T) {
|
|
testcases := []struct {
|
|
input string
|
|
output *Message
|
|
}{
|
|
// these inputs are based on
|
|
// https://github.com/ircdocs/parser-tests/blob/master/tests/msg-split.yaml
|
|
// and https://modern.ircdocs.horse/#messages
|
|
{
|
|
input: `:irc.example.com CAP LS * :multi-prefix extended-join sasl`,
|
|
output: &Message{
|
|
Source: "irc.example.com",
|
|
Command: CAP,
|
|
Parameters: []string{"LS", "*", "multi-prefix extended-join sasl"},
|
|
}},
|
|
{
|
|
input: `@id=234AB :dan!d@localhost PRIVMSG #chan :Hey what's up!`,
|
|
output: &Message{
|
|
Tags: map[string]string{"id": "234AB"},
|
|
Source: "dan!d@localhost",
|
|
Command: PRIVMSG,
|
|
Parameters: []string{"#chan", "Hey what's up!"},
|
|
}},
|
|
{
|
|
input: `@a=b;c=32;k;rt=ql7 :dan!d@localhost PRIVMSG #chan :Hey what's up!`,
|
|
output: &Message{
|
|
Tags: map[string]string{"a": "b", "c": "32", "k": "", "rt": "ql7"},
|
|
Source: "dan!d@localhost",
|
|
Command: PRIVMSG,
|
|
Parameters: []string{"#chan", "Hey what's up!"},
|
|
}},
|
|
{
|
|
input: `@tag1=value1;tag2;vendor1/tag3=value2;vendor2/tag4= :dan!d@localhost PRIVMSG #chan :Hey what's up!`,
|
|
output: &Message{
|
|
Tags: map[string]string{"tag1": "value1", "tag2": "", "vendor1/tag3": "value2", "vendor2/tag4": ""},
|
|
Source: "dan!d@localhost",
|
|
Command: PRIVMSG,
|
|
Parameters: []string{"#chan", "Hey what's up!"},
|
|
}},
|
|
{
|
|
input: `CAP REQ :sasl`,
|
|
output: &Message{
|
|
Command: CAP,
|
|
Parameters: []string{"REQ", "sasl"},
|
|
}},
|
|
{
|
|
input: `CAP REQ :`,
|
|
output: &Message{
|
|
Command: CAP,
|
|
Parameters: []string{"REQ", ""},
|
|
}},
|
|
{
|
|
input: `CAP REQ ::asdf`,
|
|
output: &Message{
|
|
Command: CAP,
|
|
Parameters: []string{"REQ", ":asdf"},
|
|
}},
|
|
{
|
|
input: `CAP REQ : asdf qwer`,
|
|
output: &Message{
|
|
Command: CAP,
|
|
Parameters: []string{"REQ", " asdf qwer"},
|
|
}},
|
|
{
|
|
input: `:coolguy PRIVMSG bar :lol :) `,
|
|
output: &Message{
|
|
Source: "coolguy",
|
|
Command: PRIVMSG,
|
|
Parameters: []string{"bar", "lol :) "},
|
|
}},
|
|
{
|
|
input: `:gravel.mozilla.org 432 #momo :Erroneous Nickname: Illegal characters`,
|
|
output: &Message{
|
|
Source: "gravel.mozilla.org",
|
|
Command: ERR_ERRONEUSNICKNAME,
|
|
Parameters: []string{"#momo", "Erroneous Nickname: Illegal characters"},
|
|
}},
|
|
{
|
|
input: `:gravel.mozilla.org MODE #tckk +n `,
|
|
output: &Message{
|
|
Source: "gravel.mozilla.org",
|
|
Command: MODE,
|
|
Parameters: []string{"#tckk", "+n"},
|
|
}},
|
|
}
|
|
for _, tc := range testcases {
|
|
m := &Message{Raw: tc.input}
|
|
m.ParseMessage()
|
|
if len(tc.output.Tags) != len(m.Tags) {
|
|
t.Logf("tags: actual: %v, expected: %v", m.Tags, tc.output.Tags)
|
|
t.Fail()
|
|
}
|
|
if tc.output.Source != m.Source {
|
|
t.Logf("source: actual: %v, expected: %v", m.Source, tc.output.Source)
|
|
t.Fail()
|
|
}
|
|
if tc.output.Command != m.Command {
|
|
t.Logf("command: actual: %v, expected: %v", m.Command, tc.output.Command)
|
|
t.Fail()
|
|
}
|
|
if len(tc.output.Parameters) != len(m.Parameters) {
|
|
t.Logf("parameters: actual: %v (%d), expected: %v (%d)", m.Parameters, len(m.Parameters), tc.output.Parameters, len(tc.output.Parameters))
|
|
t.Fail()
|
|
}
|
|
}
|
|
}
|