Skip to content

Commit

Permalink
Merge pull request #3081 from eisukeyeongjo/argument_error_when_doc_s…
Browse files Browse the repository at this point in the history
…tring_is_not_nil_or_stirng

Raise ArgumentError when doc_string is neither string nor nil
  • Loading branch information
JonRowe authored Sep 5, 2024
2 parents 5cbc238 + 16cbf09 commit d8c5e3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def self.define_example_method(name, extra_options={})
desc, *args = *all_args

unless NilClass === desc || String === desc
RSpec.warning "`#{desc.inspect}` is used as example doc string. Use a string instead"
raise ArgumentError, "Examples must be described with a string, got: `#{desc.inspect}`"
end

options = Metadata.build_hash_from(args)
Expand Down
24 changes: 10 additions & 14 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1267,35 +1267,31 @@ def extract_execution_results(group)
let(:group) { RSpec.describe }

it "accepts a string for an example doc string" do
expect { group.it('MyClass') { } }.not_to output.to_stderr
expect { group.it('MyClass') { } }.not_to raise_error
end

it "accepts example without a doc string" do
expect { group.it { } }.not_to output.to_stderr
expect { group.it { } }.not_to raise_error
end

it "emits a warning when a Class is used as an example doc string" do
it "raises ArgumentError 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
.to raise_error(ArgumentError, /`Numeric` is not acceptable for doc_string.\nit must be a string./)
end

it "emits a warning when a Module is used as an example doc string" do
it "raises ArgumentError 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
.to raise_error(ArgumentError, /`RSpec` is not acceptable for doc_string.\nit must be a string./)
end

it "emits a warning when a Symbol is used as an example doc string" do
it "raises ArgumentError 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
.to raise_error(ArgumentError, /`:foo` is not acceptable for doc_string.\nit must be a string./)
end

it "emits a warning when a Hash is used as an example doc string" do
it "raises ArgumentError 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
.to raise_error(ArgumentError, /`{:foo=>:bar}` is not acceptable for doc_string.\nit must be a string./)
end
end

Expand Down

0 comments on commit d8c5e3e

Please sign in to comment.