-
-
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/RSpec/HaveSelector
cop
This PR add new `Capybara/RSpec/HaveSelector` cop. ```ruby expect(foo).to have_selector(:css, 'bar') expect(foo).to have_css('bar') expect(foo).to have_selector(:xpath, 'bar') expect(foo).to have_xpath('bar') expect(foo).to have_selector('bar') expect(foo).to have_css('bar') expect(foo).to have_selector('bar') expect(foo).to have_xpath('bar') ```
- Loading branch information
Showing
7 changed files
with
254 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
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,87 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Capybara | ||
module RSpec | ||
# Use `have_css` or `have_xpath` instead of `have_selector`. | ||
# | ||
# @example | ||
# # bad | ||
# expect(foo).to have_selector(:css, 'bar') | ||
# | ||
# # good | ||
# expect(foo).to have_css('bar') | ||
# | ||
# # bad | ||
# expect(foo).to have_selector(:xpath, 'bar') | ||
# | ||
# # good | ||
# expect(foo).to have_xpath('bar') | ||
# | ||
# @example DefaultSelector: css (default) | ||
# # bad | ||
# expect(foo).to have_selector('bar') | ||
# | ||
# # good | ||
# expect(foo).to have_css('bar') | ||
# | ||
# @example DefaultSelector: xpath | ||
# # bad | ||
# expect(foo).to have_selector('bar') | ||
# | ||
# # good | ||
# expect(foo).to have_xpath('bar') | ||
# | ||
class HaveSelector < ::RuboCop::Cop::Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Use `%<good>s` instead of `have_selector`.' | ||
RESTRICT_ON_SEND = %i[have_selector].freeze | ||
SELECTORS = %i[css xpath].freeze | ||
|
||
def on_send(node) | ||
argument = node.first_argument | ||
on_select_with_type(node, argument) if argument.sym_type? | ||
on_select_without_type(node) if argument.str_type? | ||
end | ||
|
||
private | ||
|
||
def on_select_with_type(node, type) | ||
return unless SELECTORS.include?(type.value) | ||
|
||
add_offense(node, message: message_typed(type)) do |corrector| | ||
corrector.remove(deletion_range(type, node.arguments[1])) | ||
corrector.replace(node.loc.selector, "have_#{type.value}") | ||
end | ||
end | ||
|
||
def message_typed(type) | ||
format(MSG, good: "have_#{type.value}") | ||
end | ||
|
||
def deletion_range(first_argument, second_argument) | ||
range_between(first_argument.source_range.begin_pos, | ||
second_argument.source_range.begin_pos) | ||
end | ||
|
||
def on_select_without_type(node) | ||
add_offense(node, message: message_untyped) do |corrector| | ||
corrector.replace(node.loc.selector, "have_#{default_selector}") | ||
end | ||
end | ||
|
||
def message_untyped | ||
format(MSG, good: "have_#{default_selector}") | ||
end | ||
|
||
def default_selector | ||
cop_config.fetch('DefaultSelector', 'css') | ||
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
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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::RSpec::HaveSelector, :config do | ||
let(:cop_config) do | ||
{ 'DefaultSelector' => default_selector } | ||
end | ||
let(:default_selector) { 'css' } | ||
|
||
it 'registers an offense when using `have_selector`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector('bar') | ||
^^^^^^^^^^^^^^^^^^^^ Use `have_css` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_css('bar') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `have_selector` with `:css`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector(:css, 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_css` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_css('bar') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `have_selector` with `:xpath`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector(:xpath, 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_xpath` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_xpath('bar') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `have_css`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to have_css('bar') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `have_selector` with other sym' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to have_selector(:foo, 'bar') | ||
RUBY | ||
end | ||
|
||
context 'when DefaultSelector is xpath' do | ||
let(:default_selector) { 'xpath' } | ||
|
||
it 'registers an offense when using `have_selector`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector('bar') | ||
^^^^^^^^^^^^^^^^^^^^ Use `have_xpath` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_xpath('bar') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `have_selector` with `:xpath`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector(:xpath, 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_xpath` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_xpath('bar') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `have_selector` with `:css`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to have_selector(:css, 'bar') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_css` instead of `have_selector`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to have_css('bar') | ||
RUBY | ||
end | ||
end | ||
end |