forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#157. This PR adds new `Minitest/EmptyLinesBeforeAssertionMethod` cop. This cop enforces empty line before assertion method. ```ruby # bad do_something assert_equal(expected, actual) # good do_something assert_equal(expected, actual) ```
- Loading branch information
Showing
6 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_new_minitest_empty_line_before_assertion_method_cop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#157](https://github.com/rubocop/rubocop-minitest/issues/157): Add new `Minitest/EmptyLinesBeforeAssertionMethod` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
lib/rubocop/cop/minitest/empty_lines_before_assertion_method.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces empty line before assertion method. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# do_something | ||
# assert_equal(expected, actual) | ||
# | ||
# # good | ||
# do_something | ||
# | ||
# assert_equal(expected, actual) | ||
# | ||
class EmptyLinesBeforeAssertionMethod < Base | ||
include MinitestExplorationHelpers | ||
extend AutoCorrector | ||
|
||
MSG = 'Add empty line before assertion.' | ||
|
||
def on_send(node) | ||
return unless assertion_method?(node) | ||
return unless (previous_line_node = node.left_sibling) | ||
return if accept_previous_line?(previous_line_node, node) | ||
|
||
previous_line_node = previous_line_node.arguments.last if use_heredoc_argument?(previous_line_node) | ||
return unless no_empty_line?(previous_line_node, node) | ||
|
||
register_offense(node, previous_line_node) | ||
end | ||
|
||
private | ||
|
||
def accept_previous_line?(previous_line_node, node) | ||
return true if previous_line_node.args_type? || node.parent.basic_conditional? | ||
|
||
previous_line_node.send_type? && assertion_method?(previous_line_node) | ||
end | ||
|
||
def use_heredoc_argument?(node) | ||
node.respond_to?(:arguments) && heredoc?(node.arguments.last) | ||
end | ||
|
||
def heredoc?(last_argument) | ||
last_argument.respond_to?(:heredoc?) && last_argument.heredoc? | ||
end | ||
|
||
def no_empty_line?(previous_line_node, node) | ||
previous_line = if heredoc?(previous_line_node) | ||
previous_line_node.loc.heredoc_end.line | ||
else | ||
previous_line_node.loc.last_line | ||
end | ||
|
||
previous_line + 1 == node.loc.line | ||
end | ||
|
||
def register_offense(node, previous_line_node) | ||
add_offense(node) do |corrector| | ||
range = if heredoc?(previous_line_node) | ||
previous_line_node.loc.heredoc_end | ||
else | ||
previous_line_node | ||
end | ||
|
||
corrector.insert_after(range, "\n") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
test/rubocop/cop/minitest/empty_lines_before_assertion_method_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class EmptyLinesBeforeAssertionMethodTest < Minitest::Test | ||
def test_registers_offense_when_using_method_call_before_assertion_method | ||
assert_offense(<<~RUBY) | ||
def test_do_something | ||
do_something | ||
assert_equal(expected, actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
def test_do_something | ||
do_something | ||
assert_equal(expected, actual) | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_method_call_with_line_breaked_args_before_assertion_method | ||
assert_offense(<<~RUBY) | ||
def test_do_something | ||
do_something( | ||
foo, | ||
bar | ||
) | ||
assert_equal(expected, actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
def test_do_something | ||
do_something( | ||
foo, | ||
bar | ||
) | ||
assert_equal(expected, actual) | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_heredoc_before_assertion_method | ||
assert_offense(<<~RUBY) | ||
def test_do_something | ||
<<~EOS | ||
text | ||
EOS | ||
assert_equal(expected, actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
def test_do_something | ||
<<~EOS | ||
text | ||
EOS | ||
assert_equal(expected, actual) | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_method_call_with_heredoc_arg_before_assertion_method | ||
assert_offense(<<~RUBY) | ||
def test_do_something | ||
do_something(<<~EOS) | ||
text | ||
EOS | ||
assert_equal(expected, actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
def test_do_something | ||
do_something(<<~EOS) | ||
text | ||
EOS | ||
assert_equal(expected, actual) | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_empty_line_before_assertion_method | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
do_something | ||
assert_equal(expected, actual) | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_method_at_top_level | ||
assert_no_offenses(<<~RUBY) | ||
assert_equal(expected, actual) | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_method_at_top_of_block_body | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
do_something do | ||
assert_equal(expected, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_method_at_top_of_if_body | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
if condition | ||
assert_equal(expected, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_method_at_top_of_while_body | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
while condition | ||
assert_equal(expected, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_method_at_top_of_until_body | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
until condition | ||
assert_equal(expected, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assertion_methods_are_continuous_without_empty_line | ||
assert_no_offenses(<<~RUBY) | ||
def test_do_something | ||
assert_not foo | ||
assert bar | ||
end | ||
RUBY | ||
end | ||
end |