ssh-chat/chat/channel_test.go
2014-12-26 12:11:03 -08:00

57 lines
1.0 KiB
Go

package chat
import (
"reflect"
"testing"
)
func TestChannelServe(t *testing.T) {
ch := NewChannel()
ch.Send(NewAnnounceMsg("hello"))
received := <-ch.broadcast
actual := received.String()
expected := " * hello"
if actual != expected {
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
}
}
func TestChannelJoin(t *testing.T) {
var expected, actual []byte
s := &MockScreen{}
u := NewUser("foo")
ch := NewChannel()
defer ch.Close()
err := ch.Join(u)
if err != nil {
t.Fatal(err)
}
m := <-ch.broadcast
if m.(*AnnounceMsg) == nil {
t.Fatal("Did not receive correct msg: %v", m)
}
ch.handleMsg(m)
u.ConsumeOne(s)
expected = []byte(" * foo joined. (Connected: 1)" + Newline)
s.Read(&actual)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
}
ch.Send(NewSystemMsg("hello", u))
u.ConsumeOne(s)
expected = []byte("-> hello" + Newline)
s.Read(&actual)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
}
}