Skip to content

Commit

Permalink
Add new Capybara/ClickLinkOrButtonStyle cop
Browse files Browse the repository at this point in the history
Fix: #58
  • Loading branch information
ydah committed Aug 7, 2023
1 parent 31bd569 commit 23f72f2
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Drop Ruby 2.6 support. ([@ydah])
- Add new `Capybara/RSpec/PredicateMatcher` cop. ([@ydah])
- Add new `Capybara/ClickLinkOrButtonStyle` cop. ([@ydah])

## 2.18.0 (2023-04-21)

Expand Down
10 changes: 10 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ Capybara:
- "**/*_steps.rb"
- "**/features/step_definitions/**/*"

Capybara/ClickLinkOrButtonStyle:
Description: Checks for click button or link style.
Enabled: pending
VersionAdded: "<<next>>"
EnforcedStyle: strict
SupportedStyles:
- strict
- click_link_or_button
Reference: https://www.rubydoc.info/gems/rubocop-capybara/RuboCop/Cop/Capybara/ClickLinkOrButtonStyle

Capybara/CurrentPathExpectation:
Description: Checks that no expectations are set on Capybara's `current_path`.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

=== Department xref:cops_capybara.adoc[Capybara]

* xref:cops_capybara.adoc#capybaraclicklinkorbuttonstyle[Capybara/ClickLinkOrButtonStyle]
* xref:cops_capybara.adoc#capybaracurrentpathexpectation[Capybara/CurrentPathExpectation]
* xref:cops_capybara.adoc#capybaramatchstyle[Capybara/MatchStyle]
* xref:cops_capybara.adoc#capybaranegationmatcher[Capybara/NegationMatcher]
Expand Down
56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/cops_capybara.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
= Capybara

== Capybara/ClickLinkOrButtonStyle

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| <<next>>
| -
|===

Checks for click button or link style.

=== Examples

==== EnforcedStyle: strict (default)

[source,ruby]
----
# bad
click_link_or_button('foo')
click_on('foo')
# good
click_link('foo')
click_button('foo')
----

==== EnforcedStyle: link_or_button

[source,ruby]
----
# bad
click_link('foo')
click_button('foo')
# good
click_link_or_button('foo')
click_on('foo')
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `strict`
| `strict`, `click_link_or_button`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-capybara/RuboCop/Cop/Capybara/ClickLinkOrButtonStyle

== Capybara/CurrentPathExpectation

|===
Expand Down
64 changes: 64 additions & 0 deletions lib/rubocop/cop/capybara/click_link_or_button_style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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)
case style
when :strict
return unless strict_method?(node)

message = format(MSG_STRICT, method: node.method_name)
when :link_or_button
return unless link_or_button_method?(node)

message = format(MSG_CLICK_LINK_OR_BUTTON, method: node.method_name)
end

add_offense(node, message: message)
end

private

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
1 change: 1 addition & 0 deletions lib/rubocop/cop/capybara_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative 'capybara/rspec/predicate_matcher'

require_relative 'capybara/click_link_or_button_style'
require_relative 'capybara/current_path_expectation'
require_relative 'capybara/match_style'
require_relative 'capybara/negation_matcher'
Expand Down
66 changes: 66 additions & 0 deletions spec/rubocop/cop/capybara/click_link_or_button_style_spec.rb
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

0 comments on commit 23f72f2

Please sign in to comment.