Sha256: 479b2c047e7b398034e260c3acd9500341d3fd77c33000304f17964916264624

Contents?: true

Size: 1.87 KB

Versions: 24

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check that `all` matcher is used instead of iterating over an array.
      #
      # @example
      #   # bad
      #   it 'validates users' do
      #     [user1, user2, user3].each { |user| expect(user).to be_valid }
      #   end
      #
      #   # good
      #   it 'validates users' do
      #     expect([user1, user2, user3]).to all(be_valid)
      #   end
      #
      class IteratedExpectation < Base
        MSG = 'Prefer using the `all` matcher instead ' \
              'of iterating over an array.'

        # @!method each?(node)
        def_node_matcher :each?, <<~PATTERN
          (block
            (send ... :each)
            (args (arg $_))
            $(...)
          )
        PATTERN

        # @!method each_numblock?(node)
        def_node_matcher :each_numblock?, <<~PATTERN
          (numblock
            (send ... :each) _ $(...)
          )
        PATTERN

        # @!method expectation?(node)
        def_node_matcher :expectation?, <<~PATTERN
          (send (send nil? :expect (lvar %)) :to ...)
        PATTERN

        def on_block(node)
          each?(node) do |arg, body|
            if single_expectation?(body, arg) || only_expectations?(body, arg)
              add_offense(node.send_node)
            end
          end
        end

        def on_numblock(node)
          each_numblock?(node) do |body|
            if single_expectation?(body, :_1) || only_expectations?(body, :_1)
              add_offense(node.send_node)
            end
          end
        end

        private

        def single_expectation?(body, arg)
          expectation?(body, arg)
        end

        def only_expectations?(body, arg)
          return false unless body.each_child_node.any?

          body.each_child_node.all? { |child| expectation?(child, arg) }
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/iterated_expectation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/iterated_expectation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/iterated_expectation.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/iterated_expectation.rb