Skip to content

Commit 6c2b73c

Browse files
authored
Merge pull request #1786 from rubocop/assert_true_or_false
Add support for `assert_true` and `assert_false` to `RSpec/Rails/MinitestAssertions`
2 parents 5731d54 + 612fd64 commit 6c2b73c

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
@@ -243,6 +250,46 @@ def assertion
243250
end
244251
end
245252

253+
# :nodoc:
254+
class TrueAssertion < BasicAssertion
255+
MATCHERS = %i[
256+
assert_true
257+
].freeze
258+
259+
# @!method self.minitest_assertion(node)
260+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
261+
(send nil? {:assert_true} $_ $_?)
262+
PATTERN
263+
264+
def self.match(actual, failure_message)
265+
new(nil, actual, failure_message.first)
266+
end
267+
268+
def assertion
269+
'be(true)'
270+
end
271+
end
272+
273+
# :nodoc:
274+
class FalseAssertion < BasicAssertion
275+
MATCHERS = %i[
276+
assert_false
277+
].freeze
278+
279+
# @!method self.minitest_assertion(node)
280+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
281+
(send nil? {:assert_false} $_ $_?)
282+
PATTERN
283+
284+
def self.match(actual, failure_message)
285+
new(nil, actual, failure_message.first)
286+
end
287+
288+
def assertion
289+
'be(false)'
290+
end
291+
end
292+
246293
MSG = 'Use `%<prefer>s`.'
247294

248295
# 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
@@ -623,6 +623,100 @@
623623
end
624624
end
625625

626+
context 'with boolean assertions' do
627+
it 'registers an offense when using `assert_true`' do
628+
expect_offense(<<~RUBY)
629+
assert_true(a)
630+
^^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
631+
RUBY
632+
633+
expect_correction(<<~RUBY)
634+
expect(a).to be(true)
635+
RUBY
636+
end
637+
638+
it 'registers an offense when using `assert_true` with no parentheses' do
639+
expect_offense(<<~RUBY)
640+
assert_true a
641+
^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
642+
RUBY
643+
644+
expect_correction(<<~RUBY)
645+
expect(a).to be(true)
646+
RUBY
647+
end
648+
649+
it 'registers an offense when using `assert_true` with failure message' do
650+
expect_offense(<<~RUBY)
651+
assert_true a, "must be true"
652+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
653+
RUBY
654+
655+
expect_correction(<<~RUBY)
656+
expect(a).to(be(true), "must be true")
657+
RUBY
658+
end
659+
660+
it 'registers an offense when using `assert_true` with ' \
661+
'multi-line arguments' do
662+
expect_offense(<<~RUBY)
663+
assert_true(a,
664+
^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
665+
"must be true")
666+
RUBY
667+
668+
expect_correction(<<~RUBY)
669+
expect(a).to(be(true), "must be true")
670+
RUBY
671+
end
672+
673+
it 'registers an offense when using `assert_false`' do
674+
expect_offense(<<~RUBY)
675+
assert_false(a)
676+
^^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
677+
RUBY
678+
679+
expect_correction(<<~RUBY)
680+
expect(a).to be(false)
681+
RUBY
682+
end
683+
684+
it 'registers an offense when using `assert_false` with no parentheses' do
685+
expect_offense(<<~RUBY)
686+
assert_false a
687+
^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
688+
RUBY
689+
690+
expect_correction(<<~RUBY)
691+
expect(a).to be(false)
692+
RUBY
693+
end
694+
695+
it 'registers an offense when using `assert_false` with failure message' do
696+
expect_offense(<<~RUBY)
697+
assert_false a, "must be false"
698+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
699+
RUBY
700+
701+
expect_correction(<<~RUBY)
702+
expect(a).to(be(false), "must be false")
703+
RUBY
704+
end
705+
706+
it 'registers an offense when using `assert_false` with ' \
707+
'multi-line arguments' do
708+
expect_offense(<<~RUBY)
709+
assert_false(a,
710+
^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
711+
"must be false")
712+
RUBY
713+
714+
expect_correction(<<~RUBY)
715+
expect(a).to(be(false), "must be false")
716+
RUBY
717+
end
718+
end
719+
626720
context 'with predicate assertions' do
627721
it 'registers an offense when using `assert_predicate` with ' \
628722
'an actual predicate' do

0 commit comments

Comments
 (0)