1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-09-06 00:43:20 +00:00

add tags parsing and CAP command, with parsing tests

This commit is contained in:
Jake Bailey 2016-01-11 16:20:06 -06:00
parent 5cf08f7e9c
commit f6a94cc3a3
3 changed files with 103 additions and 4 deletions

View file

@ -1,12 +1,14 @@
package client
import (
"reflect"
"testing"
"time"
)
func TestLineCopy(t *testing.T) {
l1 := &Line{
Tags: map[string]string{"foo": "bar", "fizz": "buzz"},
Nick: "nick",
Ident: "ident",
Host: "host",
@ -20,7 +22,8 @@ func TestLineCopy(t *testing.T) {
l2 := l1.Copy()
// Ugly. Couldn't be bothered to bust out reflect and actually think.
if l2.Nick != "nick" || l2.Ident != "ident" || l2.Host != "host" ||
if l2.Tags == nil || l2.Tags["foo"] != "bar" || l2.Tags["fizz"] != "buzz" ||
l2.Nick != "nick" || l2.Ident != "ident" || l2.Host != "host" ||
l2.Src != "src" || l2.Cmd != "cmd" || l2.Raw != "raw" ||
l2.Args[0] != "arg" || l2.Args[1] != "text" || l2.Time != l1.Time {
t.Errorf("Line not copied correctly")
@ -28,6 +31,7 @@ func TestLineCopy(t *testing.T) {
}
// Now, modify l2 and verify l1 not changed
l2.Tags["foo"] = "baz"
l2.Nick = l2.Nick[1:]
l2.Ident = "foo"
l2.Host = ""
@ -35,7 +39,8 @@ func TestLineCopy(t *testing.T) {
l2.Args[1] = "bar"
l2.Time = time.Now()
if l1.Nick != "nick" || l1.Ident != "ident" || l1.Host != "host" ||
if l2.Tags == nil || l2.Tags["foo"] != "baz" || l2.Tags["fizz"] != "buzz" ||
l1.Nick != "nick" || l1.Ident != "ident" || l1.Host != "host" ||
l1.Src != "src" || l1.Cmd != "cmd" || l1.Raw != "raw" ||
l1.Args[0] != "arg" || l1.Args[1] != "text" || l1.Time == l2.Time {
t.Errorf("Original modified when copy changed")
@ -88,3 +93,56 @@ func TestLineTarget(t *testing.T) {
}
}
}
func TestLineTags(t *testing.T) {
tests := []struct {
in string
out *Line
}{
{ // Make sure non-tagged lines work
":nick!ident@host.com PRIVMSG me :Hello",
&Line{
Nick: "nick",
Ident: "ident",
Host: "host.com",
Src: "nick!ident@host.com",
Cmd: PRIVMSG,
Raw: ":nick!ident@host.com PRIVMSG me :Hello",
Args: []string{"me", "Hello"},
},
},
{ // Tags example from the spec
"@aaa=bbb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello",
&Line{
Tags: map[string]string{"aaa": "bbb", "ccc": "", "example.com/ddd": "eee"},
Nick: "nick",
Ident: "ident",
Host: "host.com",
Src: "nick!ident@host.com",
Cmd: PRIVMSG,
Raw: "@aaa=bbb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello",
Args: []string{"me", "Hello"},
},
},
{ // Test escaped characters
"@\\:=\\:;\\s=\\s;\\r=\\r;\\n=\\n :nick!ident@host.com PRIVMSG me :Hello",
&Line{
Tags: map[string]string{";": ";", " ": " ", "\r": "\r", "\n": "\n"},
Nick: "nick",
Ident: "ident",
Host: "host.com",
Src: "nick!ident@host.com",
Cmd: PRIVMSG,
Raw: "@\\:=\\:;\\s=\\s;\\r=\\r;\\n=\\n :nick!ident@host.com PRIVMSG me :Hello",
Args: []string{"me", "Hello"},
},
},
}
for i, test := range tests {
got := ParseLine(test.in)
if !reflect.DeepEqual(got, test.out) {
t.Errorf("test %d:\nexpected %#v\ngot %#v", i, test.out, got)
}
}
}