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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/example_wording.rb

Overview

Checks for common mistakes in example descriptions.

This cop will correct docstrings that begin with 'should' and 'it'.

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
# bad
it 'it does things' do
end

# good
it 'does things' do
end

See Also:

Constant Summary collapse

MSG_SHOULD =
'Do not use should when describing your tests.'
MSG_IT =
"Do not repeat 'it' when describing your tests."
SHOULD_PREFIX =
/\Ashould(?:n't)?\b/i.freeze
IT_PREFIX =
/\Ait /i.freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#it_description(node) ⇒ Object


42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 42

def_node_matcher :it_description, <<-PATTERN
  (block (send _ :it ${
    (str $_)
    (dstr (str $_ ) ...)
  } ...) ...)
PATTERN

#on_block(node) ⇒ Object


49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 49

def on_block(node)
  it_description(node) do |description_node, message|
    if message.match?(SHOULD_PREFIX)
      add_wording_offense(description_node, MSG_SHOULD)
    elsif message.match?(IT_PREFIX)
      add_wording_offense(description_node, MSG_IT)
    end
  end
end