Sha256: fbe5be660567d652c2134bbdee986c3afb538de0b6e7862c13eb87b6890fcbf4

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for any pending or skipped examples.
      #
      # @example
      #   # bad
      #   describe MyClass do
      #     it "should be true"
      #   end
      #
      #   describe MyClass do
      #     it "should be true" do
      #       pending
      #     end
      #   end
      #
      #   describe MyClass do
      #     xit "should be true" do
      #     end
      #   end
      #
      #   # good
      #   describe MyClass do
      #   end
      class Pending < Cop
        MSG = 'Pending spec found.'.freeze

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

        SKIP_SYMBOL    = s(:sym, :skip)
        PENDING_SYMBOL = s(:sym, :pending)

        def_node_matcher :metadata, <<-PATTERN
          {(send nil? #{SKIPPABLE_SELECTORS} ... (hash $...))
           (send nil? #{SKIPPABLE_SELECTORS} $...)}
        PATTERN

        def_node_matcher :pending_block?, PENDING_EXAMPLES.send_pattern

        def on_send(node)
          return unless pending_block?(node) || skipped_from_metadata?(node)

          add_offense(node, location: :expression)
        end

        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

        def skip_symbol?(symbol_node)
          symbol_node == SKIP_SYMBOL || symbol_node == PENDING_SYMBOL
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/pending.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/pending.rb