Skip to content

Commit

Permalink
Update AssertOperator and RefuteOperator to allow :[]
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Sep 26, 2023
1 parent 05da8ea commit 1a7ca4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rubocop/cop/minitest/assert_operator.rb
Expand Up @@ -18,11 +18,15 @@ class AssertOperator < Base

MSG = 'Prefer using `assert_operator(%<new_arguments>s)`.'
RESTRICT_ON_SEND = %i[assert].freeze
ALLOWED_OPERATORS = [:[]].freeze

def on_send(node)
first_argument = node.first_argument
return unless first_argument.respond_to?(:operator_method?) && first_argument.operator_method?

operator = first_argument.to_a[1]
return if ALLOWED_OPERATORS.include?(operator)

new_arguments = build_new_arguments(node)

add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector|
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/minitest/refute_operator.rb
Expand Up @@ -18,11 +18,15 @@ class RefuteOperator < Base

MSG = 'Prefer using `refute_operator(%<new_arguments>s)`.'
RESTRICT_ON_SEND = %i[refute].freeze
ALLOWED_OPERATORS = [:[]].freeze

def on_send(node)
first_argument = node.first_argument
return unless first_argument.respond_to?(:operator_method?) && first_argument.operator_method?

operator = first_argument.to_a[1]
return if ALLOWED_OPERATORS.include?(operator)

new_arguments = build_new_arguments(node)

add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector|
Expand Down
10 changes: 10 additions & 0 deletions test/rubocop/cop/minitest/assert_operator_test.rb
Expand Up @@ -81,4 +81,14 @@ def test_do_something
end
RUBY
end

def test_does_not_consider_brackets_to_be_an_offense
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert(array_of_booleans[42])
end
end
RUBY
end
end
10 changes: 10 additions & 0 deletions test/rubocop/cop/minitest/refute_operator_test.rb
Expand Up @@ -81,4 +81,14 @@ def test_do_something
end
RUBY
end

def test_does_not_consider_brackets_to_be_an_offense
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(array_of_booleans[42])
end
end
RUBY
end
end

0 comments on commit 1a7ca4d

Please sign in to comment.