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

Commit 352252d

Browse files
committed
Modified to check first argument of example for doc string
1 parent 5cbc238 commit 352252d

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

lib/rspec/core/example_group.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,14 @@ def described_class
144144
#
145145
def self.define_example_method(name, extra_options={})
146146
idempotently_define_singleton_method(name) do |*all_args, &block|
147-
desc, *args = *all_args
147+
description = all_args.shift if all_args.first.is_a? String
148+
metadata = all_args
148149

149-
unless NilClass === desc || String === desc
150-
RSpec.warning "`#{desc.inspect}` is used as example doc string. Use a string instead"
151-
end
152-
153-
options = Metadata.build_hash_from(args)
150+
options = Metadata.build_hash_from(metadata)
154151
options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
155152
options.update(extra_options)
156153

157-
RSpec::Core::Example.new(self, desc, options, block)
154+
RSpec::Core::Example.new(self, description, options, block)
158155
end
159156
end
160157

spec/rspec/core/example_group_spec.rb

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,39 +1263,43 @@ def extract_execution_results(group)
12631263
end
12641264
end
12651265

1266-
describe "example doc string" do
1267-
let(:group) { RSpec.describe }
1268-
1269-
it "accepts a string for an example doc string" do
1270-
expect { group.it('MyClass') { } }.not_to output.to_stderr
1271-
end
1266+
describe "example doc string and metadata" do
1267+
context "when first argument of example is a string" do
1268+
it "returns the string as description" do
1269+
example = RSpec.describe.it("my example")
1270+
expect(example.metadata[:description]).to eq("my example")
1271+
end
12721272

1273-
it "accepts example without a doc string" do
1274-
expect { group.it { } }.not_to output.to_stderr
1275-
end
1273+
it "returns the string as description and symbol is set in metadata" do
1274+
example = RSpec.describe.it("my example", :foo)
1275+
expect(example.metadata[:description]).to eq("my example")
1276+
expect(example.metadata[:foo]).to be_truthy
1277+
end
12761278

1277-
it "emits a warning when a Class is used as an example doc string" do
1278-
expect { group.it(Numeric) { } }
1279-
.to output(/`Numeric` is used as example doc string. Use a string instead/)
1280-
.to_stderr
1279+
it "returns the string as description and hash is set in metadata" do
1280+
example = RSpec.describe.it("my example", { "foo" => "bar" })
1281+
expect(example.metadata[:description]).to eq("my example")
1282+
expect(example.metadata["foo"]).to eq("bar")
1283+
end
12811284
end
12821285

1283-
it "emits a warning when a Module is used as an example doc string" do
1284-
expect { group.it(RSpec) { } }
1285-
.to output(/`RSpec` is used as example doc string. Use a string instead/)
1286-
.to_stderr
1287-
end
1286+
context "when first argument of example is NOT a string" do
1287+
it "returns empty string as description" do
1288+
example = RSpec.describe.it
1289+
expect(example.metadata[:description]).to be_empty
1290+
end
12881291

1289-
it "emits a warning when a Symbol is used as an example doc string" do
1290-
expect { group.it(:foo) { } }
1291-
.to output(/`:foo` is used as example doc string. Use a string instead/)
1292-
.to_stderr
1293-
end
1292+
it "returns empty string as description and symbol is set in metadata" do
1293+
example = RSpec.describe.it(:foo)
1294+
expect(example.metadata[:description]).to be_empty
1295+
expect(example.metadata[:foo]).to be_truthy
1296+
end
12941297

1295-
it "emits a warning when a Hash is used as an example doc string" do
1296-
expect { group.it(foo: :bar) { } }
1297-
.to output(/`{:foo=>:bar}` is used as example doc string. Use a string instead/)
1298-
.to_stderr
1298+
it "returns empty string as description and hash is set in metadata" do
1299+
example = RSpec.describe.it({ "foo" => "bar" })
1300+
expect(example.metadata[:description]).to be_empty
1301+
expect(example.metadata["foo"]).to eq("bar")
1302+
end
12991303
end
13001304
end
13011305

0 commit comments

Comments
 (0)