Package games.gofish

Source Code of games.gofish.TestGoFish

package games.gofish;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import game.Card;
import game.Deck;
import game.Game;
import games.gofish.state.ComputerTurnSate;
import games.gofish.state.PlayerTurnState;

import org.junit.Before;
import org.junit.Test;

public class TestGoFish
{
  private GoFish goFish;
 
  @Before
  /**
   * Make a new gofish for each test
   */
  public void setUp()
  {
    goFish = new GoFish();
    Deck.getInstance().reset();
  }
 
  @Test
  /**
   * Test the initialization
   */
  public void testInitializatigon()
  {
    assertEquals(7, goFish.getPlayer().getHand().getSize());
    assertEquals(7, goFish.getComputer().getHand().getSize());
    assertTrue(goFish.getState() instanceof PlayerTurnState);
 
 
  @Test
  /**
   * Test to make sure the state can be changed
   */
  public void testChangingState()
  {
    goFish.setState(new ComputerTurnSate());
    assertTrue(goFish.getState() instanceof ComputerTurnSate);
    goFish.setState(new PlayerTurnState());
    assertTrue(goFish.getState() instanceof PlayerTurnState);
  }
 
  @Test
  /**
   * Test to make sure the right player is asking for a card.
   * A player will automatically fish if the other player do
   * not have the card being asked for. Also if the other player
   * has the requested card, it will be given to the player that
   * is asking. Turn changes not matter what.
   */
  public void testAskingForCard()
  {
    //Player turn
    goFish.askForCard(14);
    assertEquals(8, goFish.getPlayer().getHand().getSize());
    assertEquals(7, goFish.getComputer().getHand().getSize());
   
    //Computer turn
    goFish.askForCard(14);
    assertEquals(8, goFish.getPlayer().getHand().getSize());
    assertEquals(8, goFish.getComputer().getHand().getSize());
   
    //Force a card into the computers hand for the player to ask for
    //For testing puposes I will use a card not possible to get from the deck
    goFish.getComputer().getHand().addCard(new Card(15, 'c'));
    goFish.askForCard(15);
    //Player gains a card, computer loses one
    assertEquals(9, goFish.getPlayer().getHand().getSize());
    assertEquals(8, goFish.getComputer().getHand().getSize());
    assertTrue(goFish.getPlayer().getHand().contains(new Card(15, 'c')));
    assertFalse(goFish.getComputer().getHand().contains(new Card(15, 'c')));
   
    //Force a card into the players hand for the computer to ask for
    //For testing puposes I will use a card not possible to get from the deck
    goFish.getPlayer().getHand().addCard(new Card(16, 'c'));
    goFish.askForCard(16);
    //Computer gains a card, player loses one
    assertEquals(9, goFish.getPlayer().getHand().getSize());
    assertEquals(9, goFish.getComputer().getHand().getSize());
    assertFalse(goFish.getPlayer().getHand().contains(new Card(16, 'c')));
    assertTrue(goFish.getComputer().getHand().contains(new Card(16, 'c')));
  }
 
  @Test
  /**
   * Test to make sure that gofish works
   */
  public void testGoFish()
  {
    goFish.goFish();
    assertEquals(8, goFish.getPlayer().getHand().getSize());
    goFish.setState(new ComputerTurnSate());
    goFish.goFish();
    assertEquals(8, goFish.getComputer().getHand().getSize());
  }
 
  @Test
  /**
   * Check to see if mathches are found
   * For testing cards that cannot be obtained from the deck will be used
   */
  public void testCheckForMatch()
  {
    ArrayList<Card> list = new ArrayList<Card>();
    List<Card> l = Arrays.asList(new Card[] {new Card(14, 'c'), new Card(14, 'd'), new Card(14, 'h'), new Card(14, 's')});
    list.addAll(l);
    //Test player
    goFish.getPlayer().getHand().addCards(list);
    assertTrue(goFish.checkForMatch());
   
    //Test computer
    goFish.setState(new ComputerTurnSate());
    goFish.getComputer().getHand().addCards(list);
    assertTrue(goFish.checkForMatch());
   
    //Remove all cards and make sure no matches are found
    for (int i = 0; i < goFish.getPlayer().getHand().getSize();)
    {
      goFish.getPlayer().getHand().removeCard(i);
      goFish.getComputer().getHand().removeCard(i);
    }
   
    //Test player
    goFish.setState(new PlayerTurnState());
    assertFalse(goFish.checkForMatch());
   
    //Test computer
    goFish.setState(new ComputerTurnSate());
    assertFalse(goFish.checkForMatch());
  }
}
TOP

Related Classes of games.gofish.TestGoFish

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.