Class: RuboCop::Cop::RSpec::PendingWithoutReason

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

Overview

Checks for pending or skipped examples without reason.

Examples:

# bad
pending 'does something' do
end

# bad
it 'does something', :pending do
end

# bad
it 'does something' do
  pending
end

# bad
xdescribe 'something' do
end

# bad
skip 'does something' do
end

# bad
it 'does something', :skip do
end

# bad
it 'does something' do
  skip
end

# bad
it 'does something'

# good
it 'does something' do
  pending 'reason'
end

# good
it 'does something' do
  skip 'reason'
end

# good
it 'does something', pending: 'reason' do
end

# good
it 'does something', skip: 'reason' do
end

Constant Summary collapse

MSG =
'Give the reason for pending or skip.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #numblock_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

#on_send(node) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 95

def on_send(node)
  if pending_without_reason?(node)
    add_offense(node, message: 'Give the reason for pending.')
  elsif skipped_without_reason?(node)
    add_offense(node, message: 'Give the reason for skip.')
  elsif without_reason?(node) && example?(node.parent)
    add_offense(node,
                message: "Give the reason for #{node.method_name}.")
  end
end

#pending_by_example_method?(node) ⇒ Object



63
64
65
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 63

def_node_matcher :pending_by_example_method?, block_pattern(<<~PATTERN)
  #Examples.pending
PATTERN

#pending_by_metadata_without_reason?(node) ⇒ Object



68
69
70
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 68

def_node_matcher :pending_by_metadata_without_reason?, <<~PATTERN
  (send #rspec? {#ExampleGroups.all #Examples.all} ... {<(sym :pending) ...> (hash <(pair (sym :pending) true) ...>)})
PATTERN

#skipped_by_example_group_method?(node) ⇒ Object



78
79
80
81
82
83
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 78

def_node_matcher(
  :skipped_by_example_group_method?,
  block_pattern(<<~PATTERN)
    #ExampleGroups.skipped
  PATTERN
)

#skipped_by_example_method?(node) ⇒ Object



73
74
75
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 73

def_node_matcher :skipped_by_example_method?, block_pattern(<<~PATTERN)
  #Examples.skipped
PATTERN

#skipped_by_metadata_without_reason?(node) ⇒ Object



86
87
88
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 86

def_node_matcher :skipped_by_metadata_without_reason?, <<~PATTERN
  (send #rspec? {#ExampleGroups.all #Examples.all} ... {<(sym :skip) ...> (hash <(pair (sym :skip) true) ...>)})
PATTERN

#without_reason?(node) ⇒ Object



91
92
93
# File 'lib/rubocop/cop/rspec/pending_without_reason.rb', line 91

def_node_matcher :without_reason?, <<~PATTERN
  (send nil? ${:pending :skip})
PATTERN