|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module RSpec |
| 6 | + # Checks that message expectations do not have a configured response. |
| 7 | + # |
| 8 | + # @example |
| 9 | + # |
| 10 | + # # bad |
| 11 | + # expect(foo).to receive(:bar).with(42).and_return("hello world") |
| 12 | + # |
| 13 | + # # good (without spies) |
| 14 | + # allow(foo).to receive(:bar).with(42).and_return("hello world") |
| 15 | + # expect(foo).to receive(:bar).with(42) |
| 16 | + # |
| 17 | + class StubbedMock < Base |
| 18 | + MSG = 'Prefer `%<replacement>s` over `%<method_name>s` when ' \ |
| 19 | + 'configuring a response.' |
| 20 | + |
| 21 | + # @!method message_expectation?(node) |
| 22 | + # Match message expectation matcher |
| 23 | + # |
| 24 | + # @example source that matches |
| 25 | + # receive(:foo) |
| 26 | + # |
| 27 | + # @example source that matches |
| 28 | + # receive_message_chain(:foo, :bar) |
| 29 | + # |
| 30 | + # @example source that matches |
| 31 | + # receive(:foo).with('bar') |
| 32 | + # |
| 33 | + # @param node [RuboCop::AST::Node] |
| 34 | + # @return [Array<RuboCop::AST::Node>] matching nodes |
| 35 | + def_node_matcher :message_expectation?, <<-PATTERN |
| 36 | + { |
| 37 | + (send nil? { :receive :receive_message_chain } ...) # receive(:foo) |
| 38 | + (send (send nil? :receive ...) :with ...) # receive(:foo).with('bar') |
| 39 | + } |
| 40 | + PATTERN |
| 41 | + |
| 42 | + def_node_matcher :configured_response?, <<~PATTERN |
| 43 | + { :and_return :and_raise :and_throw :and_yield |
| 44 | + :and_call_original :and_wrap_original } |
| 45 | + PATTERN |
| 46 | + |
| 47 | + # @!method expectation(node) |
| 48 | + # Match expectation |
| 49 | + # |
| 50 | + # @example source that matches |
| 51 | + # is_expected.to be_in_the_bar |
| 52 | + # |
| 53 | + # @example source that matches |
| 54 | + # expect(cocktail).to contain_exactly(:fresh_orange_juice, :campari) |
| 55 | + # |
| 56 | + # @example source that matches |
| 57 | + # expect_any_instance_of(Officer).to be_alert |
| 58 | + # |
| 59 | + # @param node [RuboCop::AST::Node] |
| 60 | + # @yield [RuboCop::AST::Node] expectation, method name, matcher |
| 61 | + def_node_matcher :expectation, <<~PATTERN |
| 62 | + (send |
| 63 | + $(send nil? $#{Expectations::ALL.node_pattern_union} ...) |
| 64 | + :to $_) |
| 65 | + PATTERN |
| 66 | + |
| 67 | + # @!method matcher_with_configured_response(node) |
| 68 | + # Match matcher with a configured response |
| 69 | + # |
| 70 | + # @example source that matches |
| 71 | + # receive(:foo).and_return('bar') |
| 72 | + # |
| 73 | + # @example source that matches |
| 74 | + # receive(:lower).and_raise(SomeError) |
| 75 | + # |
| 76 | + # @example source that matches |
| 77 | + # receive(:redirect).and_call_original |
| 78 | + # |
| 79 | + # @param node [RuboCop::AST::Node] |
| 80 | + # @yield [RuboCop::AST::Node] matcher |
| 81 | + def_node_matcher :matcher_with_configured_response, <<~PATTERN |
| 82 | + (send #message_expectation? #configured_response? _) |
| 83 | + PATTERN |
| 84 | + |
| 85 | + # @!method matcher_with_return_block(node) |
| 86 | + # Match matcher with a return block |
| 87 | + # |
| 88 | + # @example source that matches |
| 89 | + # receive(:foo) { 'bar' } |
| 90 | + # |
| 91 | + # @param node [RuboCop::AST::Node] |
| 92 | + # @yield [RuboCop::AST::Node] matcher |
| 93 | + def_node_matcher :matcher_with_return_block, <<~PATTERN |
| 94 | + (block #message_expectation? args _) # receive(:foo) { 'bar' } |
| 95 | + PATTERN |
| 96 | + |
| 97 | + # @!method matcher_with_hash(node) |
| 98 | + # Match matcher with a configured response defined as a hash |
| 99 | + # |
| 100 | + # @example source that matches |
| 101 | + # receive_messages(foo: 'bar', baz: 'qux') |
| 102 | + # |
| 103 | + # @example source that matches |
| 104 | + # receive_message_chain(:foo, bar: 'baz') |
| 105 | + # |
| 106 | + # @param node [RuboCop::AST::Node] |
| 107 | + # @yield [RuboCop::AST::Node] matcher |
| 108 | + def_node_matcher :matcher_with_hash, <<~PATTERN |
| 109 | + { |
| 110 | + (send nil? :receive_messages hash) # receive_messages(foo: 'bar', baz: 'qux') |
| 111 | + (send nil? :receive_message_chain ... hash) # receive_message_chain(:foo, bar: 'baz') |
| 112 | + } |
| 113 | + PATTERN |
| 114 | + |
| 115 | + # @!method matcher_with_blockpass(node) |
| 116 | + # Match matcher with a configured response in block-pass |
| 117 | + # |
| 118 | + # @example source that matches |
| 119 | + # receive(:foo, &canned) |
| 120 | + # |
| 121 | + # @example source that matches |
| 122 | + # receive_message_chain(:foo, :bar, &canned) |
| 123 | + # |
| 124 | + # @example source that matches |
| 125 | + # receive(:foo).with('bar', &canned) |
| 126 | + # |
| 127 | + # @param node [RuboCop::AST::Node] |
| 128 | + # @yield [RuboCop::AST::Node] matcher |
| 129 | + def_node_matcher :matcher_with_blockpass, <<~PATTERN |
| 130 | + { |
| 131 | + (send nil? { :receive :receive_message_chain } ... block_pass) # receive(:foo, &canned) |
| 132 | + (send (send nil? :receive ...) :with ... block_pass) # receive(:foo).with('foo', &canned) |
| 133 | + } |
| 134 | + PATTERN |
| 135 | + |
| 136 | + def on_send(node) |
| 137 | + expectation(node, &method(:on_expectation)) |
| 138 | + end |
| 139 | + |
| 140 | + private |
| 141 | + |
| 142 | + def on_expectation(expectation, method_name, matcher) |
| 143 | + flag_expectation = lambda do |
| 144 | + add_offense(expectation, message: msg(method_name)) |
| 145 | + end |
| 146 | + |
| 147 | + matcher_with_configured_response(matcher, &flag_expectation) |
| 148 | + matcher_with_return_block(matcher, &flag_expectation) |
| 149 | + matcher_with_hash(matcher, &flag_expectation) |
| 150 | + matcher_with_blockpass(matcher, &flag_expectation) |
| 151 | + end |
| 152 | + |
| 153 | + def msg(method_name) |
| 154 | + format(MSG, |
| 155 | + method_name: method_name, |
| 156 | + replacement: replacement(method_name)) |
| 157 | + end |
| 158 | + |
| 159 | + def replacement(method_name) |
| 160 | + case method_name |
| 161 | + when :expect |
| 162 | + :allow |
| 163 | + when :is_expected |
| 164 | + 'allow(subject)' |
| 165 | + when :expect_any_instance_of |
| 166 | + :allow_any_instance_of |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | +end |
0 commit comments