forked from 42wim/mm-go-irckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
66 lines (54 loc) · 1.28 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package irckit
import (
"reflect"
"testing"
"github.com/sorcix/irc"
)
type mockConn struct {
send chan *irc.Message
receive chan *irc.Message
host string
}
func (conn *mockConn) Close() error {
return nil
}
func (conn *mockConn) Encode(msg *irc.Message) error {
conn.send <- msg
return nil
}
func (conn *mockConn) Decode() (*irc.Message, error) {
return <-conn.receive, nil
}
func (conn *mockConn) ResolveHost() string {
return conn.host
}
func NewConnMock(host string, capacity int) *mockConn {
return &mockConn{
send: make(chan *irc.Message, capacity),
receive: make(chan *irc.Message, capacity),
host: host,
}
}
func NewUserMock(send chan *irc.Message, receive chan *irc.Message) *User {
return NewUser(&mockConn{
send: send,
receive: receive,
host: "mockhost.local",
})
}
func TestUserMock(t *testing.T) {
send, receive := make(chan *irc.Message, 1), make(chan *irc.Message, 1)
u := NewUserMock(send, receive)
expect := &irc.Message{Command: irc.RPL_WELCOME}
u.Encode(expect)
msg := <-send
if !reflect.DeepEqual(msg, expect) {
t.Errorf("got %v; want %v", msg, expect)
}
expect = &irc.Message{Command: irc.QUIT}
receive <- expect
msg, _ = u.Decode()
if !reflect.DeepEqual(msg, expect) {
t.Errorf("got %v; want %v", msg, expect)
}
}