From 13787e7af141d95f6dd63fd4449003d22cf86acb Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Mon, 12 Aug 2019 01:10:35 +0300 Subject: [PATCH] Use child matching in MultipleExpectations cop --- .../cop/rspec/multiple_expectations.rb | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/rubocop/cop/rspec/multiple_expectations.rb b/lib/rubocop/cop/rspec/multiple_expectations.rb index 86c253e9b..2910d5213 100644 --- a/lib/rubocop/cop/rspec/multiple_expectations.rb +++ b/lib/rubocop/cop/rspec/multiple_expectations.rb @@ -50,14 +50,23 @@ class MultipleExpectations < Cop MSG = 'Example has too many expectations [%d/%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) @@ -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)