catching empty arguments in IRC() + tests

This commit is contained in:
tpltnt 2014-02-15 23:42:54 +01:00
parent ceacb7cda1
commit a4d40a90d5
2 changed files with 45 additions and 1 deletions

11
irc.go
View File

@ -359,8 +359,17 @@ func (irc *Connection) Connect(server string) error {
// Create a connection with the (publicly visible) nickname and username. // 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 { func IRC(nick, user string) *Connection {
// catch invalid values
if 0 == len(nick) {
return nil
}
if 0 == len(user) {
return nil
}
irc := &Connection{ irc := &Connection{
nick: nick, nick: nick,
user: user, user: user,

View File

@ -127,3 +127,38 @@ func TestClearCallback(t *testing.T) {
t.Error("Callbacks not cleared") 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")
}
}