Created
February 10, 2014 11:05
-
-
Save tomstuart/8914032 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@h-lame had a clever answer: just wrap
let
andbefore
, so that RSpec deals with sharing and inheritance.