Sha256: 3891224e5d393711cdfcbbef490528587439e8340da2c05f637457807ffe3a5a

Contents?: true

Size: 1.58 KB

Versions: 19

Compression:

Stored size: 1.58 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)`.
      #
      # @safety
      #   This cop is unsafe because true might be expected instead of truthy.
      #   False positives cannot be prevented when this is a variable or method return value.
      #
      #   [source,ruby]
      #   ----
      #   assert_equal(true, 'truthy') # failure
      #   assert('truthy')             # success
      #   ----
      #
      # @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

19 entries across 19 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/assert_truthy.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/assert_truthy.rb