Skip to content

Instantly share code, notes, and snippets.

@tomstuart
Created February 10, 2014 11:05
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 tomstuart/8914032 to your computer and use it in GitHub Desktop.
Save tomstuart/8914032 to your computer and use it in GitHub Desktop.
module ScoreHelpers
def the_letter(letter, options)
score = options[:is_worth]
# TODO Remember the letter's score for the current example group.
end
end
module ScoreMatchers
extend RSpec::Matchers::DSL
matcher :score do |expected_score|
def scores
Hash.new(1) # TODO Return a hash of each letter's score for the current example group.
end
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
@tomstuart
Copy link
Author

@h-lame had a clever answer: just wrap let and before, so that RSpec deals with sharing and inheritance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment