Sha256: b1b4b1dad82424c12814de331b81d8ceabc8f8b5b627b76e990c257f23a5572a

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for long examples.
      #
      # A long example is usually more difficult to understand. Consider
      # extracting out some behaviour, e.g. with a `let` block, or a helper
      # method.
      #
      # @example
      #   # bad
      #   it do
      #     service = described_class.new
      #     more_setup
      #     more_setup
      #     result = service.call
      #     expect(result).to be(true)
      #   end
      #
      #   # good
      #   it do
      #     service = described_class.new
      #     result = service.call
      #     expect(result).to be(true)
      #   end
      class ExampleLength < Cop
        include CodeLength

        MSG = 'Example has too many lines [%<total>d/%<max>d].'

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

          length = code_length(node)

          return unless length > max_length

          add_offense(node, message: message(length))
        end

        private

        def code_length(node)
          node.source.lines[1..-2].count { |line| !irrelevant_line(line) }
        end

        def message(length)
          format(MSG, total: length, max: max_length)
        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/example_length.rb
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/example_length.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/example_length.rb