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] count asserts within blocks for assignments #291

Merged
merged 1 commit into from
Jan 1, 2024
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
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