Sha256: 7a26695b501f4b8c407712e883adcaadfc08a7db1e561f2efe6f5192306ae7b6

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module RSpec
      # Enforces the usage of the same method on all negative message
      # expectations.
      #
      # @example
      #   # bad
      #   it '...' do
      #     expect(false).to_not be_true
      #   end
      #
      #   # good
      #   it '...' do
      #     expect(false).not_to be_true
      #   end
      class NotToNot < Cop
        ACCEPTED_METHODS = [:not_to, :to_not].freeze

        def on_send(node)
          _receiver, method_name, *_args = *node

          if method_name == rejected_method
            add_offense(node, :expression, offense_message)
          end
        end

        private

        def accepted_method
          @accepted_method ||= begin
            method = cop_config['AcceptedMethod'].to_sym

            unless ACCEPTED_METHODS.include?(method)
              raise "Invalid AcceptedMethod value: #{method}"
            end

            method
          end
        end

        def rejected_method
          @rejected_method ||= (ACCEPTED_METHODS - [accepted_method]).first
        end

        def offense_message
          "Use `#{accepted_method}` instead of `#{rejected_method}`"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.4.1 lib/rubocop/cop/rspec/not_to_not.rb
rubocop-rspec-1.4.0 lib/rubocop/cop/rspec/not_to_not.rb