class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_print_deck(self): self.assertEqual(self.deck.__repr__(), 'Deck of 52 cards') def test_count(self): """checks that an accurate count is returned""" self.assertEqual(self.deck.count(), 52) self.deck.deal_card() self.assertEqual(self.deck.count(), 51) def test_shuffle(self): """checks that shuffle works as expected""" self.deck.shuffle() self.assertEqual(self.deck.count(), 52) self.deck.deal_card() with self.assertRaises(ValueError): self.deck.shuffle() def test_shuffle_random(self): """checks that shuffle works as expectedand gives a random first card""" self.deck.shuffle() card = self.deck.deal_card() self.assertNotEqual(Card("Hearts", "A"), card) def test_deal_card(self): """Check that only one card is dealt and it has a value and suit as expected""" suits = ("Hearts", "Diamonds", "Clubs", "Spades") values = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K") card = self.deck.deal_card() self.assertEqual(self.deck.count(), 51) self.assertIn(card.suit, suits) self.assertIn(card.value, values) card = self.deck.deal_card() self.assertEqual(self.deck.count(), 50) self.assertIn(card.suit, suits) self.assertIn(card.value, values) def test_deal_hand(self): """check that the size of the hand dealt is as expected""" hand = self.deck.deal_hand(5) self.assertEqual(len(hand), 5) self.assertEqual(self.deck.count(), 47) for card in hand: self.assertIsInstance(card, Card) hand = self.deck.deal_hand(7) self.assertEqual(len(hand), 7) self.assertEqual(self.deck.count(), 40) def test_insufficient_cards(self): hand = self.deck.deal_hand(50) hand = self.deck.deal_hand(4) self.assertEqual(len(hand), 2) with self.assertRaises(ValueError): self.deck.deal_hand(1)
class TestDeck(unittest.TestCase): def setUp(self): self.emptyDeck = Deck() self.fullDeck = Deck(range(52)) self.acesDeck = Deck([48, 49, 50, 51]) def test_init(self): self.assertEqual(self.emptyDeck.count(), 0) self.assertEqual(self.fullDeck.count(),52) def test_init_can_shuffle(self): shuffledDeck = Deck(range(52), shuffle=True) self.assertNotInSameOrder(self.fullDeck, shuffledDeck) def test_repr(self): self.assertEqual(self.emptyDeck.__repr__(), "<Deck: []>") self.assertEqual(self.acesDeck.__repr__(), "<Deck: [<AC>, <AD>, <AH>, <AS>]>") # How to test whether the deck shuffled? Start with two identical decks, # shuffle one, and then compare them card by card. It's technically possible # to shuffle a deck randomly and end up with the same order of cards, but # it's INCREDIBLY unlikely. (In fact, every time you randomly shuffle a # deck of cards, it's extremely likely that you have just created an order # which has never before been created in the history of all humans shuffling # cards. Wow. def test_shuffle(self): shuffledDeck = Deck(range(52)) shuffledDeck.shuffle() self.assertNotInSameOrder(self.fullDeck, shuffledDeck) def test_draw(self): self.assertEqual(self.acesDeck.draw(), Card(51)) self.assertEqual(self.acesDeck.count(), 3) def test_peek(self): self.assertEqual(self.acesDeck.peek(), Card(51)) self.assertEqual(self.acesDeck.count(), 4) def test_count(self): self.assertEqual(self.emptyDeck.count(), 0) self.assertEqual(self.acesDeck.count(), 4) self.assertEqual(self.fullDeck.count(), 52) def test_empty(self): self.assertTrue(self.emptyDeck.empty()) self.assertFalse(self.acesDeck.empty()) def test_add(self): self.fullDeck.add(Card(0)) self.assertEqual(self.fullDeck.count(), 53) def test_add_deck(self): self.fullDeck.add_deck(self.acesDeck) self.assertEqual(self.fullDeck.draw(), Card(51)) self.assertEqual(self.fullDeck.draw(), Card(50)) self.assertEqual(self.fullDeck.draw(), Card(49)) self.assertEqual(self.fullDeck.draw(), Card(48)) def assertNotInSameOrder(self, deck1, deck2): foundADifference = False while not deck1.empty(): if deck1.draw() != deck2.draw(): foundADifference = True break self.assertTrue(foundADifference)
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): """decks should have a cards attribute, which is a list""" 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(self.deck.__repr__(), '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.deal_card() 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 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""" 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 is not full""" self.deck._deal(1) with self.assertRaises(ValueError): self.deck.shuffle()