Skip to content

Commit

Permalink
Use hash children matching in Pending cop
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Aug 11, 2019
1 parent bc6e6ca commit 83369eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
42 changes: 20 additions & 22 deletions lib/rubocop/cop/rspec/pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ module RSpec
# end
#
# describe MyClass do
# it "should be true", skip: true do
# expect(1).to eq(2)
# end
# end
#
# describe MyClass do
# it "should be true" do
# pending
# end
Expand All @@ -28,20 +34,21 @@ module RSpec
class Pending < Cop
MSG = 'Pending spec found.'

PENDING_EXAMPLES = Examples::PENDING + Examples::SKIPPED \
+ ExampleGroups::SKIPPED
SKIPPABLE_EXAMPLES = ExampleGroups::GROUPS + Examples::EXAMPLES
SKIPPABLE_SELECTORS = SKIPPABLE_EXAMPLES.node_pattern_union
PENDING = Examples::PENDING + Examples::SKIPPED + ExampleGroups::SKIPPED
SKIPPABLE = ExampleGroups::GROUPS + Examples::EXAMPLES

SKIP_SYMBOL = s(:sym, :skip)
PENDING_SYMBOL = s(:sym, :pending)
def_node_matcher :skippable?, SKIPPABLE.send_pattern

def_node_matcher :metadata, <<-PATTERN
{(send #{RSPEC} #{SKIPPABLE_SELECTORS} ... (hash $...))
(send #{RSPEC} #{SKIPPABLE_SELECTORS} $...)}
def_node_matcher :symbol_metadata, <<-PATTERN
(send _ _ $...)
PATTERN

def_node_matcher :pending_block?, PENDING_EXAMPLES.send_pattern
def_node_matcher :skipped_in_metadata_hash?, <<-PATTERN
(send _ _ ... (hash <(pair #skip_or_pending? true) ...>))
PATTERN

def_node_matcher :skip_or_pending?, '{(sym :skip) (sym :pending)}'
def_node_matcher :pending_block?, PENDING.send_pattern

def on_send(node)
return unless pending_block?(node) || skipped_from_metadata?(node)
Expand All @@ -52,19 +59,10 @@ def on_send(node)
private

def skipped_from_metadata?(node)
(metadata(node) || []).any? { |n| skip_node?(n) }
end

def skip_node?(node)
if node.respond_to?(:key)
skip_symbol?(node.key) && node.value.truthy_literal?
else
skip_symbol?(node)
end
end
return unless skippable?(node)

def skip_symbol?(symbol_node)
[SKIP_SYMBOL, PENDING_SYMBOL].include?(symbol_node)
skipped_in_metadata_hash?(node) ||
symbol_metadata(node).any? { |n| skip_or_pending?(n) }
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions spec/rubocop/cop/rspec/pending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,61 +124,61 @@
RUBY
end

it 'does not flag describe' do
it 'ignores describe' do
expect_no_offenses(<<-RUBY)
describe 'test' do; end
RUBY
end

it 'does not flag example' do
it 'ignores example' do
expect_no_offenses(<<-RUBY)
example 'test' do; end
RUBY
end

it 'does not flag scenario' do
it 'ignores scenario' do
expect_no_offenses(<<-RUBY)
scenario 'test' do; end
RUBY
end

it 'does not flag specify' do
it 'ignores specify' do
expect_no_offenses(<<-RUBY)
specify 'test' do; end
specify do; end
RUBY
end

it 'does not flag feature' do
it 'ignores feature' do
expect_no_offenses(<<-RUBY)
feature 'test' do; end
RUBY
end

it 'does not flag context' do
it 'ignores context' do
expect_no_offenses(<<-RUBY)
context 'test' do; end
RUBY
end

it 'does not flag it' do
it 'ignores it' do
expect_no_offenses(<<-RUBY)
it 'test' do; end
RUBY
end

it 'does not flag it with skip: false metadata' do
it 'ignores it with skip: false metadata' do
expect_no_offenses(<<-RUBY)
it 'test', skip: false do; end
RUBY
end

it 'does not flag example_group' do
it 'ignores example_group' do
expect_no_offenses(<<-RUBY)
example_group 'test' do; end
RUBY
end

it 'does not flag method called pending' do
it 'ignores method called pending' do
expect_no_offenses(<<-RUBY)
subject { Project.pending }
RUBY
Expand Down

0 comments on commit 83369eb

Please sign in to comment.