COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Ass 3 Unit Testing

Posted by on October 11th, 2011 · Assignments

It turns out that testing the play() method on your classes is tricker than I had anticipated when I set assignment 3. To test this method properly, you need to set up a game in a known situation, forcing one of the players to play the card you are testing and then check that it does what you expect.

The second UnoGame constructor is useful in this situation, as it allows you to stack the deck in a controlled fashion. For example. you could test the DrawCard as follows:

    public void testPlay()   {
        // stack the deck
        ArrayList deck = new ArrayList();
                
        DrawCard redDraw2 = new DrawCard(Card.COLOUR_RED, 2);
        
        // player 0's cards - one playable and 4 unplayable
        deck.add(redDraw2);
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        
        // player 1's cards - it doesn't matter what these are
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));
        deck.add(new Card(Card.COLOUR_BLUE, 5));

        // the card that becomes the initial pile
        // chosen to allow player 0 to play the red draw 2
        
        deck.add(new Card(Card.COLOUR_RED, 1));
        
        // the draw pile -- add enough cards for player1 to draw 2
        
        deck.add(new Card(Card.COLOUR_GREEN, 2));
        deck.add(new Card(Card.COLOUR_GREEN, 3));
        deck.add(new Card(Card.COLOUR_GREEN, 4));

        // create a game using this deck (unshuffled)
        UnoGame game = new UnoGame(deck, 2);
        game.dealCards();
        
        // get the two players
        Player player0 = game.getPlayers().get(0);
        Player player1 = game.getPlayers().get(1);
        
        // player 0 plays a turn
        game.playTurn();
        
        // check the card played is the red draw 2
        assertEquals(redDraw2, game.getPile().get(0));
        
        // check how many cards each player has
        assertEquals(4, player0.getCards().size());
        assertEquals(7, player1.getCards().size());
        
        // check that we skipped player1's turn
        assertEquals(player0, game.getCurrentPlayer());        
        
        
    }

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.