lib/rubocop/cop/mixin/minitest_cop_rule.rb in rubocop-minitest-0.27.0 vs lib/rubocop/cop/mixin/minitest_cop_rule.rb in rubocop-minitest-0.28.0
- old
+ new
@@ -11,23 +11,33 @@
# define_rule :assert, target_method: :match
# define_rule :refute, target_method: :match
# define_rule :assert, target_method: :include?, preferred_method: :assert_includes
# define_rule :assert, target_method: :instance_of?, inverse: true
#
+ # @example Multiple target methods
+ # # `preferred_method` is required
+ # define_rule :assert, target_method: %i[match match? =~],
+ # preferred_method: :assert_match, inverse: 'regexp_type?'
+ #
# @param assertion_method [Symbol] Assertion method like `assert` or `refute`.
- # @param target_method [Symbol] Method name offensed by assertion method arguments.
- # @param preferred_method [Symbol] An optional param. Custom method name replaced by
+ # @param target_method [Symbol, Array<Symbol>] Method name(s) offensed by assertion method arguments.
+ # @param preferred_method [Symbol] Is required if passing multiple target methods. Custom method name replaced by
# autocorrection. The preferred method name that connects
# `assertion_method` and `target_method` with `_` is
# the default name.
# @param inverse [Boolean, String] An optional param. Order of arguments replaced by autocorrection.
# If string is passed, it becomes a predicate method for the first argument node.
# @api private
#
def define_rule(assertion_method, target_method:, preferred_method: nil, inverse: false)
- preferred_method = "#{assertion_method}_#{target_method.to_s.delete('?')}" if preferred_method.nil?
+ target_methods = Array(target_method)
+ if target_methods.size > 1 && preferred_method.nil?
+ raise ArgumentError, '`:preferred_method` keyword argument must be used if using more than one target method.'
+ end
+ preferred_method = "#{assertion_method}_#{target_methods.first.to_s.delete('?')}" if preferred_method.nil?
+
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
include ArgumentRangeHelper
extend AutoCorrector
MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)`.'
@@ -35,10 +45,11 @@
def on_send(node)
return unless node.method?(:#{assertion_method})
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first&.call_type?
- return if arguments.first.arguments.empty? || !arguments.first.method?(:#{target_method})
+ return if arguments.first.arguments.empty? ||
+ #{target_methods}.none? { |target_method| arguments.first.method?(target_method) }
add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
end
end