Sha256: afab663d0ea4db8f693e2e6debb173478923f2968063528e6a4086a1b621d4e6

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the test to use `assert(actual)` instead of using `assert_equal(true, actual)`.
      #
      # @example
      #   # bad
      #   assert_equal(true, actual)
      #   assert_equal(true, actual, 'message')
      #
      #   # good
      #   assert(actual)
      #   assert(actual, 'message')
      #
      class AssertTruthy < Base
        include ArgumentRangeHelper
        extend AutoCorrector

        MSG = 'Prefer using `assert(%<arguments>s)`.'
        RESTRICT_ON_SEND = %i[assert_equal].freeze

        def_node_matcher :assert_equal_with_truthy, <<~PATTERN
          (send nil? :assert_equal true $_ $...)
        PATTERN

        def on_send(node)
          assert_equal_with_truthy(node) do |actual, rest_receiver_arg|
            message = rest_receiver_arg.first

            arguments = [actual.source, message&.source].compact.join(', ')

            add_offense(node, message: format(MSG, arguments: arguments)) do |corrector|
              corrector.replace(node.loc.selector, 'assert')
              corrector.replace(first_and_second_arguments_range(node), actual.source)
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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