Class: RuboCop::Cop::RSpec::NotToNot

Inherits:
RuboCop::Cop show all
Includes:
ConfigurableEnforcedStyle, RSpec::SpecOnly
Defined in:
lib/rubocop/cop/rspec/not_to_not.rb

Overview

Checks for consistent method usage for negating expectations.

Examples:

# bad
it '...' do
  expect(false).to_not be_true
end

# good
it '...' do
  expect(false).not_to be_true
end

Constant Summary collapse

MSG =
'Prefer `%s` over `%s`'.freeze
METHOD_NAMES =
[:not_to, :to_not].freeze

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rubocop/cop/rspec/not_to_not.rb', line 43

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

#message(node) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/not_to_not.rb', line 33

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

#on_send(node) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/rspec/not_to_not.rb', line 24

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