Esempio n. 1
0
class DeckTests(unittest.TestCase):

    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        '''deck should have 52 cards'''
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_repr(self):
        """repr should return a string in the form of 'deck of cards'"""
        self.assertEqual(repr(self.deck), "Deck of 52 cards")

    def test_count(self):
        """count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards),20)
        self.assertEqual(self.deck.count(),32)

    def test_shuffle_full_deck(self):
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
Esempio n. 2
0
class TestingDeckClass(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        """decks should have a cards attibute which is a list"""
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_deck_repr(self):
        """Test that repr returns a deck of 52 cards when first initialized, no more no less"""
        self.assertEqual(str(self.deck), "Deck of 52 cards")

    def test_count(self):
        """ count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """_deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        """_deal should deal the number of cards left in the deck"""
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        """_deal should throw a ValueError if the deck is empty"""
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        """ deal_card whould deal a single card from the deck"""
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        """deal_hand should deal the number of cards passed"""
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards), 20)
        self.assertEqual(self.deck.count(), 32)

    def test_shuffle_full_deck(self):
        """shuffle should shuffle the deck if the deck is full"""
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        """shuffle should throw a ValueError if the deck isn't full"""
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
Esempio n. 3
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.sample_deck = Deck()

    def test_init(self):
        self.assertIsInstance(self.sample_deck.cards, list)
        self.assertEqual(len(self.sample_deck.cards), 52)

    def test_repr(self):
        self.assertEqual(repr(self.sample_deck), "Deck of 52 cards")

    def test_iter(self):
        self.assertTrue(iter(self.sample_deck))

    def test_count(self):
        self.assertEqual(self.sample_deck.count(), 52)
        self.sample_deck.cards.pop()
        self.assertEqual(self.sample_deck.count(), 51)

    def test_deal_cards(self):
        self.sample_deck._deal(10)
        self.assertEqual(len(self.sample_deck.cards), 42)
        self.assertEqual(len(self.sample_deck._new_hand), 10)

    def test_deal_last(self):
        self.sample_deck._deal(56)
        self.assertEqual(len(self.sample_deck.cards), 0)
        self.assertEqual(len(self.sample_deck._new_hand), 52)

    def test_deal_no_cards(self):
        self.sample_deck.cards.clear()
        with self.assertRaises(ValueError):
            self.sample_deck._deal(2)

    def test_deal_card(self):
        result = self.sample_deck.deal_card()
        self.assertIsInstance(result, Card)
        self.assertEqual(result, self.sample_deck._new_hand[0])
        self.assertEqual(len(self.sample_deck.cards), 51)
        self.assertEqual(len(self.sample_deck._new_hand), 1)

    def test_deal_hand(self):

        result = self.sample_deck.deal_hand(15)
        self.assertIsInstance(result, list)
        self.assertEqual(len(self.sample_deck.cards), 37)
        self.assertEqual(len(self.sample_deck._new_hand), 15)

    def test_shuffle_invalid(self):
        """shuffle raises a ValueError if the card deck is not full"""
        self.sample_deck.cards.pop()
        with self.assertRaises(ValueError):
            self.sample_deck.shuffle()

    def test_shuffle_valid(self):
        """shuffle shuffles a full deck in place"""
        original = self.sample_deck.cards[:]  #making a new copy b/c lists are mutable!
        self.sample_deck.shuffle()
        result = self.sample_deck.cards
        self.assertEqual(len(result), 52)
        self.assertFalse(original == result)