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

Fix Ruby 2.7 keyword args deprecation in DSL #1176

Merged
merged 2 commits into from Apr 8, 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
10 changes: 8 additions & 2 deletions lib/rspec/matchers/dsl.rb
@@ -1,3 +1,5 @@
RSpec::Support.require_rspec_support "with_keywords_when_needed"

module RSpec
module Matchers
# Defines the custom matcher DSL.
Expand Down Expand Up @@ -460,11 +462,12 @@ def initialize(name, declarations, matcher_execution_context, *expected, &block_
@chained_method_clauses = []
@block_arg = block_arg

class << self
klass = class << self
# See `Macros#define_user_override` above, for an explanation.
include(@user_method_defs = Module.new)
self
end.class_exec(*expected, &declarations)
end
RSpec::Support::WithKeywordsWhenNeeded.class_exec(klass, *expected, &declarations)
end

# Provides the expected value. This will return an array if
Expand Down Expand Up @@ -528,6 +531,9 @@ def method_missing(method, *args, &block)
super(method, *args, &block)
end
end
# The method_missing method should be refactored to pass kw args in RSpec 4
# then this can be removed
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions spec/rspec/matchers/dsl_spec.rb
Expand Up @@ -29,6 +29,46 @@ def ok
expect { matcher_b.i_dont_exist }.to raise_error(NameError)
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
it 'supports the use of required keyword arguments in definition block' do
RSpec::Matchers.define(:match_required_kw) do |bar:|
match { expect(actual).to eq bar }
end
expect(1).to match_required_kw(bar: 1)
end

def kw(a:)
a
end

it "supports the use of required keyword arguments on methods" do
RSpec::Matchers.define(:matcher_required_kw_on_method) {}
expect(matcher_required_kw_on_method.kw(a: 1)).to eq(1)
end
CODE
end

if RSpec::Support::RubyFeatures.kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
it 'supports the use of optional keyword arguments in definition block' do
RSpec::Matchers.define(:match_optional_kw) do |bar: nil|
match { expect(actual).to eq bar }
end
expect(1).to match_optional_kw(bar: 1)
end

def optional_kw(a: nil)
a
end

it "supports the use of optional keyword arguments on methods" do
RSpec::Matchers.define(:matcher_optional_kw_on_method) {}
expect(matcher_optional_kw_on_method.optional_kw(a: 1)).to eq(1)
benoittgt marked this conversation as resolved.
Show resolved Hide resolved
end
CODE
end

it "clears user instance variables between invocations" do
RSpec::Matchers.define(:be_just_like) do |expected|
match do |actual|
Expand Down