Class: RuboCop::Cop::RSpec::RepeatedExampleGroupBody

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

Overview

Check for repeated describe and context block body.

Examples:


# bad
describe 'cool feature x' do
  it { cool_predicate }
end

describe 'cool feature y' do
  it { cool_predicate }
end

# good
describe 'cool feature' do
  it { cool_predicate }
end

describe 'another cool feature' do
  it { another_predicate }
end

# good
context 'when case x', :tag do
  it { cool_predicate }
end

context 'when case y' do
  it { cool_predicate }
end

# good
context Array do
  it { is_expected.to respond_to :each }
end

context Hash do
  it { is_expected.to respond_to :each }
end

Constant Summary collapse

MSG =
'Repeated %<group>s block body 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

#body(node) ⇒ Object


58
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 58

def_node_matcher :body, '(block _ args $...)'

#const_arg(node) ⇒ Object


61
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 61

def_node_matcher :const_arg, '(block (send _ _ $const ...) ...)'

#metadata(node) ⇒ Object


55
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 55

def_node_matcher :metadata, '(block (send _ _ _ $...) ...)'

#on_begin(node) ⇒ Object


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

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

  repeated_group_bodies(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_body.rb', line 50

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

#skip_or_pending?(node) ⇒ Object


64
65
66
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 64

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