Sha256: e62f9938524111de0b69b2ece87f6b7dbb6dea5246ae59291a017227acdd8a68

Contents?: true

Size: 1.58 KB

Versions: 21

Compression:

Stored size: 1.58 KB

Contents

# 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.'

        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

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/message_expectation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/message_expectation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/message_expectation.rb