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.
Add RSpec/StringAsInstanceDoubleConstant
Addresses rubocop#1136 Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.
- Loading branch information
1 parent
16cf19c
commit 7c794c1
Showing
8 changed files
with
116 additions
and
1 deletion.
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
40 changes: 40 additions & 0 deletions
40
lib/rubocop/cop/rspec/string_as_instance_double_constant.rb
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Do not use a string as `instance_double` constant. | ||
# | ||
# @example | ||
# # bad | ||
# instance_double('Foo', failure_message, stubs) | ||
# | ||
# # good | ||
# instance_double(Foo, failure_message, stubs) | ||
# | ||
class StringAsInstanceDoubleConstant < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Do not use a string as `instance_double` constant.' | ||
RESTRICT_ON_SEND = %i[instance_double].freeze | ||
|
||
# @!method stringified_instance_double_const?(node) | ||
def_node_matcher :stringified_instance_double_const?, <<~PATTERN | ||
(send nil? :instance_double $str ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
stringified_instance_double_const?(node) do |args_node| | ||
add_offense(args_node) do |corrector| | ||
autocorrect(corrector, args_node) | ||
end | ||
end | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
corrector.replace(node, node.value) | ||
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
33 changes: 33 additions & 0 deletions
33
spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::StringAsInstanceDoubleConstant, | ||
:config do | ||
context 'when using a class for instance_double' do | ||
it do | ||
expect_no_offenses(<<~RUBY) | ||
instance_double(Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when using a symbol for instance_double' do | ||
it do | ||
expect_no_offenses(<<~RUBY) | ||
instance_double(:Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when using a string for instance_double' do | ||
it 'replaces the string with the class' do | ||
expect_offense <<~RUBY | ||
instance_double('Foo', failure_message, stubs) | ||
^^^^^ Do not use a string as `instance_double` constant. | ||
RUBY | ||
|
||
expect_correction <<~RUBY | ||
instance_double(Foo, failure_message, stubs) | ||
RUBY | ||
end | ||
end | ||
end |