mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-06 18:33:05 +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) {
|
func TestChannel(t *testing.T) {
|
||||||
s := &MockScreen{}
|
var expected, actual []byte
|
||||||
|
|
||||||
out := make(chan Message)
|
out := make(chan Message)
|
||||||
|
defer close(out)
|
||||||
|
|
||||||
|
s := &MockScreen{}
|
||||||
|
u := NewUser("foo")
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for msg := range out {
|
for msg := range out {
|
||||||
t.Logf("Broadcasted: %s", msg.String())
|
t.Logf("Broadcasted: ", msg.String())
|
||||||
s.Write([]byte(msg.Render(nil)))
|
u.Send(msg)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
u := NewUserScreen("foo", s)
|
|
||||||
ch := NewChannel("", out)
|
ch := NewChannel("", out)
|
||||||
err := ch.Join(u)
|
err := ch.Join(u)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []byte(" * foo joined. (Connected: 1)")
|
u.ConsumeOne(s)
|
||||||
if !reflect.DeepEqual(s.received, expected) {
|
expected = []byte(" * foo joined. (Connected: 1)")
|
||||||
t.Errorf("Got: `%s`; Expected: `%s`", s.received, expected)
|
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
|
// TODO: Cache based on theme
|
||||||
var msg string
|
var msg string
|
||||||
if m.to != nil && m.from != nil {
|
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 {
|
} 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 {
|
} else if m.to != nil {
|
||||||
msg = fmt.Sprintf("-> %s", m.Body)
|
msg = fmt.Sprintf("-> %s", m.Body)
|
||||||
} else {
|
} 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
|
// Consume message buffer into an io.Writer. Will block, should be called in a
|
||||||
// goroutine.
|
// goroutine.
|
||||||
|
// TODO: Not sure if this is a great API.
|
||||||
func (u *User) Consume(out io.Writer) {
|
func (u *User) Consume(out io.Writer) {
|
||||||
for m := range u.msg {
|
for m := range u.msg {
|
||||||
u.consumeMsg(m, out)
|
u.consumeMsg(m, out)
|
||||||
@ -126,7 +127,6 @@ func init() {
|
|||||||
DefaultUserConfig = &UserConfig{
|
DefaultUserConfig = &UserConfig{
|
||||||
Highlight: true,
|
Highlight: true,
|
||||||
Bell: false,
|
Bell: false,
|
||||||
Theme: DefaultTheme,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Seed random?
|
// TODO: Seed random?
|
||||||
|
@ -5,16 +5,9 @@ import (
|
|||||||
"testing"
|
"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) {
|
func TestMakeUser(t *testing.T) {
|
||||||
|
var actual, expected []byte
|
||||||
|
|
||||||
s := &MockScreen{}
|
s := &MockScreen{}
|
||||||
u := NewUser("foo")
|
u := NewUser("foo")
|
||||||
m := NewMessage("hello")
|
m := NewMessage("hello")
|
||||||
@ -23,7 +16,9 @@ func TestMakeUser(t *testing.T) {
|
|||||||
u.Send(*m)
|
u.Send(*m)
|
||||||
u.ConsumeOne(s)
|
u.ConsumeOne(s)
|
||||||
|
|
||||||
if !reflect.DeepEqual(string(s.received), m.String()) {
|
s.Read(&actual)
|
||||||
t.Errorf("Got: `%s`; Expected: `%s`", s.received, m.String())
|
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