Skip to content

Commit

Permalink
Merge pull request #290 from G-Rath/fix-assertion-count
Browse files Browse the repository at this point in the history
[Fix #289] don't count assertions twice when their return value is being assigned
  • Loading branch information
koic committed Dec 31, 2023
2 parents d0b6912 + c1a5405 commit 1ef32c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
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

0 comments on commit 1ef32c0

Please sign in to comment.