def test_pushInvalidAlternatingPile(self):
     """
     Tests that a Foundation pile rejects a pile with valid bottom 
     Card but is not alternating in color.
     """
     print "tableau: test_pushInvalidAlternatingPile ",
     append_cards(self.pile)
     oldCards = self.pile
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i)
         card.flip_up()
         invalidPile.enqueue(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushInvalidAlternatingPile: \
                      A pile whose top Card had an invalid rank was pushed\
                      onto the Tableau.")
 def test_pushInvalidSuitPile(self):
     """
     Tests that a Foundation pile rejects a pile that is built 
     properly, but the top card is not of the proper color.
     """
     print "tableau: test_pushInvalidSuitPile ",
     append_cards(self.pile)
     oldCards = self.pile
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.DIAMOND, i) if i % 2 == 0 else Card(Suit.SPADE, i)
         card.flip_up()
         invalidPile.enqueue(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushInvalidSuitPile: A pile \
                      whose top Card had an invalid color was pushed onto \
                      the Tableau.")
 def test_pushInvalidBuiltPile(self):
     """
     Tests that a Foundation pile rejects a pile with valid bottom 
     Card but is not built properly.
     """
     print "tableau: test_pushInvalidBuiltPile ",
     append_cards(self.pile)
     oldCards = Pile(copy.copy(list(self.pile)))
     invalidPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i) if i % 2 == 0 else Card(Suit.DIAMOND, i)
         card.flip_up()
         invalidPile.enqueue_card(card)
     card = Card(Suit.CLUB, 6)
     card.flip_up()
     invalidPile.push_card(card)
     self.pile.push_pile(invalidPile)
     self.assertEqual(self.pile, oldCards, 
                      "An invalid pile was pushed to the Tableau.")
 def test_pushValidPile(self):
     """
     Tests that a Foundation pile accepts a properly built pile 
     whose bottom card is the proper color and rank.
     """
     print "tableau: test_pushValidPile ",
     append_cards(self.pile)
     self.pile.flip_top()
     oldCards = Pile(copy.copy(list(self.pile)))
     validPile = Pile()
     for i in range(1,5):
         card = Card(Suit.SPADE, i) if i % 2 == 0 else Card(Suit.HEART, i)
         card.flip_up()
         validPile.enqueue(card)
     newCards = copy.copy(validPile)
     oldCards.extend(newCards)
     self.pile.push_pile(validPile)
     self.assertEqual(self.pile, oldCards, 
                      "TestTableauPile.test_pushValidPile: A valid pile \
                      was rejected by the Tableau pile.")
Example #5
0
 def setUp(self):
     """Initializes a new Pile."""
     self.pile = Pile()
