From a4d40a90d5546d540a61acab133da373af55ca0f Mon Sep 17 00:00:00 2001 From: tpltnt Date: Sat, 15 Feb 2014 23:42:54 +0100 Subject: [PATCH] catching empty arguments in IRC() + tests --- irc.go | 11 ++++++++++- irc_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/irc.go b/irc.go index 4e13886..2efc26f 100644 --- a/irc.go +++ b/irc.go @@ -359,8 +359,17 @@ func (irc *Connection) Connect(server string) error { // Create a connection with the (publicly visible) nickname and username. -// The nickname is later used to address the user. +// The nickname is later used to address the user. Returns nil if nick +// or user are empty. func IRC(nick, user string) *Connection { + // catch invalid values + if 0 == len(nick) { + return nil + } + if 0 == len(user) { + return nil + } + irc := &Connection{ nick: nick, user: user, diff --git a/irc_test.go b/irc_test.go index 02fe5b4..42e090f 100644 --- a/irc_test.go +++ b/irc_test.go @@ -127,3 +127,38 @@ func TestClearCallback(t *testing.T) { t.Error("Callbacks not cleared") } } + +func TestIRCemptyNick(t *testing.T) { + irccon := IRC("", "go-eventirc") + irccon = nil + if nil != irccon { + t.Error("empty nick didn't result in error") + t.Fail() + } +/* + irccon.VerboseCallbackHandler = true + irccon.Debug = true + irccon.UseTLS = true + err := irccon.Connect("irc.freenode.net:7000") + if err != nil { + t.Fatal("Can't connect to freenode.") + } + irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) + + irccon.AddCallback("366", func(e *Event) { + irccon.Privmsg("#go-eventirc", "Test Message\n") + time.Sleep(2 * time.Second) + irccon.Quit() + }) + + irccon.Loop() +*/ +} + +func TestIRCemptyUser(t *testing.T) { + irccon := IRC("go-eventirc", "") + irccon = nil + if nil != irccon { + t.Error("empty user didn't result in error") + } +}