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.
Fixes rubocop#862
- Loading branch information
Showing
7 changed files
with
230 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for usage of `let` blocks in specs. | ||
# | ||
# This cop can be configured with the option `AllowSubject` which | ||
# will configure the cop to only register offenses on explicit calls to | ||
# `let` and not calls to `subject` | ||
# | ||
# @example | ||
# # bad | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# it { expect(foo).to be_empty } | ||
# end | ||
# | ||
# describe MyClass do | ||
# subject(:foo) { [] } | ||
# it { expect(foo).to be_empty } | ||
# end | ||
# | ||
# # good | ||
# describe MyClass do | ||
# it do | ||
# foo = [] | ||
# expect(foo).to be_empty | ||
# end | ||
# end | ||
# | ||
# @example with AllowSubject configuration | ||
# | ||
# # rubocop.yml | ||
# # RSpec/NoLet: | ||
# # AllowSubject: true | ||
# | ||
# # bad | ||
# describe MyClass do | ||
# let(:foo) { [] } | ||
# it { expect(foo).to be_empty } | ||
# end | ||
# | ||
# # good | ||
# describe MyClass do | ||
# subject(:foo) { [] } | ||
# it { expect(foo).to be_empty } | ||
# end | ||
# | ||
# describe MyClass do | ||
# it do | ||
# foo = [] | ||
# expect(foo).to be_empty | ||
# end | ||
# end | ||
# | ||
class NoLet < Cop | ||
MSG = 'Avoid using `%<method>s` ' \ | ||
'– use a method call or local variable instead.' | ||
|
||
def_node_matcher :let?, <<~PATTERN | ||
(send nil? :let ...) | ||
PATTERN | ||
|
||
def_node_matcher :subject?, <<~PATTERN | ||
(send nil? :subject ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
if subject?(node) && !allow_subject? | ||
add_offense(node, message: format(MSG, method: 'subject')) | ||
elsif let?(node) | ||
add_offense(node, message: format(MSG, method: 'let')) | ||
end | ||
end | ||
|
||
private | ||
|
||
def allow_subject? | ||
cop_config['AllowSubject'] | ||
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
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::NoLet do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
# TODO: Write test code | ||
# | ||
# For example | ||
it 'flags an offense when using `#let`' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) { Foo.new } | ||
^^^^^^^^^ Avoid using `let` – use a method call or local variable instead. | ||
RUBY | ||
end | ||
|
||
it 'flags an offense when using `#subject` without name' do | ||
expect_offense(<<~RUBY) | ||
subject { Foo.new } | ||
^^^^^^^ Avoid using `subject` – use a method call or local variable instead. | ||
RUBY | ||
end | ||
|
||
it 'flags an offense when using `#subject` with name' do | ||
expect_offense(<<~RUBY) | ||
subject(:foo) { Foo.new } | ||
^^^^^^^^^^^^^ Avoid using `subject` – use a method call or local variable instead. | ||
RUBY | ||
end | ||
|
||
it 'does not flag an offense when using `#before`' do | ||
expect_no_offenses(<<~RUBY) | ||
before { foo } | ||
RUBY | ||
end | ||
|
||
context 'when using AllowSubject configuration', :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:cop_config) do | ||
{ 'AllowSubject' => true } | ||
end | ||
|
||
it 'flags an offense when using `#let`' do | ||
expect_offense(<<~RUBY) | ||
let(:foo) { Foo.new } | ||
^^^^^^^^^ Avoid using `let` – use a method call or local variable instead. | ||
RUBY | ||
end | ||
|
||
it 'does not flag an offense when using `#subject` without a name' do | ||
expect_no_offenses(<<~RUBY) | ||
subject { Foo.new } | ||
RUBY | ||
end | ||
|
||
it 'does not flag an offense when using `#subject` with a name' do | ||
expect_no_offenses(<<~RUBY) | ||
subject(:foo) { Foo.new } | ||
RUBY | ||
end | ||
end | ||
end |