Skip to content

Commit 612fd64

Browse files
committed
Add support for assert_true and assert_false to RSpec/Rails/MinitestAssertions
1 parent 89a45c1 commit 612fd64

File tree

5 files changed

+152
-3
lines changed

5 files changed

+152
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Master (Unreleased)
44

5+
- Add support for `assert_true` and `assert_false` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
56
- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath])
67
- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
78
- Support correcting some `*_predicate` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])

config/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ RSpec/Rails/InferredSpecType:
11721172
views: view
11731173

11741174
RSpec/Rails/MinitestAssertions:
1175-
Description: Check if using Minitest matchers.
1175+
Description: Check if using Minitest-like matchers.
11761176
Enabled: pending
11771177
VersionAdded: '2.17'
11781178
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/MinitestAssertions

docs/modules/ROOT/pages/cops_rspecrails.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ end
252252
| -
253253
|===
254254
255-
Check if using Minitest matchers.
255+
Check if using Minitest-like matchers.
256+
257+
Check the use of minitest-like matchers
258+
starting with `assert_` or `refute_`.
256259
257260
=== Examples
258261
@@ -265,6 +268,8 @@ assert_not_includes a, b
265268
refute_equal(a, b)
266269
assert_nil a
267270
refute_empty(b)
271+
assert_true(a)
272+
assert_false(a)
268273
269274
# good
270275
expect(b).to eq(a)
@@ -273,6 +278,8 @@ expect(a).not_to include(b)
273278
expect(b).not_to eq(a)
274279
expect(a).to eq(nil)
275280
expect(a).not_to be_empty
281+
expect(a).to be(true)
282+
expect(a).to be(false)
276283
----
277284
278285
=== References

lib/rubocop/cop/rspec_rails/minitest_assertions.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ module RuboCop
44
module Cop
55
module RSpec
66
module Rails
7-
# Check if using Minitest matchers.
7+
# Check if using Minitest-like matchers.
8+
#
9+
# Check the use of minitest-like matchers
10+
# starting with `assert_` or `refute_`.
811
#
912
# @example
1013
# # bad
@@ -14,6 +17,8 @@ module Rails
1417
# refute_equal(a, b)
1518
# assert_nil a
1619
# refute_empty(b)
20+
# assert_true(a)
21+
# assert_false(a)
1722
#
1823
# # good
1924
# expect(b).to eq(a)
@@ -22,6 +27,8 @@ module Rails
2227
# expect(b).not_to eq(a)
2328
# expect(a).to eq(nil)
2429
# expect(a).not_to be_empty
30+
# expect(a).to be(true)
31+
# expect(a).to be(false)
2532
#
2633
class MinitestAssertions < Base
2734
extend AutoCorrector
@@ -215,6 +222,46 @@ def assertion
215222
end
216223
end
217224

225+
# :nodoc:
226+
class TrueAssertion < BasicAssertion
227+
MATCHERS = %i[
228+
assert_true
229+
].freeze
230+
231+
# @!method self.minitest_assertion(node)
232+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
233+
(send nil? {:assert_true} $_ $_?)
234+
PATTERN
235+
236+
def self.match(actual, failure_message)
237+
new(nil, actual, failure_message.first)
238+
end
239+
240+
def assertion
241+
'be(true)'
242+
end
243+
end
244+
245+
# :nodoc:
246+
class FalseAssertion < BasicAssertion
247+
MATCHERS = %i[
248+
assert_false
249+
].freeze
250+
251+
# @!method self.minitest_assertion(node)
252+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
253+
(send nil? {:assert_false} $_ $_?)
254+
PATTERN
255+
256+
def self.match(actual, failure_message)
257+
new(nil, actual, failure_message.first)
258+
end
259+
260+
def assertion
261+
'be(false)'
262+
end
263+
end
264+
218265
MSG = 'Use `%<prefer>s`.'
219266

220267
# TODO: replace with `BasicAssertion.subclasses` in Ruby 3.1+

spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,100 @@
508508
end
509509
end
510510

511+
context 'with boolean assertions' do
512+
it 'registers an offense when using `assert_true`' do
513+
expect_offense(<<~RUBY)
514+
assert_true(a)
515+
^^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
516+
RUBY
517+
518+
expect_correction(<<~RUBY)
519+
expect(a).to be(true)
520+
RUBY
521+
end
522+
523+
it 'registers an offense when using `assert_true` with no parentheses' do
524+
expect_offense(<<~RUBY)
525+
assert_true a
526+
^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
527+
RUBY
528+
529+
expect_correction(<<~RUBY)
530+
expect(a).to be(true)
531+
RUBY
532+
end
533+
534+
it 'registers an offense when using `assert_true` with failure message' do
535+
expect_offense(<<~RUBY)
536+
assert_true a, "must be true"
537+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
538+
RUBY
539+
540+
expect_correction(<<~RUBY)
541+
expect(a).to(be(true), "must be true")
542+
RUBY
543+
end
544+
545+
it 'registers an offense when using `assert_true` with ' \
546+
'multi-line arguments' do
547+
expect_offense(<<~RUBY)
548+
assert_true(a,
549+
^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
550+
"must be true")
551+
RUBY
552+
553+
expect_correction(<<~RUBY)
554+
expect(a).to(be(true), "must be true")
555+
RUBY
556+
end
557+
558+
it 'registers an offense when using `assert_false`' do
559+
expect_offense(<<~RUBY)
560+
assert_false(a)
561+
^^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
562+
RUBY
563+
564+
expect_correction(<<~RUBY)
565+
expect(a).to be(false)
566+
RUBY
567+
end
568+
569+
it 'registers an offense when using `assert_false` with no parentheses' do
570+
expect_offense(<<~RUBY)
571+
assert_false a
572+
^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
573+
RUBY
574+
575+
expect_correction(<<~RUBY)
576+
expect(a).to be(false)
577+
RUBY
578+
end
579+
580+
it 'registers an offense when using `assert_false` with failure message' do
581+
expect_offense(<<~RUBY)
582+
assert_false a, "must be false"
583+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
584+
RUBY
585+
586+
expect_correction(<<~RUBY)
587+
expect(a).to(be(false), "must be false")
588+
RUBY
589+
end
590+
591+
it 'registers an offense when using `assert_false` with ' \
592+
'multi-line arguments' do
593+
expect_offense(<<~RUBY)
594+
assert_false(a,
595+
^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
596+
"must be false")
597+
RUBY
598+
599+
expect_correction(<<~RUBY)
600+
expect(a).to(be(false), "must be false")
601+
RUBY
602+
end
603+
end
604+
511605
context 'with predicate assertions' do
512606
it 'registers an offense when using `assert_predicate` with ' \
513607
'an actual predicate' do

0 commit comments

Comments
 (0)