Permalink
Browse files

fix block arity bug in matcher.rb

- was only a problem in 1.9.1
  • Loading branch information...
1 parent 078e10d commit 5ac0a9fd3b49f6ab3429a8fde62da2827a1b8f96 @dchelimsky dchelimsky committed Jan 5, 2010
Showing with 26 additions and 29 deletions.
  1. +26 −13 lib/spec/matchers/matcher.rb
  2. +0 −16 spec/spec/matchers/matcher_spec.rb
@@ -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}"},
@@ -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
@@ -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
@@ -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

0 comments on commit 5ac0a9f

Please sign in to comment.