Class: RuboCop::Cop::Workit::RSpecCapybaraPredicateMatcher

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RSpecPredicateMatcherBase
Defined in:
lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb

Overview

Prefer using predicate matcher over using predicate method directly.

Capybara defines magic matchers for predicate methods. This cop recommends to use the predicate matcher instead of using predicate method directly.

Examples:

Strict: true, EnforcedStyle: inflected (default)

# bad
expect(foo.matches_css?(bar: 'baz')).to be_truthy
expect(foo.matches_selector?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_xpath?(bar: 'baz')).to be_truthy

# good
expect(foo).to match_css(bar: 'baz')
expect(foo).to match_selector(bar: 'baz')
expect(foo).to match_style(bar: 'baz')
expect(foo).to match_xpath(bar: 'baz')

# also good - It checks "true" strictly.
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: inflected

# bad
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be(true)

# good
expect(foo).to match_style(bar: 'baz')

Strict: true, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be_truthy

Constant Summary collapse

MATCHER_SUFFIX =
%w[css selector style xpath].freeze
INFLECTED_MATCHER =
MATCHER_SUFFIX.each.map do |suffix|
  "match_#{suffix}"
end.freeze
EXPLICIT_MATCHER =
MATCHER_SUFFIX.each.map do |suffix|
  "matches_#{suffix}?"
end.freeze

Constants included from RSpecPredicateMatcherBase

RuboCop::Cop::Workit::RSpecPredicateMatcherBase::RESTRICT_ON_SEND

Constants included from RSpecExplicitHelp

RuboCop::Cop::Workit::RSpecExplicitHelp::BUILT_IN_MATCHERS, RuboCop::Cop::Workit::RSpecExplicitHelp::MSG_EXPLICIT

Constants included from RSpecInflectedHelp

RuboCop::Cop::Workit::RSpecInflectedHelp::MSG_INFLECTED

Instance Method Summary collapse

Methods included from RSpecPredicateMatcherBase

#on_block, #on_send

Instance Method Details

#predicate?(sym) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb', line 73

def predicate?(sym)
  EXPLICIT_MATCHER.include?(sym.to_s)
end

#predicate_matcher_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb', line 62

def predicate_matcher_name?(name)
  name = name.to_s
  return false if allowed_explicit_matchers.include?(name)

  INFLECTED_MATCHER.include?(name)
end

#to_predicate_matcher(name) ⇒ Object



69
70
71
# File 'lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb', line 69

def to_predicate_matcher(name)
  name.to_s.sub("matches_", "match_")[0..-2]
end

#to_predicate_method(matcher) ⇒ Object



77
78
79
# File 'lib/rubocop/cop/workit/rspec_capybara_predicate_matcher.rb', line 77

def to_predicate_method(matcher)
  "#{matcher.to_s.sub("match_", "matches_")}?"
end