forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rubocop#1604 from rubocop/be-empty-cop
Add new `RSpec/BeEmpty` cop
- Loading branch information
Showing
12 changed files
with
132 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Prefer using `be_empty` when checking for an empty array. | ||
# | ||
# @example | ||
# # bad | ||
# expect(array).to contain_exactly | ||
# expect(array).to match_array([]) | ||
# | ||
# # good | ||
# expect(array).to be_empty | ||
# | ||
class BeEmpty < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `be_empty` matchers for checking an empty array.' | ||
RESTRICT_ON_SEND = %i[contain_exactly match_array].freeze | ||
|
||
# @!method expect_array_matcher?(node) | ||
def_node_matcher :expect_array_matcher?, <<~PATTERN | ||
(send | ||
(send nil? :expect _) | ||
#Runners.all | ||
${ | ||
(send nil? :match_array (array)) | ||
(send nil? :contain_exactly) | ||
} | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
expect_array_matcher?(node.parent) do |expect| | ||
add_offense(expect) do |corrector| | ||
corrector.replace(expect, 'be_empty') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::BeEmpty, :config do | ||
it 'registers an offense when using `expect(array).to contain_exactly`' do | ||
expect_offense(<<~RUBY) | ||
expect(array).to contain_exactly | ||
^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(array).to be_empty | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `expect(array).to match_array([])`' do | ||
expect_offense(<<~RUBY) | ||
expect(array).to match_array([]) | ||
^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(array).to be_empty | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters