diff --git a/lib/rspec/core/example_group.rb b/lib/rspec/core/example_group.rb index 9f106191c4..cac79b3f41 100644 --- a/lib/rspec/core/example_group.rb +++ b/lib/rspec/core/example_group.rb @@ -144,17 +144,14 @@ def described_class # def self.define_example_method(name, extra_options={}) idempotently_define_singleton_method(name) do |*all_args, &block| - desc, *args = *all_args + description = all_args.shift if all_args.is_a? String + metadata = all_args - unless NilClass === desc || String === desc - RSpec.warning "`#{desc.inspect}` is used as example doc string. Use a string instead" - end - - options = Metadata.build_hash_from(args) + options = Metadata.build_hash_from(metadata) options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block options.update(extra_options) - RSpec::Core::Example.new(self, desc, options, block) + RSpec::Core::Example.new(self, description, options, block) end end diff --git a/spec/rspec/core/example_group_spec.rb b/spec/rspec/core/example_group_spec.rb index cb6a992932..4fc1ba8fb5 100644 --- a/spec/rspec/core/example_group_spec.rb +++ b/spec/rspec/core/example_group_spec.rb @@ -1263,39 +1263,43 @@ def extract_execution_results(group) end end - describe "example doc string" do - let(:group) { RSpec.describe } - - it "accepts a string for an example doc string" do - expect { group.it('MyClass') { } }.not_to output.to_stderr - end + describe "example doc string and metadata" do + context "when first argument of example is a string" do + it "returns the string as description" do + example = RSpec.describe.it("my example") + expect(example.metadata[:description]).to eq("my example") + end - it "accepts example without a doc string" do - expect { group.it { } }.not_to output.to_stderr - end + it "returns the string as description and symbol is set in metadata" do + example = RSpec.describe.it("my example", :foo) + expect(example.metadata[:description]).to eq("my example") + expect(example.metadata[:foo]).to be_truthy + end - it "emits a warning when a Class is used as an example doc string" do - expect { group.it(Numeric) { } } - .to output(/`Numeric` is used as example doc string. Use a string instead/) - .to_stderr + it "returns the string as description and hash is set in metadata" do + example = RSpec.describe.it("my example", { "foo" => "bar" }) + expect(example.metadata[:description]).to eq("my example") + expect(example.metadata["foo"]).to eq("bar") + end end - it "emits a warning when a Module is used as an example doc string" do - expect { group.it(RSpec) { } } - .to output(/`RSpec` is used as example doc string. Use a string instead/) - .to_stderr - end + context "when first argument of example is NOT a string" do + it "returns empty string as description" do + example = RSpec.describe.it + expect(example.metadata[:description]).to be_empty + end - it "emits a warning when a Symbol is used as an example doc string" do - expect { group.it(:foo) { } } - .to output(/`:foo` is used as example doc string. Use a string instead/) - .to_stderr - end + it "returns empty string as description and symbol is set in metadata" do + example = RSpec.describe.it(:foo) + expect(example.metadata[:description]).to be_empty + expect(example.metadata[:foo]).to be_truthy + end - it "emits a warning when a Hash is used as an example doc string" do - expect { group.it(foo: :bar) { } } - .to output(/`{:foo=>:bar}` is used as example doc string. Use a string instead/) - .to_stderr + it "returns empty string as description and hash is set in metadata" do + example = RSpec.describe.it({ "foo" => "bar" }) + expect(example.metadata[:description]).to be_empty + expect(example.metadata["foo"]).to eq("bar") + end end end