summaryrefslogtreecommitdiff
path: root/chat/chat_test.go
blob: b695b44319d47c26cd2f8d9ca464f7ac9b8c8417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package chat

import (
	"testing"
)

func TestJoining(t *testing.T) {
	InitRooms(2)

	b1, r1, err := Join("test", "Foo")
	if err != nil {
		t.Fatalf("Could not join room: %s", err)
	}
	defer b1.Leave()

	if _, _, err = Join("test", "Foo"); err != NickAlreadyInUse {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, NickAlreadyInUse)
	}

	b2, r2, err := Join("test", "Bar")
	if err != nil {
		t.Fatalf("Could not join room a second time: %s", err)
	}
	defer b2.Leave()

	// since we joined to the same room, r1 and r2 should be equal
	if r1 != r2 {
		t.Error("r1 and r2 are not equal")
	}

	if _, _, err = Join("test", "Baz"); err != RoomIsFull {
		t.Fatalf("Got error \"%s\", expected \"%s\"", err, RoomIsFull)
	}
}

func TestLeaving(t *testing.T) {
	InitRooms(10)

	b1, _, err := Join("test", "Foo")
	if err != nil {
		t.Fatalf("Could not join room: %s", err)
	}

	b1.Leave()

	// The room is now empty. It should no longer exist.
	if _, ok := rooms["test"]; ok {
		t.Error("Room test still exists, although no user is left")
	}
}