-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
431 lines (345 loc) · 14.8 KB
/
tests.py
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import unittest
from game import *
class TestOverallBehavior(unittest.TestCase):
def setUp(self):
self.card = Card(9, 'D')
self.card.x = 120
self.card.xmax = self.card.x + self.card.image.get_size()[0]
self.card.y = 50
self.card.ymax = self.card.y + self.card.image.get_size()[1]
self.deck = Deck()
def tearDown(self):
del self.card
def test_within_true(self):
actual = within(self.card, 130, 69)
self.assertTrue(actual)
def test_within_false(self):
actual = within(self.card, 110, 79)
self.assertFalse(actual)
def test_sum(self):
expected = 32
actual = sum(14, 18)
self.assertEqual(expected, actual)
def test_five_cards_first_dial(self):
for player in PLAYERS:
player.hand = []
first_dial(self.deck)
actual = all(map(lambda p: len(p.hand) == 5, PLAYERS))
self.assertTrue(actual)
def test_eight_cards(self):
for player in PLAYERS:
player.hand = []
first_dial(self.deck)
second_dial(self.deck)
actual = all(map(lambda p: len(p.hand) == 8, PLAYERS))
self.assertTrue(actual)
def test_cycle_players(self):
expected = [P0, P1, P00, P11, P0]
player = cycle_players(P0)
player0 = next(player)
player1 = next(player)
player2 = next(player)
player3 = next(player)
player4 = next(player)
actual = [player0, player1, player2, player3, player4]
self.assertEqual(expected, actual)
def test_has_color_true(self):
P0.hand = []
P0.hand.append(self.card)
self.assertTrue(has_color(P0, 'D'))
def test_has_color_false(self):
P0.hand = []
P0.hand.append(self.card)
self.assertFalse(has_color(P0, 'S'))
def test_teammates_true(self):
self.assertTrue(teammates(P0, P00))
def test_teammates_false(self):
self.assertFalse(teammates(P1, P00))
class TestCalculation(unittest.TestCase):
def setUp(self):
self.card1 = Card(11, 'S') # вале пика
self.card2 = Card(12, 'D') # дама каро
self.card3 = Card(11, 'C') # вале спатия
self.card4 = Card(9, 'H') # 9 купа
self.cards = [self.card1, self.card2, self.card3, self.card4]
def tearDown(self):
for card in [self.card1, self.card2, self.card3, self.card4]:
del card
def test_calculate_clubs(self):
expected = 25
actual = calculate(self.cards, 1)
self.assertEqual(expected, actual)
def test_calculate_diamonds(self):
expected = 7
actual = calculate(self.cards, 2)
self.assertEqual(expected, actual)
def test_calculate_hearts(self):
expected = 21
actual = calculate(self.cards, 3)
self.assertEqual(expected, actual)
def test_calculate_spades(self):
expected = 25
actual = calculate(self.cards, 4)
self.assertEqual(expected, actual)
def test_calculate_no_trumps(self):
expected = 14
actual = calculate(self.cards, 5)
self.assertEqual(expected, actual)
def test_calculate_all_trumps(self):
expected = 57
actual = calculate(self.cards, 6)
self.assertEqual(expected, actual)
def test_calculated_points_all_trumps(self):
P0.taken = [self.cards[0]]
P1.taken = self.cards[1:3]
P00.taken = []
P11.taken = [self.cards[3]]
expected = {0: 30, 1: 37}
actual = calculate_points(6, P0)
self.assertEqual(expected, actual)
def test_calculated_points_no_trumps(self):
P0.taken = [self.cards[0]]
P1.taken = self.cards[1:3]
P00.taken = []
P11.taken = [self.cards[3]]
expected = {0: 24, 1: 10}
actual = calculate_points(5, P0)
self.assertEqual(expected, actual)
def test_shape_result(self):
small_result = {0: 94, 1: 68}
bonuses = {P0: {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 1},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}
expected = {0: 13, 1: 7}
actual = shape_result(small_result, P0, bonuses)
self.assertEqual(expected, actual)
def test_shape_result_underdogs_win(self):
small_result = {0: 123, 1: 135}
bonuses = {P0: {3: 0, 4: 1, 5: 0, '4ofakind': 0, 'belote': 0},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 1},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}
expected = {0: 33, 1: 0}
actual = shape_result(small_result, P1, bonuses)
self.assertEqual(expected, actual)
def test_shape_result_kapo(self):
small_result = {0: 0, 1: 162}
bonuses = {P0: {3: 0, 4: 1, 5: 0, '4ofakind': 0, 'belote': 0},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 1},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}
expected = {0: 5, 1: 27}
actual = shape_result(small_result, P1, bonuses)
self.assertEqual(expected, actual)
class TestWinnerInHand(unittest.TestCase):
def setUp(self):
self.card1 = Card(10, 'S') # десетка пика
self.card1.played_by = P0
self.card2 = Card(13, 'H') # поп купа
self.card2.played_by = P1
self.card3 = Card(9, 'S') # 9 пика
self.card3.played_by = P00
self.card4 = Card(11, 'S') # вале пика
self.card4.played_by = P11
self.on_table = [self.card1, self.card2, self.card3, self.card4]
def tearDown(self):
for card in [self.card1, self.card2, self.card3, self.card4]:
del card
def test_trump(self):
expected = P1
actual = winner_in_hand(self.on_table, 3) # игра на купа
self.assertEqual(expected, actual)
def test_wanted_trump(self):
expected = P11
actual = winner_in_hand(self.on_table, 4) # игра на купа
self.assertEqual(expected, actual)
def test_no_trumps(self):
expected = P0
actual = winner_in_hand(self.on_table, 5) # игра на купа
self.assertEqual(expected, actual)
def test_all_trumps(self):
expected = P11
actual = winner_in_hand(self.on_table, 6) # игра на купа
self.assertEqual(expected, actual)
def test_decision_making_certain_trumps_first_call(self):
c1 = Card(14, 'S')
c2 = Card(14, 'D')
c3 = Card(11, 'D')
c4 = Card(13, 'H')
c5 = Card(7, 'D')
P0.hand = [c1, c2, c3, c4, c5]
availables = list(range(7))
self.assertEqual(2, decide_announce(P0, availables))
def test_decision_making_no_trumps_first_call(self):
c1 = Card(14, 'S')
c2 = Card(14, 'D')
c3 = Card(11, 'C')
c4 = Card(13, 'H')
c5 = Card(7, 'C')
P0.hand = [c1, c2, c3, c4, c5]
availables = list(range(7))
self.assertEqual(5, decide_announce(P0, availables))
def test_decision_making_no_trumps_second_call(self):
c1 = Card(14, 'S')
c2 = Card(14, 'D')
c3 = Card(11, 'S')
c4 = Card(13, 'H')
c5 = Card(7, 'C')
P0.hand = [c1, c2, c3, c4, c5]
availables = list(range(4, 7))
self.assertEqual(5, decide_announce(P0, availables, P11))
def test_decision_making_no_trumps_no_call(self):
c1 = Card(14, 'S')
c2 = Card(14, 'D')
c3 = Card(11, 'S')
c4 = Card(13, 'H')
c5 = Card(7, 'C')
P0.hand = [c1, c2, c3, c4, c5]
availables = list(range(4, 7))
self.assertEqual(0, decide_announce(P0, availables, P00))
def test_decision_making_all_trumps_partner_called(self):
c1 = Card(9, 'S')
c2 = Card(9, 'D')
c3 = Card(11, 'S')
c4 = Card(13, 'H')
c5 = Card(7, 'C')
P0.hand = [c1, c2, c3, c4, c5]
availables = list(range(4, 7))
self.assertEqual(6, decide_announce(P0, availables, P00, 3))
class TestBonuses(unittest.TestCase):
def setUp(self):
self.card1 = Card(10, 'D')
self.card2 = Card(8, 'D')
self.card3 = Card(9, 'D')
self.card4 = Card(11, 'S')
self.card5 = Card(12, 'D')
self.card6 = Card(11, 'D')
self.card7 = Card(13, 'D')
self.card8 = Card(11, 'H')
self.card9 = Card(11, 'C')
def tearDown(self):
for card in [self.card1, self.card2, self.card3, self.card4,
self.card5, self.card6, self.card7, self.card8,
self.card9]:
del card
def test_are_consecutive_true(self):
nominals = [10, 11, 12, 13]
self.assertTrue(are_consecutive(nominals))
def test_are_consecutive_false(self):
nominals = [10, 12, 13]
self.assertFalse(are_consecutive(nominals))
def test_check_for_consecutive_three(self):
cards = [self.card1, self.card2, self.card3, self.card4, self.card5]
expected = [1, [self.card1, self.card2, self.card3]]
actual = check_for_consecutive(cards, 3)
self.assertEqual(expected, actual)
def test_check_for_consecutive_five(self):
cards = [self.card1, self.card2, self.card3,
self.card4, self.card5, self.card6]
expected = [1, [self.card1, self.card2, self.card3,
self.card5, self.card6]]
actual = check_for_consecutive(cards, 5)
self.assertEqual(expected, actual)
def test_no_bonuses(self):
cards = [self.card1, self.card8, self.card2, self.card4, self.card6]
expected = {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}
actual = check_bonuses(6, cards)
self.assertEqual(expected, actual)
def test_three_in_a_row(self):
cards = [self.card1, self.card2, self.card3, self.card4, self.card5]
expected = {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}
actual = check_bonuses(6, cards)
self.assertEqual(expected, actual)
def test_four_in_a_row(self):
cards = [self.card1, self.card2, self.card3, self.card4, self.card6]
expected = {3: 0, 4: 1, 5: 0, '4ofakind': 0, 'belote': 0}
actual = check_bonuses(6, cards)
self.assertEqual(expected, actual)
def test_five_in_a_row(self):
cards = [self.card1, self.card2, self.card3, self.card5, self.card6]
expected = {3: 0, 4: 0, 5: 1, '4ofakind': 0, 'belote': 0}
actual = check_bonuses(6, cards)
self.assertEqual(expected, actual)
def test_four_of_a_kind(self):
cards = [self.card4, self.card6, self.card8, self.card3, self.card9]
expected = {3: 0, 4: 0, 5: 0, '4ofakind': 1, 'belote': 0}
actual = check_bonuses(6, cards)
self.assertEqual(expected, actual)
def test_check_for_belote_true(self):
P0.hand = [self.card5, self.card7]
bonuses = {P0: {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}
expected = {P0: {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 1},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}, True
actual = check_for_belote(self.card5, P0, 2, bonuses)
self.assertEqual(expected, actual)
def test_check_for_belote_false(self):
P0.hand = [self.card5, self.card8]
bonuses = {P0: {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}
expected = {P0: {3: 1, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P1: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P00: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0},
P11: {3: 0, 4: 0, 5: 0, '4ofakind': 0, 'belote': 0}}, False
actual = check_for_belote(self.card5, P0, 2, bonuses)
self.assertEqual(expected, actual)
class TestCardAvailability(unittest.TestCase):
def setUp(self):
self.card1 = Card(10, 'D')
self.card2 = Card(8, 'D')
self.card3 = Card(9, 'D')
self.card4 = Card(11, 'S')
self.card5 = Card(12, 'C')
self.card6 = Card(11, 'D')
self.card7 = Card(13, 'C')
self.card8 = Card(11, 'H')
P0.hand = [self.card1, self.card4]
P1.hand = [self.card3, self.card2]
P00.hand = [self.card5, self.card6]
P11.hand = [self.card7, self.card8]
def tearDown(self):
for card in [self.card1, self.card2, self.card3, self.card4,
self.card5, self.card6, self.card7, self.card8]:
del card
def test_wrong_color(self):
on_table = [self.card1]
self.assertFalse(is_available(self.card4, on_table, P0, 'D', 4))
def test_trump_wanted_true(self):
on_table = [self.card1, self.card8, self.card2]
self.assertTrue(is_available(self.card3, on_table, P1, 'D', 2))
def test_trump_wanted_false(self):
on_table = [self.card1, self.card8, self.card2]
self.assertFalse(is_available(self.card2, on_table, P1, 'D', 2))
def test_player_has_to_play_trump_true(self):
on_table = [self.card4]
self.card4.played_by = P0
self.assertTrue(is_available(self.card7, on_table, P11, 'S', 1))
def test_player_has_to_play_trump_false(self):
on_table = [self.card4]
self.card4.played_by = P0
self.assertFalse(is_available(self.card8, on_table, P11, 'S', 1))
def test_player_free_because_partner_wins(self):
self.card1.played_by = P0
self.card5.played_by = P1
self.card3.played_by = P00
on_table = [self.card1, self.card5, self.card3]
self.assertTrue(is_available(self.card8, on_table, P11, 'S', 1))
def test_expected_rise_all_trumps_true(self):
on_table = [self.card1]
P0.hand = [self.card2, self.card3]
self.assertTrue(is_available(self.card3, on_table, P0, 'D', 6))
def test_expected_rise_all_trumps_false(self):
on_table = [self.card1]
P0.hand = [self.card2, self.card3]
self.assertFalse(is_available(self.card2, on_table, P0, 'D', 6))
def main():
unittest.main()
if __name__ == '__main__':
main()