Skip to content

Commit

Permalink
[Fix #289] count asserts within blocks for assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Dec 31, 2023
1 parent 1ef32c0 commit 1cbbe83
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rubocop/cop/minitest/multiple_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ def assertions_count_based_on_type(node)
when :block, :numblock
assertions_count(node.body)
when *RuboCop::AST::Node::ASSIGNMENTS
# checking assignment bodies is handled by assertion_method?
0
assertions_count_in_assignment(node)
else
node.each_child_node.sum { |child| assertions_count(child) }
end
end

def assertions_count_in_assignment(node)
# checking the direct expression is handled by assertion_method?
return 0 unless node.expression.block_type? || node.expression.numblock_type?

# this will only trigger the branches for :block and :numblock type nodes
assertions_count_based_on_type(node.expression)
end

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

def test_assignments_are_counted_normally
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_asserts_twice
^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
_ = assert_equal(1, 2)
_ = assert_equal(1, 2)
end
end
RUBY
end

def test_assignments_are_not_counted_complex
assert_offense(<<~RUBY)
class FooTest < ActiveSupport::TestCase
Expand All @@ -140,6 +152,77 @@ class FooTest < ActiveSupport::TestCase
RUBY
end

def test_assignments_are_not_counted_but_block_bodies_still_are
assert_offense(<<~RUBY)
class FooTest < ActiveSupport::TestCase
test "#render errors include stack traces" do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [5/1].
err = assert_raises React::ServerRendering::PrerenderError do
@renderer.render("NonExistentComponent", {}, nil)
assert_equal 1, 1
assert_equal 1, 1
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_assignments_with_nested_blocks_are_counted_correctly
assert_offense(<<~RUBY)
class FooTest < ActiveSupport::TestCase
test "#render errors include stack traces" do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [9/1].
err = assert_raises React::ServerRendering::PrerenderError do
assert_equal 1, 1
assert_raises React::ServerRendering::PrerenderError do
assert_equal 1, 1
end
_ = assert_raises React::ServerRendering::PrerenderError do
assert_equal 1, 1
assert_equal 1, 1
end
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_assignments_with_numblocks_are_counted_correctly
assert_offense(<<~RUBY)
class FooTest < ActiveSupport::TestCase
test "#render errors include stack traces" do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [9/1].
err = assert_raises React::ServerRendering::PrerenderError do
assert_equal _1, 1
assert_raises React::ServerRendering::PrerenderError do
assert_equal _1, 1
end
_ = assert_raises React::ServerRendering::PrerenderError do
assert_equal _1, 1
assert_equal _1, 1
end
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 Down

0 comments on commit 1cbbe83

Please sign in to comment.