class RuboCop::Cop::InternalAffairs::EmptyLineBetweenExpectOffenseAndCorrection

Checks whether ‘expect_offense` and correction expectation methods (i.e. `expect_correction` and `expect_no_corrections`) are separated by empty line.

@example

# bad
it 'registers and corrects an offense' do
  expect_offense(<<~RUBY)
    bad_method
    ^^^^^^^^^^ Use `good_method`.
  RUBY
  expect_correction(<<~RUBY)
    good_method
  RUBY
end

# good
it 'registers and corrects an offense' do
  expect_offense(<<~RUBY)
    bad_method
    ^^^^^^^^^^ Use `good_method`.
  RUBY

  expect_correction(<<~RUBY)
    good_method
  RUBY
end

Constants

CORRECTION_EXPECTATION_METHODS
MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb, line 40
def on_send(node)
  return unless (next_sibling = node.right_sibling) && next_sibling.send_type?

  method_name = next_sibling.method_name
  return unless CORRECTION_EXPECTATION_METHODS.include?(method_name)

  range = offense_range(node)
  return unless range.last_line + 1 == next_sibling.loc.line

  add_offense(range, message: format(MSG, expect_correction: method_name)) do |corrector|
    corrector.insert_after(range, "\n")
  end
end

Private Instance Methods

offense_range(node) click to toggle source
# File lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb, line 56
def offense_range(node)
  first_argument = node.first_argument

  if first_argument.respond_to?(:heredoc?) && first_argument.heredoc?
    first_argument.loc.heredoc_end
  else
    node
  end
end