package games.memory;
import static org.junit.Assert.*;
import game.Card;
import game.Deck;
import games.memory.difficulty.HardDifficulty;
import games.memory.state.PlayingState;
import org.junit.Before;
import org.junit.Test;
/**
* Tests associated with Memory
* @author Ian Keefer
*/
public class TestMemory {
/**
* Ensures the tests don't fail due to the deck
*/
@Before
public void resetDeck()
{
Deck.getInstance().reset();
}
/**
* tests the initialization of the memory game
*/
@Test
public void testInitilization()
{
Memory.reset();
Memory m = Memory.getInstance();
assertNotNull(m);
assertEquals(10, m.getLives());
assertNull(m.getState());
}
/**
* tests that the Memory class only ever has once instance
*/
@Test
public void testSingleton()
{
Memory.reset();
Memory m = Memory.getInstance();
Memory n = Memory.getInstance();
assertSame(m, n);
}
/**
* test the selection getters and setters
*/
@Test
public void testSelection()
{
Memory.reset();
Memory m = Memory.getInstance();
m.setSelections(new Card(1, 'h'), new Card(2, 'd'));
assertEquals("1-h", m.getSelectionOne().toString());
assertEquals("2-d", m.getSelectionTwo().toString());
}
/**
* test setting difficulty and state
*/
@Test
public void testDifficultyAndState()
{
Memory.reset();
Memory m = Memory.getInstance();
m.setDifficulty(new HardDifficulty());
m.setState(new PlayingState(m));
assertTrue(m.getDifficulty() instanceof HardDifficulty);
assertTrue(m.getState() instanceof PlayingState);
}
/**
* tests setting lives
*/
@Test
public void testLives()
{
Memory.reset();
Memory m = Memory.getInstance();
m.setLives(100);
assertEquals(100, m.getLives());
}
/**
* Tests to make sure there isn't two of the same cards within the gameBoard 2d array
* without comparing the same card.
*/
@Test
public void testCreateBoard()
{
Memory.reset();
Memory m = Memory.getInstance();
Card currentCard;
for(int i = 0; i < m.gameBoard.length; i++) // Current Card
{
for(int j = 0; j < m.gameBoard[i].length; j++)
{
currentCard = m.gameBoard[i][j];
for(int t = 0; t < m.gameBoard.length; t++) // Secondary Card
{
for(int r = 0; r < m.gameBoard[i].length; r++) {
if(t != i || r != j) // Checks to make sure it isn't the same card position.
{
assertTrue(currentCard != m.gameBoard[t][r]);
}
}
}
}
}
}
/**
* tests comparing cards that are selected
*/
@Test
public void testCompareMemoryMatches()
{
Memory.reset();
Memory m = Memory.getInstance();
m.setSelections(new Card(1, 'h'), new Card(1, 'd')); // same value, different suits
assertTrue(m.compareCards());
m.setSelections(new Card(1, 'h'), new Card(1, 'c')); // same value, different color
assertFalse(m.compareCards());
m.setSelections(new Card(1, 'h'), new Card(2, 'd')); // different value, difference suit
assertFalse(m.compareCards());
}
}