Sha256: 986fa81e69e1e842d15d1de465f46f31e3ab3eed28f6c3e9631608964cc986a1

Contents?: true

Size: 1.21 KB

Versions: 20

Compression:

Stored size: 1.21 KB

Contents

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.".freeze

        def on_block(node)
          return unless example_group?(node)

          repeated_examples(node).each do |repeated_example|
            add_offense(repeated_example, location: :expression)
          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

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/repeated_example.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/repeated_example.rb