From 027db53f1778a11c57d2a4b8fd1c7833ddfc793a Mon Sep 17 00:00:00 2001 From: masato-bkn <37011138+masato-bkn@users.noreply.github.com> Date: Sat, 7 Sep 2024 12:15:20 +0900 Subject: [PATCH] Add missing tests for `Rails/CompactBlank` when receiver is a hash --- spec/rubocop/cop/rails/compact_blank_spec.rb | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/spec/rubocop/cop/rails/compact_blank_spec.rb b/spec/rubocop/cop/rails/compact_blank_spec.rb index 93d1ca2bd7..3b3cd9d77b 100644 --- a/spec/rubocop/cop/rails/compact_blank_spec.rb +++ b/spec/rubocop/cop/rails/compact_blank_spec.rb @@ -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? } @@ -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? } @@ -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? } @@ -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? }