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.
To check context with single example. Closes rubocop#2001
- Loading branch information
1 parent
162d91a
commit 1039e7a
Showing
4 changed files
with
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Remove redundant `around` hook. | ||
# | ||
# @example | ||
# # bad | ||
# context 'when condition' do | ||
# it 'tests something' do | ||
# end | ||
# end | ||
# | ||
# # good | ||
# it 'tests something when condition' do | ||
# end | ||
# | ||
class RedundantContext < Base | ||
MSG = 'Redundant context with single example.' | ||
|
||
# @!method redundant_context?(node) | ||
def_node_matcher :redundant_context?, <<~PATTERN | ||
(block | ||
(send #rspec? :context _) | ||
_ | ||
(block (send _ :it ...) ...)) | ||
PATTERN | ||
|
||
def on_block(node) | ||
return unless redundant_context?(node) | ||
|
||
add_offense(node) | ||
end | ||
alias on_numblock on_block | ||
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::RedundantContext do | ||
it 'registers an offense when single example inside context' do | ||
expect_offense(<<~RUBY) | ||
context 'when condition' do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant context with single example. | ||
it 'does something' do | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register offense when multiple examples inside context' do | ||
expect_no_offenses(<<~RUBY) | ||
context 'when condition' do | ||
it 'does something' do | ||
end | ||
it 'does something else' do | ||
end | ||
end | ||
RUBY | ||
end | ||
end |