class RuboCop::Cop::Minitest::AssertMatch
assert_match(matcher, string, ‘the message’)
assert_match(regex, string)
# good
assert(matcher.match(string), ‘the message’)
assert(matcher.match(string))
# bad
@example
instead of using ‘assert(matcher.match(string))`.
This cop enforces the test to use `assert_match`
def autocorrect(node)
def autocorrect(node) lambda do |corrector| assert_with_match(node) do |_, matcher, actual| corrector.replace(node.loc.selector, 'assert_match') replacement = [matcher, actual].map(&:source).join(', ') corrector.replace(first_argument_range(node), replacement) end end end
def node_arguments(matcher, actual, message)
def node_arguments(matcher, actual, message) [matcher.source, actual.source, message&.source].compact.join(', ') end
def on_send(node)
def on_send(node) assert_with_match(node) do |first_receiver_arg, matcher, actual, rest_receiver_arg| message = rest_receiver_arg.first arguments = node_arguments(matcher, actual, message) receiver = [first_receiver_arg.source, message&.source].compact.join(', ') offense_message = format(MSG, arguments: arguments, receiver: receiver) add_offense(node, message: offense_message) end end