Sha256: 052a9f35397ac071a590dc0af326a7c7d42855d9b2ab33651e2ae69095f64778

Contents?: true

Size: 1.48 KB

Versions: 20

Compression:

Stored size: 1.48 KB

Contents

module RuboCop
  module Cop
    module RSpec
      # Check for repeated description strings in example groups.
      #
      # @example
      #
      #     # bad
      #     RSpec.describe User do
      #       it 'is valid' do
      #         # ...
      #       end
      #
      #       it 'is valid' do
      #         # ...
      #       end
      #     end
      #
      #     # good
      #     RSpec.describe User do
      #       it 'is valid when first and last name are present' do
      #         # ...
      #       end
      #
      #       it 'is valid when last name only is present' do
      #         # ...
      #       end
      #     end
      #
      class RepeatedDescription < Cop
        MSG = "Don't repeat descriptions within an example group.".freeze

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

          repeated_descriptions(node).each do |repeated_description|
            add_offense(repeated_description, location: :expression)
          end
        end

        private

        # Select examples in the current scope with repeated description strings
        def repeated_descriptions(node)
          grouped_examples =
            RuboCop::RSpec::ExampleGroup.new(node)
              .examples
              .group_by(&:doc_string)

          grouped_examples
            .select { |description, group| description && group.size > 1 }
            .values
            .flatten
            .map(&:definition)
        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_description.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/repeated_description.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/repeated_description.rb