Skip to content

Commit

Permalink
Add missing tests for Rails/CompactBlank when receiver is a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
masato-bkn committed Sep 7, 2024
1 parent 93f9a89 commit 027db53
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 027db53

Please sign in to comment.