예제 #1
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def test_deck_init(self):
        self.assertTrue(isinstance(self.deck.card_deck, list))

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

    def test_count(self):
        self.assertEqual(self.deck.count(), 52)
        self.deck.card_deck.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        self.deck._deal(55)
        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(55)

    def test_deal_card(self):
        card = self.deck.card_deck[-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 shuffle_full_deck(self):
        cards = self.deck.card_deck[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.card_deck)
        self.assertEqual(len(self.deck.card_deck), 52)

    def shuffle_not_full_deck(self):
        self.deck.deal_hand(20)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
예제 #2
0
class Testing(unittest.TestCase):
    def setUp(self):
        self.player = Card('Hearts', 'A')
        self.playerone = Deck()

    def test_suit(self):
        self.assertEqual(self.player.suit, 'Hearts')

    def test_suit_value(self):
        self.assertEqual(self.player.value, 'A')

    def test_represent_card(self):
        self.assertEqual(repr(self.player), 'A of Hearts')

    def test_tot_cards(self):
        self.assertEqual(len(self.playerone.cards), 52)

    def test_represent_deck(self):
        self.assertEqual(repr(self.playerone), 'Deck of 52 cards')

    def test_count(self):
        self.assertEqual(self.playerone.count(), 52)

    def test_shuffle(self):
        self.assertEqual(len(self.playerone.shuffle()), 52)
        self.assertNotEqual(self.playerone.cards[0:4], self.playerone.shuffle()[0:4])

    def test_deal_card(self):
        popped = self.playerone.cards[-1]
        dealt = self.playerone.deal_card()
        self.assertEqual(dealt, popped)
        self.assertEqual(self.playerone.count(), 51)

    def test_deal_hand(self):
        popped = self.playerone.cards[-1:-11:-1]
        dealt = self.playerone.deal_hand(10)
        self.assertEqual(dealt, popped)
        self.assertEqual(self.playerone.count(), 42)

    def test_add_card_removal(self):
        with self.assertRaises(ValueError):
            self.playerone.deal_hand(52)
            self.playerone.deal_card()

    def test_full_deck_shuffle(self):
        with self.assertRaises(ValueError):
            self.playerone.deal_card()
            self.playerone.shuffle()
예제 #3
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        """ a deck should have a cards attribute which is a list consisting of 52 cards """
        self.assertIsInstance(self.deck.cards, list)
        self.assertEqual(len(self.deck.cards), 52)

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

    def test_count(self):
        """ count should return count of the number of cards in the deck"""
        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 number of cards specified """
        self.cards = self.deck._deal(10)
        self.assertEqual(len(self.cards), 10)
        self.assertEqual(self.deck.count(), 42)

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

    def test_deal_no_cards(self):
        """_deal should throw a ValueError if there is no cards left in the deck"""
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        """deal_card should 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 number of cards specified from the deck"""
        hand = self.deck.deal_hand(20)
        self.assertEqual(len(hand), 20)
        self.assertEqual(self.deck.count(), 32)

    def test_shuffle_deck(self):
        """ shuffle should shuffle the deck only when it 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 when tried shuffling not a full deck"""
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
예제 #4
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

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

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

    def test_count(self):
        """count should return a count of the number of cards in the deck."""
        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, if possible"""
        cards = self.deck._deal(10)
        self.assertTrue(isinstance(cards, list),
                        "_deal(10) should return a list of 10 cards")
        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, if more cards are requested"""
        cards = self.deck._deal(100)
        self.assertTrue(
            isinstance(cards, list),
            "_deal(100) should return a list with all cards left in deck")
        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 should 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 into it"""
        cards = self.deck.deal_hand(20)
        self.assertTrue(isinstance(cards, list),
                        "_deal(20) should return a list with 20 cards in it")
        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 of the deck isn't full"""
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()