Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Tweak keyword argument specs for shared examples #2719

Merged
merged 1 commit into from
Apr 10, 2020
Merged
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
67 changes: 53 additions & 14 deletions spec/rspec/core/shared_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,59 @@ def find_implementation_block(registry, scope, name)
expect(Kernel).to_not respond_to(shared_method_name)
end

# These keyword specs cover all 4 of the keyword / keyword like syntax varients
# they should be warning free.

if RSpec::Support::RubyFeatures.required_kw_args_supported?
it 'supports required keyword arguments' do
binding.eval(<<-CODE, __FILE__, __LINE__)
group.__send__ shared_method_name, "shared context expects keywords" do |foo:|
it "has an expected value" do
expect(foo).to eq("bar")
end
end

group.__send__ shared_method_name, "shared context expects hash" do |a_hash|
it "has an expected value" do
expect(a_hash[:foo]).to eq("bar")
end
end

group.it_behaves_like "shared context expects keywords", foo: "bar"
group.it_behaves_like "shared context expects keywords", { foo: "bar" }

group.it_behaves_like "shared context expects hash", foo: "bar"
group.it_behaves_like "shared context expects hash", { foo: "bar" }
CODE
expect(group.run).to eq true
end
end

if RSpec::Support::RubyFeatures.kw_args_supported?
it 'supports optional keyword arguments' do
binding.eval(<<-CODE, __FILE__, __LINE__)
group.__send__ shared_method_name, "shared context expects keywords" do |foo: nil|
it "has an expected value" do
expect(foo).to eq("bar")
end
end

group.__send__ shared_method_name, "shared context expects hash" do |a_hash|
it "has an expected value" do
expect(a_hash[:foo]).to eq("bar")
end
end

group.it_behaves_like "shared context expects keywords", foo: "bar"
group.it_behaves_like "shared context expects keywords", { foo: "bar" }

group.it_behaves_like "shared context expects hash", foo: "bar"
group.it_behaves_like "shared context expects hash", { foo: "bar" }
CODE
expect(group.run).to eq true
end
end

it "displays a warning when adding an example group without a block", :unless => RUBY_VERSION == '1.8.7' do
expect_warning_with_call_site(__FILE__, __LINE__ + 1)
group.send(shared_method_name, 'name but no block')
Expand Down Expand Up @@ -543,20 +596,6 @@ 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