Class: RuboCop::Cop::Capybara::SpecificMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/capybara/specific_matcher.rb

Overview

Checks for there is a more specific matcher offered by Capybara.

Examples:


# bad
expect(page).to have_selector('button')
expect(page).to have_no_selector('button.cls')
expect(page).to have_css('button')
expect(page).to have_no_css('a.cls', href: 'http://example.com')
expect(page).to have_css('table.cls')
expect(page).to have_css('select')
expect(page).to have_css('input', exact_text: 'foo')

# good
expect(page).to have_button
expect(page).to have_no_button(class: 'cls')
expect(page).to have_button
expect(page).to have_no_link('foo', class: 'cls', href: 'http://example.com')
expect(page).to have_table(class: 'cls')
expect(page).to have_select
expect(page).to have_field(with: 'foo')

Constant Summary collapse

MSG =
'Prefer `%<good_matcher>s` over `%<bad_matcher>s`.'
RESTRICT_ON_SEND =
%i[have_selector have_no_selector have_css
have_no_css].freeze
SPECIFIC_MATCHER =
{
  'button' => 'button',
  'a' => 'link',
  'table' => 'table',
  'select' => 'select',
  'input' => 'field'
}.freeze

Instance Method Summary collapse

Instance Method Details

#first_argument(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/capybara/specific_matcher.rb', line 41

def_node_matcher :first_argument, <<~PATTERN
  (send nil? _ (str $_) ... )
PATTERN

#on_send(node) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/capybara/specific_matcher.rb', line 50

def on_send(node)
  first_argument(node) do |arg|
    next unless (matcher = specific_matcher(arg))
    next if CssSelector.multiple_selectors?(arg)
    next unless replaceable?(node, arg, matcher)

    add_offense(node, message: message(node, matcher))
  end
end

#text_with_regexp?(node) ⇒ Object



46
47
48
# File 'lib/rubocop/cop/capybara/specific_matcher.rb', line 46

def_node_search :text_with_regexp?, <<~PATTERN
  (pair (sym {:text :exact_text}) (regexp ...))
PATTERN