Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ruby2_keywords for passing arguments to dynamic matchers. #1222

Merged
merged 2 commits into from Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 9 additions & 23 deletions lib/rspec/matchers.rb
Expand Up @@ -955,31 +955,17 @@ def self.configuration
HAS_REGEX = /^(?:have_)(.*)/
DYNAMIC_MATCHER_REGEX = Regexp.union(BE_PREDICATE_REGEX, HAS_REGEX)

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
def method_missing(method, *args, **kwargs, &block)
case method.to_s
when BE_PREDICATE_REGEX
BuiltIn::BePredicate.new(method, *args, **kwargs, &block)
when HAS_REGEX
BuiltIn::Has.new(method, *args, **kwargs, &block)
else
super
end
end
CODE
else
def method_missing(method, *args, &block)
case method.to_s
when BE_PREDICATE_REGEX
BuiltIn::BePredicate.new(method, *args, &block)
when HAS_REGEX
BuiltIn::Has.new(method, *args, &block)
else
super
end
def method_missing(method, *args, &block)
case method.to_s
when BE_PREDICATE_REGEX
BuiltIn::BePredicate.new(method, *args, &block)
when HAS_REGEX
BuiltIn::Has.new(method, *args, &block)
else
super
end
end
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)

if RUBY_VERSION.to_f >= 1.9
def respond_to_missing?(method, *)
Expand Down
29 changes: 5 additions & 24 deletions lib/rspec/matchers/built_in/has.rb
Expand Up @@ -7,17 +7,10 @@ module BuiltIn
class DynamicPredicate < BaseMatcher
include BeHelpers

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
def initialize(method_name, *args, **kwargs, &block)
@method_name, @args, @kwargs, @block = method_name, args, kwargs, block
end
CODE
else
def initialize(method_name, *args, &block)
@method_name, @args, @block = method_name, args, block
end
def initialize(method_name, *args, &block)
@method_name, @args, @block = method_name, args, block
end
ruby2_keywords :initialize if respond_to?(:ruby2_keywords, true)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key bit I was missing before was accidentally not including private methods when checking for ruby2_keywords I think, there is some thinking to do around how we handle this in Ruby 3 I suspect but for now this passes both the existing tests and the new one this PR adds.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is a tricky bit (not forgetting the , true), it also bit me once.
Ruby 3 still has ruby2_keywords, so it will work fine.
On Ruby 3, *args, **kwargs-delegation like above works correctly, but it's probably not worth maintaining 2 versions of the method while ruby2_keywords exists (the current plan seems to remove it after Ruby 2 EOL).


# @private
def matches?(actual, &block)
Expand Down Expand Up @@ -70,20 +63,8 @@ def private_predicate?
end
end

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
def predicate_result
if @kwargs.empty?
@predicate_result = actual.__send__(predicate_method_name, *@args, &@block)
else
@predicate_result = actual.__send__(predicate_method_name, *@args, **@kwargs, &@block)
end
end
CODE
else
def predicate_result
@predicate_result = actual.__send__(predicate_method_name, *@args, &@block)
end
def predicate_result
@predicate_result = actual.__send__(predicate_method_name, *@args, &@block)
end

def predicate_method_name
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/matchers/built_in/be_spec.rb
Expand Up @@ -110,6 +110,24 @@ def object.predicate?(return_val); return_val; end
expect { expect(object).not_to be_predicate(true) }.to fail
end

it 'handles arguments to the predicate implementing to_hash' do
object = Object.new
def object.predicate?(value); value.to_return; end

hash_a_like =
Class.new do
def to_hash
{:to_return => true}
end

def to_return
true
end
end

expect(object).to be_predicate(hash_a_like.new)
end

it 'handles keyword arguments to the predicate', :if => RSpec::Support::RubyFeatures.required_kw_args_supported? do
object = Object.new
binding.eval(<<-CODE, __FILE__, __LINE__)
Expand Down