Example #6
0
class TestPile(unittest.TestCase):    
    """
    
    Tests the functionality of a Pile.
    
    """

    def setUp(self):
        """Initializes a new Pile."""
        self.pile = Pile()

    def tearDown(self):
        """Does nothing."""
        return
    
    def test_init(self):
        """Tests the Pile init() function"""
        print "\npile: test_init ",
        self.assertEqual(self.pile.name, "Pile")
        self.assertEqual(len(self.pile), 0)
    
    def test_size(self):
        """
        Tests that len(self.pile) returns the number of 
        Cards in the Pile.
        """
        print "\npile: test_size() ",
        self.assertEqual(len(self.pile), 0)
        append_cards(self.pile)
        self.assertEqual(len(self.pile), 13)
    
    def test_maxlen(self):
        """Tests that the maximum size is properly set."""
        print "\npile: test_maximumSize ",
        self.assertEqual(self.pile.maxlen, 52)
    
    def test_isEmpty(self):
        """
        Tests that is_empty() returns True for empty Piles 
        and False for nonempty Piles.
        """
        print "\npile: test_isEmpty ",
        self.assertTrue(self.pile.is_empty())
        self.pile.append(Card(Suit.SPADE, 1))
        self.assertFalse(self.pile.is_empty())
    
    def test_enqueue(self):
        """
        Tests that enqueue() inserts a Card at the bottom 
        of a Pile.
        """
        print "\npile: test_enqueue ",
        for i in range(1,6):
            card = Card(Suit.HEART, i)
            self.pile.enqueue(card)
            self.assertEqual(self.pile[0], card, 
                             "The card at the bottom of the Pile is not the \
                             card that was enqueued.")
    
    def test_enqueueCard(self):
        """
        Tests that enqueue_card() does not insert a Card at the bottom 
        of a Pile.
        """
        print "\npile: test_enqueueCard ",
        for i in range(1,6):
            card = Card(Suit.HEART, i)
            self.pile.enqueue_card(card)
            self.assertIn(card, self.pile, 
                             "The Card wasn't enqueued to the Pile.")
    
    def test_push(self):
        """
        Tests that push() inserts a Card at the top of 
        a Pile.
        """
        print "\npile: test_push ",
        for i in range(1,6):
            card = Card(Suit.HEART, i)
            self.pile.push(card)
            self.assertEqual(self.pile[-1], card, 
                             "The card at the top of the Pile was not the \
                             card that was pushed.")
    
    def test_pushCard(self):
        """
        Tests that push() inserts a Card at the top of 
        a Pile, provided the resulting Pile does not exceed 
        its maximum number of cards.
        """
        print "\npile: test_pushCard ",
        for i in range(1,6):
            card = Card(Suit.DIAMOND, i)
            self.pile.push_card(card)
            self.assertEqual(self.pile[-1], card, 
                             "The card at the top of the Pile was not the \
                             card that was pushed.")

    def test_pushCardExceedMaximum(self):
        """
        Tests that push() inserts a Card at the top of 
        a Pile, provided the resulting Pile does not exceed 
        its maximum number of cards.
        """
        print "\npile: test_pushCardExceedMaximum ",
        create_deck(self.pile)
        card = Card(Suit.DIAMOND, 1)
        self.pile.push_card(card)
        self.assertEqual(len(self.pile), 52)

    def test_pushPile(self):
        """Tests that piles cannot be pushed to a Pile.
        
        @todo: FINISH TEST
        @todo: FAILURE MESSAGE
        """
        print "\npile: test_pushPile ",
        new_pile = Pile()
        append_cards(new_pile)
        self.pile.push_pile(new_pile)
        self.assertEqual(len(self.pile), 13, 
                         "")
    
    def test_peek(self):
        """Tests that peek returns the top Card.
        
        @todo: FAILURE MESSAGE
        """
        print "\npile: test_peek ",
        append_cards(self.pile)
        top = Card('D', 4)
        self.pile.push(top)
        self.assertEqual(self.pile.peek(), top, 
                         "")
    
    def test_pop(self):
        """
        Tests that pop returns and removes the top Card.
        
        @todo: FAILURE MESSAGE
        """
        print "\npile: test_pop ",
        append_cards(self.pile)
        top = Card(Suit.DIAMOND, 4)
        self.pile.push(top)
        self.assertEqual(self.pile.pop(), top, 
                         "")
        self.assertEqual(len(self.pile), 13, 
                         "")
    
    def test_popCard(self):
        """
        Tests that popping a card removes the top Card from 
        a nonempty Pile.
        
        @todo: FAILURE MESSAGE
        """
        print "\npile: test_popCard ",
        append_cards(self.pile)
        top = Card(Suit.DIAMOND, 4)
        self.pile.push(top)
        self.assertEqual(self.pile.pop_card(), top, 
                         "The card popped was not the last card in the Pile.")
        self.assertEqual(len(self.pile), 13, 
                         "The size of the pile was incorrect after popping.")

    def test_popCardEmpty(self):
        """
        Tests that popping a card from an empty Pile
        returns None.
        """
        print "\npile: test_popCardEmpty ",
        self.assertIsNone(self.pile.pop_card(), 
                          "A card was popped from an empty pile.")
        self.assertEqual(len(self.pile), 0)
    
    def test_popPile(self):
        """Tests that popping a pile correctly splits the Pile."""
        print "\npile: test_popPile ",
        append_cards(self.pile)
        topCard = self.pile[-5]
        self.assertIn(topCard, self.pile.pop_pile(topCard))
        self.assertNotIn(topCard, self.pile)
    
    def test_flipTop(self):
        """
        Tests that flip_top() flips the top Card face-up.
        
        @todo: FAILURE MESSAGE
        """
        print "\npile: test_flipTop ",
        append_cards(self.pile)
        top = self.pile[-1]
        self.pile.flip_top()
        self.assertTrue(top.is_face_up(), 
                        "")