Skip to content

Commit

Permalink
Merge pull request #1791 from G-Rath/support-delta
Browse files Browse the repository at this point in the history
feat: support correcting `*_in_delta` assertions in `RSpec/Rails/MinitestAssertions`
  • Loading branch information
pirj authored Feb 2, 2024
2 parents f062f92 + d528148 commit 31c6344
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
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 ' \
'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

0 comments on commit 31c6344

Please sign in to comment.