Sha256: 3d952dd42e58a9ece76c16d625c2fc42d1fd83ceeb6bae37c9f1320f7c13844a

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Tries to detect when a user accidentally used
      # `assert` when they meant to use `assert_equal`.
      #
      # @safety
      #   This cop is unsafe because it is not possible to determine
      #   whether the second argument of `assert` is a message or not.
      #
      # @example
      #   # bad
      #   assert(3, my_list.length)
      #   assert(expected, actual)
      #
      #   # good
      #   assert_equal(3, my_list.length)
      #   assert_equal(expected, actual)
      #   assert(foo, 'message')
      #
      class AssertWithExpectedArgument < Base
        MSG = 'Did you mean to use `assert_equal(%<arguments>s)`?'
        RESTRICT_ON_SEND = %i[assert].freeze

        def_node_matcher :assert_with_two_arguments?, <<~PATTERN
          (send nil? :assert $_ $_)
        PATTERN

        def on_send(node)
          assert_with_two_arguments?(node) do |_expected, message|
            return if message.str_type? || message.dstr_type?

            arguments = node.arguments.map(&:source).join(', ')
            add_offense(node, message: format(MSG, arguments: arguments))
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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