Skip to content

Commit

Permalink
POC usage of node pattern generic parameters
Browse files Browse the repository at this point in the history
#804 (comment)
Support introduced in rubocop/rubocop-ast#31
Presumably this will land in RuboCop 1.0 or 1.0.1 worst case.
  • Loading branch information
pirj committed Jul 15, 2020
1 parent 1be4c09 commit 1a47e9e
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Expand Up @@ -50,17 +50,12 @@ class MultipleExpectations < Cop

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

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

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

Expand Down Expand Up @@ -89,12 +84,12 @@ def example_with_aggregate_failures?(example_node)
node_with_aggregate_failures = find_aggregate_failures(example_node)
return false unless node_with_aggregate_failures

aggregate_failures?(node_with_aggregate_failures)
aggregate_failures?(node_with_aggregate_failures, :true_type?.to_proc)
end

def find_aggregate_failures(example_node)
example_node.send_node.each_ancestor(:block)
.find { |block_node| aggregate_failures_present?(block_node) }
.find { |block_node| aggregate_failures?(block_node, ANYTHING) }
end

def find_expectation(node, &block)
Expand Down

0 comments on commit 1a47e9e

Please sign in to comment.