-
-
Notifications
You must be signed in to change notification settings - Fork 277
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 #1666 from rubocop/feature/1658
Add new `RSpec/IsExpectedSpecify` cop
- Loading branch information
Showing
8 changed files
with
137 additions
and
0 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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Check for `specify` with `is_expected` and one-liner expectations. | ||
# | ||
# @example | ||
# # bad | ||
# specify { is_expected.to be_truthy } | ||
# | ||
# # good | ||
# it { is_expected.to be_truthy } | ||
# | ||
# # good | ||
# specify do | ||
# # ... | ||
# end | ||
# specify { expect(sqrt(4)).to eq(2) } | ||
# | ||
class IsExpectedSpecify < Base | ||
extend AutoCorrector | ||
|
||
RESTRICT_ON_SEND = %i[specify].freeze | ||
IS_EXPECTED_METHODS = ::Set[:is_expected, :are_expected].freeze | ||
MSG = 'Use `it` instead of `specify`.' | ||
|
||
# @!method offense?(node) | ||
def_node_matcher :offense?, <<~PATTERN | ||
(block (send _ :specify) _ (send (send _ IS_EXPECTED_METHODS) ...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
block_node = node.parent | ||
return unless block_node&.single_line? && offense?(block_node) | ||
|
||
selector = node.loc.selector | ||
add_offense(selector) do |corrector| | ||
corrector.replace(selector, 'it') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::IsExpectedSpecify, :config do | ||
it 'registers an offense when using `specify` and one-liner style' do | ||
expect_offense(<<~RUBY) | ||
specify { is_expected.to be_truthy } | ||
^^^^^^^ Use `it` instead of `specify`. | ||
specify { are_expected.to be_falsy } | ||
^^^^^^^ Use `it` instead of `specify`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it { is_expected.to be_truthy } | ||
it { are_expected.to be_falsy } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `specify` ' \ | ||
'and not one-liner style' do | ||
expect_no_offenses(<<~RUBY) | ||
specify { expect(sqrt(4)).to eq(2) } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `specify` and multi line' do | ||
expect_no_offenses(<<~RUBY) | ||
specify do | ||
is_expected.to be_truthy | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `it` and one-liner style' do | ||
expect_no_offenses(<<~RUBY) | ||
it { is_expected.to be_truthy } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `specify` with metadata' do | ||
expect_no_offenses(<<~RUBY) | ||
specify "pending", :pending | ||
RUBY | ||
end | ||
end |