Skip to content

Commit

Permalink
Merge pull request #1725 from rubocop/fix-incorrect-autocorrect-https…
Browse files Browse the repository at this point in the history
…tatus

Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`
  • Loading branch information
bquorning authored Sep 25, 2023
2 parents 35d0a2f + 5394ada commit 7b930a0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah])

## 2.24.1 (2023-09-23)

- Fix an error when using `RSpec/FilePath` and revert to enabled by default. If you have already moved to `RSpec/SpecFilePathSuffix` and `RSpec/SpecFilePathFormat`, disable `RSpec/FilePath` explicitly as `Enabled: false`. The `RSpec/FilePath` before migration and the `RSpec/SpecFilePathSuffix` and `RSpec/SpecFilePathFormat` as the target are available respectively. ([@ydah])
Expand Down
30 changes: 12 additions & 18 deletions lib/rubocop/cop/rspec/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class HttpStatus < Base

def on_send(node)
http_status(node) do |arg|
return if arg.str_type? && arg.heredoc?

checker = checker_class.new(arg)
return unless checker.offensive?

Expand Down Expand Up @@ -105,6 +107,10 @@ def message
format(MSG, prefer: prefer, current: current)
end

def current
offense_range.source
end

def offense_range
node
end
Expand All @@ -129,18 +135,14 @@ def prefer
symbol.inspect
end

def current
node.value.inspect
end

private

def symbol
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
end

def number
node.source.delete('"').to_i
node.value.to_i
end
end

Expand All @@ -154,10 +156,6 @@ def prefer
number.to_s
end

def current
symbol.inspect
end

private

def symbol
Expand Down Expand Up @@ -190,26 +188,22 @@ def prefer
end
end

def current
offense_range.source
end

private

def symbol
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
end

def number
node.source.to_i
node.value.to_i
end

def normalize_str
normalized = node.source.delete('"')
if normalized.match?(/\A\d+\z/)
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(normalized.to_i)
str = node.value.to_s
if str.match?(/\A\d+\z/)
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(str.to_i)
else
normalized
str
end
end
end
Expand Down
83 changes: 80 additions & 3 deletions spec/rubocop/cop/rspec/rails/http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
RUBY
end

it 'registers an offense when using string value' do
it 'registers an offense when using double quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status "200" }
^^^^^ Prefer `:ok` over `"200"` to describe HTTP status code.
Expand All @@ -26,6 +26,36 @@
RUBY
end

it 'registers an offense when using single quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status '200' }
^^^^^ Prefer `:ok` over `'200'` to describe HTTP status code.
RUBY

expect_correction(<<-RUBY)
it { is_expected.to have_http_status :ok }
RUBY
end

it 'registers an offense when using percent string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status %[200] }
^^^^^^ Prefer `:ok` over `%[200]` to describe HTTP status code.
RUBY

expect_correction(<<-RUBY)
it { is_expected.to have_http_status :ok }
RUBY
end

it 'does not register an offense when using here document' do
expect_no_offenses(<<-RUBY)
it { is_expected.to have_http_status <<~HTTP }
200
HTTP
RUBY
end

it 'does not register an offense when using symbolic value' do
expect_no_offenses(<<-RUBY)
it { is_expected.to have_http_status :ok }
Expand Down Expand Up @@ -66,7 +96,7 @@
RUBY
end

it 'registers an offense when using string value' do
it 'registers an offense when using double quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status "ok" }
^^^^ Prefer `200` over `"ok"` to describe HTTP status code.
Expand All @@ -77,6 +107,17 @@
RUBY
end

it 'registers an offense when using single quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status 'ok' }
^^^^ Prefer `200` over `'ok'` to describe HTTP status code.
RUBY

expect_correction(<<-RUBY)
it { is_expected.to have_http_status 200 }
RUBY
end

it 'does not register an offense when using numeric value' do
expect_no_offenses(<<-RUBY)
it { is_expected.to have_http_status 200 }
Expand Down Expand Up @@ -131,7 +172,7 @@
RUBY
end

it 'registers an offense when using string value' do
it 'registers an offense when using double quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status "200" }
^^^^^^^^^^^^^^^^^^^^^^ Prefer `be_ok` over `have_http_status "200"` to describe HTTP status code.
Expand All @@ -145,6 +186,42 @@
RUBY
end

it 'registers an offense when using single quoted string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status '200' }
^^^^^^^^^^^^^^^^^^^^^^ Prefer `be_ok` over `have_http_status '200'` to describe HTTP status code.
it { is_expected.to have_http_status 'ok' }
^^^^^^^^^^^^^^^^^^^^^ Prefer `be_ok` over `have_http_status 'ok'` to describe HTTP status code.
RUBY

expect_correction(<<-RUBY)
it { is_expected.to be_ok }
it { is_expected.to be_ok }
RUBY
end

it 'registers an offense when using percent string value' do
expect_offense(<<-RUBY)
it { is_expected.to have_http_status %[200] }
^^^^^^^^^^^^^^^^^^^^^^^ Prefer `be_ok` over `have_http_status %[200]` to describe HTTP status code.
it { is_expected.to have_http_status %[ok] }
^^^^^^^^^^^^^^^^^^^^^^ Prefer `be_ok` over `have_http_status %[ok]` to describe HTTP status code.
RUBY

expect_correction(<<-RUBY)
it { is_expected.to be_ok }
it { is_expected.to be_ok }
RUBY
end

it 'does not register an offense when using here document' do
expect_no_offenses(<<-RUBY)
it { is_expected.to have_http_status <<~HTTP }
200
HTTP
RUBY
end

it 'does not register an offense when using numeric value' do
expect_no_offenses(<<-RUBY)
it { is_expected.to be_ok }
Expand Down

0 comments on commit 7b930a0

Please sign in to comment.