def test_determine_winners(self): """ Test that the dealer hits at the appropriate times and that winning hands are appropriated updated as such. """ # setup deck deck = Deck() deck.create() deck.shuffle() # setup gambler, dealer & hands dealer = Dealer() dealer_hand = Hand() dealer.hands.append(dealer_hand) gambler = Gambler("Test") gambler_hand = Hand() gambler.hands.append(gambler_hand) players = [gambler, dealer] # must hit a soft 17 (A, 6) gambler_hand.final_value = 20 dealer_hand.cards.append(Card('Hearts', 'Ace', 1)) dealer_hand.cards.append(Card('Hearts', 'Six', 6)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) > 2) # must hit on 16 or less (K, 6) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'King', 10)) dealer_hand.cards.append(Card('Hearts', 'Six', 6)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) > 2) # check dealer bust (K, 6, J) dealer_hand.cards.pop() dealer_hand.cards.append(Card('Hearts', 'Jack', 10)) bj.determine_winners(players, deck) self.assertTrue(dealer_hand.busted) self.assertTrue(gambler_hand.win) # check dealer stands on 17 (K, 7) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'King', 10)) dealer_hand.cards.append(Card('Hearts', 'Seven', 7)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) == 2) # gambler wins with 20 to dealer's 17 self.assertTrue(gambler_hand.win) # check dealer stands on soft 18 (Ace, 7) self.reset_hand_attrs(dealer_hand) dealer_hand.cards.append(Card('Hearts', 'Ace', 1)) dealer_hand.cards.append(Card('Hearts', 'Seven', 7)) bj.determine_winners(players, deck) self.assertTrue(len(dealer_hand.cards) == 2)
def test_deck_size(self): """ Make sure each deck is exactly 52 cards Check for each shoe size. """ for i in range(bj.MIN_DECKS, bj.MAX_DECKS + 1): deck = Deck(num_decks=i) deck.create() self.assertEqual(len(deck.cards), 52 * i)
class TestHand(unittest.TestCase): def test_deal_card(self): """ Test that dealing a card removes 1 card from the deck and adds 1 card to the hand. """ self.deck = Deck(num_decks=1) self.deck.create() self.deck.shuffle() self.hand = Hand() self.hand.deal_card(self.deck) self.assertEqual(len(self.deck.cards), 51) self.assertEqual(len(self.hand.cards), 1) def test_check_busted(self): """ Check to see if a hand is a bust (> 21) or not. """ # hand is busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Jack', 10)) self.hand.check_busted() self.assertTrue(self.hand.busted) # hand is not busted self.hand = Hand() self.hand.cards.append(Card('Hearts', 'King', 10)) self.hand.cards.append(Card('Hearts', 'Three', 3)) self.hand.cards.append(Card('Hearts', 'Six', 6)) self.hand.check_busted() self.assertFalse(self.hand.busted) def test_get_hand_value(self): """ Make sure get_hand_value() returns accurate hand values. """ # test hand values with Aces self.hand = Hand() self.hand.cards.append(Card('Hearts', 'Ace', 1)) self.hand.cards.append(Card('Spades', 'Ace', 1)) self.hand.cards.append(Card('Hearts', 'Six', 6)) hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [8, 18]) # test hand values with hidden card (Dealer) self.hand.cards[2].hidden = True hand_values = self.hand.get_hand_value() self.assertEqual(hand_values, [2, 12]) # test hand values with hidden card included (Dealer) hand_values = self.hand.get_hand_value(include_hidden=True) self.assertEqual(hand_values, [8, 18])