From c35e43e9cf0f915236016a3f73a6ee6bc9962e0b Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Tue, 25 Mar 2014 22:50:07 +0100 Subject: Added TestChat for testing message broadcasting. --- chat/chat_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ chat/messages.go | 17 +++++++++------ 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/chat/chat_test.go b/chat/chat_test.go index fcdf4c9..03cc948 100644 --- a/chat/chat_test.go +++ b/chat/chat_test.go @@ -1,6 +1,7 @@ package chat import ( + "fmt" "testing" ) @@ -68,3 +69,66 @@ func TestLeaving(t *testing.T) { t.Error("Room test still exists, although no user is left") } } + +func checkMsg(t *testing.T, m Message, typ MsgType, user string, txt string) { + fail := fmt.Sprintf("Expected a %s message from %s", typ, user) + if txt != "" { + fail += fmt.Sprintf(" with message '%s'.", txt) + } + + failed := false + + if m.Type != typ { + failed = true + fail += fmt.Sprintf(" Type is wrong (%s)", m.Type) + } + if m.User != user { + failed = true + fail += fmt.Sprintf(" User is wrong (%s)", m.User) + } + if txt != "" && m.Text != txt { + failed = true + fail += fmt.Sprintf(" Text is wrong (%s)", m.Text) + } + + if failed { + t.Error(fail) + } +} + +func TestChatting(t *testing.T) { + InitRooms(10) + + // In this test, we will ignore the errors of Join(), since we already tested that stuff in the tests above. + + b1, _, _ := Join("test", "Foo") + b2, _, _ := Join("test", "Bar") + + checkMsg(t, <-b1.Receive, MsgJoin, "Bar", "") + checkMsg(t, <-b2.Receive, MsgJoin, "Bar", "") + + b2.Say("Hello") + checkMsg(t, <-b1.Receive, MsgChat, "Bar", "Hello") + checkMsg(t, <-b2.Receive, MsgChat, "Bar", "Hello") + + b1.Say(":)") + checkMsg(t, <-b1.Receive, MsgChat, "Foo", ":)") + checkMsg(t, <-b2.Receive, MsgChat, "Foo", ":)") + + b3, _, _ := Join("test", "Baz") + + checkMsg(t, <-b1.Receive, MsgJoin, "Baz", "") + checkMsg(t, <-b2.Receive, MsgJoin, "Baz", "") + checkMsg(t, <-b3.Receive, MsgJoin, "Baz", "") + + b3.Say("!") + + checkMsg(t, <-b1.Receive, MsgChat, "Baz", "!") + checkMsg(t, <-b2.Receive, MsgChat, "Baz", "!") + checkMsg(t, <-b3.Receive, MsgChat, "Baz", "!") + + b2.Leave() + + checkMsg(t, <-b1.Receive, MsgLeave, "Bar", "") + checkMsg(t, <-b3.Receive, MsgLeave, "Bar", "") +} diff --git a/chat/messages.go b/chat/messages.go index f03eae0..dbcd150 100644 --- a/chat/messages.go +++ b/chat/messages.go @@ -2,7 +2,6 @@ package chat import ( "encoding/json" - "errors" ) // MsgType describes the purpose of a message @@ -14,17 +13,21 @@ const ( MsgLeave ) -func (mt *MsgType) MarshalJSON() ([]byte, error) { - switch *mt { +func (mt MsgType) String() string { + switch mt { case MsgChat: - return json.Marshal("chat") + return "chat" case MsgJoin: - return json.Marshal("join") + return "join" case MsgLeave: - return json.Marshal("leave") + return "leave" } - return nil, errors.New("Unknown message type") + return "???" +} + +func (mt *MsgType) MarshalJSON() ([]byte, error) { + return json.Marshal(mt.String()) } // Message represents a message that can be sent to a buddy. The Text field has no meaning, if Type != MsgChat. -- cgit v1.2.3-70-g09d2