Skip to content

Commit

Permalink
fix block arity bug in matcher.rb
Browse files Browse the repository at this point in the history
- was only a problem in 1.9.1
  • Loading branch information
dchelimsky committed Jan 5, 2010
1 parent 078e10d commit 5ac0a9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
39 changes: 26 additions & 13 deletions lib/spec/matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(name, *expected, &declarations)
@expected = expected
@actual = nil
@diffable = false
@error_to_watch_for = nil
@expected_exception = nil
@messages = {
:description => lambda {"#{name_to_sentence}#{expected_to_sentence}"},
:failure_message_for_should => lambda {|actual| "expected #{actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"},
Expand All @@ -24,38 +24,41 @@ def initialize(name, *expected, &declarations)
end

def matches?(actual)
if @error_to_watch_for
@actual = actual
if @expected_exception
begin
instance_exec(@actual = actual, &@match_block)
instance_exec(actual, &@match_block)
true
rescue @error_to_watch_for
rescue @expected_exception
false
end
else
instance_exec(@actual = actual, &@match_block)
begin
instance_exec(actual, &@match_block)
rescue Spec::Expectations::ExpectationNotMetError
false
end
end
rescue Spec::Expectations::ExpectationNotMetError
false
end

def description(&block)
cache_or_call_cached(:description, &block)
end

def failure_message_for_should(&block)
cache_or_call_cached(:failure_message_for_should, actual, &block)
cache_or_call_cached(:failure_message_for_should, &block)
end

def failure_message_for_should_not(&block)
cache_or_call_cached(:failure_message_for_should_not, actual, &block)
cache_or_call_cached(:failure_message_for_should_not, &block)
end

def match(&block)
@match_block = block
end

def match_unless_raises(error=StandardError, &block)
@error_to_watch_for = error
def match_unless_raises(exception=Exception, &block)
@expected_exception = exception
match(&block)
end

Expand Down Expand Up @@ -86,8 +89,18 @@ def making_declared_methods_public # :nodoc:
(private_methods - orig_private_methods).each {|m| st.__send__ :public, m}
end

def cache_or_call_cached(key, actual=nil, &block)
block ? @messages[key] = block : @messages[key].call(actual)
def cache_or_call_cached(key, &block)
block ? cache(key, &block) : call_cached(key)
end

def cache(key, &block)
@messages[key] = block
end

def call_cached(key)
@messages[key].arity == 1 ?
@messages[key].call(@actual) :
@messages[key].call
end

def name_to_sentence
Expand Down
16 changes: 0 additions & 16 deletions spec/spec/matchers/matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,6 @@ def second_word
matcher.matches?(8).should be_true
end

it "lets you override the actual() in messages" do
matcher = Spec::Matchers::Matcher.new(:be_foo) do
match do |actual|
@submitted = actual
false
end

def actual
"replaced"
end
end

matcher.matches?("foo")
matcher.failure_message_for_should.should =~ /replaced/
end

describe "#match_unless_raises" do
context "with a passing assertion" do
let(:mod) do
Expand Down

0 comments on commit 5ac0a9f

Please sign in to comment.