Class: RuboCop::Cop::RSpec::SharedExamples

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/shared_examples.rb

Overview

Enforces use of string to titleize shared examples.

Examples:

# bad
it_behaves_like :foo_bar_baz
it_should_behave_like :foo_bar_baz
shared_examples :foo_bar_baz
shared_examples_for :foo_bar_baz
include_examples :foo_bar_baz

# good
it_behaves_like 'foo bar baz'
it_should_behave_like 'foo bar baz'
shared_examples 'foo bar baz'
shared_examples_for 'foo bar baz'
include_examples 'foo bar baz'

Defined Under Namespace

Classes: Checker

Constant Summary

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/shared_examples.rb', line 37

def autocorrect(node)
  lambda do |corrector|
    checker = Checker.new(node)
    corrector.replace(node.loc.expression, checker.preferred_style)
  end
end

#on_send(node) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/rspec/shared_examples.rb', line 27

def on_send(node)
  shared_examples(node) do
    ast_node = node.first_argument
    next unless ast_node&.sym_type?

    checker = Checker.new(ast_node)
    add_offense(checker.node, message: checker.message)
  end
end