Skip to content

Commit 1af3602

Browse files
authored
Merge pull request #1783 from G-Rath/support-assert-match
feat: support "match" assertions in `RSpec/Rails/MinitestAssertions`
2 parents d6c7d87 + c5a8916 commit 1af3602

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
6+
- Support correcting `*_match` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
67
- Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
78
- Support correcting `*_includes` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
89
- Support correcting `assert_not_equal` and `assert_not_nil` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])

lib/rubocop/cop/rspec_rails/minitest_assertions.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MinitestAssertions < Base
3434
assert_not_instance_of
3535
assert_includes
3636
assert_not_includes
37+
assert_match
3738
assert_nil
3839
assert_not_nil
3940
assert_empty
@@ -43,6 +44,7 @@ class MinitestAssertions < Base
4344
refute_includes
4445
refute_nil
4546
refute_empty
47+
refute_match
4648
].freeze
4749

4850
# @!method minitest_equal(node)
@@ -60,6 +62,11 @@ class MinitestAssertions < Base
6062
(send nil? {:assert_includes :assert_not_includes :refute_includes} $_ $_ $_?)
6163
PATTERN
6264

65+
# @!method minitest_match(node)
66+
def_node_matcher :minitest_match, <<~PATTERN
67+
(send nil? {:assert_match :refute_match} $_ $_ $_?)
68+
PATTERN
69+
6370
# @!method minitest_nil(node)
6471
def_node_matcher :minitest_nil, <<~PATTERN
6572
(send nil? {:assert_nil :assert_not_nil :refute_nil} $_ $_?)
@@ -86,6 +93,11 @@ def on_send(node) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
8693
failure_message.first))
8794
end
8895

96+
minitest_match(node) do |matcher, actual, failure_message|
97+
on_assertion(node, MatchAssertion.new(matcher, actual,
98+
failure_message.first))
99+
end
100+
89101
minitest_nil(node) do |actual, failure_message|
90102
on_assertion(node, NilAssertion.new(nil, actual,
91103
failure_message.first))
@@ -159,6 +171,17 @@ def assertion
159171
end
160172
end
161173

174+
# :nodoc:
175+
class MatchAssertion < BasicAssertion
176+
def negated?(node)
177+
!node.method?(:assert_match)
178+
end
179+
180+
def assertion
181+
"match(#{@expected})"
182+
end
183+
end
184+
162185
# :nodoc:
163186
class NilAssertion < BasicAssertion
164187
def negated?(node)

spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,79 @@
254254
end
255255
end
256256

257+
context 'with match assertions' do
258+
it 'registers an offense when using `assert_match`' do
259+
expect_offense(<<~RUBY)
260+
assert_match(/xyz/, b)
261+
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to match(/xyz/)`.
262+
RUBY
263+
264+
expect_correction(<<~RUBY)
265+
expect(b).to match(/xyz/)
266+
RUBY
267+
end
268+
269+
it 'registers an offense when using `assert_match` with no parentheses' do
270+
expect_offense(<<~RUBY)
271+
assert_match /xyz/, b
272+
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to match(/xyz/)`.
273+
RUBY
274+
275+
expect_correction(<<~RUBY)
276+
expect(b).to match(/xyz/)
277+
RUBY
278+
end
279+
280+
it 'registers an offense when using `assert_match` with failure message' do
281+
expect_offense(<<~RUBY)
282+
assert_match /xyz/, b, "must match"
283+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(match(/xyz/), "must match")`.
284+
RUBY
285+
286+
expect_correction(<<~RUBY)
287+
expect(b).to(match(/xyz/), "must match")
288+
RUBY
289+
end
290+
291+
it 'registers an offense when using `assert_match` with ' \
292+
'multi-line arguments' do
293+
expect_offense(<<~RUBY)
294+
assert_match(/xyz/,
295+
^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(match(/xyz/), "must match")`.
296+
b,
297+
"must match")
298+
RUBY
299+
300+
expect_correction(<<~RUBY)
301+
expect(b).to(match(/xyz/), "must match")
302+
RUBY
303+
end
304+
305+
it 'registers an offense when using `refute_match`' do
306+
expect_offense(<<~RUBY)
307+
refute_match /xyz/, b
308+
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to match(/xyz/)`.
309+
RUBY
310+
311+
expect_correction(<<~RUBY)
312+
expect(b).not_to match(/xyz/)
313+
RUBY
314+
end
315+
316+
it 'does not register an offense when using `expect(b).to match(/xyz/)`' do
317+
expect_no_offenses(<<~RUBY)
318+
expect(b).to match(/xyz/)
319+
RUBY
320+
end
321+
322+
it 'does not register an offense when ' \
323+
'using `expect(b).not_to match(/xyz/)`' do
324+
expect_no_offenses(<<~RUBY)
325+
expect(b).not_to match(/xyz/)
326+
RUBY
327+
end
328+
end
329+
257330
context 'with nil assertions' do
258331
it 'registers an offense when using `assert_nil`' do
259332
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)