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

Explicitly support keyword arguments in shared context on 2.7.0 #2716

Merged
merged 2 commits into from
Apr 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/rspec/core/shared_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,31 @@ def include_in(klass, inclusion_line, args, customization_block)
klass.update_inherited_metadata(@metadata) unless @metadata.empty?

SharedExampleGroupInclusionStackFrame.with_frame(@description, inclusion_line) do
klass.class_exec(*args, &@definition)
klass_exec(klass, *args, &@definition)
klass.class_exec(&customization_block) if customization_block
end
end

private

if RSpec::Support::RubyFeatures.kw_args_supported?
# Remove this in RSpec 4 in favour of explictly passed in kwargs down the entire
# stack, e.g. rspec/rspec-core#2711
binding.eval(<<-CODE, __FILE__, __LINE__)
def klass_exec(klass, *args, &definition)
if RSpec::Support::MethodSignature.new(definition).has_kw_args_in?(args)
kwargs = args.pop
klass.class_exec(*args, **kwargs, &definition)
else
klass.class_exec(*args, &definition)
end
end
CODE
else
def klass_exec(klass, *args, &definition)
klass.class_exec(*args, &definition)
end
end
end

# Shared example groups let you define common context and/or common
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/core/shared_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ def self.bar; 'bar'; end
expect(group).to have_example_descriptions("a different spec")
end
end

if RSpec::Support::RubyFeatures.required_kw_args_supported?
binding.eval(<<-CODE, __FILE__, __LINE__)
context "supporting kwargs" do
__send__ shared_method_name, "shared context" do |foo:|
it "has an expected value" do
expect(foo).to eq("bar")
end
end

it_behaves_like "shared context", foo: "bar"
end
CODE
end
end
end
end
Expand Down