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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 53 additions & 14 deletions spec/rspec/core/shared_example_group_spec.rb
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