Skip to content

Commit

Permalink
Simplify nested node pattern to nested matching
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 18, 2020
1 parent 604860b commit 67018b7
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions lib/rubocop/cop/rspec/stubbed_mock.rb
Expand Up @@ -30,51 +30,49 @@ class StubbedMock < Base
:and_call_original :and_wrap_original }
PATTERN

def self.expectation_with(matcher)
<<~PATTERN
(send
$(send nil? $#{Expectations::ALL.node_pattern_union} ...)
:to #{matcher}
)
PATTERN
end
def_node_matcher :expectation, <<~PATTERN
(send
$(send nil? $#{Expectations::ALL.node_pattern_union} ...)
:to $_)
PATTERN

def_node_matcher :expectation_with_configured_response,
expectation_with(<<~PATTERN)
(send #message_expectation? #configured_response? _)
PATTERN
def_node_matcher :matcher_with_configured_response, <<~PATTERN
(send #message_expectation? #configured_response? _)
PATTERN

def_node_matcher :expectation_with_return_block,
expectation_with(<<~PATTERN)
(block #message_expectation? args _)
PATTERN
def_node_matcher :matcher_with_return_block, <<~PATTERN
(block #message_expectation? args _)
PATTERN

def_node_matcher :expectation_with_blockpass_or_hash,
expectation_with(<<~PATTERN)
{
(send nil? { :receive :receive_message_chain } ... block_pass)
(send (send nil? :receive ...) :with ... block_pass)
(send nil? :receive_messages hash)
(send nil? :receive_message_chain ... hash)
}
PATTERN
def_node_matcher :matcher_with_blockpass_or_hash, <<~PATTERN
{
(send nil? { :receive :receive_message_chain } ... block_pass)
(send (send nil? :receive ...) :with ... block_pass)
(send nil? :receive_messages hash)
(send nil? :receive_message_chain ... hash)
}
PATTERN

def on_send(node)
expectation_with_configured_response(node) do |match, method_name|
add_offense(match, message: msg(method_name))
expectation(node, &method(:on_expectation))
end

private

def on_expectation(expectation, method_name, matcher)
matcher_with_configured_response(matcher) do
add_offense(expectation, message: msg(method_name))
end

expectation_with_return_block(node) do |match, method_name|
add_offense(match, message: msg(method_name))
matcher_with_return_block(matcher) do
add_offense(expectation, message: msg(method_name))
end

expectation_with_blockpass_or_hash(node) do |match, method_name|
add_offense(match, message: msg(method_name))
matcher_with_blockpass_or_hash(matcher) do
add_offense(expectation, message: msg(method_name))
end
end

private

def msg(method_name)
format(MSG,
method_name: method_name,
Expand Down

0 comments on commit 67018b7

Please sign in to comment.