-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new
Capybara/NegationMatcherAfterVisit
cop
Resolve: #7 This PR adds a new `Capybara/NegationMatcherAfterVisit` cop.
- Loading branch information
Showing
9 changed files
with
145 additions
and
10 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
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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Capybara | ||
# Do not allow negative matchers to be used immediately after `visit`. | ||
# | ||
# @example | ||
# # bad | ||
# visit foo_path | ||
# expect(page).to have_no_link('bar') | ||
# expect(page).to have_css('a') | ||
# | ||
# # good | ||
# visit foo_path | ||
# expect(page).to have_css('a') | ||
# expect(page).to have_no_link('bar') | ||
# | ||
# # bad | ||
# visit foo_path | ||
# expect(page).not_to have_link('bar') | ||
# expect(page).to have_css('a') | ||
# | ||
# # good | ||
# visit foo_path | ||
# expect(page).to have_css('a') | ||
# expect(page).not_to have_link('bar') | ||
# | ||
class NegationMatcherAfterVisit < ::RuboCop::Cop::Base | ||
include CapybaraHelp | ||
|
||
MSG = 'Do not use negation matcher immediately after visit.' | ||
RESTRICT_ON_SEND = %i[visit].freeze | ||
|
||
# @!method negation_matcher?(node) | ||
def_node_matcher :negation_matcher?, <<~PATTERN | ||
{ | ||
(send (send nil? :expect _) :to (send nil? %NEGATIVE_MATCHERS ...)) | ||
(send (send nil? :expect _) :not_to (send nil? %POSITIVE_MATCHERS ...)) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
negation_matcher?(node.right_sibling) do | ||
add_offense(node.right_sibling) | ||
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
30 changes: 30 additions & 0 deletions
30
spec/rubocop/cop/capybara/negation_matcher_after_visit_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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::NegationMatcherAfterVisit, :config do | ||
it 'registers an offense when using `have_no_*` after ' \ | ||
'immediately `visit` method call' do | ||
expect_offense(<<~RUBY) | ||
visit foo_path | ||
expect(page).to have_no_link('bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use negation matcher immediately after visit. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `not_to` with `have_*` after ' \ | ||
'immediately `visit` method call' do | ||
expect_offense(<<~RUBY) | ||
visit foo_path | ||
expect(page).not_to have_link('bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use negation matcher immediately after visit. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using positive matchers after ' \ | ||
'immediately `visit` method call' do | ||
expect_no_offenses(<<~RUBY) | ||
visit foo_path | ||
expect(page).to have_css('a') | ||
expect(page).to have_no_link('bar') | ||
RUBY | ||
end | ||
end |