Skip to content

Commit

Permalink
RSpec/MetadataStyle: Support multiple string arguments before metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Oct 27, 2023
1 parent a39b57b commit 950a5b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah])
- Change to be inline disable for `RSpec/SpecFilePathFormat` like `RSpec/FilePath`. ([@ydah])
- Fix a false positive for `RSpec/MetadataStyle` with example groups having multiple string arguments. ([@franzliedke])

## 2.24.1 (2023-09-23)

Expand Down Expand Up @@ -842,6 +843,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@faucct]: https://github.com/faucct
[@foton]: https://github.com/foton
[@francois-ferrandis]: https://github.com/francois-ferrandis
[@franzliedke]: https://github.com/franzliedke
[@g-rath]: https://github.com/G-Rath
[@geniou]: https://github.com/geniou
[@gsamokovarov]: https://github.com/gsamokovarov
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/rspec/metadata_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class MetadataStyle < Base # rubocop:disable Metrics/ClassLength
PATTERN

def on_metadata(symbols, hash)
# RSpec example groups accept two string arguments. In such a case,
# the rspec_metadata matcher will interpret the second string
# argument as a metadata symbol.
symbols.shift if symbols.first&.str_type?

symbols.each do |symbol|
on_metadata_symbol(symbol)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/rspec/metadata_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@
end
end

context 'with boolean hash metadata after 2 string arguments' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
describe 'Something', 'Something else', { a: true } do
end
RUBY
end
end

context 'with 1 symbol metadata' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down Expand Up @@ -234,6 +243,21 @@
end
end

context 'with symbol metadata after 2 string arguments' do
it 'registers offense' do
expect_offense(<<~RUBY)
describe 'Something', 'Something else', :a do
^^ Use hash style for metadata.
end
RUBY

expect_correction(<<~RUBY)
describe 'Something', 'Something else', a: true do
end
RUBY
end
end

context 'with symbol metadata with parentheses' do
it 'registers offense' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 950a5b4

Please sign in to comment.