Skip to content

Instantly share code, notes, and snippets.

@h-lame
Forked from tomstuart/scrabble_spec.rb
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h-lame/8916169 to your computer and use it in GitHub Desktop.
Save h-lame/8916169 to your computer and use it in GitHub Desktop.
module Scoring
def self.included(base)
base.class_eval do
let(:__score_keeper) { ScoreKeeper.new }
extend ScoreHelpers
include ScoreMatchers
end
end
class ScoreKeeper
def initialize()
@scores = Hash.new(0)
end
def assign_score(letter, value)
@scores[letter.upcase] = value
end
def score_for(letter)
@scores[letter.upcase]
end
end
module ScoreHelpers
def the_letter(letter, options)
score = options[:is_worth]
before { __score_keeper.assign_score(letter, score) }
end
end
module ScoreMatchers
extend RSpec::Matchers::DSL
matcher :score do |expected_score|
def score_for(word)
word.chars.map { |c| __score_keeper.score_for(c) }.inject(0, :+)
end
match do |word|
score_for(word) == expected_score
end
end
end
end
describe 'Scrabble' do
include Scoring
context 'with the letters D, O and G' do
the_letter 'D', is_worth: 2
the_letter 'O', is_worth: 1
the_letter 'G', is_worth: 2
specify { expect('DOG').to score 5 }
context 'with the letter E' do
the_letter 'E', is_worth: 1
specify { expect('DOGE').to score 6 }
end
end
context 'with the letters C, A and T' do
the_letter 'C', is_worth: 3
the_letter 'A', is_worth: 1
the_letter 'T', is_worth: 1
specify { expect('CAT').to score 5 }
context 'with C on a triple letter score' do
the_letter 'C', is_worth: 9
specify { expect('CAT').to score 11 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment