class RuboCop::Cop::Minitest::AssertMatch
assert_match(matcher, string, ‘message’)
assert_match(regex, string)
# good
assert(matcher.match(string), ‘message’)
assert_operator(matcher, :=~, string)
assert(matcher =~ string)
assert(matcher.match?(string))
assert(matcher.match(string))
# bad
@example
instead of using ‘assert(matcher.match(string))`.
Enforces the test to use `assert_match`
def on_send(node)
def on_send(node) assert_match(node) do |expected, actual, rest_args| basic_arguments = order_expected_and_actual(expected, actual) preferred = (message_arg = rest_args.first) ? "#{basic_arguments}, #{message_arg.source}" : basic_arguments message = format(MSG, preferred: preferred) add_offense(node, message: message) do |corrector| corrector.replace(node.loc.selector, 'assert_match') range = if node.method?(:assert) node.first_argument else node.first_argument.source_range.begin.join(node.arguments[2].source_range.end) end corrector.replace(range, basic_arguments) end end end
def order_expected_and_actual(expected, actual)
def order_expected_and_actual(expected, actual) if actual.regexp_type? [actual, expected] else [expected, actual] end.map(&:source).join(', ') end