Sha256: 95de6ba1c7ca21a66ce613b6c3c258173a0257c7121256ea6270f5609caf59d2

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Checks if test cases contain too many assertion calls.
      # The maximum allowed assertion calls is configurable.
      #
      # @example Max: 1
      #   # bad
      #   class FooTest < Minitest::Test
      #     def test_asserts_twice
      #       assert_equal(42, do_something)
      #       assert_empty(array)
      #     end
      #   end
      #
      #   # good
      #   class FooTest < Minitest::Test
      #     def test_asserts_once
      #       assert_equal(42, do_something)
      #     end
      #
      #     def test_another_asserts_once
      #       assert_empty(array)
      #     end
      #   end
      #
      class MultipleAssertions < Base
        include ConfigurableMax
        include MinitestExplorationHelpers

        MSG = 'Test case has too many assertions [%<total>d/%<max>d].'

        def on_class(class_node)
          return unless test_class?(class_node)

          test_cases(class_node).each do |node|
            assertions_count = assertions_count(node)

            next unless assertions_count > max_assertions

            self.max = assertions_count

            message = format(MSG, total: assertions_count, max: max_assertions)
            add_offense(node, message: message)
          end
        end

        private

        def assertions_count(node)
          base = assertion_method?(node) ? 1 : 0
          base + node.each_child_node.sum { |c| assertions_count(c) }
        end

        def max_assertions
          Integer(cop_config.fetch('Max', 3))
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubocop-minitest-0.26.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.24.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.23.2 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.23.1 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.23.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.22.2 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.22.1 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.22.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.21.1 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.21.0 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.20.1 lib/rubocop/cop/minitest/multiple_assertions.rb
rubocop-minitest-0.20.0 lib/rubocop/cop/minitest/multiple_assertions.rb