-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
7 changed files
with
282 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for consistent verified double reference style. | ||
# | ||
# Only investigates references that are one of the supported styles. | ||
# | ||
# @see https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles | ||
# | ||
# This cop can be configured in your configuration using the | ||
# `EnforcedStyle` option and supports `--auto-gen-config`. | ||
# | ||
# @example `EnforcedStyle: constant` (default) | ||
# # bad | ||
# let(:foo) do | ||
# instance_double('ClassName', method_name: 'returned_value') | ||
# end | ||
# | ||
# # good | ||
# let(:foo) do | ||
# instance_double(ClassName, method_name: 'returned_value') | ||
# end | ||
# | ||
# @example `EnforcedStyle: string` | ||
# # bad | ||
# let(:foo) do | ||
# instance_double(ClassName, method_name: 'returned_value') | ||
# end | ||
# | ||
# # good | ||
# let(:foo) do | ||
# instance_double('ClassName', method_name: 'returned_value') | ||
# end | ||
# | ||
# @example Reference is not in the supported style list. No enforcement | ||
# | ||
# # good | ||
# let(:foo) do | ||
# instance_double(@klass, method_name: 'returned_value') | ||
# end | ||
class VerifiedDoubleReference < Base | ||
extend AutoCorrector | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG = 'Use a %<style>s class reference for verified doubles.' | ||
|
||
RESTRICT_ON_SEND = Set[ | ||
:class_double, | ||
:class_spy, | ||
:instance_double, | ||
:instance_spy, | ||
:mock_model, | ||
:object_double, | ||
:object_spy, | ||
:stub_model | ||
].freeze | ||
|
||
REFERENCE_TYPE_STYLES = { | ||
str: :string, | ||
const: :constant | ||
}.freeze | ||
|
||
# @!method verified_double(node) | ||
def_node_matcher :verified_double, <<~PATTERN | ||
(send | ||
nil? | ||
RESTRICT_ON_SEND | ||
$_class_reference | ||
...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
verified_double(node) do |class_reference| | ||
break correct_style_detected unless opposing_style?(class_reference) | ||
|
||
message = format(MSG, style: style) | ||
expression = class_reference.loc.expression | ||
|
||
add_offense(expression, message: message) do |corrector| | ||
violation = class_reference.children.last.to_s | ||
corrector.replace(expression, correct_style(violation)) | ||
|
||
opposite_style_detected | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def opposing_style?(class_reference) | ||
class_reference_style = REFERENCE_TYPE_STYLES[class_reference.type] | ||
|
||
# Only enforce supported styles | ||
return false unless class_reference_style | ||
|
||
class_reference_style != style | ||
end | ||
|
||
def correct_style(violation) | ||
if style == :string | ||
"'#{violation}'" | ||
else | ||
violation | ||
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::VerifiedDoubleReference do | ||
verified_doubles = %w[ | ||
class_double | ||
class_spy | ||
instance_double | ||
instance_spy | ||
mock_model | ||
object_double | ||
object_spy | ||
stub_model | ||
] | ||
|
||
verified_doubles.each do |verified_double| | ||
describe verified_double do | ||
context 'when EnforcedStyle is constant' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'constant' } | ||
end | ||
|
||
it 'does not flag a violation when using a constant reference' do | ||
expect_no_offenses("#{verified_double}(ClassName)") | ||
end | ||
|
||
it 'flags a violation when using a string reference' do | ||
expect_offense(<<~RUBY, verified_double: verified_double) | ||
%{verified_double}('ClassName') | ||
_{verified_double} ^^^^^^^^^^^ Use a constant class reference for verified doubles. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
#{verified_double}(ClassName) | ||
RUBY | ||
end | ||
|
||
include_examples 'detects style', | ||
"#{verified_double}(ClassName)", | ||
'constant' | ||
end | ||
|
||
context 'when EnforcedStyle is string' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'string' } | ||
end | ||
|
||
it 'does not flag a violation when using a string reference' do | ||
expect_no_offenses("#{verified_double}('ClassName')") | ||
end | ||
|
||
it 'flags a violation when using a constant reference' do | ||
expect_offense(<<~RUBY, verified_double: verified_double) | ||
%{verified_double}(ClassName) | ||
_{verified_double} ^^^^^^^^^ Use a string class reference for verified doubles. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
#{verified_double}('ClassName') | ||
RUBY | ||
end | ||
|
||
include_examples 'detects style', | ||
"#{verified_double}('ClassName')", | ||
'string' | ||
end | ||
end | ||
end | ||
|
||
it 'does not flag a violation when reference is not a supported style' do | ||
expect_no_offenses(<<~RUBY) | ||
klass = Array | ||
instance_double(klass) | ||
@sut = Array | ||
let(:double) { instance_double(@sut) } | ||
object_double([]) | ||
class_double(:Model) | ||
RUBY | ||
end | ||
end |