Permalink
Browse files

Fail when the wrong number of card strings is given to the parser

  • Loading branch information...
camillebaldock committed Sep 7, 2014
1 parent d4f9dc3 commit c135042fe6ba939e661c7504611485f806d7f7ad
Showing with 47 additions and 25 deletions.
  1. +4 −1 README.md
  2. +5 −2 lib/hand_parser.rb
  3. +4 −0 lib/wrong_number_of_cards_error.rb
  4. +34 −22 spec/hand_parser_spec.rb
View
@@ -21,4 +21,7 @@ the ```pip_count``` and ```suit_count``` methods are essentially duplicates of e
In this case ```pip_count``` and ```suit_count``` don't describe the intention of those methods
4) Address the ```#TODO: live in a helper, utility``` comment
The consecutive_cards? methods is mainly checking whether an array is made of consecutive integers. The logic for this is now moved to the ArrayHelper.
The consecutive_cards? methods is mainly checking whether an array is made of consecutive integers. The logic for this is now moved to the ArrayHelper.
5) Address the ```#TODO: fail nicely when not 5 cards``` comment
The HandParser now gets initialized with an array of card strings and fails if the array is the wrong size.
View
@@ -2,8 +2,11 @@
require 'card'
class HandParser
#TODO: fail nicely when not 5 cards
def parse(array_of_string_cards)
if array_of_string_cards.size != 5
raise WrongNumberOfCardsError.new(array_of_string_cards.size)
end
cards = array_of_string_cards.map do |card_string|
make_card(card_string)
end
@@ -12,7 +15,7 @@ def parse(array_of_string_cards)
private
SUITS = { "h" => :heart, "d" => :diamonds,
SUITS = { "h" => :hearts, "d" => :diamonds,
"s" => :spades, "c" => :clubs}
PIPS_FOR_HEADS = { "j" => 11, "q" => 12, "k" => 13, "a" => 14}
@@ -0,0 +1,4 @@
class WrongNumberOfCardsError < Exception
def initialize(size)
end
end
View
@@ -1,33 +1,45 @@
require "spec_helper"
require "hand_parser"
require "wrong_number_of_cards_error"
describe HandParser do
describe "#parse"
it "returns a hand of cards" do
array_of_cards = ["5h", "6d", "ks", "qc", "1h"]
hand = described_class.new.parse(array_of_cards)
expect(hand.cards.count).to eq 5
end
it "returns the right cards" do
array_of_cards = ["6d"]
hand = described_class.new.parse(array_of_cards)
expect(hand.cards.first.suit).to eq :diamonds
expect(hand.cards.first.pips).to eq 6
end
let(:array_of_five_cards) { ["5h", "10d", "ks", "qc", "1h"] }
let(:array_of_four_cards) { ["5h", "10d", "ks", "qc"] }
describe "#initialize" do
it "succeeds with an array of five elements" do
expect{described_class.new.parse(array_of_five_cards)}.not_to raise_error
end
it "returns the right cards" do
array_of_cards = ["10d"]
hand = described_class.new.parse(array_of_cards)
expect(hand.cards.first.suit).to eq :diamonds
expect(hand.cards.first.pips).to eq 10
it "fails with an array that does not have five elements" do
expect{described_class.new.parse(array_of_four_cards)}.to raise_error(WrongNumberOfCardsError)
end
end
it "returns the right cards" do
array_of_cards = ["qd"]
hand = described_class.new.parse(array_of_cards)
expect(hand.cards.first.suit).to eq :diamonds
expect(hand.cards.first.pips).to eq 12
describe "#parse" do
it "returns a hand of 5 cards" do
hand = described_class.new.parse(array_of_five_cards)
expect(hand.cards.count).to eq 5
end
it "returns the right cards" do
hand = described_class.new.parse(array_of_five_cards)
expect(hand.cards.first.suit).to eq :hearts
expect(hand.cards.first.pips).to eq 5
end
it "returns the right cards" do
hand = described_class.new.parse(array_of_five_cards)
expect(hand.cards[1].suit).to eq :diamonds
expect(hand.cards[1].pips).to eq 10
end
it "returns the right cards" do
hand = described_class.new.parse(array_of_five_cards)
expect(hand.cards[2].suit).to eq :spades
expect(hand.cards[2].pips).to eq 13
end
end
end

0 comments on commit c135042

Please sign in to comment.