Skip to content

Commit

Permalink
Merge pull request #1357 from masato-bkn/add-tests-for-rails-compact-…
Browse files Browse the repository at this point in the history
…blank

Add missing tests for `Rails/CompactBlank` when receiver is a hash
  • Loading branch information
koic authored Sep 7, 2024
2 parents 93f9a89 + 027db53 commit 16b7f21
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spec/rubocop/cop/rails/compact_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
RUBY
end

it 'registers and corrects an offense when using `reject { |k, v| v.blank? }`' do
expect_offense(<<~RUBY)
collection.reject { |k, v| v.blank? }
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
RUBY

expect_correction(<<~RUBY)
collection.compact_blank
RUBY
end

it 'registers and corrects an offense when using `delete_if { |e| e.blank? }`' do
expect_offense(<<~RUBY)
collection.delete_if { |e| e.blank? }
Expand All @@ -46,6 +57,17 @@
RUBY
end

it 'registers and corrects an offense when using `delete_if { |k, v| v.blank? }`' do
expect_offense(<<~RUBY)
collection.delete_if { |k, v| v.blank? }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank!` instead.
RUBY

expect_correction(<<~RUBY)
collection.compact_blank!
RUBY
end

it 'does not registers an offense when using `reject! { |e| e.blank? }`' do
expect_no_offenses(<<~RUBY)
collection.reject! { |e| e.blank? }
Expand Down Expand Up @@ -91,6 +113,17 @@
RUBY
end

it 'registers and corrects an offense when using `select { |k, v| v.present? }`' do
expect_offense(<<~RUBY)
collection.select { |k, v| v.present? }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank` instead.
RUBY

expect_correction(<<~RUBY)
collection.compact_blank
RUBY
end

it 'registers and corrects an offense when using `keep_if { |e| e.present? }`' do
expect_offense(<<~RUBY)
collection.keep_if { |e| e.present? }
Expand All @@ -113,6 +146,17 @@
RUBY
end

it 'registers and corrects an offense when using `keep_if { |k, v| v.present? }`' do
expect_offense(<<~RUBY)
collection.keep_if { |k, v| v.present? }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `compact_blank!` instead.
RUBY

expect_correction(<<~RUBY)
collection.compact_blank!
RUBY
end

it 'does not register an offense when using `select! { |e| e.present? }`' do
expect_no_offenses(<<~RUBY)
collection.select! { |e| e.present? }
Expand Down

0 comments on commit 16b7f21

Please sign in to comment.