Class: RuboCop::Cop::RSpec::RepeatedExampleGroupDescription

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/repeated_example_group_description.rb

Overview

Check for repeated example group descriptions.

Examples:


# bad
describe 'cool feature' do
  # example group
end

describe 'cool feature' do
  # example group
end

# bad
context 'when case x' do
  # example group
end

describe 'when case x' do
  # example group
end

# good
describe 'cool feature' do
  # example group
end

describe 'another cool feature' do
  # example group
end

# good
context 'when case x' do
  # example group
end

context 'when another case' do
  # example group
end

Constant Summary collapse

MSG =
'Repeated %<group>s block description on line(s) %<loc>s'

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

#doc_string_and_metadata(node) ⇒ Object


55
56
57
# File 'lib/rubocop/cop/rspec/repeated_example_group_description.rb', line 55

def_node_matcher :doc_string_and_metadata, <<-PATTERN
  (block (send _ _ $_ $...) ...)
PATTERN

#empty_description?(node) ⇒ Object


65
# File 'lib/rubocop/cop/rspec/repeated_example_group_description.rb', line 65

def_node_matcher :empty_description?, '(block (send _ _) ...)'

#on_begin(node) ⇒ Object


67
68
69
70
71
72
73
# File 'lib/rubocop/cop/rspec/repeated_example_group_description.rb', line 67

def on_begin(node)
  return unless several_example_groups?(node)

  repeated_group_descriptions(node).each do |group, repeats|
    add_offense(group, message: message(group, repeats))
  end
end

#several_example_groups?(node) ⇒ Object


50
51
52
# File 'lib/rubocop/cop/rspec/repeated_example_group_description.rb', line 50

def_node_matcher :several_example_groups?, <<-PATTERN
  (begin <#example_group? #example_group? ...>)
PATTERN

#skip_or_pending?(node) ⇒ Object


60
61
62
# File 'lib/rubocop/cop/rspec/repeated_example_group_description.rb', line 60

def_node_matcher :skip_or_pending?, <<-PATTERN
  (block <(send nil? {:skip :pending}) ...>)
PATTERN