Sha256: 28c7830966cdc7460a3c022115dc819c54c721749e6c789a72bda2e923efe84d

Contents?: true

Size: 1.25 KB

Versions: 35

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check for repeated examples within example groups.
      #
      # @example
      #
      #    it 'is valid' do
      #      expect(user).to be_valid
      #    end
      #
      #    it 'validates the user' do
      #      expect(user).to be_valid
      #    end
      #
      class RepeatedExample < Base
        MSG = "Don't repeat examples within an example group."

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless example_group?(node)

          repeated_examples(node).each do |repeated_example|
            add_offense(repeated_example)
          end
        end

        private

        def repeated_examples(node)
          RuboCop::RSpec::ExampleGroup.new(node)
            .examples
            .group_by { |example| example_signature(example) }
            .values
            .reject(&:one?)
            .flatten
            .map(&:to_node)
        end

        def example_signature(example)
          key_parts = [example.metadata, example.implementation]

          if example.definition.method?(:its)
            key_parts << example.definition.arguments
          end

          key_parts
        end
      end
    end
  end
end

Version data entries

35 entries across 33 versions & 4 rubygems

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