-
-
Save tomstuart/8916355 to your computer and use it in GitHub Desktop.
@h-lame’s clever answer to https://gist.github.com/tomstuart/8914032
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 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