Add some tests to the IRC client

This commit is contained in:
Ken-Håvard Lieng 2015-06-10 05:48:54 +02:00
parent ff9cae7794
commit da8915328d
15 changed files with 3088 additions and 39 deletions

119
irc/client_test.go Normal file
View file

@ -0,0 +1,119 @@
package irc
import (
"net"
"testing"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/stretchr/testify/assert"
)
var c *Client
var conn *mockConn
type mockConn struct {
hook chan string
net.Conn
}
func (c *mockConn) Write(b []byte) (int, error) {
c.hook <- string(b)
return len(b), nil
}
func init() {
initTestClient()
}
func initTestClient() {
c = NewClient("test", "testing")
conn = &mockConn{hook: make(chan string, 1)}
c.conn = conn
go c.send()
}
func TestPass(t *testing.T) {
c.Pass("pass")
assert.Equal(t, "PASS pass\r\n", <-conn.hook)
}
func TestNick(t *testing.T) {
c.Nick("test2")
assert.Equal(t, "test2", c.GetNick())
assert.Equal(t, "NICK test2\r\n", <-conn.hook)
}
func TestUser(t *testing.T) {
c.User("user", "rn")
assert.Equal(t, "USER user 0 * :rn\r\n", <-conn.hook)
}
func TestOper(t *testing.T) {
c.Oper("name", "pass")
assert.Equal(t, "OPER name pass\r\n", <-conn.hook)
}
func TestMode(t *testing.T) {
c.Mode("#chan", "+o", "user")
assert.Equal(t, "MODE #chan +o user\r\n", <-conn.hook)
}
func TestQuit(t *testing.T) {
c.connected = true
c.Quit()
assert.Equal(t, "QUIT\r\n", <-conn.hook)
_, ok := <-c.quit
assert.Equal(t, false, ok)
initTestClient()
}
func TestJoin(t *testing.T) {
c.Join("#a")
assert.Equal(t, "JOIN #a\r\n", <-conn.hook)
c.Join("#b", "#c")
assert.Equal(t, "JOIN #b,#c\r\n", <-conn.hook)
}
func TestPart(t *testing.T) {
c.Part("#a")
assert.Equal(t, "PART #a\r\n", <-conn.hook)
c.Part("#b", "#c")
assert.Equal(t, "PART #b,#c\r\n", <-conn.hook)
}
func TestTopic(t *testing.T) {
c.Topic("#chan")
assert.Equal(t, "TOPIC #chan\r\n", <-conn.hook)
}
func TestInvite(t *testing.T) {
c.Invite("user", "#chan")
assert.Equal(t, "INVITE user #chan\r\n", <-conn.hook)
}
func TestKick(t *testing.T) {
c.Kick("#chan", "user")
assert.Equal(t, "KICK #chan user\r\n", <-conn.hook)
c.Kick("#chan", "a", "b")
assert.Equal(t, "KICK #chan a,b\r\n", <-conn.hook)
}
func TestPrivmsg(t *testing.T) {
c.Privmsg("user", "the message")
assert.Equal(t, "PRIVMSG user :the message\r\n", <-conn.hook)
}
func TestNotice(t *testing.T) {
c.Notice("user", "the message")
assert.Equal(t, "NOTICE user :the message\r\n", <-conn.hook)
}
func TestWhois(t *testing.T) {
c.Whois("user")
assert.Equal(t, "WHOIS user\r\n", <-conn.hook)
}
func TestAway(t *testing.T) {
c.Away("not here")
assert.Equal(t, "AWAY :not here\r\n", <-conn.hook)
}

18
irc/conn_test.go Normal file
View file

@ -0,0 +1,18 @@
package irc
import (
"testing"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/stretchr/testify/assert"
)
func TestWrite(t *testing.T) {
c.write("test")
assert.Equal(t, "test\r\n", <-conn.hook)
c.Write("test")
assert.Equal(t, "test\r\n", <-conn.hook)
c.writef("test %d", 2)
assert.Equal(t, "test 2\r\n", <-conn.hook)
c.Writef("test %d", 2)
assert.Equal(t, "test 2\r\n", <-conn.hook)
}

43
irc/message_test.go Normal file
View file

@ -0,0 +1,43 @@
package irc
import (
"testing"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/stretchr/testify/assert"
)
func TestParseMessage(t *testing.T) {
cases := []struct {
input string
expected *Message
}{
{
":user CMD #chan :some message\r\n",
&Message{
Prefix: "user",
Nick: "user",
Command: "CMD",
Params: []string{"#chan", "some message"},
Trailing: "some message",
},
}, {
":nick!user@host.com CMD a b\r\n",
&Message{
Prefix: "nick!user@host.com",
Nick: "nick",
Command: "CMD",
Params: []string{"a", "b"},
},
}, {
"CMD a b :\r\n",
&Message{
Command: "CMD",
Params: []string{"a", "b"},
},
},
}
for _, tc := range cases {
assert.Equal(t, tc.expected, parseMessage(tc.input))
}
}