mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-14 16:17:17 +03:00
Fix message rendering, tests pass.
This commit is contained in:
parent
137e84db79
commit
bcfacb89b1
@ -6,26 +6,41 @@ import (
|
||||
)
|
||||
|
||||
func TestChannel(t *testing.T) {
|
||||
s := &MockScreen{}
|
||||
var expected, actual []byte
|
||||
|
||||
out := make(chan Message)
|
||||
defer close(out)
|
||||
|
||||
s := &MockScreen{}
|
||||
u := NewUser("foo")
|
||||
|
||||
go func() {
|
||||
for msg := range out {
|
||||
t.Logf("Broadcasted: %s", msg.String())
|
||||
s.Write([]byte(msg.Render(nil)))
|
||||
t.Logf("Broadcasted: ", msg.String())
|
||||
u.Send(msg)
|
||||
}
|
||||
}()
|
||||
|
||||
u := NewUserScreen("foo", s)
|
||||
ch := NewChannel("", out)
|
||||
err := ch.Join(u)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
expected := []byte(" * foo joined. (Connected: 1)")
|
||||
if !reflect.DeepEqual(s.received, expected) {
|
||||
t.Errorf("Got: `%s`; Expected: `%s`", s.received, expected)
|
||||
u.ConsumeOne(s)
|
||||
expected = []byte(" * foo joined. (Connected: 1)")
|
||||
s.Read(&actual)
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||
}
|
||||
|
||||
m := NewMessage("hello").From(u)
|
||||
ch.Send(*m)
|
||||
|
||||
u.ConsumeOne(s)
|
||||
expected = []byte("foo: hello")
|
||||
s.Read(&actual)
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||
}
|
||||
}
|
||||
|
@ -42,9 +42,9 @@ func (m *Message) Render(*Theme) string {
|
||||
// TODO: Cache based on theme
|
||||
var msg string
|
||||
if m.to != nil && m.from != nil {
|
||||
msg = fmt.Sprintf("[PM from %s] %s", m.from, m.Body)
|
||||
msg = fmt.Sprintf("[PM from %s] %s", m.from.Name(), m.Body)
|
||||
} else if m.from != nil {
|
||||
msg = fmt.Sprintf("%s: %s", m.from, m.Body)
|
||||
msg = fmt.Sprintf("%s: %s", m.from.Name(), m.Body)
|
||||
} else if m.to != nil {
|
||||
msg = fmt.Sprintf("-> %s", m.Body)
|
||||
} else {
|
||||
|
51
chat/screen_test.go
Normal file
51
chat/screen_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Used for testing
|
||||
type MockScreen struct {
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func (s *MockScreen) Write(data []byte) (n int, err error) {
|
||||
s.buffer = append(s.buffer, data...)
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (s *MockScreen) Read(p *[]byte) (n int, err error) {
|
||||
*p = s.buffer
|
||||
s.buffer = []byte{}
|
||||
return len(*p), nil
|
||||
}
|
||||
|
||||
func TestScreen(t *testing.T) {
|
||||
var actual, expected []byte
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: %v; Expected: %v", actual, expected)
|
||||
}
|
||||
|
||||
actual = []byte("foo")
|
||||
expected = []byte("foo")
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: %v; Expected: %v", actual, expected)
|
||||
}
|
||||
|
||||
s := &MockScreen{}
|
||||
|
||||
expected = nil
|
||||
s.Read(&actual)
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: %v; Expected: %v", actual, expected)
|
||||
}
|
||||
|
||||
expected = []byte("hello, world")
|
||||
s.Write(expected)
|
||||
s.Read(&actual)
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: %v; Expected: %v", actual, expected)
|
||||
}
|
||||
}
|
@ -80,6 +80,7 @@ func (u *User) Close() {
|
||||
|
||||
// Consume message buffer into an io.Writer. Will block, should be called in a
|
||||
// goroutine.
|
||||
// TODO: Not sure if this is a great API.
|
||||
func (u *User) Consume(out io.Writer) {
|
||||
for m := range u.msg {
|
||||
u.consumeMsg(m, out)
|
||||
@ -126,7 +127,6 @@ func init() {
|
||||
DefaultUserConfig = &UserConfig{
|
||||
Highlight: true,
|
||||
Bell: false,
|
||||
Theme: DefaultTheme,
|
||||
}
|
||||
|
||||
// TODO: Seed random?
|
||||
|
@ -5,16 +5,9 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockScreen struct {
|
||||
received []byte
|
||||
}
|
||||
|
||||
func (s *MockScreen) Write(data []byte) (n int, err error) {
|
||||
s.received = append(s.received, data...)
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func TestMakeUser(t *testing.T) {
|
||||
var actual, expected []byte
|
||||
|
||||
s := &MockScreen{}
|
||||
u := NewUser("foo")
|
||||
m := NewMessage("hello")
|
||||
@ -23,7 +16,9 @@ func TestMakeUser(t *testing.T) {
|
||||
u.Send(*m)
|
||||
u.ConsumeOne(s)
|
||||
|
||||
if !reflect.DeepEqual(string(s.received), m.String()) {
|
||||
t.Errorf("Got: `%s`; Expected: `%s`", s.received, m.String())
|
||||
s.Read(&actual)
|
||||
expected = []byte(m.String())
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user