Create a simple mock that implements net.Conn.

This commit is contained in:
Alex Bramley 2011-08-21 13:38:12 +01:00
parent 045679dd42
commit 425557e8bd
2 changed files with 98 additions and 14 deletions

View File

@ -1,14 +0,0 @@
package client
import (
"testing"
)
// Not really sure what or how to test something that basically requires a
// connection to an IRC server to function, but we need some tests or when this
// is present in the go package tree builds fail hard :-(
func TestIRC(t *testing.T) {
if c := New("test", "test", "Testing IRC"); c == nil {
t.FailNow()
}
}

View File

@ -0,0 +1,98 @@
package client
import (
"net"
"os"
"testing"
)
type mockNetConn struct {
*testing.T
In, Out chan string
in, out chan []byte
closed bool
rt, wt int64
}
func MockNetConn(t *testing.T) (*mockNetConn) {
// Our mock connection is a testing object
m := &mockNetConn{T: t}
// set known values for conn info
m.closed = false
m.rt = 0
m.wt = 0
// buffer input
m.In = make(chan string, 20)
m.in = make(chan []byte)
go func() {
for !m.closed {
m.in <- []byte(<-m.In)
}
}()
// buffer output
m.Out = make(chan string)
m.out = make(chan []byte, 20)
go func() {
for !m.closed {
m.Out <- string(<-m.out)
}
}()
return m
}
// Test helper
func (m *mockNetConn) Expect(e string) {
s := <-m.Out
if e + "\r\n" != s {
m.Errorf("Mock connection received unexpected value.\n\t" +
"Expected: %s\n\tGot: %s", e, s)
}
}
// Implement net.Conn interface
func (m *mockNetConn) Read(b []byte) (int, os.Error) {
s := <-m.in
copy(b, s)
return len(s), nil
}
func (m *mockNetConn) Write(s []byte) (int, os.Error) {
b := make([]byte, len(s))
copy(b, s)
m.out <- b
return len(b), nil
}
func (m *mockNetConn) Close() os.Error {
m.closed = true
return nil
}
func (m *mockNetConn) LocalAddr() net.Addr {
return &net.IPAddr{net.IPv4(127,0,0,1)}
}
func (m *mockNetConn) RemoteAddr() net.Addr {
return &net.IPAddr{net.IPv4(127,0,0,1)}
}
func (m *mockNetConn) SetTimeout(ns int64) os.Error {
m.rt = ns
m.wt = ns
return nil
}
func (m *mockNetConn) SetReadTimeout(ns int64) os.Error {
m.rt = ns
return nil
}
func (m *mockNetConn) SetWriteTimeout(ns int64) os.Error {
m.wt = ns
return nil
}