Skip to content

Commit

Permalink
Merge pull request #2719 from rspec/further-improve-kw-args-specs
Browse files Browse the repository at this point in the history
Tweak keyword argument specs for shared examples
  • Loading branch information
JonRowe committed Apr 10, 2020
2 parents 02233cd + 004d581 commit c91f74c
Showing 1 changed file with 53 additions and 14 deletions.
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

0 comments on commit c91f74c

Please sign in to comment.