Sha256: 014cd3ec926c8c424cffdf1f26aac04ca367d2773fab13eed80cadf784ae3735

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 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 < Cop
        MSG = "Don't repeat examples within an example group."

        def on_block(node)
          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_name == :its
            key_parts << example.definition.arguments
          end

          key_parts
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.42.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/repeated_example.rb