Skip to content

AssertMatch/RefuteMatch autofix can be incorrect #310

Description

@movermeyer

When applying Minitest/AssertMatch (or Minitest/RefuteMatch)'s auto-fix to the following the working code:

require "minitest/autorun"

class AssertMatchTest < Minitest::Test
  def test_assert_match_autofix
    hello = "Hello World!"
    greeting = %r{Hello}
    assert hello.match?(%r{World})
    assert hello.match? greeting
  end
end

Expected behavior

It would auto-correct assert hello.match? greeting to assert_match greeting, hello.
Indeed, it works correctly for assert hello.match?(%r{World}).

Actual behavior

It auto-corrects assert hello.match? greeting to assert_match hello, greeting (note the order of parameters), which causes the test to error:

TypeError: no implicit conversion of Regexp into String

since it didn't swap the order of the parameters.

Steps to reproduce the problem

  1. Save the above code into a file (foo_test.rb)
  2. Run the test to verify that it passes (ruby foo_test.rb)
  3. Autoformat the code (rubocop -a foo_test.rb)
  4. Run the test again (ruby foo_test.rb)

See the test error.

RuboCop version

➜ bundle exec rubocop -V
1.64.1 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.1.4) [arm64-darwin23]
  - rubocop-minitest 0.35.0
  - rubocop-performance 1.21.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions