Permalink
Browse files

copy instance_eval_with_args from rspec-core (a bit cleaner)

  • Loading branch information...
1 parent 90eeb1f commit 2102bcd9238ff1f26e2def4e66797a36d897329e @dchelimsky dchelimsky committed Sep 30, 2011
View
2 lib/rspec/matchers.rb
@@ -174,7 +174,7 @@ module Matchers
end
end
-require 'rspec/matchers/extensions/instance_exec'
+require 'rspec/matchers/extensions/instance_eval_with_args'
require 'rspec/matchers/pretty'
require 'rspec/matchers/matcher'
require 'rspec/matchers/operator_matcher'
View
22 ...spec/matchers/extensions/instance_exec.rb → ...ers/extensions/instance_eval_with_args.rb
@@ -1,15 +1,23 @@
module RSpec
module Matchers
- module InstanceExec
- unless respond_to?(:instance_exec)
+ module Extensions
+ module InstanceEvalWithArgs
# based on Bounded Spec InstanceExec (Mauricio Fernandez)
# http://eigenclass.org/hiki/bounded+space+instance_exec
- # - uses singleton_class of matcher instead of global
- # InstanceExecHelper module
- # - this keeps it scoped to this class only, which is the
- # only place we need it
+ # - uses singleton_class instead of global InstanceExecHelper module
+ # - this keeps it scoped to classes/modules that include this module
# - only necessary for ruby 1.8.6
- def instance_exec(*args, &block)
+ def instance_eval_with_args(*args, &block)
+ return instance_exec(*args, &block) if respond_to?(:instance_exec)
+
+ # If there are no args and the block doesn't expect any, there's no
+ # need to fake instance_exec with our hack below.
+ # Notes:
+ # * lambda { }.arity # => -1
+ # * lambda { || }.arity # => 0
+ # * lambda { |*a| }.arity # -1
+ return instance_eval(&block) if block.arity < 1 && args.empty?
+
singleton_class = (class << self; self; end)
begin
orig_critical, Thread.critical = Thread.critical, true
View
10 lib/rspec/matchers/matcher.rb
@@ -1,7 +1,7 @@
module RSpec
module Matchers
class Matcher
- include RSpec::Matchers::InstanceExec
+ include RSpec::Matchers::Extensions::InstanceEvalWithArgs
include RSpec::Matchers::Pretty
include RSpec::Matchers
@@ -20,7 +20,7 @@ def initialize(name, *expected, &declarations)
:failure_message_for_should_not => lambda {|actual| "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"}
}
making_declared_methods_public do
- instance_exec(*@expected, &declarations)
+ instance_eval_with_args(*@expected, &declarations)
end
end
@@ -29,14 +29,14 @@ def matches?(actual)
@actual = actual
if @expected_exception
begin
- instance_exec(actual, &@match_block)
+ instance_eval_with_args(actual, &@match_block)
true
rescue @expected_exception => @rescued_exception
false
end
else
begin
- instance_exec(actual, &@match_block)
+ instance_eval_with_args(actual, &@match_block)
rescue RSpec::Expectations::ExpectationNotMetError
false
end
@@ -47,7 +47,7 @@ def matches?(actual)
def does_not_match?(actual)
@actual = actual
@match_for_should_not_block ?
- instance_exec(actual, &@match_for_should_not_block) :
+ instance_eval_with_args(actual, &@match_for_should_not_block) :
!matches?(actual)
end

0 comments on commit 2102bcd

Please sign in to comment.