Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new treat_symbols_as_metadata_keys_with_true_values option.
- Loading branch information
1 parent
ebc4bff
commit 2dd6fab
Showing
9 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module RSpec | ||
module Core | ||
module MetadataHashBuilder | ||
def build_metadata_hash_from(args) | ||
metadata = args.last.is_a?(Hash) ? args.pop : {} | ||
|
||
if RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values? | ||
add_symbols_to_hash(metadata, args) | ||
else | ||
warn_about_deprecated_symbol_usage(args) | ||
end | ||
|
||
metadata | ||
end | ||
|
||
private | ||
|
||
def add_symbols_to_hash(hash, args) | ||
while args.last.is_a?(Symbol) | ||
hash.merge!(args.pop => true) | ||
end | ||
end | ||
|
||
def warn_about_deprecated_symbol_usage(args) | ||
symbols = args.select { |a| a.is_a?(Symbol) } | ||
return if symbols.empty? | ||
|
||
Kernel.warn <<-NOTICE | ||
***************************************************************** | ||
DEPRECATION WARNING: you are using deprecated behaviour that will | ||
be removed from RSpec 3.0. | ||
You have passed symbols (#{symbols.inspect}) as additional | ||
arguments for a doc string. | ||
In RSpec 3.0, these symbols will be treated as metadata keys with | ||
a value of `true`. To get this behavior now (and prevent this | ||
warning), you can set a configuration option: | ||
RSpec.configure do |c| | ||
c.treat_symbols_as_metadata_keys_with_true_values = true | ||
end | ||
***************************************************************** | ||
NOTICE | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
shared_examples_for "metadata hash builder" do | ||
context "when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to true" do | ||
let(:hash) { metadata_hash(:foo, :bar, :bazz => 23) } | ||
|
||
before(:each) do | ||
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = true } | ||
end | ||
|
||
it 'treats symbols as metadata keys with a true value' do | ||
hash[:foo].should == true | ||
hash[:bar].should == true | ||
end | ||
|
||
it 'does not treat the first argument as a metadata key' do | ||
hash.should_not include(:group) | ||
end | ||
|
||
it 'still processes hash values normally' do | ||
hash[:bazz].should == 23 | ||
end | ||
end | ||
|
||
context "when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to false" do | ||
let(:warning_receiver) { Kernel } | ||
|
||
before(:each) do | ||
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = false } | ||
warning_receiver.stub(:warn) | ||
end | ||
|
||
it 'prints a deprecation warning about any symbols given as arguments' do | ||
warning_receiver.should_receive(:warn).with(/you have passed symbols \(\[:foo, :bar\]\) as additional\s+arguments for a doc string/i) | ||
metadata_hash(:foo, :bar, :key => 'value') | ||
end | ||
|
||
it 'does not treat symbols as metadata keys' do | ||
metadata_hash(:foo, :bar, :key => 'value').should_not include(:foo, :bar) | ||
end | ||
|
||
it 'does not print a warning if there are no symbol arguments' do | ||
warning_receiver.should_not_receive(:warn) | ||
metadata_hash(:foo => 23, :bar => 17) | ||
end | ||
end | ||
end |