Class: RuboCop::Cop::RSpec::ExampleWording

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

Overview

Checks that example descriptions do not start with “should”.

The autocorrect is experimental - use with care! It can be configured with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).

Examples:

# bad
it 'should find nothing' do
end

# good
it 'finds nothing' do
end

See Also:

Constant Summary collapse

MSG =
'Do not use should when describing your tests.'.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(range) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 44

def autocorrect(range)
  lambda do |corrector|
    corrector.replace(
      range,
      RuboCop::RSpec::Wording.new(
        range.source,
        ignore: ignored_words,
        replace: custom_transform
      ).rewrite
    )
  end
end

#on_block(node) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/LineLength



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 26

def on_block(node) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/LineLength
  method, = *node
  _, method_name, *args = *method

  return unless method_name.equal?(:it)

  arguments = args.first.to_a
  message = arguments.first.to_s
  return unless message.downcase.start_with?('should')

  arg1 = args.first.loc.expression
  message = Parser::Source::Range.new(arg1.source_buffer,
                                      arg1.begin_pos + 1,
                                      arg1.end_pos - 1)

  add_offense(message, message)
end