Sha256: 6ad2b89a896aaf94824d79594d02e80d6dd9fdf78132091a8cff3253e418558c

Contents?: true

Size: 1.97 KB

Versions: 18

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module RuboCop
  module RSpec
    # Wrapper for RSpec example groups
    class ExampleGroup < Concept
      # @!method scope_change?(node)
      #
      #   Detect if the node is an example group or shared example
      #
      #   Selectors which indicate that we should stop searching
      #
      def_node_matcher :scope_change?, (
        ExampleGroups::ALL + SharedGroups::ALL + Includes::ALL
      ).block_pattern

      def subjects
        subjects_in_scope(node)
      end

      def examples
        examples_in_scope(node).map(&Example.public_method(:new))
      end

      def hooks
        hooks_in_scope(node).map(&Hook.public_method(:new))
      end

      private

      def subjects_in_scope(node)
        node.each_child_node.flat_map do |child|
          find_subjects(child)
        end
      end

      def find_subjects(node)
        return [] if scope_change?(node)

        if subject?(node)
          [node]
        else
          subjects_in_scope(node)
        end
      end

      def hooks_in_scope(node)
        node.each_child_node.flat_map do |child|
          find_hooks(child)
        end
      end

      def find_hooks(node)
        return [] if scope_change?(node) || example?(node)

        if hook?(node)
          [node]
        else
          hooks_in_scope(node)
        end
      end

      def examples_in_scope(node, &blk)
        node.each_child_node.flat_map do |child|
          find_examples(child, &blk)
        end
      end

      # Recursively search for examples within the current scope
      #
      # Searches node for examples and halts when a scope change is detected
      #
      # @param node [RuboCop::Node] node to recursively search for examples
      #
      # @return [Array<RuboCop::Node>] discovered example nodes
      def find_examples(node)
        return [] if scope_change?(node)

        if example?(node)
          [node]
        else
          examples_in_scope(node)
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rubocop-rspec-1.40.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.39.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.38.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.38.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.37.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.37.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.36.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.35.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.34.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.34.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.33.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.32.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.31.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.30.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.30.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.29.1 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.29.0 lib/rubocop/rspec/example_group.rb
rubocop-rspec-1.28.0 lib/rubocop/rspec/example_group.rb