Skip to content

Commit

Permalink
Use child matching in MultipleExpectations cop
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 12, 2019
1 parent ffaa134 commit 13787e7
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Expand Up @@ -50,14 +50,23 @@ class MultipleExpectations < Cop

MSG = 'Example has too many expectations [%<total>d/%<max>d].'

def_node_search :with_aggregate_failures?, '(sym :aggregate_failures)'
def_node_search :disabled_aggregate_failures?, <<-PATTERN
(pair (sym :aggregate_failures) (false))
def_node_matcher :aggregate_failures?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) true) ...>))
} ...)
PATTERN

def_node_matcher :aggregate_failures_present?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) _) ...>))
} ...)
PATTERN

def_node_matcher :expect?, Expectations::ALL.send_pattern
def_node_matcher :aggregate_failures_block?, <<-PATTERN
(block (send _ :aggregate_failures ...) ...)
(block (send nil? :aggregate_failures ...) ...)
PATTERN

def on_block(node)
Expand Down Expand Up @@ -88,23 +97,6 @@ def find_aggregate_failures(example_node)
.find { |block_node| aggregate_failures_present?(block_node) }
end

def aggregate_failures_present?(node)
metadata(node)&.any?(&method(:with_aggregate_failures?))
end

def aggregate_failures?(example_or_group_node)
metadata(example_or_group_node)&.any? do |metadata|
with_aggregate_failures?(metadata) &&
!disabled_aggregate_failures?(metadata)
end
end

def metadata(example_or_group_node)
RuboCop::RSpec::Example
.new(example_or_group_node)
.metadata
end

def find_expectation(node, &block)
yield if expect?(node) || aggregate_failures_block?(node)

Expand Down

0 comments on commit 13787e7

Please sign in to comment.