2015-06-10 03:48:54 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-06-10 03:48:54 +00:00
|
|
|
)
|
|
|
|
|
2016-01-13 19:05:14 +00:00
|
|
|
func testClient() *Client {
|
|
|
|
return NewClient("test", "testing")
|
2015-06-11 00:04:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 19:05:14 +00:00
|
|
|
func testClientSend() (*Client, chan string) {
|
|
|
|
c := testClient()
|
|
|
|
conn := &mockConn{hook: make(chan string, 16)}
|
2015-06-11 00:04:51 +00:00
|
|
|
c.conn = conn
|
2016-01-15 07:26:06 +00:00
|
|
|
c.sendRecv.Add(1)
|
2015-06-11 00:04:51 +00:00
|
|
|
go c.send()
|
2016-01-13 19:05:14 +00:00
|
|
|
return c, conn.hook
|
2015-06-11 00:04:51 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 03:48:54 +00:00
|
|
|
type mockConn struct {
|
|
|
|
hook chan string
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockConn) Write(b []byte) (int, error) {
|
|
|
|
c.hook <- string(b)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:04:51 +00:00
|
|
|
func (c *mockConn) Close() error {
|
|
|
|
return nil
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPass(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-11 02:57:52 +00:00
|
|
|
c.writePass("pass")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "PASS pass\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNick(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Nick("test2")
|
|
|
|
assert.Equal(t, "test2", c.GetNick())
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "NICK test2\r\n", <-out)
|
2015-06-11 02:57:52 +00:00
|
|
|
|
|
|
|
c.writeNick("nick")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "NICK nick\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-11 02:57:52 +00:00
|
|
|
c.writeUser("user", "rn")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOper(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Oper("name", "pass")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "OPER name pass\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMode(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Mode("#chan", "+o", "user")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "MODE #chan +o user\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuit(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.connected = true
|
|
|
|
c.Quit()
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "QUIT\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
_, ok := <-c.quit
|
|
|
|
assert.Equal(t, false, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoin(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Join("#a")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "JOIN #a\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Join("#b", "#c")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "JOIN #b,#c\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPart(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Part("#a")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "PART #a\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Part("#b", "#c")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "PART #b,#c\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopic(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Topic("#chan")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "TOPIC #chan\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvite(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Invite("user", "#chan")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "INVITE user #chan\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKick(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Kick("#chan", "user")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "KICK #chan user\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Kick("#chan", "a", "b")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "KICK #chan a,b\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrivmsg(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Privmsg("user", "the message")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "PRIVMSG user :the message\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotice(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Notice("user", "the message")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "NOTICE user :the message\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhois(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Whois("user")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "WHOIS user\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAway(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-10 03:48:54 +00:00
|
|
|
c.Away("not here")
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "AWAY :not here\r\n", <-out)
|
2015-06-10 03:48:54 +00:00
|
|
|
}
|
2015-06-11 02:57:52 +00:00
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
2016-01-13 19:05:14 +00:00
|
|
|
c, out := testClientSend()
|
2015-06-11 02:57:52 +00:00
|
|
|
c.nick = "nick"
|
|
|
|
c.Username = "user"
|
|
|
|
c.Realname = "rn"
|
|
|
|
c.register()
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "NICK nick\r\n", <-out)
|
|
|
|
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
2015-06-11 02:57:52 +00:00
|
|
|
|
|
|
|
c.Password = "pass"
|
|
|
|
c.register()
|
2016-01-13 19:05:14 +00:00
|
|
|
assert.Equal(t, "PASS pass\r\n", <-out)
|
|
|
|
assert.Equal(t, "NICK nick\r\n", <-out)
|
|
|
|
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
|
2015-06-11 02:57:52 +00:00
|
|
|
}
|
2016-02-03 20:12:32 +00:00
|
|
|
|
|
|
|
func TestFlushChannels(t *testing.T) {
|
|
|
|
c, out := testClientSend()
|
|
|
|
c.addChannel("#chan1")
|
|
|
|
c.flushChannels()
|
|
|
|
assert.Equal(t, <-out, "JOIN #chan1\r\n")
|
|
|
|
c.addChannel("#chan2")
|
|
|
|
c.addChannel("#chan3")
|
|
|
|
c.flushChannels()
|
|
|
|
assert.Equal(t, <-out, "JOIN #chan2,#chan3\r\n")
|
|
|
|
}
|