Sha256: a2913ccee6cf796f0e3c98f566c4edb453e3ddfc3a87d2f9197a308a51c7b9ca

Contents?: true

Size: 1.51 KB

Versions: 17

Compression:

Stored size: 1.51 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`
      #
      #   # 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 < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Prefer `%<style>s` for setting message expectations.'.freeze

        SUPPORTED_STYLES = %w[allow expect].freeze

        def_node_matcher :message_expectation, <<-PATTERN
          (send $(send nil? {:expect :allow} ...) :to #receive_message?)
        PATTERN

        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, location: :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

17 entries across 17 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/message_expectation.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/message_expectation.rb