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

Commit 922aabd

Browse files
committed
Add a warning when doc string is not a string
#2921 (comment)
1 parent 18968e0 commit 922aabd

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

lib/rspec/core/example_group.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def self.define_example_method(name, extra_options={})
146146
idempotently_define_singleton_method(name) do |*all_args, &block|
147147
desc, *args = *all_args
148148

149+
unless NilClass === desc || String === desc
150+
RSpec.warning "`#{desc.inspect}` is used as example doc string. Use a string instead"
151+
end
152+
149153
options = Metadata.build_hash_from(args)
150154
options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
151155
options.update(extra_options)
@@ -264,6 +268,11 @@ def self.define_example_group_method(name, metadata={})
264268

265269
begin
266270
description = args.shift
271+
272+
unless NilClass === description || String === description || Module === description
273+
RSpec.warning "`#{description.inspect}` is used as example group doc string. Use a string instead"
274+
end
275+
267276
combined_metadata = metadata.dup
268277
combined_metadata.merge!(args.pop) if args.last.is_a? Hash
269278
args << combined_metadata

spec/rspec/core/example_group_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,72 @@ def extract_execution_results(group)
12311231

12321232
end
12331233

1234+
describe "example group doc string" do
1235+
it "accepts a string for an example group doc string" do
1236+
expect { RSpec.describe 'MyClass' }.not_to output.to_stderr
1237+
end
1238+
1239+
it "accepts a class for an example group doc string" do
1240+
expect { RSpec.describe Numeric }.not_to output.to_stderr
1241+
end
1242+
1243+
it "accepts a module for an example group doc string" do
1244+
expect { RSpec.describe RSpec }.not_to output.to_stderr
1245+
end
1246+
1247+
it "accepts example group without a doc string" do
1248+
expect { RSpec.describe }.not_to output.to_stderr
1249+
end
1250+
1251+
it "emits a warning when a Symbol is used as an example group doc string" do
1252+
expect { RSpec.describe :foo }
1253+
.to output(/`:foo` is used as example group doc string. Use a string instead/)
1254+
.to_stderr
1255+
end
1256+
1257+
it "emits a warning when a Hash is used as an example group doc string" do
1258+
expect { RSpec.describe(foo: :bar) { } }
1259+
.to output(/`{:foo=>:bar}` is used as example group doc string. Use a string instead/)
1260+
.to_stderr
1261+
end
1262+
end
1263+
1264+
describe "example doc string" do
1265+
let(:group) { RSpec.describe }
1266+
1267+
it "accepts a string for an example doc string" do
1268+
expect { group.it('MyClass') { } }.not_to output.to_stderr
1269+
end
1270+
1271+
it "accepts example without a doc string" do
1272+
expect { group.it { } }.not_to output.to_stderr
1273+
end
1274+
1275+
it "emits a warning when a Class is used as an example doc string" do
1276+
expect { group.it(Numeric) { } }
1277+
.to output(/`Numeric` is used as example doc string. Use a string instead/)
1278+
.to_stderr
1279+
end
1280+
1281+
it "emits a warning when a Module is used as an example doc string" do
1282+
expect { group.it(RSpec) { } }
1283+
.to output(/`RSpec` is used as example doc string. Use a string instead/)
1284+
.to_stderr
1285+
end
1286+
1287+
it "emits a warning when a Symbol is used as an example doc string" do
1288+
expect { group.it(:foo) { } }
1289+
.to output(/`:foo` is used as example doc string. Use a string instead/)
1290+
.to_stderr
1291+
end
1292+
1293+
it "emits a warning when a Hash is used as an example doc string" do
1294+
expect { group.it(foo: :bar) { } }
1295+
.to output(/`{:foo=>:bar}` is used as example doc string. Use a string instead/)
1296+
.to_stderr
1297+
end
1298+
end
1299+
12341300
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
12351301

12361302
describe "A sample nested group", :nested_describe => "yep" do

0 commit comments

Comments
 (0)