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

[Fix #289] don't count assertions twice when their return value is being assigned #290

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_don_t_count_assertions_twice_when_their.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#289](https://github.com/rubocop/rubocop-minitest/issues/289): Don't count assertions twice when their return value is being assigned. ([@G-Rath][])
29 changes: 17 additions & 12 deletions lib/rubocop/cop/minitest/multiple_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,27 @@ def on_class(class_node)
def assertions_count(node)
return 0 unless node.is_a?(RuboCop::AST::Node)

assertions =
case node.type
when :if, :case, :case_match
assertions_count_in_branches(node.branches)
when :rescue
assertions_count(node.body) + assertions_count_in_branches(node.branches)
when :block, :numblock
assertions_count(node.body)
else
node.each_child_node.sum { |child| assertions_count(child) }
end

assertions = assertions_count_based_on_type(node)
assertions += 1 if assertion_method?(node)
assertions
end

def assertions_count_based_on_type(node)
case node.type
when :if, :case, :case_match
assertions_count_in_branches(node.branches)
when :rescue
assertions_count(node.body) + assertions_count_in_branches(node.branches)
when :block, :numblock
assertions_count(node.body)
when *RuboCop::AST::Node::ASSIGNMENTS
# checking assignment bodies is handled by assertion_method?
0
else
node.each_child_node.sum { |child| assertions_count(child) }
end
end

def assertions_count_in_branches(branches)
branches.map { |branch| assertions_count(branch) }.max
end
Expand Down
40 changes: 40 additions & 0 deletions test/rubocop/cop/minitest/multiple_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ def test_asserts_once
RUBY
end

def test_assignments_are_not_counted_twice
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_asserts_once
_ = assert_equal(1, 2)
end
end
RUBY
end

def test_assignments_are_not_counted_complex
assert_offense(<<~RUBY)
class FooTest < ActiveSupport::TestCase
test "#render errors include stack traces" do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [3/1].
err = assert_raises React::ServerRendering::PrerenderError do
@renderer.render("NonExistentComponent", {}, nil)
end

assert_match(/NonExistentComponent/, err.to_s, "it names the component")

assert_match(/\n/, err.to_s, "it includes the multi-line backtrace")
end
end
RUBY
end

def test_does_not_register_offense_when_using_or_assigning_a_value_to_an_object_attribute
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
Expand All @@ -125,6 +152,19 @@ def test_asserts_once
RUBY
end

def test_does_not_register_offense_when_using_or_assigning_an_assertion_return_value_to_an_object_attribute
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_asserts_once
^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
var ||= assert_equal(1, 2)

assert_equal(foo, bar)
end
end
RUBY
end

def test_generates_a_todo_based_on_the_worst_violation
inspect_source(<<-RUBY, @cop, 'test/foo_test.rb')
class FooTest < Minitest::Test
Expand Down