forked from rubocop/rubocop-minitest
/
multiple_assertions.rb
63 lines (53 loc) · 1.61 KB
/
multiple_assertions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# This cop checks if test cases contain too many assertion calls.
# The maximum allowed assertion calls is configurable.
#
# @example Max: 1
# # bad
# class FooTest < Minitest::Test
# def test_asserts_twice
# assert_equal(42, do_something)
# assert_empty(array)
# end
# end
#
# # good
# class FooTest < Minitest::Test
# def test_asserts_once
# assert_equal(42, do_something)
# end
#
# def test_another_asserts_once
# assert_empty(array)
# end
# end
#
class MultipleAssertions < Base
include ConfigurableMax
include MinitestExplorationHelpers
MSG = 'Test case has too many assertions [%<total>d/%<max>d].'
def on_class(class_node)
return unless test_class?(class_node)
test_cases(class_node).each do |node|
assertions_count = assertions_count(node)
next unless assertions_count > max_assertions
self.max = assertions_count
message = format(MSG, total: assertions_count, max: max_assertions)
add_offense(node.loc.name, message: message)
end
end
private
def assertions_count(node)
base = assertion?(node) ? 1 : 0
base + node.each_child_node.sum { |c| assertions_count(c) }
end
def max_assertions
Integer(cop_config.fetch('Max', 3))
end
end
end
end
end