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

Commit c99129d

Browse files
committed
Merge pull request #2719 from rspec/further-improve-kw-args-specs
Tweak keyword argument specs for shared examples
1 parent ab52c5a commit c99129d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

spec/rspec/core/shared_example_group_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,59 @@ def find_implementation_block(registry, scope, name)
6969
expect(Kernel).to_not respond_to(shared_method_name)
7070
end
7171

72+
# These keyword specs cover all 4 of the keyword / keyword like syntax varients
73+
# they should be warning free.
74+
75+
if RSpec::Support::RubyFeatures.required_kw_args_supported?
76+
it 'supports required keyword arguments' do
77+
binding.eval(<<-CODE, __FILE__, __LINE__)
78+
group.__send__ shared_method_name, "shared context expects keywords" do |foo:|
79+
it "has an expected value" do
80+
expect(foo).to eq("bar")
81+
end
82+
end
83+
84+
group.__send__ shared_method_name, "shared context expects hash" do |a_hash|
85+
it "has an expected value" do
86+
expect(a_hash[:foo]).to eq("bar")
87+
end
88+
end
89+
90+
group.it_behaves_like "shared context expects keywords", foo: "bar"
91+
group.it_behaves_like "shared context expects keywords", { foo: "bar" }
92+
93+
group.it_behaves_like "shared context expects hash", foo: "bar"
94+
group.it_behaves_like "shared context expects hash", { foo: "bar" }
95+
CODE
96+
expect(group.run).to eq true
97+
end
98+
end
99+
100+
if RSpec::Support::RubyFeatures.kw_args_supported?
101+
it 'supports optional keyword arguments' do
102+
binding.eval(<<-CODE, __FILE__, __LINE__)
103+
group.__send__ shared_method_name, "shared context expects keywords" do |foo: nil|
104+
it "has an expected value" do
105+
expect(foo).to eq("bar")
106+
end
107+
end
108+
109+
group.__send__ shared_method_name, "shared context expects hash" do |a_hash|
110+
it "has an expected value" do
111+
expect(a_hash[:foo]).to eq("bar")
112+
end
113+
end
114+
115+
group.it_behaves_like "shared context expects keywords", foo: "bar"
116+
group.it_behaves_like "shared context expects keywords", { foo: "bar" }
117+
118+
group.it_behaves_like "shared context expects hash", foo: "bar"
119+
group.it_behaves_like "shared context expects hash", { foo: "bar" }
120+
CODE
121+
expect(group.run).to eq true
122+
end
123+
end
124+
72125
it "displays a warning when adding an example group without a block", :unless => RUBY_VERSION == '1.8.7' do
73126
expect_warning_with_call_site(__FILE__, __LINE__ + 1)
74127
group.send(shared_method_name, 'name but no block')

0 commit comments

Comments
 (0)