summaryrefslogtreecommitdiff
path: root/chat/chat_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'chat/chat_test.go')
-rw-r--r--chat/chat_test.go64
1 files changed, 64 insertions, 0 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", "")
+}