Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for assert_true and assert_false to RSpec/Rails/MinitestAssertions #1786

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master (Unreleased)

- Add support for `assert_true` and `assert_false` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath])
- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
- Support correcting some `*_predicate` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ RSpec/Rails/InferredSpecType:
views: view

RSpec/Rails/MinitestAssertions:
Description: Check if using Minitest matchers.
Description: Check if using Minitest-like matchers.
Enabled: pending
VersionAdded: '2.17'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/MinitestAssertions
Expand Down
9 changes: 8 additions & 1 deletion docs/modules/ROOT/pages/cops_rspec_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ end
| -
|===

Check if using Minitest matchers.
Check if using Minitest-like matchers.

Check the use of minitest-like matchers
starting with `assert_` or `refute_`.

=== Examples

Expand All @@ -265,6 +268,8 @@ assert_not_includes a, b
refute_equal(a, b)
assert_nil a
refute_empty(b)
assert_true(a)
ydah marked this conversation as resolved.
Show resolved Hide resolved
assert_false(a)

# good
expect(b).to eq(a)
Expand All @@ -273,6 +278,8 @@ expect(a).not_to include(b)
expect(b).not_to eq(a)
expect(a).to eq(nil)
expect(a).not_to be_empty
expect(a).to be(true)
expect(a).to be(false)
----

=== References
Expand Down
49 changes: 48 additions & 1 deletion lib/rubocop/cop/rspec/rails/minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module RuboCop
module Cop
module RSpec
module Rails
# Check if using Minitest matchers.
# Check if using Minitest-like matchers.
#
# Check the use of minitest-like matchers
# starting with `assert_` or `refute_`.
#
# @example
# # bad
Expand All @@ -14,6 +17,8 @@ module Rails
# refute_equal(a, b)
# assert_nil a
# refute_empty(b)
# assert_true(a)
# assert_false(a)
ydah marked this conversation as resolved.
Show resolved Hide resolved
#
# # good
# expect(b).to eq(a)
Expand All @@ -22,6 +27,8 @@ module Rails
# expect(b).not_to eq(a)
# expect(a).to eq(nil)
# expect(a).not_to be_empty
# expect(a).to be(true)
# expect(a).to be(false)
#
class MinitestAssertions < Base
extend AutoCorrector
Expand Down Expand Up @@ -215,6 +222,46 @@ def assertion
end
end

# :nodoc:
class TrueAssertion < BasicAssertion
MATCHERS = %i[
assert_true
].freeze

# @!method self.minitest_assertion(node)
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
(send nil? {:assert_true} $_ $_?)
PATTERN

def self.match(actual, failure_message)
new(nil, actual, failure_message.first)
end

def assertion
'be(true)'
end
end

# :nodoc:
class FalseAssertion < BasicAssertion
MATCHERS = %i[
assert_false
].freeze

# @!method self.minitest_assertion(node)
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
(send nil? {:assert_false} $_ $_?)
PATTERN

def self.match(actual, failure_message)
new(nil, actual, failure_message.first)
end

def assertion
'be(false)'
end
end

MSG = 'Use `%<prefer>s`.'

# TODO: replace with `BasicAssertion.subclasses` in Ruby 3.1+
Expand Down
94 changes: 94 additions & 0 deletions spec/rubocop/cop/rspec/rails/minitest_assertions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,100 @@
end
end

context 'with boolean assertions' do
it 'registers an offense when using `assert_true`' do
ydah marked this conversation as resolved.
Show resolved Hide resolved
expect_offense(<<~RUBY)
assert_true(a)
^^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be(true)
RUBY
end

it 'registers an offense when using `assert_true` with no parentheses' do
expect_offense(<<~RUBY)
assert_true a
^^^^^^^^^^^^^ Use `expect(a).to be(true)`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be(true)
RUBY
end

it 'registers an offense when using `assert_true` with failure message' do
expect_offense(<<~RUBY)
assert_true a, "must be true"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
RUBY

expect_correction(<<~RUBY)
expect(a).to(be(true), "must be true")
RUBY
end

it 'registers an offense when using `assert_true` with ' \
'multi-line arguments' do
expect_offense(<<~RUBY)
assert_true(a,
^^^^^^^^^^^^^^ Use `expect(a).to(be(true), "must be true")`.
"must be true")
RUBY

expect_correction(<<~RUBY)
expect(a).to(be(true), "must be true")
RUBY
end

it 'registers an offense when using `assert_false`' do
expect_offense(<<~RUBY)
assert_false(a)
^^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be(false)
RUBY
end

it 'registers an offense when using `assert_false` with no parentheses' do
expect_offense(<<~RUBY)
assert_false a
^^^^^^^^^^^^^^ Use `expect(a).to be(false)`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be(false)
RUBY
end

it 'registers an offense when using `assert_false` with failure message' do
expect_offense(<<~RUBY)
assert_false a, "must be false"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
RUBY

expect_correction(<<~RUBY)
expect(a).to(be(false), "must be false")
RUBY
end

it 'registers an offense when using `assert_false` with ' \
'multi-line arguments' do
expect_offense(<<~RUBY)
assert_false(a,
^^^^^^^^^^^^^^^ Use `expect(a).to(be(false), "must be false")`.
"must be false")
RUBY

expect_correction(<<~RUBY)
expect(a).to(be(false), "must be false")
RUBY
end
end

context 'with predicate assertions' do
it 'registers an offense when using `assert_predicate` with ' \
'an actual predicate' do
Expand Down