def testAddAndRemove(self): c1 = hw5_cards_ec1.Card(0, 2) c2 = hw5_cards_ec1.Card(1, 1) init_list = [c1, c2] hand1 = hw5_cards_ec1.Hand(init_list) #c2 should not be added to the hand #c2 is already in the hand len_before = len(hand1.init_card) hand1.add_card(c2) len_after = len(hand1.init_card) self.assertEqual(len_after, len_before) #c3 should be added successfullt c3 = hw5_cards_ec1.Card(suit=1, rank=7) len_before = len(hand1.init_card) hand1.add_card(c3) len_after = len(hand1.init_card) self.assertEqual(len_after, len_before + 1) #remove one card #remove c3 self.assertEqual(c3, hand1.remove_card(c3)) #cannot remove c3, c3 is not in the hand self.assertEqual(None, hand1.remove_card(c3))
def test_construct_Card(self): c1 = hw5_cards_ec1.Card(0, 2) c2 = hw5_cards_ec1.Card(1, 1) init_list = [c1, c2] hand1 = hw5_cards_ec1.Hand(init_list) self.assertEqual(hand1.init_card, init_list) self.assertIsInstance(hand1.init_card[0], hw5_cards_ec1.Card)
def test_construct_Hand(self): cards_already_in_hands = \ [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)] hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands) self.assertIsInstance(hand_1, hw5_cards_ec1.Hand) self.assertIsInstance(hand_1.init_card, list) self.assertEqual(hand_1.init_card[0].__str__(), "Ace of Diamonds") self.assertEqual(hand_1.init_card[1].__str__(), "4 of Clubs") self.assertEqual(hand_1.init_card[2].__str__(), "Jack of Spades") self.assertEqual(hand_1.init_card[3].__str__(), "Queen of Spades")
def testAddAndRemove(self): c1 = hw5_cards_ec1.Card(1,2) c2 = hw5_cards_ec1.Card(2,4) h2 = hw5_cards_ec1.Hand() n0 = len(h2.cards) h2.add_card(c1) n1 = len(h2.cards) h2.remove_card(c1) n2 = len(h2.cards) self.assertEqual(n0,n2) self.assertEqual(n1-1,n2)
def test_q2(self): ''' 1. fill in your test method for question 1: Test that if you create a card instance with suit 1, its suit_name will be "Clubs" 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' self.assertEqual(hw5_cards_ec1.Card(1, 12).suit_name, 'Clubs') return hw5_cards_ec1.Card(1, 12).suit_name, 'Clubs'
def test_draw(self): c1 = hw5_cards_ec1.Card(0, 1) c2 = hw5_cards_ec1.Card(0, 2) d1 = hw5_cards_ec1.Deck() d1.shuffle() h1 = hw5_cards_ec1.Hand(d1.deal_hand(5)) self.assertEqual(len(h1.init_cards), 5) self.assertEqual(len(d1.cards), 47) h1.draw(d1) self.assertEqual(len(h1.init_cards), 6) self.assertEqual(len(d1.cards), 46) h1.draw(d1) self.assertEqual(len(h1.init_cards), 7) self.assertEqual(len(d1.cards), 45)
def test_q3(self): ''' 1. fill in your test method for question 3: Test that if you invoke the __str__ method of a card instance that is created with suit=3, rank=13, it returns the string "King of Spades" 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' self.assertEqual(hw5_cards_ec1.Card(3, 13).__str__(), 'King of Spades') return hw5_cards_ec1.Card(3, 13).__str__(), 'King of Spades'
def test_init(self): init_cards = [ hw5_cards_ec1.Card(3, 12), hw5_cards_ec1.Card(0, 13), hw5_cards_ec1.Card(2, 6), hw5_cards_ec1.Card(1, 5), hw5_cards_ec1.Card(3, 3) ] hand = hw5_cards_ec1.Hand(init_cards) self.assertIsInstance(hand, hw5_cards_ec1.Hand) self.assertIsInstance(hand.cards, list) self.assertEqual(hand.cards[0].__str__(), "Queen of Spades") self.assertEqual(hand.cards[1].__str__(), "King of Diamonds") self.assertEqual(hand.cards[2].__str__(), "6 of Hearts") self.assertEqual(hand.cards[3].__str__(), "5 of Clubs") self.assertEqual(hand.cards[4].__str__(), "3 of Spades")
def test_q9(self): ''' test that hand is initialized properly ''' new_card = hw5_cards_ec1.Card() hand = hw5_cards_ec1.Hand([new_card]) self.assertEqual(hand.cards, [new_card])
def test_draw(self): deck = hw5_cards_ec1.Deck() init_cards = [ hw5_cards_ec1.Card(3, 12), hw5_cards_ec1.Card(0, 13), hw5_cards_ec1.Card(2, 6), hw5_cards_ec1.Card(1, 5), hw5_cards_ec1.Card(3, 3) ] hand = hw5_cards_ec1.Hand(init_cards) # Remove the cards that are in hand from the deck deck_str = [] for i in deck.cards: deck_str.append(i.__str__()) hand_str = [] for i in hand.cards: hand_str.append(i.__str__()) for i in hand_str: for j in deck_str: if j == i: idx = deck_str.index(j) deck.cards.pop(idx) # Make sure the cards in hand and the deck cards are compensate with each other. initial_deck_length = len(deck.cards) initial_hand_length = len(hand.cards) self.assertEqual(initial_deck_length + initial_hand_length, 52, msg="Compensate pair") # Make sure the draw function works hand.draw(deck) after_deck_length = len(deck.cards) after_hand_length = len(hand.cards) self.assertEqual(after_deck_length + after_hand_length, 52, msg="Compensate pair") self.assertEqual(after_deck_length, initial_deck_length - 1, msg="Remove one from deck") self.assertEqual(after_hand_length, initial_hand_length + 1, msg="Add one to the hand")
def test_construct_Card(self): c1 = hw5_cards_ec1.Card(0, 2) c2 = hw5_cards_ec1.Card(1, 1) self.assertEqual(c1.suit, 0) self.assertEqual(c1.suit_name, "Diamonds") self.assertEqual(c1.rank, 2) self.assertEqual(c1.rank_name, "2") self.assertIsInstance(c1.suit, int) self.assertIsInstance(c1.suit_name, str) self.assertIsInstance(c1.rank, int) self.assertIsInstance(c1.rank_name, str) self.assertEqual(c2.suit, 1) self.assertEqual(c2.suit_name, "Clubs") self.assertEqual(c2.rank, 1) self.assertEqual(c2.rank_name, "Ace")
def test_add_remove(self): ''' test that add_card and remove_card function functions as inteneded ''' new_card = hw5_cards_ec1.Card() hand = hw5_cards_ec1.Hand([]) hand.add_card(new_card) self.assertEqual(hand.cards, [new_card]) hand.remove_card(new_card) self.assertEqual(hand.cards, [])
def test_initialize_Hand(self): ''' Test that a hand is initialized properly ''' c1 = hw5_cards_ec1.Card(0, 2) c2 = hw5_cards_ec1.Card(1, 1) c3 = hw5_cards_ec1.Card(2, 12) c4 = hw5_cards_ec1.Card(3, 6) c5 = hw5_cards_ec1.Card(0, 13) h = hw5_cards_ec1.Hand([c1, c2, c3, c4, c5]) # Initialize a hand with five cards self.assertIsInstance(h.init_cards, list) # check the type of the Instance Attribute self.assertEqual(len(h.init_cards), 5) # check the number of cards self.assertEqual(type(h.init_cards[0]), hw5_cards_ec1.Card) # check the type of the card self.assertEqual(str(h.init_cards[1]), str(c2)) # check if the card corresponds to the input
def testAddAndRemove(self): cards_already_in_hands = \ [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)] hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands) # test a card already in hand card_in = hand_1.init_card[2] initial_length = len(hand_1.init_card) hand_1.add_card(card_in) length_after_add = len(hand_1.init_card) self.assertEqual(initial_length, length_after_add) card_to_remove = hand_1.remove_card(card_in) self.assertEqual(card_to_remove.__str__(), "Jack of Spades") self.assertEqual(len(hand_1.init_card), length_after_add - 1) # test a card not in hand card_not_in = card_in # we've already removed it, so not in response = hand_1.remove_card(card_not_in) self.assertEqual(response, None) current_length = len(hand_1.init_card) hand_1.add_card(card_not_in) self.assertEqual(len(hand_1.init_card), current_length + 1)
def testAddAndRemove(self): #test add c1 = hw5_cards_ec1.Card(0, 1) c2 = hw5_cards_ec1.Card(0, 2) c3 = hw5_cards_ec1.Card(2, 13) hand1 = [c1, c2] hand2 = [c1, c2, c3] h1 = hw5_cards_ec1.Hand(hand1) #add existing card h1.add_card(c1) self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards) #add other card h1.add_card(c3) self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand2).init_cards) #test remove #remove card in hand h1.remove_card(c3) self.assertEqual(len(h1.init_cards), 2) self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards) #remove card not in hand h1.remove_card(c3) self.assertEqual(len(h1.init_cards), 2) self.assertEqual(h1.init_cards, hw5_cards_ec1.Hand(hand1).init_cards)
def testAddAndRemove(self): init_cards = [ hw5_cards_ec1.Card(3, 12), hw5_cards_ec1.Card(0, 13), hw5_cards_ec1.Card(2, 6), hw5_cards_ec1.Card(1, 5), hw5_cards_ec1.Card(3, 3) ] hand = hw5_cards_ec1.Hand( init_cards) #create a Hand object for convenience handlength_0 = len(hand.cards) #Add a card (invalid card, not adding anything) hand.add_card(hw5_cards_ec1.Card(3, 12)) handlength_1 = len(hand.cards) self.assertEqual(handlength_0, handlength_1, msg="Test adding a card when the card is in hand.") #Add a card (valid card) hand.add_card(hw5_cards_ec1.Card(0, 1)) handlength_2 = len(hand.cards) self.assertEqual( handlength_1 + 1, handlength_2, msg="Test adding a card when the card is not in hand.") #Remove a card (valid card) card_to_remove = hand.remove_card(hw5_cards_ec1.Card(0, 1)) handlength_3 = len(hand.cards) self.assertEqual(handlength_2, handlength_3 + 1, msg="Test removing a card that is in hand") self.assertEqual(card_to_remove.__str__(), "Ace of Diamonds", msg="Test the card that is removed is as specified") #Remove a card (invalid card, not really removing anything) card_to_remove2 = hand.remove_card(hw5_cards_ec1.Card(3, 8)) handlength_4 = len(hand.cards) self.assertEqual(handlength_3, handlength_4, msg="Test removing a card that is not in hand.") self.assertEqual(card_to_remove2, None)
def test_draw(self): # construct hand_1, instance of Hand cards_already_in_hands = \ [hw5_cards_ec1.Card(0, 1), hw5_cards_ec1.Card(1, 4), hw5_cards_ec1.Card(3, 11), hw5_cards_ec1.Card(3, 12)] hand_1 = hw5_cards_ec1.Hand(cards_already_in_hands) # construct deck, instance of Deck deck = hw5_cards_ec1.Deck() # eliminate cards already in hand_1 from deck deck_existing_card_str = [] for deck_card in deck.cards: deck_existing_card_str.append(deck_card.__str__()) hand_existing_card_str = [] for hand_card in hand_1.init_card: hand_existing_card_str.append(hand_card.__str__()) deck_str_to_use = [] for deck_card_str in deck_existing_card_str: if deck_card_str not in hand_existing_card_str: deck_str_to_use.append(deck_card_str) deck_to_use = [] for deck_card in deck.cards: if deck_card.__str__() in deck_str_to_use: deck_to_use.append(deck_card) deck.cards = deck_to_use # test starts current_len_hand_cards = len(hand_1.init_card) current_len_deck_cards = len(deck.cards) # make sure they compensate for each other self.assertEqual(current_len_hand_cards + current_len_deck_cards, 52) hand_1.draw(deck) after_len_hand_cards = len(hand_1.init_card) after_len_deck_cards = len(deck.cards) self.assertEqual(after_len_hand_cards, current_len_hand_cards + 1) self.assertEqual(after_len_deck_cards, current_len_deck_cards - 1)
def test_construct_Card(self): hand = [] for i in range(1, 4): hand.append(hw5_cards_ec1.Card(0, i)) h1 = hw5_cards_ec1.Hand(hand) self.assertEqual(h1.init_cards[0].suit, 0) self.assertEqual(h1.init_cards[0].suit_name, "Diamonds") self.assertEqual(h1.init_cards[0].rank, 1) self.assertEqual(h1.init_cards[0].rank_name, "Ace") self.assertEqual(h1.init_cards[1].suit, 0) self.assertEqual(h1.init_cards[1].suit_name, "Diamonds") self.assertEqual(h1.init_cards[1].rank, 2) self.assertEqual(h1.init_cards[1].rank_name, "2") self.assertEqual(h1.init_cards[2].suit, 0) self.assertEqual(h1.init_cards[2].suit_name, "Diamonds") self.assertEqual(h1.init_cards[2].rank, 3) self.assertEqual(h1.init_cards[2].rank_name, "3")
def test_q8(self): ''' 1. fill in your test method for question 8: Test that if you invoke the replace_card method with a card that is already in the deck, the deck size is not affected.(The function must silently ignore it if you try to add a card that’s already in the deck) 2. remove the pass command 3. uncomment the return command and 3b. change X, Y to the values from your assert statement ### please note: normally unit test methods do not have return statements. But returning will allow for unit testing of your unit test, and allow you to check your answer with the autograder. This is optional today. ''' deck = hw5_cards_ec1.Deck() deck.replace_card(hw5_cards_ec1.Card(0, 1)) self.assertEqual(len(deck.cards), 52) return len(deck.cards), 52
def testAddAndRemove(self): ''' Test that add_card( ) and remove_card( ) behave as specified. ''' c1 = hw5_cards_ec1.Card(0, 2) c2 = hw5_cards_ec1.Card(1, 1) c3 = hw5_cards_ec1.Card(2, 12) c4 = hw5_cards_ec1.Card(3, 6) c5 = hw5_cards_ec1.Card(0, 13) c6 = hw5_cards_ec1.Card(1, 3) list_init_cards = [c1, c2, c3, c4, c5] h1 = hw5_cards_ec1.Hand( list_init_cards) # Initialize a hand with five cards h2 = hw5_cards_ec1.Hand( list_init_cards) # Initialize a hand with five cards num_init_cards = len(h1.init_cards) # Test add_card() h1.add_card(c3) # Add the card that is already in the hand self.assertEqual( len(h1.init_cards), num_init_cards) # Check if the number of cards does not change h1.add_card(c6) # Add a new card self.assertEqual( len(h1.init_cards), num_init_cards + 1) # Check if the number of cards has increased by one self.assertTrue(str(c6) in [str(card) for card in h1.init_cards ]) # Check if the new card is in the hand # Test remove_card() h2.remove_card(c6) # Remove a card that is not in the hand self.assertEqual( len(h2.init_cards), num_init_cards) # Check if the number of cards does not change removed_card = h2.remove_card( c4) # Remove the card that is in the hand self.assertEqual( len(h2.init_cards), num_init_cards - 1) # Check if the number of cards has decreased by one self.assertEqual(str(removed_card), str(c4)) # Check if the removed card is correct
def test_init(self): c1 = hw5_cards_ec1.Card(1,2) c2 = hw5_cards_ec1.Card(2,4) h1 = hw5_cards_ec1.Hand([c1,c2]) self.assertIsInstance(h1,hw5_cards_ec1.Hand)