Пример #1
0
class TestBlackWinnerLogic(unittest.TestCase):
    def setUp(self):
        self.testPlayer = Player()
        self.testDealer = Dealer()
        self.winner = None

    def tearDown(self):
        del self.testPlayer
        del self.testDealer

    def test_Winnner_push(self):
        #  both player and dealer have Ace and Face card
        self.testPlayer.recieve_card(
            Card(CardFaceType.ACE, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.ACE, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertTrue((self.winner is None),
                        "there should be no winner here")

    def test_Winner_playerBust(self):
        # dealer has lower hand, player busts
        self.testPlayer.recieve_card(
            Card(CardFaceType.KING, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.FIVE, SuitType.DIAMOND, CardFaceState.UP))

        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.ACE, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Dealer, "Dealer should have won")

    def test_winner_dealerBust(self):
        #         player has lower hand, dealer busts
        self.testPlayer.recieve_card(
            Card(CardFaceType.SIX, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.UP))

        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.TEN, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.ACE, SuitType.SPADE, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.FOUR, SuitType.DIAMOND, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Player, "Player should have won")

    def test_winner_playerBlackjack(self):
        # player has a blackjack
        self.testPlayer.recieve_card(
            Card(CardFaceType.ACE, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.NINE, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Player, "Player Should have won")

    def test_winner_dealerBlackJack(self):
        # dealer has blackjack
        self.testPlayer.recieve_card(
            Card(CardFaceType.SEVEN, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.ACE, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Dealer, "Dealer should have won")

    def test_winner_player(self):
        #player has better hand < blackJack
        self.testPlayer.recieve_card(
            Card(CardFaceType.NINE, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.EIGHT, SuitType.HEART, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.THREE, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Player, "Player should have won")

    def test_winner_dealer(self):
        #dealer has better hand < blackJack
        self.testPlayer.recieve_card(
            Card(CardFaceType.TWO, SuitType.CLUB, CardFaceState.UP))
        self.testPlayer.recieve_card(
            Card(CardFaceType.NINE, SuitType.HEART, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.KING, SuitType.DIAMOND, CardFaceState.UP))
        self.testDealer.recieve_card(
            Card(CardFaceType.SIX, SuitType.SPADE, CardFaceState.UP))

        self.winner = blackjack.determine_blackjack_winner(
            self.testPlayer, self.testDealer)
        self.assertIsInstance(self.winner, Dealer, "Dealer should have won")
class TestDealer(unittest.TestCase):
    def setUp(self):
        self.testDealer = Dealer()

    def tearDown(self):
        del self.testDealer

    def test_hittable_case1(self):
        # draw some cards , face up, value > 13 < 17
        # assert on return of hittable status
        self.testDealer.recieve_card(Card(CardFaceType.KING, SuitType.CLUB))
        self.testDealer.recieve_card(Card(CardFaceType.FIVE, SuitType.DIAMOND))
        self.assertTrue(self.testDealer.should_hit(True),
                        "dealer should have hit")

    def test_hittable_case2(self):
        # draw some cards , face up, one ace,  value > 13 < 17
        # assert on return of hittable status
        self.testDealer.recieve_card(Card(CardFaceType.ACE, SuitType.CLUB))
        self.testDealer.recieve_card(Card(CardFaceType.THREE, SuitType.HEART))
        self.assertTrue(self.testDealer.should_hit(True),
                        "dealer should have hit")

    def test_hittable_case3(self):
        # draw some cards, face up with a value greater than 17
        self.testDealer.recieve_card(Card(CardFaceType.KING, SuitType.CLUB))
        self.testDealer.recieve_card(Card(CardFaceType.FIVE, SuitType.CLUB))
        self.testDealer.recieve_card(Card(CardFaceType.FIVE, SuitType.HEART))
        self.assertFalse(self.testDealer.should_hit(True),
                         "dealer sould have stayed")

    def test_hittable_case4(self):
        self.testDealer.recieve_card(Card(CardFaceType.ACE, SuitType.CLUB))
        self.testDealer.recieve_card(Card(CardFaceType.ACE, SuitType.SPADE))
        self.testDealer.recieve_card(Card(CardFaceType.SEVEN, SuitType.HEART))
        # draw cards, at least 2 aces
        self.assertFalse(self.testDealer.should_hit(True),
                         "dealer should have stayed")

    def test_visable_value_case1(self):
        self.testDealer.recieve_card(Card(CardFaceType.EIGHT, SuitType.SPADE))
        self.testDealer.recieve_card(
            Card(CardFaceType.JACK, SuitType.HEART, CardFaceState.DOWN))
        visible_value = self.testDealer.get_visible_hand_value()
        self.assertEqual(visible_value, 8, "error on visible hand value")
        self.testDealer.show_card_faces()
        visible_value = self.testDealer.get_visible_hand_value()
        self.assertEqual(visible_value, 18,
                         "error in visible hand value after turning cards")

    def test_visable_value_case2(self):
        self.testDealer.recieve_card(Card(CardFaceType.EIGHT, SuitType.SPADE))
        self.testDealer.recieve_card(
            Card(CardFaceType.ACE, SuitType.HEART, CardFaceState.DOWN))
        visible_value = self.testDealer.get_visible_hand_value()
        self.assertEqual(visible_value, 8, "error on visible hand value")
        self.testDealer.show_card_faces()
        visible_value = self.testDealer.get_visible_hand_value()
        self.assertEqual(visible_value, 19,
                         "error in visible hand value after turning cards")