-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_blackjack.py
295 lines (251 loc) · 9.84 KB
/
test_blackjack.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import unittest
from blackjack import *
class CardTestCase(unittest.TestCase):
"""Unit tests for Blackjack Card class"""
def test_card_validity(self):
"""Only valid cards can be created"""
card = Card("3","♡")
with self.assertRaises(AssertionError):
card = Card("13","♡")
with self.assertRaises(AssertionError):
card = Card("A", "x")
def test_card_representation(self):
"""Is card representation correct?"""
card = Card("A", "♡")
self.assertEqual(str(card), " A♡")
card = Card("10", "♡")
self.assertEqual(str(card), "10♡")
def test_value_of_card(self):
"""Does card return correct value?"""
card = Card("5", "♡")
self.assertEqual(card.value(), 5)
card = Card("A", "♡")
self.assertEqual(card.value(), 11)
card = Card("J", "♡")
self.assertEqual(card.value(), 10)
def test_card_is_ace(self):
"""Is an Ace recognised correctly?"""
card = Card("A", "♡")
self.assertTrue(card.ace())
card = Card("8", "♡")
self.assertFalse(card.ace())
class DeckTestCase(unittest.TestCase):
"""Unit tests for Blackjack Deck class"""
def test_size_of_deck(self):
"""Are there 52 cards in the deck?"""
deck = Deck()
self.assertEqual(len(deck.cards), 52)
def test_shuffle_randomizes_deck(self):
"""Does the deck get shuffled?"""
deck_one = Deck()
deck_one.shuffle()
deck_two = Deck()
deck_two.shuffle()
self.assertNotEqual(str(deck_one), str(deck_two))
def test_deal_removes_a_card(self):
"""Does a deal remove one card from the deck?"""
deck = Deck()
num_before = len(deck.cards)
deck.deal()
num_after = len(deck.cards)
self.assertEqual(num_before, num_after + 1)
def test_deal_returns_a_card(self):
"""Does calling deal return a card?"""
deck = Deck()
self.assertIsInstance(deck.deal(), Card)
def test_empty_deck_refills(self):
"""Does an empty deck get refilled?"""
deck = Deck()
deck.cards = []
deck.deal()
self.assertEqual(len(deck.cards), 51)
class HandTestCase(unittest.TestCase):
"""Unit tests for Blackjack Hand class"""
def test_hand_representation(self):
"""Is hand representation correct?"""
hand = Hand()
hand.add_card(Card("A", "♡"))
hand.add_card(Card("5", "♡"))
self.assertEqual(str(hand), " A♡ 5♡")
def test_does_a_new_card_get_added(self):
"""Does a new dealt card end up in the hand?"""
hand = Hand()
card = Card("A", "♡")
num_before = len(hand.cards)
hand.add_card(card)
num_after = len(hand.cards)
self.assertEqual(num_after, num_before + 1)
self.assertIs(hand.cards[-1], card)
def test_calculates_points_correctly(self):
"""Does the value of the hand calculate correctly?"""
hand = Hand()
hand.add_card(Card("A", "♡"))
hand.add_card(Card("5", "♡"))
self.assertEqual(hand.value(), 16)
hand.add_card(Card("A", "♡"))
self.assertEqual(hand.value(), 17)
hand.add_card(Card("7", "♡"))
self.assertEqual(hand.value(), 14)
def test_blackjack_detected(self):
"""Does 'blackjack' get detected correctly?"""
hand = Hand()
hand.add_card(Card("A", "♡"))
hand.add_card(Card("J", "♡"))
self.assertTrue(hand.blackjack())
hand.add_card(Card("10", "♡"))
self.assertFalse(hand.blackjack())
def test_twenty_one_detected(self):
"""Does 'twenty one' get detected correctly?"""
hand = Hand()
hand.add_card(Card("A", "♡"))
hand.add_card(Card("5", "♡"))
self.assertFalse(hand.twenty_one())
hand.add_card(Card("5", "♡"))
self.assertTrue(hand.twenty_one())
def test_bust_detected(self):
"""Does a 'bust' get detected correctly?"""
hand = Hand()
hand.add_card(Card("J", "♡"))
hand.add_card(Card("5", "♡"))
self.assertFalse(hand.bust())
hand.add_card(Card("10", "♡"))
self.assertTrue(hand.bust())
def test_split_hand(self):
"""Does a split work for splittable hand?"""
card = Card("J", "♡")
hand_one = Hand()
hand_one.add_card(card)
hand_one.add_card(card)
hand_one.add_card(Card("5", "♢"))
with self.assertRaises(AssertionError):
hand_one.split()
hand_one.cards.pop()
hand_two = hand_one.split()
self.assertIsInstance(hand_two, Hand)
self.assertTrue(str(hand_one) == str(hand_two))
class PlayerTestCase(unittest.TestCase):
"""Unit tests for the Blackjack Player class"""
def test_player_has_chips(self):
"""Does the player report chips left correctly?"""
player = Player("Wazza", 100)
self.assertTrue(player.has_chips())
self.assertFalse(player.has_chips(150))
player.chips = 0
self.assertFalse(player.has_chips())
def test_player_can_bet(self):
"""Is the player able to record a bet?"""
player = Player("Wazza", 100)
bet = player.bet(10)
self.assertEqual(bet, 10)
self.assertEqual(player.chips, 90)
def test_player_is_able_to_double_down(self):
"""Does the player report double down is possible correctly?"""
player = Player("Wazza", 100)
hand = Hand(50)
hand.add_card(Card("5", "♢"))
hand.add_card(Card("2", "♢"))
self.assertTrue(player.can_double_down(hand))
player.chips = 0
self.assertFalse(player.can_double_down(hand))
player.chips = 100
hand.add_card(Card("2", "♢"))
self.assertTrue(player.can_double_down(hand))
hand.add_card(Card("5", "♢"))
self.assertFalse(player.can_double_down(hand))
def test_player_can_split_hand(self):
"""Is a splitting a hand possible?"""
player = Player("Wazza", 100)
hand = Hand(50)
hand.add_card(Card("J", "♡"))
hand.add_card(Card("5", "♢"))
self.assertFalse(player.can_split(hand))
hand.add_card(Card("J", "♡"))
self.assertFalse(player.can_split(hand))
hand.cards.pop()
hand.cards.pop()
hand.add_card(Card("J", "♡"))
self.assertTrue(player.can_split(hand))
player.chips = 0
self.assertFalse(player.can_split(hand))
def test_player_wins_bet(self):
"""Does the player record wins correctly?"""
player = Player("Wazza", 100)
player.win(10, 1.5)
self.assertEqual(player.chips, 125)
self.assertEqual(player.results['wins'], 1)
def test_player_loses_bet(self):
"""Does the player record losses correctly?"""
player = Player("Wazza", 100)
player.loss()
self.assertEqual(player.chips, 100)
self.assertEqual(player.results['losses'], 1)
def test_player_pushes_bet(self):
"""Does the player record pushes correctly?"""
player = Player("Wazza", 100)
player.push(10)
self.assertEqual(player.chips, 110)
self.assertEqual(player.results['ties'], 1)
def test_available_hand_generator(self):
"""Available hands should adjust by status"""
player = Player("Wazza", 100)
hand_1 = Hand(10)
hand_2 = Hand(20)
hand_3 = Hand(30)
player.hands = [hand_1, hand_2, hand_3]
result = player.active_hands()
self.assertEqual(next(result), hand_1)
hand_2.active = False
self.assertEqual(next(result), hand_3)
class GameTestCase(unittest.TestCase):
"""Unit tests for the Blackjack Game class"""
def test_new_game_creates_players(self):
"""Is the player collection initialised?"""
names = "foo bar baz".split()
chips = 10
game = Game(names, chips)
self.assertEqual(len(game.players), 3)
self.assertEqual(game.players[0].name, names[0])
def test_max_name_len_calculated(self):
"""Does the longest name get correctly calculated"""
names = "foo bar wazza".split()
game = Game(names, 100)
self.assertEqual(game.max_name_len, len("Dealer"))
names = "foo bar wazza verylongname".split()
game = Game(names, 100)
self.assertEqual(game.max_name_len, len("verylongname"))
def test_format_text(self):
"""Format text produces desired output"""
game = Game(['foo'], 100)
blue = "\x1b[34m"
stop = "\x1b[0m"
resp = game.format_text('foo', 'testing', 'blue')
test = "{}{} > {}{}".format(blue, 'foo'.rjust(len("Dealer")), 'testing', stop)
self.assertEqual(resp, test)
def test_players_with_chips(self):
"""Test list of players with remaining chips"""
game = Game(['foo'], 100)
self.assertTrue(game.players_with_chips())
player = game.players[0]
player.chips = 0
self.assertFalse(game.players_with_chips())
def test_active_players(self):
"""Test that players with active hands works"""
game = Game(['foo', 'bar', 'baz'], 100)
for player in game.players:
player.hands.append(Hand(10))
result = game.active_players()
self.assertEqual(next(result).name, 'foo')
game.players[1].hands[0].active = False
self.assertEqual(next(result).name, 'baz')
def test_has_active_hands(self):
"""Test that we can detetc active hands"""
game = Game(['foo'], 100)
game.players[0].hands.append(Hand(10))
self.assertTrue(game.has_active_hands())
game.players[0].hands[0].active = False
self.assertFalse(game.has_active_hands())
if __name__ == '__main__':
unittest.main()