Sha256: 1f6aa9ffc20a481c9dec5a75932b854af62db4e780afa336bf128367ebe667d2

Contents?: true

Size: 1.25 KB

Versions: 18

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

18 entries across 18 versions & 4 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/repeated_example.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/repeated_example.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/repeated_example.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/repeated_example.rb