class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): '''Creates the deck''' self.assertEqual(repr(self.deck), 'Deck of 52 cards') def test_count(self): '''Deck should have 52 cards''' self.assertEqual(self.deck.count(), 52) def test_shuffle_full_deck(self): '''Shuffles the deck full deck''' initial_cards = self.deck.cards[:] self.deck.shuffle() self.assertNotEqual(initial_cards, self.deck.cards) self.assertEqual(len(initial_cards), len(self.deck.cards)) def test_shuffle_not_full_deck(self): '''Cannot shuffle a deck that's not full''' self.deck._deal(1) with self.assertRaises(ValueError): self.deck.shuffle() def test_deal_card(self): '''Deals a card''' self.assertIsInstance(self.deck.deal_card(), Card)
def lucky_card(): d = Deck() d.shuffle() try: user = input("Hello! What's your name? ") deal_amount = int(input(f"How many random cards would you like to be dealt {user}? ")) if deal_amount >= 53: print("There are only 52 cards in a deck!") elif deal_amount == 1: print(f"Here is your lucky card {user} ", d._deal(deal_amount)) elif deal_amount <= 1: print("You have to enter positive number, there isn't a negative suit.") elif 52 >= deal_amount > 1: print(f"Here are your {deal_amount} lucky cards {user}", d._deal(deal_amount)) except ValueError: print("Next Time Enter a Number!")
class DeckTest(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): self.assertEqual(type(self.deck.suits), list) self.assertEqual(type(self.deck.values), list) self.assertEqual(type(self.deck.cards[0]), Card) def test_repr(self): self.assertEqual(repr(self.deck), f"Deck of {self.deck.count()} cards") def test_count(self): self.assertEqual(self.deck.count(), 52) self.deck.cards.pop() self.assertEqual(self.deck.count(), 51) self.deck = Deck() hand = self.deck._deal(53) self.assertTrue(hand) self.assertEqual(type(hand), list) self.assertEqual(len(hand), 52) self.assertEqual(self.deck.count(), 0) def test_deal(self): self.assertEqual(type(self.deck._deal(1)), list) self.assertEqual(self.deck.count(), 51) def test_deal_card(self): self.assertEqual(type(self.deck.deal_card()), Card) self.assertEqual(self.deck.count(), 51) def test_deal_hand(self): self.assertEqual(type(self.deck.deal_hand(5)), list) self.assertEqual(self.deck.count(), 47) self.assertEqual(type(self.deck.deal_hand(5)[0]), Card) def test_shuffle(self): deck_cpy = self.deck.cards[:] self.assertEqual(self.deck.cards, deck_cpy) self.deck.shuffle() self.assertNotEqual(self.deck, deck_cpy) self.assertEqual(self.deck.count(), 52) self.deck.deal_hand(5) with self.assertRaises(ValueError): self.deck.shuffle()
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): self.assertTrue(isinstance(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): self.assertEqual(self.deck.count(), 52) self.deck.cards.pop() self.assertEqual(self.deck.count(), 51) def test_deal_sufficient_amount_of_cards(self): cards = self.deck._deal(10) self.assertEqual(len(cards), 10) self.assertEqual(self.deck.count(), 42) def test_insufficient_amount_of_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_cards(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()
class TestDeck(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): self.assertIsInstance(self.deck.cards, list) def test_count(self): self.assertEqual(len(self.deck.cards), 52) self.deck.cards.pop() self.assertEqual(len(self.deck.cards), 51) def test_repr(self): self.assertEqual(repr(self.deck), 'Deck of 52 cards') def test_deal_some(self): cards = self.deck._deal(5) self.assertEqual(len(cards), 5) self.assertEqual(self.deck.count(), 47) def test_deal_all(self): cards = self.deck._deal(60) self.assertEqual(len(cards), 52) self.assertEqual(self.deck.count(), 0) def test_deal_no_cards(self): self.deck._deal(52) with self.assertRaises(ValueError): self.deck._deal(1) def test_deal_cards(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(12) self.assertEqual(len(cards), 12) self.assertEqual(self.deck.count(), 40) def test_shuffle_unsufficient(self): self.deck._deal(10) self.assertEqual(self.deck.count(), 42) with self.assertRaises(ValueError): self.deck.shuffle() def test_shuffle_sufficient(self): clone_cards = self.deck.cards[:] self.deck.shuffle() self.assertEqual(self.deck.count(), 52) self.assertNotEqual(clone_cards, self.deck.cards)
class TestDeck(unittest.TestCase): def setUp(self): self.d1 = Deck() def test_init(self): """ decks should be card attribute, has 52 cards """ self.assertTrue(isinstance(self.d1.cards, list)) self.assertEqual(len(self.d1.cards), 52) def test_repr(self): """ test repr of duck !""" self.assertEqual(repr(self.d1), "Deck of 52 cards") def test_count(self): """ count how many card left has deck """ self.assertEqual(self.d1.count(), 52) self.d1.cards.pop() self.assertEqual(self.d1.count(), 51) def test_deal_sufficient_card(self): """ check sufficient card has on the deck """ self.assertEqual(len(self.d1._deal(10)), 10) self.assertEqual(len(self.d1._deal(42)), 42) def test_deal_insufficient_card(self): """ check insufficient card has on the deck """ self.assertEqual(len(self.d1._deal(100)), 52) with self.assertRaises(ValueError): self.d1._deal(52) self.assertEqual(len(self.d1.cards), 0) def test_deal_cards(self): """test deal cards with the last card""" last_card = self.d1.cards[-1] deal_card = self.d1.deal_card() self.assertEqual(last_card, deal_card) self.assertEqual(self.d1.count(), 51) def test_deal_hand(self): """test deal hand with removing cards""" self.assertEqual(len(self.d1.deal_hand(52)), 52) with self.assertRaises(ValueError): self.d1.deal_hand(10) def test_shuffle_with_full_deck(self): cards = self.d1.cards[:] self.d1.shuffle() self.assertNotEqual(cards, self.d1.cards) self.assertEqual(len(cards), len(self.d1.cards)) def test_shuffle_with_no_full_deck(self): self.d1._deal(1) with self.assertRaises(ValueError): self.d1.shuffle()
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): self.assertTrue(isinstance(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): self.assertEqual(self.deck.count(), 52) self.deck.cards.pop() self.assertEqual(self.deck.count(), 51) def test_deal_sufficient_cards(self): cards = self.deck._deal(100) self.assertEqual(len(cards), 52) self.assertEqual(self.deck.count(), 0)
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): """__init__ should create a deck/list of 52 cards""" self.assertEqual(len(self.deck.cards), 52) self.assertIsInstance(self.deck.cards, list) # Colt's # self.assertTrue(isinstance(self.deck.cards, list)) def test_repr(self): """__repr__ returns 'Deck of {self.count()} cards'""" self.assertEqual(repr(self.deck), "Deck of 52 cards") def test_reset(self): """Should reset back to init state""" self.assertEqual(self.deck, self.deck.reset()) def test_shuffle_insufficient(self): """Only shuffle decks of 52 cards""" # How to properly call shuffle()? Deck.shuffle()? self.deck.shuffle(self.deck.cards)? # self.assertSequenceEqual(self.deck.cards, Deck.shuffle(self.deck.cards)) # Keep getting != <bound method Deck.shffle of Deck of 52 cards> # Broken: self.assertEqual(self.deck.cards[0:5], self.deck.shuffle()) self.deck.deal_card() with self.assertRaises(ValueError): # Deck.shuffle(self.deck) self.deck.shuffle() # Colt's shuffle full deck solution def test_shuffle_full_deck(self): """shuffle should shuffle the deck if the deck is full""" cards = self.deck.cards[:] # A way to copy original state self.deck.shuffle() self.assertNotEqual(cards, self.deck.cards) self.assertEqual(self.deck.count(), 52) # This is key since [] != full deck # Colt tested the _deal method directly. Here's his tests: 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) # Colt's. Similar to my _insufficient below 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) # Colt's test for ValueError def test_deal_no_cards(self): """_deal should throw a ValueError if the deck is empty""" self.deck._deal( self.deck.count()) # clever way to deal all/empty the deck with self.assertRaises(ValueError): self.deck._deal(1) def test_deal_card_insufficient(self): """Should deal all remaining cards or raise ValueError when zero cards left""" # First dealing out all cards. self.assertEqual(self.deck.cards[-self.deck.count()::], self.deck.deal_hand(53)[::-1]) # Then trying to deal single card when no cards left (self.deck.count() = 0) with self.assertRaises(ValueError): self.deck.deal_card() def test_deal_card(self): """Subtracts 1 card from deck and returns card instance""" # swap order so [-1] gets retrieved before deal pops() card # if going to call deal_card() directly, need to place at top to get count of 51 self.deck.deal_card() self.assertEqual(self.deck.count(), 51) self.assertEqual(self.deck.cards[-1], self.deck.deal_card()) # if using assertNotIn, can I just use self.deck? Seems that way... self.assertNotIn( self.deck.deal_card(), self.deck, msg="Once dealt, the card shouldn't be in original deck") # Colt's solution. Nice use of variables (card and dealt_card) # 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 should be dealt from the top (end) of the deck""" self.assertEqual(self.deck.cards[-6::], self.deck.deal_hand(6)[::-1]) # Colt's solution. # cards = self.deck.deal_hand(20) # self.assertEqual(len(cards), 20) # self.assertEqual(self.deck.count(), 32) def test_count(self): """Return the number of cards in Deck.cards""" self.assertEqual(self.deck.count(), 52) self.deck.deal_hand(10) # Colt's using pop() then checking count() = 51 # self.deck.cards.pop() self.assertEqual(self.deck.count(), 42)
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() #setup sets deck to the full deck through Deck() #runs before every test case def test_init(self): """decks should have a cards attribute, which is a list of 52 items""" #doesn't test if all 52 items are cards, just that there are 52 things in this list self.assertTrue(isinstance(self.deck.cards, list)) self.assertEqual(len(self.deck.cards), 52) def test_repr(self): """should print out deck of 52 cards""" self.assertEqual(repr(self.deck), 'Deck of 52 cards') def test_count(self): """count of deck should be 52 cards and should be 51 after running a pop""" self.assertEqual(self.deck.count(), 52) self.deck.cards.pop() self.assertEqual(self.deck.count(), 51) def test_deal_sufficient_cards(self): """should print out message on _deal() when there is enough cards to deal""" cards = self.deck._deal(10) self.assertEqual(len(cards), 10) self.assertEqual(self.deck.count(), 42) def test_deal_insufficient_cards(self): """should deal the remaining cards 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 ValueErrorif the deck is empty when calling _deal""" #first line deals the entire deck self.deck.count() is 52 so we deal 52 self.deck._deal(self.deck.count()) #raises ValueError when 1 card id dealt when the deck is empty with self.assertRaises(ValueError): self.deck._deal(1) def test_deal_card(self): """deal_card should remove a single card from the deck""" #card will be the last card in the deck card = self.deck.cards[-1] #self.deck.deal() is also the last card in the deck 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 in as an argument""" 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""" #sets the cards variable to one instance of the deck cards = self.deck.cards[:] #shuffles the initial deck (not cards) self.deck.shuffle() #checks if cards and self.deck.cards are not the same (self.deck.cards has been shuffled self.assertNotEqual(cards, self.deck.cards) #checks if self.deck contains 52 cards necessary to run shuffle() self.assertEqual(self.deck.count(), 52) def test_shuffle_not_full_deck(self): """throws a ValueError when shuffling a not full deck""" #deals 1 card from the deck which makes self.deck.count() 51 self.deck._deal(1) #tries shuffling the 51 card deck and raises a valueerror. with self.assertRaises(ValueError): self.deck.shuffle()
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): """decks should have a cards attribute, which is a """ self.assertTrue(len(self.deck.cards), 52) def test_repr(self): """repr should return a string of the form 'Deck of 52' cards """ self.assertEqual(repr(self.deck), "Deck of 52 cards") def test_count(self): """count should return a count of the number of cards remaining """ 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 remaining """ 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 """ self.deck._deal(self.deck.count()) with self.assertRaises(ValueError): self.deck._deal(1) def test_deal_card(self): """deal_card should deal a sinle 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 """ cards = self.deck.cards[:] self.deck.shuffle() self.assertEqual(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()
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 cards """ 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, and the deck count has gone down to all minus dealt cards""" 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 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.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""" self.deck._deal(1) with self.assertRaises(ValueError): self.deck.shuffle()
class DeckTests(unittest.TestCase): def setUp(self): self.deck = Deck() def test_init(self): """decks should have a cards list attribute with length 52""" self.assertTrue(isinstance(self.deck.cards, list)) self.assertEqual(len(self.deck.cards), 52) def test_repr(self): """decks should be represented as 'Deck of COUNT cards'""" self.assertEqual(repr(self.deck), "Deck of 52 cards") def test_count(self): """count should return 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 there are enough cards in the deck""" 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 if there's not enough cards 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_cards should deal a single card""" 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 shoudl deal the number of cards passed in""" 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 not shuffle an incomplete deck""" self.deck._deal(1) with self.assertRaises(ValueError): self.deck.shuffle()