-
-
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 new
Capybara/ClickLinkOrButtonStyle
cop
Fix: #58
- Loading branch information
Showing
8 changed files
with
204 additions
and
1 deletion.
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Capybara | ||
# Checks for click button or link style. | ||
# | ||
# @example EnforcedStyle: strict (default) | ||
# # bad | ||
# click_link_or_button('foo') | ||
# click_on('foo') | ||
# | ||
# # good | ||
# click_link('foo') | ||
# click_button('foo') | ||
# | ||
# @example EnforcedStyle: link_or_button | ||
# # bad | ||
# click_link('foo') | ||
# click_button('foo') | ||
# | ||
# # good | ||
# click_link_or_button('foo') | ||
# click_on('foo') | ||
# | ||
class ClickLinkOrButtonStyle < ::RuboCop::Cop::Base | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG_STRICT = | ||
'Use `click_link` or `click_button` instead of `%<method>s`.' | ||
MSG_CLICK_LINK_OR_BUTTON = | ||
'Use `click_link_or_button` or `click_on` instead of `%<method>s`.' | ||
STRICT_METHODS = %i[click_link click_button].freeze | ||
CLICK_LINK_OR_BUTTON = %i[click_link_or_button click_on].freeze | ||
RESTRICT_ON_SEND = (STRICT_METHODS + CLICK_LINK_OR_BUTTON).freeze | ||
|
||
def on_send(node) | ||
return unless offense?(node) | ||
|
||
add_offense(node, message: offense_message(node)) | ||
end | ||
|
||
private | ||
|
||
def offense?(node) | ||
style == :strict && !strict_method?(node) || | ||
style == :link_or_button && !link_or_button_method?(node) | ||
end | ||
|
||
def offense_message(node) | ||
if style == :strict | ||
format(MSG_STRICT, method: node.method_name) | ||
elsif style == :link_or_button | ||
format(MSG_CLICK_LINK_OR_BUTTON, method: node.method_name) | ||
end | ||
end | ||
|
||
def strict_method?(node) | ||
STRICT_METHODS.include?(node.method_name) | ||
end | ||
|
||
def link_or_button_method?(node) | ||
CLICK_LINK_OR_BUTTON.include?(node.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
66 changes: 66 additions & 0 deletions
66
spec/rubocop/cop/capybara/click_link_or_button_style_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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::ClickLinkOrButtonStyle do | ||
let(:cop_config) { { 'EnforcedStyle' => enforced_style } } | ||
let(:enforced_style) { 'strict' } | ||
|
||
context 'when EnforcedStyle is `strict`' do | ||
let(:enforced_style) { 'strict' } | ||
|
||
it 'registers an offense when using `click_link_or_button`' do | ||
expect_offense(<<~RUBY) | ||
click_link_or_button('foo') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `click_link` or `click_button` instead of `click_link_or_button`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `click_on`' do | ||
expect_offense(<<~RUBY) | ||
click_on('foo') | ||
^^^^^^^^^^^^^^^ Use `click_link` or `click_button` instead of `click_on`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `click_link`' do | ||
expect_no_offenses(<<~RUBY) | ||
click_link('foo') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `click_button`' do | ||
expect_no_offenses(<<~RUBY) | ||
click_button('foo') | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle is `link_or_button`' do | ||
let(:enforced_style) { 'link_or_button' } | ||
|
||
it 'registers an offense when using `click_link`' do | ||
expect_offense(<<~RUBY) | ||
click_link('foo') | ||
^^^^^^^^^^^^^^^^^ Use `click_link_or_button` or `click_on` instead of `click_link`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `click_button`' do | ||
expect_offense(<<~RUBY) | ||
click_button('foo') | ||
^^^^^^^^^^^^^^^^^^^ Use `click_link_or_button` or `click_on` instead of `click_button`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `click_link_or_button`' do | ||
expect_no_offenses(<<~RUBY) | ||
click_link_or_button('foo') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `click_on`' do | ||
expect_no_offenses(<<~RUBY) | ||
click_on('foo') | ||
RUBY | ||
end | ||
end | ||
end |