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

Minitest/RefuteInDelta (& Minitest/AssertInDelta?) not safe for autocorrect #62

Open
MatzFan opened this issue Aug 12, 2024 · 0 comments

Comments

@MatzFan
Copy link

MatzFan commented Aug 12, 2024

.rubocop.yml

---
require:
  - rubocop-minitest

AllCops:
  NewCops: enable

test/test_minitest_assert_in_delta.rb

# frozen_string_literal: true

require 'minitest/autorun'

# test
class AssertInDeltaTest < Minitest::Test
  def test_assert_in_delta_cop
    refute_equal(4.2, 420_001 / 100_000.0)
  end
end

$ ruby test/test_minitest_assert_in_delta.rb
=> pass

$ rubocop

Offenses:

test/test_minitest_assert_in_delta.rb:10:5: C: [Correctable] Minitest/RefuteInDelta: Prefer using refute_in_delta(4.2, 420_001 / 100_000.0).
    refute_equal(4.2, 420_001 / 100_000.0)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

$ rubocop -a
=> autocorrected

$ ruby test/test_minitest_assert_in_delta.rb
=> fail

Fails because the default delta value for refute_in_delta is 0.001. As Rubocop can never know the user's intended delta value this cop is not safe for autocorrect.

Don't know whether you'd consider Minitest/RefuteInDelta unsafe also, as if you replace the assertion in the above example with assert_equal, autocorrect turns a failing test into one that passes..

I'd also suggest the suggestion text for both cops use the good code examples where an explicit delta value is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant