refactoring: hasConnectionValues() integrated into Connection() + more detailed errors

This commit is contained in:
tpltnt 2014-02-16 02:01:53 +01:00
parent 4dcf7d03c5
commit 48983c2abf
2 changed files with 84 additions and 119 deletions

48
irc.go
View File

@ -328,6 +328,29 @@ func (irc *Connection) Connect(server string) error {
irc.server = server
irc.stopped = false
// make sure everything is ready for connection
if 0 == len(irc.server) {
return errors.New("empty 'server'")
}
if 1 != strings.Count(irc.server, ":") {
return errors.New("wrong number of ':' in address")
}
if 0 == strings.Index(irc.server, ":") {
return errors.New("hostname is missing")
}
if (len(irc.server) - 1) == strings.Index(irc.server, ":") {
return errors.New("port missing")
}
if nil == irc.Log {
return errors.New("'Log' points to nil")
}
if 0 == len(irc.nick) {
return errors.New("empty 'user'")
}
if 0 == len(irc.user) {
return errors.New("empty 'user'")
}
var err error
if irc.UseTLS {
if irc.netsock, err = net.DialTimeout("tcp", irc.server, irc.Timeout); err == nil {
@ -389,28 +412,3 @@ func IRC(nick, user string) *Connection {
return irc
}
// Returns true if all values in struct allow for
// establishing a connection.
func (irc *Connection) hasConnectionValues() bool {
if 0 == len(irc.nick) {
return false
}
if 0 == len(irc.Version) {
return false
}
if 0 == len(irc.user) {
return false
}
if 0 == len(irc.server) {
return false
}
if nil == irc.Log {
return false
}
return true
}

View File

@ -49,6 +49,67 @@ func TestConnectionSSL(t *testing.T) {
irccon.Loop()
}
func TestConnectionEmtpyServer(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("")
if err == nil {
t.Fatal("emtpy server string not detected")
}
}
func TestConnectionDoubleColon(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("::")
if err == nil {
t.Fatal("wrong number of ':' not detected")
}
}
func TestConnectionMissingHost(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect(":6667")
if err == nil {
t.Fatal("missing host not detected")
}
}
func TestConnectionMissingPort(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
err := irccon.Connect("chat.freenode.net:")
if err == nil {
t.Fatal("missing port not detected")
}
}
func TestConnectionMissingLog(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
irccon.Log = nil
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("missing 'Log' not detected")
}
}
func TestConnectionEmptyUser(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
// user may be changed after creation
irccon.user = ""
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("empty 'user' not detected")
}
}
func TestConnectionEmptyNick(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
// nick may be changed after creation
irccon.nick = ""
err := irccon.Connect("chat.freenode.net:6667")
if err == nil {
t.Fatal("empty 'nick' not detected")
}
}
func TestRemoveCallback(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
irccon.VerboseCallbackHandler = true
@ -135,24 +196,6 @@ func TestIRCemptyNick(t *testing.T) {
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) {
@ -161,79 +204,3 @@ func TestIRCemptyUser(t *testing.T) {
t.Error("empty user didn't result in error")
}
}
func TestHasConnectionValues0(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
irccon.server = "foo"
if false == irccon.hasConnectionValues() {
t.Error("valid struct not detected as such")
}
}
func TestHasConnectionValues1(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
irccon.server = "foo"
irccon.Version = ""
if irccon.hasConnectionValues() {
t.Error("empty 'Version' not detected")
}
}
func TestHasValidValues2(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
irccon.server = "foo"
irccon.nick = ""
if irccon.hasConnectionValues() {
t.Error("empty 'nick' not detected")
}
}
func TestHasConnectionValues3(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
irccon.server = "foo"
irccon.user = ""
if irccon.hasConnectionValues() {
t.Error("empty 'user' not detected")
}
}
func TestHasConnectionValues4(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
irccon.server = "foo"
irccon.Log = nil
if irccon.hasConnectionValues() {
t.Error("nil pointer 'Log' not detected")
}
}
func TestHasConnectionValues5(t *testing.T) {
irccon := IRC("go-eventirc", "go-eventirc")
if nil == irccon {
t.Error("creating IRC struct failed")
}
if irccon.hasConnectionValues() {
t.Error("empty 'server' not detected")
}
}