Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor Matcher

  • Loading branch information...
commit bac659bce5a5917d63a723822ed5cd34b7de37c8 1 parent 18632ff
@dchelimsky authored
View
17 features/matchers/create_matcher.feature
@@ -2,9 +2,9 @@ Feature: custom matcher shortcut
In order to express my domain clearly in my code examples
As an RSpec user
- I want to a shortcut for create custom matchers
+ I want a shortcut for create custom matchers
- Scenario: create a matcher with default messages
+ Scenario: creating a matcher with default messages
Given the following spec:
"""
Spec::Matchers.create :be_a_multiple_of do |expected|
@@ -44,7 +44,7 @@ Feature: custom matcher shortcut
And the stdout should match "expected 9 to be a multiple of 4"
And the stdout should match "expected 9 not to be a multiple of 3"
- Scenario: override the failure_message_for_should
+ Scenario: overriding the failure_message_for_should
Given the following spec:
"""
Spec::Matchers.create :be_a_multiple_of do |expected|
@@ -66,7 +66,7 @@ Feature: custom matcher shortcut
And the stdout should match "1 example, 1 failure"
And the stdout should match "expected that 9 would be a multiple of 4"
- Scenario: override the failure_message_for_should_not
+ Scenario: overriding the failure_message_for_should_not
Given the following spec:
"""
Spec::Matchers.create :be_a_multiple_of do |expected|
@@ -88,7 +88,7 @@ Feature: custom matcher shortcut
And the stdout should match "1 example, 1 failure"
And the stdout should match "expected that 9 would not be a multiple of 3"
- Scenario: override the description
+ Scenario: overriding the description
Given the following spec:
"""
Spec::Matchers.create :be_a_multiple_of do |expected|
@@ -99,11 +99,11 @@ Feature: custom matcher shortcut
"be multiple of #{expected}"
end
end
-
+
describe 9 do
it {should be_a_multiple_of(3)}
end
-
+
describe 9 do
it {should_not be_a_multiple_of(4)}
end
@@ -113,6 +113,3 @@ Feature: custom matcher shortcut
And the stdout should match "2 examples, 0 failures"
And the stdout should match "should be multiple of 3"
And the stdout should match "should not be multiple of 4"
-
-
-
View
42 lib/spec/matchers/matcher.rb
@@ -7,56 +7,42 @@ def initialize(name, expected=nil, &block_passed_to_init)
@name = name
@expected = expected
@block = block_passed_to_init
- # FIXME - the next line has a hard coded description (ish)
- @description = lambda { "#{name_to_sentence} #{expected}" }
- @failure_message_for_should = lambda do |actual|
- "expected #{actual} to #{name_to_sentence} #{expected}"
- end
- @failure_message_for_should_not = lambda do |actual|
- "expected #{actual} not to #{name_to_sentence} #{expected}"
- end
+ @messages = {
+ :description => lambda {"#{name_to_sentence} #{expected}"},
+ :failure_message_for_should => lambda {|actual| "expected #{actual} to #{name_to_sentence} #{expected}"},
+ :failure_message_for_should_not => lambda {|actual| "expected #{actual} not to #{name_to_sentence} #{expected}"}
+ }
end
def matches?(actual)
@actual = actual
instance_exec @expected, &@block
- instance_exec actual, &@match_block
+ instance_exec @actual, &@match_block
end
def description(&block)
- block ? set_description(block) : eval_description
+ cache_or_call_cached(:description, &block)
end
- def failure_message
- @failure_message_for_should.call(@actual)
+ def failure_message_for_should(&block)
+ cache_or_call_cached(:failure_message_for_should, @actual, &block)
end
- def negative_failure_message
- @failure_message_for_should_not.call(@actual)
+ def failure_message_for_should_not(&block)
+ cache_or_call_cached(:failure_message_for_should_not, @actual, &block)
end
def match(&block)
@match_block = block
end
- def failure_message_for_should(&block)
- @failure_message_for_should = block
- end
-
- def failure_message_for_should_not(&block)
- @failure_message_for_should_not = block
- end
-
private
- def set_description(block)
- @description = block
+ def cache_or_call_cached(key, actual=nil, &block)
+ block ? @messages[key] = block :
+ @messages[key].call(actual)
end
- def eval_description
- @description.call
- end
-
def name_to_sentence
@name_to_sentence ||= @name.to_s.gsub(/_/,' ')
end
View
8 spec/spec/matchers/matcher_spec.rb
@@ -19,12 +19,12 @@ module Matchers
it "provides a default failure message for #should" do
@matcher.matches?(8)
- @matcher.failure_message.should == "expected 8 to be a multiple of 3"
+ @matcher.failure_message_for_should.should == "expected 8 to be a multiple of 3"
end
it "provides a default failure message for #should_not" do
@matcher.matches?(9)
- @matcher.negative_failure_message.should == "expected 9 not to be a multiple of 3"
+ @matcher.failure_message_for_should_not.should == "expected 9 not to be a multiple of 3"
end
end
@@ -61,12 +61,12 @@ module Matchers
it "overrides the failure message for #should" do
@matcher.matches?(false)
- @matcher.failure_message.should == "expected false to be the boolean true"
+ @matcher.failure_message_for_should.should == "expected false to be the boolean true"
end
it "overrides the failure message for #should_not" do
@matcher.matches?(true)
- @matcher.negative_failure_message.should == "expected true not to be the boolean true"
+ @matcher.failure_message_for_should_not.should == "expected true not to be the boolean true"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.