Skip to content

Add assert_includes suggestion to Minitest/AssertMatch cop - #348

Open
bquorning wants to merge 2 commits into
masterfrom
assert-match-to-includes
Open

Add assert_includes suggestion to Minitest/AssertMatch cop#348
bquorning wants to merge 2 commits into
masterfrom
assert-match-to-includes

Conversation

@bquorning

@bquorning bquorning commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Extends the Minitest/AssertIncludes cop to suggest assert_includes when assert_match is used with simple literal regexes that don't require regex-specific features.

See also rubocop/rubocop-rspec#2169.

Changes

  • Adds detection for assert_match calls with regexes that contain only literal characters (no anchors, character classes, quantifiers, alternations, metacharacters, etc.)
  • Suggests assert_includes as a more semantic and clearer alternative for simple substring matching
  • Makes regexp_parser a direct dependency (already a transitive dependency via rubocop) to properly parse and analyze regex patterns
  • Handles escaped literals in regexes (e.g., http:\/\/example\.comhttp://example.com)
  • Converts simple regexes to their string equivalents with proper quote handling

Examples

# bad
assert_match(/foo/, 'foobar')
assert_match(/http:\/\/example\.com/, response.body)

# good
assert_includes('foobar', 'foo')
assert_includes(response.body, 'http://example.com')

# still uses assert_match (regex features needed)
assert_match(/^foo/, 'foobar')     # has anchor
assert_match(/foo\d+/, 'foobar')   # has quantifier
assert_match(/foo.bar/, 'foobar')  # has metacharacter

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

🤖 Generated with Claude Code

@bquorning
bquorning force-pushed the assert-match-to-includes branch 2 times, most recently from 5495d54 to a7b7bf7 Compare April 30, 2026 13:42
@bquorning
bquorning marked this pull request as ready for review April 30, 2026 13:43
@bquorning
bquorning requested a review from koic April 30, 2026 13:45
@bquorning
bquorning force-pushed the assert-match-to-includes branch 2 times, most recently from 9884fc0 to 2857521 Compare April 30, 2026 16:06
@bquorning

Copy link
Copy Markdown
Contributor Author

cc @koic

@bquorning
bquorning force-pushed the assert-match-to-includes branch 2 times, most recently from 0e4257f to af585bf Compare June 6, 2026 17:12
bquorning and others added 2 commits June 23, 2026 16:34
Before adding extra checks to assert_includes, it helps to unpack the
on_send method first.
Extend the AssertIncludes cop to suggest `assert_includes` instead of
`assert_match` when the regex is a simple string literal without
regex-specific features (anchors, quantifiers, character classes, etc.).

Examples:
  # bad
  assert_match(/foo/, 'foobar')
  assert_match(/http:\/\/example\.com/, response.body)

  # good
  assert_includes('foobar', 'foo')
  assert_includes(response.body, 'http://example.com')

  # still uses assert_match (has regex features)
  assert_match(/^foo/, 'foobar')      # anchor
  assert_match(/foo+/, 'foobar')      # quantifier
  assert_match(/fo[ob]/, 'foobar')    # character class

Implementation:
- Override on_send to handle both assert(collection.include?) and
  assert_match patterns
- Add regexp_parser dependency for regex analysis
- Expand RESTRICT_ON_SEND to include :assert_match
- Add helper methods to detect simple regexes and convert to strings

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@bquorning
bquorning force-pushed the assert-match-to-includes branch from af585bf to 1586af0 Compare June 23, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant