Permalink
Browse files

Implement card parsing error cases

  • Loading branch information...
camillebaldock committed Sep 7, 2014
1 parent 2e4dc54 commit 1da5e47e8cdca6e7cb0de702b4c36619d19fa7c3
Showing with 72 additions and 22 deletions.
  1. +39 −9 lib/card_parser.rb
  2. +13 −0 lib/errors.rb
  3. +2 −0 lib/hand.rb
  4. +2 −3 lib/hand_parser.rb
  5. +0 −4 lib/wrong_number_of_cards_error.rb
  6. +15 −5 spec/card_parser_spec.rb
  7. +0 −1 spec/hand_parser_spec.rb
  8. +1 −0 spec/hand_spec.rb
View
@@ -1,26 +1,56 @@
require "errors"
class CardParser
def parse(card_string)
end_of_card_number_index = (card_string.length)-2
pips_string = card_string[0..end_of_card_number_index]
suit_string = card_string[end_of_card_number_index+1]
{
:pips => pips_lookup(pips_string),
:suit => suit_lookup(suit_string)
}
end
private
SUITS = {
"h" => :hearts,
"d" => :diamonds,
"s" => :spades,
"c" => :clubs
}
PIPS_FOR_HEADS = {
PIPS = {
"1" => 14,
"2" => 2,
"3" => 3,
"4" => 4,
"5" => 5,
"6" => 6,
"7" => 7,
"8" => 8,
"9" => 9,
"10" => 10,
"j" => 11,
"q" => 12,
"k" => 13,
"a" => 14
}
def parse(card_string)
end_of_card_number_index = (card_string.length)-2
pips_string = card_string[0..end_of_card_number_index]
number_of_pips = PIPS_FOR_HEADS.fetch(pips_string, pips_string.to_i)
{
:pips => number_of_pips,
:suit => SUITS.fetch(card_string[end_of_card_number_index+1])
}
def pips_lookup(pips_string)
begin
PIPS.fetch(pips_string)
rescue KeyError
raise UnrecognisedPipsError
end
end
def suit_lookup(suit_string)
begin
SUITS.fetch(suit_string)
rescue KeyError
raise UnrecognisedSuitError
end
end
end
View
@@ -0,0 +1,13 @@
#TODO: good messages missing
class WrongNumberOfCardsError < Exception
end
class UnrecognisedSuitError < Exception
end
class UnrecognisedCardError < Exception
end
class UnrecognisedPipsError < Exception
end
View
@@ -1,3 +1,5 @@
require "card"
class Hand
include ArrayHelper
View
@@ -1,5 +1,4 @@
require 'hand'
require 'card'
require "errors"
class HandParser
@@ -9,7 +8,7 @@ def initialize(card_parser)
def parse(array_of_string_cards)
if array_of_string_cards.size != 5
raise WrongNumberOfCardsError.new(array_of_string_cards.size)
raise WrongNumberOfCardsError
end
array_of_string_cards.map do |card_string|
@card_parser.parse(card_string)
@@ -1,4 +0,0 @@
class WrongNumberOfCardsError < Exception
def initialize(size)
end
end
View
@@ -9,7 +9,7 @@
let(:queen_card_string) { "qc" }
let(:king_card_string) { "ks" }
let(:ace_card_string) { "ad" }
let(:ace_as_one_card_string) { "1h" }
let(:ace_as_one_card_string) { "1d" }
it "parses a card string with a single digit number" do
parsed_card = described_class.new.parse(single_digit_card_string)
@@ -47,12 +47,22 @@
expect(parsed_card.fetch(:pips)).to eq 14
end
it "parses an ace card string when it is written as a 1"
it "parses an ace card string when it is written as a 1" do
parsed_card = described_class.new.parse(ace_as_one_card_string)
expect(parsed_card.fetch(:suit)).to eq :diamonds
expect(parsed_card.fetch(:pips)).to eq 14
end
it "fails if the card string does not have a recognised suit"
it "fails if the card string does not have a recognised suit" do
expect{described_class.new.parse("5z")}.to raise_error(UnrecognisedSuitError)
end
it "fails if the card string is in the wrong format"
it "fails if the wrong capitalisation is used for the suit" do
expect{described_class.new.parse("5D")}.to raise_error(UnrecognisedSuitError)
end
it "fails if the wrong capitalisation is used"
it "fails if the wrong capitalisation is used for the pips" do
expect{described_class.new.parse("Qd")}.to raise_error(UnrecognisedPipsError)
end
end
View
@@ -1,6 +1,5 @@
require "spec_helper"
require "hand_parser"
require "wrong_number_of_cards_error"
describe HandParser do
View
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'hand'
require 'hand_parser'
require 'card_parser'
describe Hand do

0 comments on commit 1da5e47

Please sign in to comment.