-
Notifications
You must be signed in to change notification settings - Fork 2
/
hand_test.go
324 lines (254 loc) · 8.54 KB
/
hand_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package rung_test
import (
"testing"
"github.com/minhajuddinkhan/pattay"
"github.com/minhajuddinkhan/rung"
"github.com/stretchr/testify/assert"
)
func TestHand_EmptyHandShouldHaveZeroCards(t *testing.T) {
hand := rung.NewHand(nil)
assert.Equal(t, len(hand.Cards()), 0)
}
func TestHand_EmptyHandShouldHaveNoHouse(t *testing.T) {
hand := rung.NewHand(nil)
_, err := hand.House()
assert.NotNil(t, err)
}
func TestHand_AddCardToHand(t *testing.T) {
hand := rung.NewHand(nil)
player := pattay.NewPlayer(pattay.SouthPlayer)
card := pattay.NewCard(pattay.Spade, pattay.Ace)
err := player.ReceiveCard(card)
assert.Nil(t, err)
hand.AddCard(player, pattay.FirstCardAtHand)
assert.Equal(t, len(hand.Cards()), 1)
}
func TestHand_NoSamePlayerCanAddToHand(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.EastPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.Ace)
c2 := pattay.NewCard(pattay.Club, pattay.Ace)
err := p1.ReceiveCard(c1)
assert.Nil(t, err)
err = p1.ReceiveCard(c2)
assert.Nil(t, err)
err = hand.AddCard(p1, pattay.FirstCardAtHand)
assert.Nil(t, err)
err = hand.AddCard(p1, pattay.FirstCardAtHand)
assert.NotNil(t, err)
}
func TestHand_NoMoreThanFourCardsAtOneHand(t *testing.T) {
hand := rung.NewHand(nil)
player1 := pattay.NewPlayer(pattay.SouthPlayer)
player2 := pattay.NewPlayer(pattay.WestPlayer)
player3 := pattay.NewPlayer(pattay.EastPlayer)
player4 := pattay.NewPlayer(pattay.NorthPlayer)
player5 := pattay.NewPlayer("Wrong player")
c1 := pattay.NewCard(pattay.Spade, pattay.Ace)
err := player1.ReceiveCard(c1)
assert.Nil(t, err)
c2 := pattay.NewCard(pattay.Spade, pattay.Two)
err = player2.ReceiveCard(c2)
assert.Nil(t, err)
c3 := pattay.NewCard(pattay.Spade, pattay.Three)
err = player3.ReceiveCard(c3)
assert.Nil(t, err)
c4 := pattay.NewCard(pattay.Spade, pattay.Four)
err = player4.ReceiveCard(c4)
assert.Nil(t, err)
c5 := pattay.NewCard(pattay.Spade, pattay.Five)
err = player5.ReceiveCard(c5)
assert.Nil(t, err)
err = hand.AddCard(player1, pattay.FirstCardAtHand)
assert.Nil(t, err)
err = hand.AddCard(player2, pattay.FirstCardAtHand)
assert.Nil(t, err)
err = hand.AddCard(player3, pattay.FirstCardAtHand)
assert.Nil(t, err)
err = hand.AddCard(player4, pattay.FirstCardAtHand)
assert.Nil(t, err)
err = hand.AddCard(player5, pattay.FirstCardAtHand)
assert.NotNil(t, err)
}
func TestHand_EmptyHandShouldHaveNoHead(t *testing.T) {
hand := rung.NewHand(nil)
headPlayer, err := hand.Head()
assert.Nil(t, headPlayer)
assert.NotNil(t, err)
}
func TestHand_ColorOfHandShouldBeOfTheFirstCardPlacedOnHand(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.WestPlayer)
c1 := pattay.NewCard(pattay.Diamond, pattay.Ace)
err := p1.ReceiveCard(c1)
assert.Nil(t, err)
err = hand.AddCard(p1, pattay.FirstCardAtHand)
assert.Nil(t, err)
houseOfHand, err := hand.House()
assert.Nil(t, err)
assert.Equal(t, houseOfHand, c1.House())
}
func TestHand_HeadShouldBeOfTheMostPowerfullCardAtHand(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.EastPlayer)
p3 := pattay.NewPlayer(pattay.WestPlayer)
p4 := pattay.NewPlayer(pattay.NorthPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.King)
c2 := pattay.NewCard(pattay.Spade, pattay.Ace)
c3 := pattay.NewCard(pattay.Spade, pattay.Queen)
c4 := pattay.NewCard(pattay.Spade, pattay.Jack)
p1.ReceiveCard(c1)
p2.ReceiveCard(c2)
p3.ReceiveCard(c3)
p4.ReceiveCard(c4)
hand.AddCard(p1, pattay.FirstCardAtHand)
hand.AddCard(p2, pattay.FirstCardAtHand)
hand.AddCard(p3, pattay.FirstCardAtHand)
hand.AddCard(p4, pattay.FirstCardAtHand)
head, err := hand.Head()
assert.Nil(t, err)
assert.Equal(t, head.Name(), p2.Name())
}
func TestHand_PlayerCannotPlayCard_OfDifferentHouseThanHouseOfHand(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.EastPlayer)
spadeAce := pattay.NewCard(pattay.Spade, pattay.Ace)
spadeKing := pattay.NewCard(pattay.Spade, pattay.King)
heartAce := pattay.NewCard(pattay.Heart, pattay.Ace)
//p1 has spadeAce and heartAce
p1.ReceiveCard(spadeAce)
p1.ReceiveCard(heartAce)
//p2 has spadeKing
p2.ReceiveCard(spadeKing)
//p2 plays spadeKing as first card
hand.AddCard(p2, pattay.FirstCardAtHand)
//p1 plays heartAce on a spade hand eve
err := hand.AddCard(p1, pattay.SecondCardAtHand)
assert.NotNil(t, err)
}
func TestHand_PlayerCanMakeTrump(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.NorthPlayer)
p3 := pattay.NewPlayer(pattay.EastPlayer)
p4 := pattay.NewPlayer(pattay.WestPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.Ace)
c2 := pattay.NewCard(pattay.Spade, pattay.King)
c3 := pattay.NewCard(pattay.Spade, pattay.Queen)
c4 := pattay.NewCard(pattay.Heart, pattay.Three)
p1.ReceiveCard(c1)
p2.ReceiveCard(c2)
p3.ReceiveCard(c3)
p4.ReceiveCard(c4)
hand.AddCard(p1, pattay.FirstCardAtHand)
hand.AddCard(p2, pattay.FirstCardAtHand)
hand.AddCard(p4, pattay.FirstCardAtHand)
hand.AddCard(p3, pattay.FirstCardAtHand)
h, _ := hand.House()
trump, _ := hand.Trump()
assert.Equal(t, h, pattay.Spade)
assert.Equal(t, trump, pattay.Heart)
}
func TestHand_CannotMakeTrumpAgainIfTrumpAlreadyDeclared(t *testing.T) {
trump := pattay.Spade
hand := rung.NewHand(&trump)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.NorthPlayer)
p3 := pattay.NewPlayer(pattay.EastPlayer)
p4 := pattay.NewPlayer(pattay.WestPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.Three)
c2 := pattay.NewCard(pattay.Spade, pattay.King)
c3 := pattay.NewCard(pattay.Spade, pattay.Queen)
c4 := pattay.NewCard(pattay.Heart, pattay.Ace)
p1.ReceiveCard(c1)
p2.ReceiveCard(c2)
p3.ReceiveCard(c3)
p4.ReceiveCard(c4)
hand.AddCard(p1, pattay.FirstCardAtHand)
hand.AddCard(p2, pattay.FirstCardAtHand)
hand.AddCard(p4, pattay.FirstCardAtHand)
hand.AddCard(p3, pattay.FirstCardAtHand)
h, _ := hand.House()
trump, _ = hand.Trump()
player, _ := hand.Head()
assert.Equal(t, h, pattay.Spade)
assert.Equal(t, trump, pattay.Spade)
assert.Equal(t, player.Name(), p2.Name())
}
func TestHand_CardNotWithPlayerAddedInHand(t *testing.T) {
p := pattay.NewPlayer(pattay.SouthPlayer)
hand := rung.NewHand(nil)
err := hand.AddCard(p, 2)
assert.NotNil(t, err)
}
func TestHand_CutByTrumpCard(t *testing.T) {
trump := pattay.Diamond
hand := rung.NewHand(&trump)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.NorthPlayer)
p3 := pattay.NewPlayer(pattay.EastPlayer)
p4 := pattay.NewPlayer(pattay.WestPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.Three)
c2 := pattay.NewCard(pattay.Spade, pattay.King)
c3 := pattay.NewCard(pattay.Spade, pattay.Queen)
c4 := pattay.NewCard(pattay.Diamond, pattay.Two)
p1.ReceiveCard(c1)
p2.ReceiveCard(c2)
p3.ReceiveCard(c3)
p4.ReceiveCard(c4)
hand.AddCard(p1, pattay.FirstCardAtHand)
hand.AddCard(p2, pattay.FirstCardAtHand)
hand.AddCard(p3, pattay.FirstCardAtHand)
hand.AddCard(p4, pattay.FirstCardAtHand)
x, err := hand.Head()
assert.Nil(t, err)
assert.Equal(t, p4.Name(), x.Name())
}
func TestHand_CutByBiggerTrumpCard(t *testing.T) {
trump := pattay.Diamond
hand := rung.NewHand(&trump)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.NorthPlayer)
p3 := pattay.NewPlayer(pattay.EastPlayer)
p4 := pattay.NewPlayer(pattay.WestPlayer)
c1 := pattay.NewCard(pattay.Spade, pattay.Three)
c2 := pattay.NewCard(pattay.Diamond, pattay.Ace)
c3 := pattay.NewCard(pattay.Spade, pattay.Queen)
c4 := pattay.NewCard(pattay.Diamond, pattay.King)
p1.ReceiveCard(c1)
p2.ReceiveCard(c2)
p3.ReceiveCard(c3)
p4.ReceiveCard(c4)
hand.AddCard(p1, pattay.FirstCardAtHand)
hand.AddCard(p2, pattay.FirstCardAtHand)
hand.AddCard(p3, pattay.FirstCardAtHand)
hand.AddCard(p4, pattay.FirstCardAtHand)
x, err := hand.Head()
assert.Nil(t, err)
assert.Equal(t, p2.Name(), x.Name())
}
func TestHand_SameCardsInOneHand(t *testing.T) {
hand := rung.NewHand(nil)
p1 := pattay.NewPlayer(pattay.SouthPlayer)
p2 := pattay.NewPlayer(pattay.WestPlayer)
c := pattay.NewCard(pattay.Spade, pattay.Ace)
p1.ReceiveCard(c)
p2.ReceiveCard(c)
hand.AddCard(p1, pattay.FirstCardAtHand)
err := hand.AddCard(p2, pattay.FirstCardAtHand)
assert.Error(t, err)
}
func TestHand_HandHasCard(t *testing.T) {
hand := rung.NewHand(nil)
has, index := hand.HasCard(pattay.NewCard(pattay.Spade, pattay.Ace))
assert.False(t, has)
assert.Equal(t, -1, index)
}
func TestHand_EmptyTrump(t *testing.T) {
hand := rung.NewHand(nil)
tr, err := hand.Trump()
assert.Empty(t, tr)
assert.Error(t, err)
}