-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport the addition of new cop RSpec/ExpectInLet added in #1885 from the main branch to the 2-x-stable branch. We want to get as much feedback as possible on new cops before (eventually) enabling them when v3 is released.
- Loading branch information
Showing
8 changed files
with
147 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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Do not use `expect` in let. | ||
# | ||
# @example | ||
# # bad | ||
# let(:foo) do | ||
# expect(something).to eq 'foo' | ||
# end | ||
# | ||
# # good | ||
# it do | ||
# expect(something).to eq 'foo' | ||
# end | ||
# | ||
class ExpectInLet < Base | ||
MSG = 'Do not use `%<expect>s` in let' | ||
|
||
# @!method expectation(node) | ||
def_node_search :expectation, '(send nil? #Expectations.all ...)' | ||
|
||
def on_block(node) | ||
return unless let?(node) | ||
return if node.body.nil? | ||
|
||
expectation(node.body) do |expect| | ||
add_offense(expect.loc.selector, message: message(expect)) | ||
end | ||
end | ||
|
||
alias on_numblock on_block | ||
|
||
private | ||
|
||
def message(expect) | ||
format(MSG, expect: expect.method_name) | ||
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::ExpectInLet do | ||
it 'adds an offense for `expect` in let' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) do | ||
expect(something).to eq 'foo' | ||
^^^^^^ Do not use `expect` in let | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds an offense for `is_expected` in let' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) do | ||
is_expected.to eq 'foo' | ||
^^^^^^^^^^^ Do not use `is_expected` in let | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds an offense for `expect_any_instance_of` in let' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) do | ||
expect_any_instance_of(Something).to receive :foo | ||
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in let | ||
end | ||
RUBY | ||
end | ||
|
||
it 'adds offenses for multiple expectations in let' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) do | ||
expect(something).to eq 'foo' | ||
^^^^^^ Do not use `expect` in let | ||
is_expected.to eq 'foo' | ||
^^^^^^^^^^^ Do not use `is_expected` in let | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts an empty let' do | ||
expect_no_offenses(<<~RUBY) | ||
let(:foo) {} | ||
RUBY | ||
end | ||
|
||
it 'accepts `expect` in `it`' do | ||
expect_no_offenses(<<~RUBY) | ||
it do | ||
expect(something).to eq 'foo' | ||
is_expected.to eq 'foo' | ||
expect_any_instance_of(Something).to receive :foo | ||
end | ||
RUBY | ||
end | ||
end |