Sha256: 169385616a07229b3ee910bb6ba687e3c0bdf4080f81dbe569f6863de13c3e8f

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module RuboCop
  module Cop
    module RSpec
      # Checks for consistent method usage for negating 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
        include RuboCop::RSpec::SpecOnly,
                RuboCop::Cop::ConfigurableEnforcedStyle

        MSG = 'Prefer `%s` over `%s`'.freeze

        METHOD_NAMES = [:not_to, :to_not].freeze

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

          return unless METHOD_NAMES.include?(method_name)

          return if style.equal?(method_name)
          add_offense(node, :expression)
        end

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

          if method_name.equal?(:not_to)
            format(MSG, 'to_not', 'not_to')
          else
            format(MSG, 'not_to', 'to_not')
          end
        end

        def autocorrect(node)
          _receiver, method_name, *_args = *node
          lambda do |corrector|
            corrector.replace(node.loc.selector,
                              method_name.equal?(:not_to) ? 'to_not' : 'not_to')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.8.0 lib/rubocop/cop/rspec/not_to_not.rb
rubocop-rspec-1.7.0 lib/rubocop/cop/rspec/not_to_not.rb