forked from rnixik/durak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom_test.go
117 lines (107 loc) · 3.18 KB
/
room_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"testing"
)
func TestNewRoom(t *testing.T) {
client := &Client{nickname: "test_nickname"}
room := newRoom(1, client, nil)
got := room.owner.client
expected := client
if got != expected {
t.Errorf("TestNewRoom expected: %v, got: %v", expected, got)
}
membersNum := len(room.members)
if membersNum != 1 {
t.Errorf("TestNewRoom expected members: 1, got: %v", membersNum)
}
}
func TestName(t *testing.T) {
client := &Client{nickname: "test_nickname"}
room := newRoom(1, client, nil)
got := room.Name()
expected := "test_nickname"
if got != expected {
t.Errorf("TestName expected: %v, got: %v", expected, got)
}
}
func TestId(t *testing.T) {
client := &Client{id: 123}
room := newRoom(12345, client, nil)
got := room.Id()
expected := uint64(12345)
if got != expected {
t.Errorf("TestId expected: %v, got: %v", expected, got)
}
}
func TestToRoomInList(t *testing.T) {
client := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client, nil)
roomInList := room.toRoomInList()
got := getNameOfStruct(roomInList)
expected := "RoomInList"
if got != expected {
t.Errorf("TestToRoomInList expected: %v, got: %v", expected, got)
}
}
func TestToRoomInfo(t *testing.T) {
client := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client, nil)
roomInfo := room.toRoomInfo()
got := getNameOfStruct(roomInfo)
expected := "RoomInfo"
if got != expected {
t.Errorf("TestToRoomInfo expected: %v, got: %v", expected, got)
}
}
func TestAddClient(t *testing.T) {
client := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client, nil)
client2 := &Client{id: 456, nickname: "test_nickname2"}
room.addClient(client2)
got := len(room.members)
expected := 2
if got != expected {
t.Errorf("TestAddClient expected: %v, got: %v", expected, got)
}
}
func TestRemoveLastClient(t *testing.T) {
client := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client, nil)
_, roomBecameEmpty := room.removeClient(client)
if !roomBecameEmpty {
t.Errorf("TestRemoveLastClient expected that room became empty")
}
}
func TestRemoveOwnerClient(t *testing.T) {
client1 := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client1, nil)
client2 := &Client{id: 456, nickname: "test_nickname2"}
room.addClient(client2)
changedOwner, _ := room.removeClient(client1)
if !changedOwner {
t.Errorf("TestRemoveOwnerClient expected that room changed owner")
}
got := room.owner.client
expected := client2
if got != expected {
t.Errorf("TestRemoveOwnerClient expected: %v, got: %v", expected, got)
}
}
func TestRemoveRegularClient(t *testing.T) {
client1 := &Client{id: 123, nickname: "test_nickname"}
room := newRoom(1, client1, nil)
client2 := &Client{id: 456, nickname: "test_nickname2"}
room.addClient(client2)
changedOwner, roomBecameEmpty := room.removeClient(client2)
if changedOwner {
t.Errorf("TestRemoveRegularClient expected that room did not change owner")
}
if roomBecameEmpty {
t.Errorf("TestRemoveRegularClient expected that room did not become empty")
}
got := room.owner.client
expected := client1
if got != expected {
t.Errorf("TestRemoveRegularClient expected: %v, got: %v", expected, got)
}
}