Permalink
Browse files

Expand test coverage

  • Loading branch information...
1 parent c1dcc4b commit 0a538b309ae4714a64bdbfa60671f94d98ca8d6f @marktabler committed Dec 30, 2011
Showing with 101 additions and 31 deletions.
  1. +14 −3 lib/deuces/hand.rb
  2. +26 −19 test/card_test.rb
  3. +9 −9 test/deck_test.rb
  4. +52 −0 test/hand_test.rb
View
@@ -10,18 +10,29 @@ class Hand
def self.parse(hand_code)
cards = hand_code.split(' ')
hand = Hand.new
- hand.stuff(cards)
+ hand.stuff(*cards)
hand
end
+ # If given a deck object as a parameter, then this will create the hand by
+ # drawing five cards from the deck. If no deck object is provided, then the
+ # new hand will be empty.
def initialize(deck = nil)
deck ? @cards = deck.deal(5) : @cards = []
end
-
+
+ # Returns all the card objects in the hand.
def cards
@cards
end
-
+
+ # Returns the number of cards in the hand.
+ def size
+ @cards.size
+ end
+
+ # Calls to_s on each of the cards in the hand and joins them with spaces
+ # into a single string.
def to_s
@cards.map(&:to_s).join(' ')
end
View
@@ -3,28 +3,35 @@
class TestCard < MiniTest::Unit::TestCase
- def test_wilds
- @wild_card = Deuces::Card.parse('2HW')
- @not_wild_card = Deuces::Card.parse('2C')
- @joker = Deuces::Card.parse('JO')
- assert @wild_card.wild?
- assert !@not_wild_card.wild?
- assert @joker.wild?
+ def test_wild_parsing
+ @card = Deuces::Card.parse('2HW')
+ assert @card.wild?
end
- def test_parse
- @ace_of_hearts = Deuces::Card.parse('AH')
- assert_equal "AH", @ace_of_hearts.to_s
- assert_equal @ace_of_hearts.suit, Deuces::Card::SUITS['H']
- assert_equal @ace_of_hearts.face, Deuces::Card::FACES['A']
- assert_equal @ace_of_hearts.face, 14
+ def test_joker_parsing
+ @card = Deuces::Card.parse('JO')
+ assert @card.wild?
+ assert @card.joker?
+ end
+
+ def test_non_wild_deuce_parsing
+ @card = Deuces::Card.parse('2C')
+ assert !@card.wild?
+ assert_equal 2, @card.face
+ assert_equal 1, @card.suit
+ end
- @eight_of_spades = Deuces::Card.parse('8S')
- assert_equal "8S", @eight_of_spades.to_s
- assert_equal @eight_of_spades.suit, Deuces::Card::SUITS['S']
- assert_equal @eight_of_spades.face, Deuces::Card::FACES['8']
- assert_equal @eight_of_spades.face, 8
+ def test_parse_non_deuce
+ @card = Deuces::Card.parse('8S')
+ assert_equal "8S", @card.to_s
+ assert_equal @card.suit, Deuces::Card::SUITS['S']
+ assert_equal @card.face, Deuces::Card::FACES['8']
+ assert_equal @card.face, 8
+ end
- assert_equal @eight_of_spades, Deuces::Card.parse(@eight_of_spades)
+ def test_parse_accepts_card_object
+ @card = Deuces::Card.new(5, 2)
+ assert_equal @card, Deuces::Card.parse(@card)
end
+
end
View
@@ -5,40 +5,40 @@ class TestDeck < MiniTest::Unit::TestCase
def setup
end
- def test_deck_build
+ def test_standard_deck_build
@standard_deck = Deuces::Deck.new(:standard)
assert_equal 52, @standard_deck.cards.size
assert_equal 4, @standard_deck.cards.map(&:suit).uniq.size
assert_equal 13, @standard_deck.cards.map(&:face).uniq.size
- assert_equal @standard_deck.cards.compact.uniq, @standard_deck.cards
assert !@standard_deck.cards.map(&:wild?).any?
end
- def test_deuces_build
+ def test_deuces_deck_build
@deuces_deck = Deuces::Deck.new(:deuces_wild)
assert_equal 52, @deuces_deck.cards.size
assert_equal 4, @deuces_deck.cards.map(&:suit).uniq.size
assert_equal 13, @deuces_deck.cards.map(&:face).uniq.size
- assert_equal @deuces_deck.cards.compact.uniq, @deuces_deck.cards
assert @deuces_deck.cards.select { |card| card.wild? }.size == 4
end
- def test_joker_build
+ def test_joker_deck_build
@two_jokers_deck = Deuces::Deck.new(:two_jokers)
assert_equal 54, @two_jokers_deck.cards.size
- assert_equal @two_jokers_deck.cards.compact.uniq, @two_jokers_deck.cards
assert_equal 2, @two_jokers_deck.cards.select { |card| card.wild? }.size
end
- def test_deal_with_reshuffle
+ def test_deal_with_reshuffle_continues_after_deck_out
@deck = Deuces::Deck.new(:standard)
toss = @deck.deal(50)
@hand = @deck.deal(5)
assert_equal 5, @hand.size
end
- def test_deal_without_reshuffle
-
+ def test_deal_without_reshuffle_raises_error_on_deck_out
+ @deck = Deuces::Deck.new(:standard)
+ toss = @deck.deal(50)
+ assert_raises(RuntimeError) { @hand = @deck.deal(5, false) }
end
+
end
View
@@ -0,0 +1,52 @@
+require 'minitest/autorun'
+require 'deuces'
+class TestHand < MiniTest::Unit::TestCase
+
+ def setup
+ @deck = Deuces::Deck.new('S')
+ end
+
+ def test_new_hand_from_deck
+ @hand = Deuces::Hand.new(@deck)
+ assert_equal 5, @hand.size
+ end
+
+ def test_new_hand_from_parse
+ @hand = Deuces::Hand.parse("AH KH QH JH TH")
+ assert_equal 5, @hand.size
+ end
+
+ def test_discard
+ @hand = Deuces::Hand.parse("AH KH QH JH TH")
+ @hand.discard([0, 2, 4])
+ assert_equal 2, @hand.size
+ assert_equal "KH JH", @hand.to_s
+ end
+
+ def test_draw
+ @hand = Deuces::Hand.parse("AH KH")
+ @hand.draw(@deck, 3)
+ assert_equal 5, @hand.size
+ end
+
+ def test_full_house_beats_two_pair
+ @hand = Deuces::Hand.parse("2H 2C 3C 3H 3S")
+ assert @hand.two_pair?
+ assert @hand.three_of_a_kind?
+ assert @hand.full_house?
+ assert_equal "Full House", @hand.score
+ end
+
+ def test_wild_royal_beats_four_of_a_kind
+ @hand = Deuces::Hand.parse("2HW 2CW 2DW AH QH")
+ assert @hand.wild_royal?
+ assert @hand.four_of_a_kind?
+ assert @hand.full_house?
+ assert @hand.three_of_a_kind?
+ assert @hand.flush?
+ assert @hand.straight?
+ assert !@hand.natural_royal?
+ assert_equal "Royal Flush w/ Wild", @hand.score
+ end
+
+end

0 comments on commit 0a538b3

Please sign in to comment.