-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
265 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,88 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Enforces consistent use of `be_a` or `be_kind_of`. | ||
# | ||
# @example EnforcedStyle: be_a (default) | ||
# # bad | ||
# expect(object).to be_kind_of(String) | ||
# expect(object).to be_a_kind_of(String) | ||
# | ||
# # good | ||
# expect(object).to be_a(String) | ||
# expect(object).to be_an(String) | ||
# | ||
# @example EnforcedStyle: be_kind_of | ||
# # bad | ||
# expect(object).to be_a(String) | ||
# expect(object).to be_an(String) | ||
# | ||
# # good | ||
# expect(object).to be_kind_of(String) | ||
# expect(object).to be_a_kind_of(String) | ||
# | ||
class ClassCheck < Base | ||
extend AutoCorrector | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG = 'Prefer `%<preferred>s` over `%<current>s`.' | ||
|
||
METHOD_NAMES_FOR_BE_A = ::Set[ | ||
:be_a, | ||
:be_an | ||
].freeze | ||
|
||
METHOD_NAMES_FOR_KIND_OF = ::Set[ | ||
:be_a_kind_of, | ||
:be_kind_of | ||
].freeze | ||
|
||
RESTRICT_ON_SEND = %i[ | ||
be_a | ||
be_a_kind_of | ||
be_an | ||
be_kind_of | ||
].freeze | ||
|
||
def on_send(node) | ||
return if preferred_method_name?(node.method_name) | ||
|
||
add_offense( | ||
node.loc.selector, | ||
message: format_message(node) | ||
) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node) | ||
corrector.replace(node.loc.selector, style) | ||
end | ||
|
||
def format_message(node) | ||
format( | ||
MSG, | ||
current: node.method_name, | ||
preferred: style | ||
) | ||
end | ||
|
||
def preferred_method_name?(method_name) | ||
preferred_method_names.include?(method_name) | ||
end | ||
|
||
def preferred_method_names | ||
if style == :be_a | ||
METHOD_NAMES_FOR_BE_A | ||
else | ||
METHOD_NAMES_FOR_KIND_OF | ||
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,103 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::ClassCheck do | ||
context 'when enforced style is be_a and be_a is used' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).to be_a(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_a and be_an is used' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).to be_an(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_a and be_kind_of is used' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
expect(a).to be_kind_of(b) | ||
^^^^^^^^^^ Prefer `be_a` over `be_kind_of`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to be_a(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_a and be_a_kind_of is used' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
expect(a).to be_a_kind_of(b) | ||
^^^^^^^^^^^^ Prefer `be_a` over `be_a_kind_of`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to be_a(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_kind_of and be_kind_of is used' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'be_kind_of' } | ||
end | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).to be_kind_of(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_kind_of and be_a_kind_of is used' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'be_kind_of' } | ||
end | ||
|
||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).to be_a_kind_of(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_kind_of and be_a is used' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'be_kind_of' } | ||
end | ||
|
||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
expect(a).to be_a(b) | ||
^^^^ Prefer `be_kind_of` over `be_a`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to be_kind_of(b) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when enforced style is be_kind_of and be_an is used' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'be_kind_of' } | ||
end | ||
|
||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY) | ||
expect(a).to be_an(b) | ||
^^^^^ Prefer `be_kind_of` over `be_an`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to be_kind_of(b) | ||
RUBY | ||
end | ||
end | ||
end |