Class: RuboCop::Cop::UmtsCustomCops::PredicateMethodMatcher

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/umts-custom-cops/predicate_method_matcher.rb

Overview

See the specs for examples.

Constant Summary collapse

MSG =
'Prefer predicate matcher over checking the return value of a predicate method.'
BOOLEAN_EQUALITY_MATCHERS =
i(be_true be_false)
GENERIC_EQUALITY_MATCHERS =
i(be eq eql equal)

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/umts-custom-cops/predicate_method_matcher.rb', line 15

def on_send(node)
  return unless i(to not_to).include? node.method_name
  return unless node.child_nodes
  expectation = node.child_nodes.first
  return unless expectation.method_name == :expect
  match_value = expectation.child_nodes.first
  return unless match_value.method_name.to_s.end_with? '?'
  matcher = node.child_nodes[1]
  if GENERIC_EQUALITY_MATCHERS.include? matcher.method_name
    matcher_arg = matcher.child_nodes.first
    return unless matcher_arg.true_type? || matcher_arg.false_type?
  else return unless BOOLEAN_EQUALITY_MATCHERS.include? matcher.method_name
  end
  add_offense node, :expression, MSG
end