1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for consistent message expectation style.
#
# This cop can be configured in your configuration using the
# `EnforcedStyle` option and supports `--auto-gen-config`.
#
# @example `EnforcedStyle: allow` (default)
#
# # bad
# expect(foo).to receive(:bar)
#
# # good
# allow(foo).to receive(:bar)
#
# @example `EnforcedStyle: expect`
#
# # bad
# allow(foo).to receive(:bar)
#
# # good
# expect(foo).to receive(:bar)
#
class MessageExpectation < Base
include ConfigurableEnforcedStyle
MSG = 'Prefer `%<style>s` for setting message expectations.'
SUPPORTED_STYLES = %w[allow expect].freeze
RESTRICT_ON_SEND = %i[to].freeze
# @!method message_expectation(node)
def_node_matcher :message_expectation, <<-PATTERN
(send $(send nil? {:expect :allow} ...) :to #receive_message?)
PATTERN
# @!method receive_message?(node)
def_node_search :receive_message?, '(send nil? :receive ...)'
def on_send(node)
message_expectation(node) do |match|
return correct_style_detected if preferred_style?(match)
message = format(MSG, style: style)
add_offense(match.loc.selector, message: message) do
opposite_style_detected
end
end
end
private
def preferred_style?(expectation)
expectation.method_name.equal?(style)
end
end
end
end
end
|