Skip to content

Commit

Permalink
Add object inspection for example doc string
Browse files Browse the repository at this point in the history
  • Loading branch information
eisukeyeongjo committed Mar 24, 2024
1 parent 5cbc238 commit c64ba4b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
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.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

0 comments on commit c64ba4b

Please sign in to comment.