-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_poker.py
275 lines (248 loc) · 7.89 KB
/
test_poker.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
from random import shuffle
import pytest
from pytest_dependency import depends
import poker
def get_requested_tests_by_pattern(request, pattern):
return [item.name for item in request.session.items if f'{pattern}[' in item.name]
def test_get_deck():
deck = poker.get_deck()
rank_set = {card[0] for card in deck}
suit_set = {card[1] for card in deck}
assert len(set(deck)) == 52
assert rank_set == {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'}
assert suit_set == {'C', 'D', 'H', 'S'}
@pytest.mark.dependency()
@pytest.mark.parametrize(('n', 'deck', 'expected'), [
pytest.param(
1,
[('2', 'C'), ('A', 'H')],
[('2', 'C')],
id='0 < n < len(deck)',
),
pytest.param(
2,
[('2', 'C'), ('A', 'H')],
[('2', 'C'), ('A', 'H')],
id='n = len(deck)',
),
])
def test_get_cards(n, deck, expected):
reference_deck = deck[n:]
cards = poker.get_cards(n, deck)
assert cards == expected
assert deck == reference_deck
@pytest.mark.parametrize(('n', 'deck', 'expected'), [
pytest.param(
0,
[('2', 'C'), ('A', 'H')],
ValueError('n must be > 0!'),
id='n = 0',
),
pytest.param(
-2,
[('2', 'C'), ('A', 'H')],
ValueError('n must be > 0!'),
id='n < 0',
),
pytest.param(
10,
[('2', 'C'), ('A', 'H')],
ValueError('n must be <= len(deck)!'),
id='n > len(deck)',
),
pytest.param(
1,
[],
ValueError('n must be <= len(deck)!'),
id='len(deck) = 0',
),
])
def test_get_cards_exception(n, deck, expected, request):
depends(request, get_requested_tests_by_pattern(request, 'test_get_cards'))
with pytest.raises(Exception) as exc_info:
poker.get_cards(n, deck)
assert exc_info.type == type(expected)
assert str(exc_info.value) == str(expected)
@pytest.mark.dependency()
@pytest.mark.parametrize(('cards', 'expected'), [
pytest.param(
[('2', 'C'), ('5', 'H'), ('9', 'D'), ('10', 'S'), ('K', 'S')],
[('2', 'C'), ('5', 'H'), ('9', 'D'), ('10', 'S'), ('K', 'S')],
id='sorted',
),
pytest.param(
[('K', 'S'), ('10', 'S'), ('9', 'D'), ('5', 'H'), ('2', 'C')],
[('2', 'C'), ('5', 'H'), ('9', 'D'), ('10', 'S'), ('K', 'S')],
id='reversed',
),
pytest.param(
[('9', 'C'), ('2', 'H'), ('4', 'D'), ('Q', 'S'), ('A', 'S')],
[('2', 'H'), ('4', 'D'), ('9', 'C'), ('Q', 'S'), ('A', 'S')],
id='shuffled',
),
])
def test_sort_cards(cards, expected):
assert poker.sort_cards(cards) == expected
@pytest.mark.dependency()
@pytest.mark.parametrize(('hand', 'expected'), [
pytest.param(
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
'royal flush',
id='royal flush',
),
pytest.param(
[('4', 'C'), ('5', 'C'), ('6', 'C'), ('7', 'C'), ('8', 'C')],
'straight flush',
id='straight flush',
),
pytest.param(
[('2', 'C'), ('7', 'C'), ('8', 'C'), ('J', 'C'), ('A', 'C')],
'flush',
id='flush',
),
pytest.param(
[('4', 'C'), ('4', 'H'), ('4', 'D'), ('4', 'S'), ('A', 'S')],
'four of a kind',
id='four of a kind-1',
),
pytest.param(
[('2', 'C'), ('A', 'C'), ('A', 'D'), ('A', 'H'), ('A', 'S')],
'four of a kind',
id='four of a kind-2',
),
pytest.param(
[('10', 'D'), ('10', 'C'), ('J', 'D'), ('J', 'H'), ('J', 'S')],
'full house',
id='full house-1',
),
pytest.param(
[('3', 'D'), ('3', 'C'), ('3', 'S'), ('J', 'H'), ('J', 'S')],
'full house',
id='full house-2',
),
pytest.param(
[('4', 'D'), ('5', 'C'), ('6', 'H'), ('7', 'S'), ('8', 'S')],
'straight',
id='straight',
),
pytest.param(
[('5', 'C'), ('5', 'H'), ('5', 'D'), ('6', 'H'), ('A', 'S')],
'three of a kind',
id='three of a kind-1',
),
pytest.param(
[('2', 'C'), ('7', 'C'), ('7', 'D'), ('7', 'H'), ('A', 'S')],
'three of a kind',
id='three of a kind-2',
),
pytest.param(
[('2', 'C'), ('5', 'C'), ('A', 'D'), ('A', 'H'), ('A', 'S')],
'three of a kind',
id='three of a kind-3',
),
pytest.param(
[('7', 'H'), ('7', 'C'), ('10', 'D'), ('10', 'H'), ('K', 'S')],
'two pairs',
id='two pairs-1',
),
pytest.param(
[('3', 'S'), ('7', 'H'), ('7', 'C'), ('10', 'D'), ('10', 'H')],
'two pairs',
id='two pairs-2',
),
pytest.param(
[('7', 'H'), ('7', 'C'), ('9', 'S'), ('10', 'D'), ('10', 'H')],
'two pairs',
id='two pairs-3',
),
pytest.param(
[('8', 'H'), ('8', 'C'), ('9', 'D'), ('J', 'H'), ('Q', 'S')],
'pair',
id='pair-1',
),
pytest.param(
[('8', 'H'), ('9', 'C'), ('9', 'D'), ('J', 'H'), ('Q', 'S')],
'pair',
id='pair-2',
),
pytest.param(
[('8', 'H'), ('9', 'C'), ('J', 'D'), ('J', 'H'), ('Q', 'S')],
'pair',
id='pair-3',
),
pytest.param(
[('8', 'H'), ('9', 'C'), ('10', 'D'), ('Q', 'H'), ('Q', 'S')],
'pair',
id='pair-4',
),
pytest.param(
[('5', 'H'), ('6', 'C'), ('9', 'D'), ('Q', 'H'), ('K', 'S')],
'high card',
id='high card',
),
])
def test_determine_hand(hand, expected, request):
depends(request, get_requested_tests_by_pattern(request, 'test_sort_cards'))
shuffle(hand)
assert poker.determine_hand(hand) == expected
@pytest.mark.parametrize(('hand', 'prefix', 'expected'), [
pytest.param(
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
'User hand: ',
"User hand: [('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')] - royal flush\n\n",
id='User hand with royal flush',
),
])
def test_print_hand(hand, prefix, expected, request, capsys):
depends(request, get_requested_tests_by_pattern(request, 'test_determine_hand'))
shuffle(hand)
poker.print_hand(hand, prefix)
captured = capsys.readouterr()
assert captured.out == expected
@pytest.mark.parametrize(('hand', 'card', 'new_card', 'expected'), [
pytest.param(
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
('Q', 'C'),
('K', 'H'),
[('10', 'C'), ('J', 'C'), ('K', 'H'), ('K', 'C'), ('A', 'C')],
id="('Q', 'C') -> ('K', 'H')",
),
])
def test_replace_card(hand, card, new_card, expected):
poker.replace_card(hand, card, new_card)
assert hand == expected
@pytest.mark.parametrize(('hand_1', 'hand_2', 'expected'), [
pytest.param(
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
[('10', 'C'), ('10', 'D'), ('10', 'H'), ('K', 'H'), ('K', 'S')],
0,
id='hand_1_power > hand_2_power',
),
pytest.param(
[('10', 'C'), ('10', 'D'), ('10', 'H'), ('K', 'H'), ('K', 'S')],
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
1,
id='hand_1_power < hand_2_power',
),
pytest.param(
[('10', 'C'), ('J', 'H'), ('Q', 'C'), ('K', 'S'), ('A', 'C')],
[('9', 'C'), ('10', 'D'), ('J', 'H'), ('Q', 'H'), ('K', 'S')],
0,
id='hand_1_sum > hand_2_sum',
),
pytest.param(
[('9', 'C'), ('10', 'D'), ('J', 'H'), ('Q', 'H'), ('K', 'S')],
[('10', 'C'), ('J', 'H'), ('Q', 'C'), ('K', 'S'), ('A', 'C')],
1,
id='hand_1_sum < hand_2_sum',
),
pytest.param(
[('10', 'C'), ('J', 'C'), ('Q', 'C'), ('K', 'C'), ('A', 'C')],
[('10', 'H'), ('J', 'H'), ('Q', 'H'), ('K', 'H'), ('A', 'H')],
2,
id='hand_1 = hand_2',
),
])
def test_compare_hands(hand_1, hand_2, expected, request):
depends(request, get_requested_tests_by_pattern(request, 'test_determine_hand'))
assert poker.compare_hands(hand_1, hand_2) == expected