Fix up parseLine to use these new-fangled constants; fix Copy doc and test.

This commit is contained in:
Alex Bramley 2013-03-10 15:54:37 +00:00
parent 0c25d2d602
commit 332ff0a27d
2 changed files with 11 additions and 9 deletions

View File

@ -17,7 +17,7 @@ type Line struct {
Time time.Time Time time.Time
} }
// NOTE: this doesn't copy l.Time (this should be read-only anyway) // Copy() returns a deep copy of the Line.
func (l *Line) Copy() *Line { func (l *Line) Copy() *Line {
nl := *l nl := *l
nl.Args = make([]string, len(l.Args)) nl.Args = make([]string, len(l.Args))
@ -25,6 +25,7 @@ func (l *Line) Copy() *Line {
return &nl return &nl
} }
// parseLine() creates a Line from an incoming message from the IRC server.
func parseLine(s string) *Line { func parseLine(s string) *Line {
line := &Line{Raw: s} line := &Line{Raw: s}
if s[0] == ':' { if s[0] == ':' {
@ -62,7 +63,7 @@ func parseLine(s string) *Line {
// So, I think CTCP and (in particular) CTCP ACTION are better handled as // So, I think CTCP and (in particular) CTCP ACTION are better handled as
// separate events as opposed to forcing people to have gargantuan // separate events as opposed to forcing people to have gargantuan
// handlers to cope with the possibilities. // handlers to cope with the possibilities.
if (line.Cmd == "PRIVMSG" || line.Cmd == "NOTICE") && if (line.Cmd == PRIVMSG || line.Cmd == NOTICE) &&
len(line.Args[1]) > 2 && len(line.Args[1]) > 2 &&
strings.HasPrefix(line.Args[1], "\001") && strings.HasPrefix(line.Args[1], "\001") &&
strings.HasSuffix(line.Args[1], "\001") { strings.HasSuffix(line.Args[1], "\001") {
@ -72,16 +73,16 @@ func parseLine(s string) *Line {
// Replace the line with the unwrapped CTCP // Replace the line with the unwrapped CTCP
line.Args[1] = t[1] line.Args[1] = t[1]
} }
if c := strings.ToUpper(t[0]); c == "ACTION" && line.Cmd == "PRIVMSG" { if c := strings.ToUpper(t[0]); c == ACTION && line.Cmd == PRIVMSG {
// make a CTCP ACTION it's own event a-la PRIVMSG // make a CTCP ACTION it's own event a-la PRIVMSG
line.Cmd = c line.Cmd = c
} else { } else {
// otherwise, dispatch a generic CTCP/CTCPREPLY event that // otherwise, dispatch a generic CTCP/CTCPREPLY event that
// contains the type of CTCP in line.Args[0] // contains the type of CTCP in line.Args[0]
if line.Cmd == "PRIVMSG" { if line.Cmd == PRIVMSG {
line.Cmd = "CTCP" line.Cmd = CTCP
} else { } else {
line.Cmd = "CTCPREPLY" line.Cmd = CTCPREPLY
} }
line.Args = append([]string{c}, line.Args...) line.Args = append([]string{c}, line.Args...)
} }

View File

@ -5,7 +5,7 @@ import (
"time" "time"
) )
func TestCopy(t *testing.T) { func TestLineCopy(t *testing.T) {
l1 := &Line{ l1 := &Line{
Nick: "nick", Nick: "nick",
Ident: "ident", Ident: "ident",
@ -22,7 +22,7 @@ func TestCopy(t *testing.T) {
// Ugly. Couldn't be bothered to bust out reflect and actually think. // Ugly. Couldn't be bothered to bust out reflect and actually think.
if l2.Nick != "nick" || l2.Ident != "ident" || l2.Host != "host" || if l2.Nick != "nick" || l2.Ident != "ident" || l2.Host != "host" ||
l2.Src != "src" || l2.Cmd != "cmd" || l2.Raw != "raw" || l2.Src != "src" || l2.Cmd != "cmd" || l2.Raw != "raw" ||
l2.Args[0] != "arg" || l2.Args[1] != "text" { l2.Args[0] != "arg" || l2.Args[1] != "text" || l2.Time != l1.Time {
t.Errorf("Line not copied correctly") t.Errorf("Line not copied correctly")
t.Errorf("l1: %#v\nl2: %#v", l1, l2) t.Errorf("l1: %#v\nl2: %#v", l1, l2)
} }
@ -33,10 +33,11 @@ func TestCopy(t *testing.T) {
l2.Host = "" l2.Host = ""
l2.Args[0] = l2.Args[0][1:] l2.Args[0] = l2.Args[0][1:]
l2.Args[1] = "bar" l2.Args[1] = "bar"
l2.Time = time.Now()
if l1.Nick != "nick" || l1.Ident != "ident" || l1.Host != "host" || if l1.Nick != "nick" || l1.Ident != "ident" || l1.Host != "host" ||
l1.Src != "src" || l1.Cmd != "cmd" || l1.Raw != "raw" || l1.Src != "src" || l1.Cmd != "cmd" || l1.Raw != "raw" ||
l1.Args[0] != "arg" || l1.Args[1] != "text" { l1.Args[0] != "arg" || l1.Args[1] != "text" || l1.Time == l2.Time {
t.Errorf("Original modified when copy changed") t.Errorf("Original modified when copy changed")
t.Errorf("l1: %#v\nl2: %#v", l1, l2) t.Errorf("l1: %#v\nl2: %#v", l1, l2)
} }