Skip to content

Commit

Permalink
Fix a false negative for Minitest/EmptyLineBeforeAssertionMethods
Browse files Browse the repository at this point in the history
Fixes part of #189.

This PR fixes a false negative for `Minitest/EmptyLineBeforeAssertionMethods`
when using non assertion method statement before assertion method used in a block.
  • Loading branch information
koic committed Nov 1, 2022
1 parent 7864020 commit 7d612d7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#189](https://github.com/rubocop/rubocop-minitest/issues/189): Fix a false negative for `Minitest/EmptyLineBeforeAssertionMethods` when using non assertion method statement before assertion method used in a block. ([@koic][])
18 changes: 13 additions & 5 deletions lib/rubocop/cop/minitest/empty_line_before_assertion_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ class EmptyLineBeforeAssertionMethods < Base
MSG = 'Add empty line before assertion.'

def on_send(node)
return unless assertion_method?(node)
return unless (previous_line_node = node.left_sibling)
return unless (assertion_method = assertion_method(node))
return unless (previous_line_node = assertion_method.left_sibling)
return unless previous_line_node.is_a?(RuboCop::AST::Node)
return if accept_previous_line?(previous_line_node, node)
return if accept_previous_line?(previous_line_node, assertion_method)

previous_line_node = previous_line_node.arguments.last if use_heredoc_argument?(previous_line_node)
return unless no_empty_line?(previous_line_node, node)
return unless no_empty_line?(previous_line_node, assertion_method)

register_offense(node, previous_line_node)
register_offense(assertion_method, previous_line_node)
end

private

def assertion_method(node)
if assertion_method?(node)
node
elsif node.parent&.block_type? && assertion_method?(node.parent.body)
node.parent
end
end

def accept_previous_line?(previous_line_node, node)
return true if previous_line_node.args_type? || node.parent.basic_conditional?

Expand Down
1 change: 1 addition & 0 deletions test/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_changelog_has_either_entries_headers_or_empty_lines

def test_changelog_has_link_definitions_for_all_implicit_links
implicit_link_names = @changelog.scan(/\[([^\]]+)\]\[\]/).flatten.uniq

implicit_link_names.each do |name|
assert_includes(
@changelog, "[#{name}]: http",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,46 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_statement_before_assertion_method_used_in_block
assert_offense(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
set.each do |thing|
^^^^^^^^^^^^^^^^^^^ Add empty line before assertion.
refute_nil(thing)
end
end
RUBY

assert_correction(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
set.each do |thing|
refute_nil(thing)
end
end
RUBY
end

def test_registers_offense_when_using_statement_before_single_line_assertion_method_used_in_block
assert_offense(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
set.each { |thing| refute_nil(thing) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion.
end
RUBY

assert_correction(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
set.each { |thing| refute_nil(thing) }
end
RUBY
end

def test_does_not_register_offense_when_using_empty_line_before_assertion_methods
assert_no_offenses(<<~RUBY)
def test_do_something
Expand Down Expand Up @@ -173,4 +213,34 @@ def test_do_something
end
RUBY
end

def test_registers_offense_when_using_assertion_method_before_assertion_method_used_in_block
assert_no_offenses(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
refute_nil(set)
set.each do |thing|
refute_nil(thing)
end
end
RUBY
end

def test_does_not_register_offense_when_using_statement_before_non_assertion_method_used_in_block
assert_no_offenses(<<~RUBY)
def test_do_something
set = Set.new([1,2,3])
set.each do |thing|
do_something(thing)
end
end
RUBY
end

def test_does_not_register_offense_when_using_only_non_assertion_method
assert_no_offenses(<<~RUBY)
do_something(thing)
RUBY
end
end

0 comments on commit 7d612d7

Please sign in to comment.