From 57f0bb108a55bfafe6c501afd96f2a8951ffde5c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jan 2024 07:08:21 +1300 Subject: [PATCH] fix: support asserts with messages in `Rspec/BeEmpty` --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/be_empty.rb | 1 + spec/rubocop/cop/rspec/be_empty_spec.rb | 31 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c69da08..087cd4b61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Master (Unreleased) +- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath]) - Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah]) - Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath]) - Support correcting `*_includes` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath]) diff --git a/lib/rubocop/cop/rspec/be_empty.rb b/lib/rubocop/cop/rspec/be_empty.rb index 68d3cd69d..c262944df 100644 --- a/lib/rubocop/cop/rspec/be_empty.rb +++ b/lib/rubocop/cop/rspec/be_empty.rb @@ -28,6 +28,7 @@ class BeEmpty < Base (send nil? :match_array (array)) (send nil? :contain_exactly) } + $_? ) PATTERN diff --git a/spec/rubocop/cop/rspec/be_empty_spec.rb b/spec/rubocop/cop/rspec/be_empty_spec.rb index c3cc51dd4..58c6cee12 100644 --- a/spec/rubocop/cop/rspec/be_empty_spec.rb +++ b/spec/rubocop/cop/rspec/be_empty_spec.rb @@ -12,6 +12,18 @@ RUBY end + it 'registers an offense when ' \ + 'using `expect(array).to contain_exactly, "with a message"`' do + expect_offense(<<~RUBY) + expect(array).to contain_exactly, "with a message" + ^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. + RUBY + + expect_correction(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end + it 'registers an offense when using `expect(array).to match_array([])`' do expect_offense(<<~RUBY) expect(array).to match_array([]) @@ -23,9 +35,28 @@ RUBY end + it 'registers an offense when ' \ + 'using `expect(array).to match_array([]), "with a message"`' do + expect_offense(<<~RUBY) + expect(array).to match_array([]), "with a message" + ^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. + RUBY + + expect_correction(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end + it 'does not register an offense when using `expect(array).to be_empty`' do expect_no_offenses(<<~RUBY) expect(array).to be_empty RUBY end + + it 'does not register an offense when ' \ + 'using `expect(array).to be_empty, "with a message"`' do + expect_no_offenses(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end end