-
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix RSpec/SortMetadata
cop to sort strings and variables first
#1948
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,54 @@ | |
RUBY | ||
end | ||
|
||
it 'does not register an offense for a symbol metadata before a non-symbol ' \ | ||
'argument' do | ||
expect_no_offenses(<<~RUBY) | ||
RSpec.describe 'Something', :z, :a, 'string' do | ||
end | ||
|
||
RSpec.describe 'Something', :z, :a, variable, foo: :bar do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense only for trailing symbol metadata not in ' \ | ||
'alphabetical order' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe 'Something', :z, :a, 'string', :c, :b do | ||
^^^^^^ Sort metadata alphabetically. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.describe 'Something', :z, :a, 'string', :b, :c do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when a symbol metadata not in alphabetical order ' \ | ||
'is before a variable argument being the last argument ' \ | ||
'as it could be a hash' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe 'Something', :z, :a, some_hash do | ||
^^^^^^ Sort metadata alphabetically. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
RSpec.describe 'Something', :a, :z, some_hash do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using a second level description ' \ | ||
'not in alphabetical order with symbol metadata' do | ||
expect_no_offenses(<<~RUBY) | ||
RSpec.describe 'Something', 'second docstring', :a, :b do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invalid syntax There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this one is correct. It's when there are more than 2 non-symbol arguments that the |
||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using only a hash of metadata ' \ | ||
'with keys in alphabetical order' do | ||
expect_no_offenses(<<~RUBY) | ||
|
@@ -100,27 +148,27 @@ | |
'and the hash values are complex objects' do | ||
expect_offense(<<~RUBY) | ||
it 'Something', variable, 'B', :a, key => {}, foo: ->(x) { bar(x) }, Identifier.sample => true, baz: Snafu.new do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort metadata alphabetically. | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Sort metadata alphabetically. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it 'Something', :a, 'B', variable, baz: Snafu.new, foo: ->(x) { bar(x) }, Identifier.sample => true, key => {} do | ||
it 'Something', variable, 'B', :a, baz: Snafu.new, foo: ->(x) { bar(x) }, Identifier.sample => true, key => {} do | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense only when example or group has a block' do | ||
expect_offense(<<~RUBY) | ||
shared_examples 'a difficult situation', 'B', :a do |x, y| | ||
^^^^^^^ Sort metadata alphabetically. | ||
shared_examples 'a difficult situation', 'B', :z, :a do |x, y| | ||
^^^^^^ Sort metadata alphabetically. | ||
end | ||
|
||
include_examples 'a difficult situation', 'value', 'another value' | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
shared_examples 'a difficult situation', :a, 'B' do |x, y| | ||
shared_examples 'a difficult situation', 'B', :a, :z do |x, y| | ||
end | ||
|
||
include_examples 'a difficult situation', 'value', 'another value' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it's a
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it would register an offense and would autocorrect it like this:
This test was inspired from this
MetadataStyle
cop test:rubocop-rspec/spec/rubocop/cop/rspec/metadata_style_spec.rb
Lines 354 to 364 in 74d7837
It ensures that a variable is not moved at the start of the arguments list if this variable is at the end of the arguments list, because it could hold a hash.
Should I update the spec to swap
:a
and:z
and expect the offense and correction as described above?