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

Modified to allow only string as example doc string #3074

Closed
Show file tree
Hide file tree
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
11 changes: 4 additions & 7 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.first.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

Expand Down
58 changes: 31 additions & 27 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down