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

feat: support correcting *_in_delta assertions in RSpec/Rails/MinitestAssertions #1791

Merged
merged 3 commits into from
Feb 2, 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 @@ -5,6 +5,7 @@
- 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])
- Support correcting `*_in_delta` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_match` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_includes` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
Expand Down
28 changes: 28 additions & 0 deletions lib/rubocop/cop/rspec/rails/minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,34 @@ def assertion
end
end

# :nodoc:
class InDeltaAssertion < BasicAssertion
MATCHERS = %i[
assert_in_delta
assert_not_in_delta
refute_in_delta
].freeze

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

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

def initialize(expected, actual, delta, fail_message)
super(expected, actual, fail_message)

@delta = delta&.source || '0.001'
end

def assertion
"be_within(#{@delta}).of(#{expected})"
end
end

# :nodoc:
class PredicateAssertion < BasicAssertion
MATCHERS = %i[
Expand Down
115 changes: 115 additions & 0 deletions spec/rubocop/cop/rspec/rails/minitest_assertions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,121 @@
end
end

context 'with in_delta assertions' do
it 'registers an offense when using `assert_in_delta`' do
expect_offense(<<~RUBY)
assert_in_delta(a, b)
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_within(0.001).of(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).to be_within(0.001).of(a)
RUBY
end

it 'registers an offense when using `assert_in_delta` with ' \
'no parentheses' do
expect_offense(<<~RUBY)
assert_in_delta a, b
^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_within(0.001).of(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).to be_within(0.001).of(a)
RUBY
end

it 'registers an offense when using `assert_in_delta` with ' \
'a custom delta' do
expect_offense(<<~RUBY)
assert_in_delta a, b, 1
^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_within(1).of(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).to be_within(1).of(a)
RUBY
end

it 'registers an offense when using `assert_in_delta` with ' \
'a custom delta from a variable' do
expect_offense(<<~RUBY)
delta = 1

assert_in_delta a, b, delta
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_within(delta).of(a)`.
RUBY

expect_correction(<<~RUBY)
delta = 1

expect(b).to be_within(delta).of(a)
RUBY
end

it 'registers an offense when using `assert_in_delta` with ' \
'a custom delta and a failure message' do
expect_offense(<<~RUBY)
assert_in_delta a, b, 1, "must be within delta"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(be_within(1).of(a), "must be within delta")`.
RUBY

expect_correction(<<~RUBY)
expect(b).to(be_within(1).of(a), "must be within delta")
RUBY
end

it 'registers an offense when using `assert_in_delta` with ' \
pirj marked this conversation as resolved.
Show resolved Hide resolved
'multi-line arguments' do
expect_offense(<<~RUBY)
assert_in_delta(a,
^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_within(1).of(a)`.
b,
1)
RUBY

expect_correction(<<~RUBY)
expect(b).to be_within(1).of(a)
RUBY
end

it 'registers an offense when using `assert_not_in_delta`' do
expect_offense(<<~RUBY)
assert_not_in_delta a, b
^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to be_within(0.001).of(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).not_to be_within(0.001).of(a)
RUBY
end

it 'registers an offense when using `refute_in_delta`' do
expect_offense(<<~RUBY)
refute_in_delta a, b
^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to be_within(0.001).of(a)`.
RUBY

expect_correction(<<~RUBY)
expect(b).not_to be_within(0.001).of(a)
RUBY
end

it 'does not register an offense when ' \
'using `expect(b).to be_within(1).of(a)`' do
expect_no_offenses(<<~RUBY)
expect(b).to be_within(1).of(a)
RUBY
end

it 'does not register an offense when ' \
'using `expect(b).not_to be_within(1).of(a)`' do
expect_no_offenses(<<~RUBY)
expect(b).not_to be_within(1).of(a)
RUBY
end
end

context 'with match assertions' do
it 'registers an offense when using `assert_match`' do
expect_offense(<<~RUBY)
Expand Down