Skip to content

Commit 13787e7

Browse files
committed
Use child matching in MultipleExpectations cop
1 parent ffaa134 commit 13787e7

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

lib/rubocop/cop/rspec/multiple_expectations.rb

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,23 @@ class MultipleExpectations < Cop
5050

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

53-
def_node_search :with_aggregate_failures?, '(sym :aggregate_failures)'
54-
def_node_search :disabled_aggregate_failures?, <<-PATTERN
55-
(pair (sym :aggregate_failures) (false))
53+
def_node_matcher :aggregate_failures?, <<-PATTERN
54+
(block {
55+
(send _ _ <(sym :aggregate_failures) ...>)
56+
(send _ _ ... (hash <(pair (sym :aggregate_failures) true) ...>))
57+
} ...)
58+
PATTERN
59+
60+
def_node_matcher :aggregate_failures_present?, <<-PATTERN
61+
(block {
62+
(send _ _ <(sym :aggregate_failures) ...>)
63+
(send _ _ ... (hash <(pair (sym :aggregate_failures) _) ...>))
64+
} ...)
5665
PATTERN
5766

5867
def_node_matcher :expect?, Expectations::ALL.send_pattern
5968
def_node_matcher :aggregate_failures_block?, <<-PATTERN
60-
(block (send _ :aggregate_failures ...) ...)
69+
(block (send nil? :aggregate_failures ...) ...)
6170
PATTERN
6271

6372
def on_block(node)
@@ -88,23 +97,6 @@ def find_aggregate_failures(example_node)
8897
.find { |block_node| aggregate_failures_present?(block_node) }
8998
end
9099

91-
def aggregate_failures_present?(node)
92-
metadata(node)&.any?(&method(:with_aggregate_failures?))
93-
end
94-
95-
def aggregate_failures?(example_or_group_node)
96-
metadata(example_or_group_node)&.any? do |metadata|
97-
with_aggregate_failures?(metadata) &&
98-
!disabled_aggregate_failures?(metadata)
99-
end
100-
end
101-
102-
def metadata(example_or_group_node)
103-
RuboCop::RSpec::Example
104-
.new(example_or_group_node)
105-
.metadata
106-
end
107-
108100
def find_expectation(node, &block)
109101
yield if expect?(node) || aggregate_failures_block?(node)
110102

0 commit comments

Comments
 (0)