Sha256: a973393c93ff0980fcef3fe3c26f9e44d9a456505be2c8c7338bb2212ec15d0d

Contents?: true

Size: 1.46 KB

Versions: 38

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks where `contain_exactly` is used.
      #
      # This cop checks for the following:
      # - Prefer `match_array` when matching array values.
      # - Prefer `be_empty` when using `contain_exactly` with no arguments.
      #
      # @example
      #   # bad
      #   it { is_expected.to contain_exactly(*array1, *array2) }
      #
      #   # good
      #   it { is_expected.to match_array(array1 + array2) }
      #
      #   # good
      #   it { is_expected.to contain_exactly(content, *array) }
      #
      class ContainExactly < Base
        extend AutoCorrector

        MSG = 'Prefer `match_array` when matching array values.'
        RESTRICT_ON_SEND = %i[contain_exactly].freeze

        def on_send(node)
          return if node.arguments.empty?

          check_populated_collection(node)
        end

        private

        def check_populated_collection(node)
          return unless node.each_child_node.all?(&:splat_type?)

          add_offense(node) do |corrector|
            autocorrect_for_populated_array(node, corrector)
          end
        end

        def autocorrect_for_populated_array(node, corrector)
          arrays = node.arguments.map do |splat_node|
            splat_node.children.first
          end
          corrector.replace(
            node,
            "match_array(#{arrays.map(&:source).join(' + ')})"
          )
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 7 rubygems

Version Path
rubocop-rspec-2.27.0 lib/rubocop/cop/rspec/contain_exactly.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/contain_exactly.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/contain_exactly.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/contain_exactly.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/contain_exactly.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/contain_exactly.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/contain_exactly.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/contain_exactly.rb