forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_method_name.rb
69 lines (58 loc) · 1.73 KB
/
test_method_name.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
64
65
66
67
68
69
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Enforces that test method names start with `test_` prefix.
# It aims to prevent tests that aren't executed by forgetting to start test method name with `test_`.
#
# @example
# # bad
# class FooTest < Minitest::Test
# def does_something
# assert_equal 42, do_something
# end
# end
#
# # good
# class FooTest < Minitest::Test
# def test_does_something
# assert_equal 42, do_something
# end
# end
#
# # good
# class FooTest < Minitest::Test
# def helper_method(argument)
# end
# end
#
class TestMethodName < Base
include MinitestExplorationHelpers
include DefNode
extend AutoCorrector
MSG = 'Test method name should start with `test_` prefix.'
def on_class(class_node)
return unless test_class?(class_node)
class_def_nodes(class_node).each do |node|
next unless offense?(node)
test_method_name = node.loc.name
add_offense(test_method_name) do |corrector|
corrector.replace(test_method_name, "test_#{node.method_name}")
end
end
end
private
def offense?(node)
return false if assertions(node).none?
public?(node) && node.arguments.empty? && !test_method_name?(node) && !lifecycle_hook_method?(node)
end
def public?(node)
!non_public?(node)
end
def test_method_name?(node)
node.method_name.to_s.start_with?('test_')
end
end
end
end
end