Changed HandTest to use more formal mocking.
This commit is contained in:
parent
60fe77f044
commit
021886be4d
@ -10,6 +10,7 @@ mainClassName = "uk.co.neviyn.pokergame.App"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile) {
|
||||
@ -20,6 +21,7 @@ dependencies {
|
||||
compile 'org.slf4j:slf4j-api:1.7.22'
|
||||
compile 'org.slf4j:slf4j-simple:1.7.22'
|
||||
testCompile group: 'junit', name: 'junit', version: '4.11'
|
||||
testCompile "org.mockito:mockito-core:2.+"
|
||||
}
|
||||
|
||||
test {
|
||||
|
@ -1,9 +1,10 @@
|
||||
package game;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import uk.co.neviyn.pokergame.game.Hand;
|
||||
import uk.co.neviyn.pokergame.game.IDeck;
|
||||
import uk.co.neviyn.pokergame.model.Card;
|
||||
@ -11,254 +12,163 @@ import uk.co.neviyn.pokergame.model.Result;
|
||||
import uk.co.neviyn.pokergame.model.Suit;
|
||||
import uk.co.neviyn.pokergame.model.Value;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HandTest {
|
||||
|
||||
private FakeDeck deck = new FakeDeck();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
deck.clearDeck();
|
||||
}
|
||||
@Mock IDeck deck;
|
||||
|
||||
@Test
|
||||
public void royalStraightFlush() {
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.JACK));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.QUEEN));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.KING));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK),
|
||||
new Card(Suit.CLUBS, Value.QUEEN), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
Assert.assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void royalStraightFlushWithJoker() {
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.JACK));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.KING));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fiveOfAKind() {
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TEN));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.SPADES, Value.TEN),
|
||||
new Card(Suit.HEARTS, Value.TEN), new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.JOKER, Value.JOKER));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FIVE_OF_A_KIND, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straightFlush() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.THREE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
|
||||
new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straightFlushWithJoker() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.THREE));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.THREE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.THREE),
|
||||
new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
|
||||
hand.newRound();
|
||||
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.THREE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
|
||||
new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER));
|
||||
hand.newRound();
|
||||
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fourOfAKind() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.TWO));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.TWO));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO),
|
||||
new Card(Suit.DIAMONDS, Value.TWO), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fourOfAKindWithJoker() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.TWO));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fullHouse() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.JACK));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.JACK));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
|
||||
new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.JACK), new Card(Suit.SPADES, Value.JACK));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FULL_HOUSE, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flush() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.ACE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.NINE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.KING));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR),
|
||||
new Card(Suit.SPADES, Value.ACE), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flushWithJoker() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.NINE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.KING));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.FLUSH, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straight() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
|
||||
new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.STRAIGHT, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nearStraight() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.THREE));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.THREE), new Card(Suit.DIAMONDS, Value.THREE),
|
||||
new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.ONE_PAIR, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void straightWithJoker() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.STRAIGHT, hand.checkForWin());
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.DIAMONDS, Value.THREE),
|
||||
new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
|
||||
hand.newRound();
|
||||
assertEquals(Result.STRAIGHT, hand.checkForWin());
|
||||
deck.addCard(new Card(Suit.SPADES, Value.TWO));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
|
||||
new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER));
|
||||
hand.newRound();
|
||||
assertEquals(Result.STRAIGHT, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void threeOfAKind() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
|
||||
new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void threeOfAKindWithJoker() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.THREE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
|
||||
new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoPair() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
|
||||
new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.ACE), new Card(Suit.SPADES, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.TWO_PAIR, hand.checkForWin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onePair() {
|
||||
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
|
||||
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
|
||||
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
|
||||
deck.addCard(new Card(Suit.SPADES, Value.ACE));
|
||||
when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
|
||||
new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.SIX), new Card(Suit.SPADES, Value.ACE));
|
||||
Hand hand = new Hand(deck);
|
||||
assertEquals(Result.ONE_PAIR, hand.checkForWin());
|
||||
}
|
||||
|
||||
private class FakeDeck implements IDeck {
|
||||
|
||||
private Stack<Card> cards = new Stack<>();
|
||||
|
||||
public void addCard(Card card) {
|
||||
cards.push(card);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Card drawCard() {
|
||||
return cards.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetDeck() {
|
||||
// FakeDeck has no base deck to reset to.
|
||||
}
|
||||
|
||||
protected void clearDeck() {
|
||||
cards = new Stack<>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user