Skip to content

Instantly share code, notes, and snippets.

@tomstuart
Forked from h-lame/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 0 You must be signed in to fork a gist
  • Save tomstuart/8916355 to your computer and use it in GitHub Desktop.
Save tomstuart/8916355 to your computer and use it in GitHub Desktop.
module ScoreHelpers
def self.extended(base)
base.class_eval do
let(:scores) { {} }
end
end
def the_letter(letter, options)
score = options[:is_worth]
before(:each) do
scores[letter] = score
end
end
end
module ScoreMatchers
extend RSpec::Matchers::DSL
matcher :score do |expected_score|
def score_for(word)
scores.values_at(*word.chars).inject(0, :+)
end
match do |word|
score_for(word) == expected_score
end
end
end
describe 'Scrabble' do
extend ScoreHelpers
include ScoreMatchers
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