forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
non_executable_test_method.rb
51 lines (44 loc) · 1.45 KB
/
non_executable_test_method.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
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Checks for the use of test methods outside of a test class.
#
# Test methods should be defined within a test class to ensure their execution.
#
# NOTE: This cop assumes that classes whose superclass name includes the word
# "`Test`" are test classes, in order to prevent false positives.
#
# @example
#
# # bad
# class FooTest < Minitest::Test
# end
# def test_method_should_be_inside_test_class
# end
#
# # good
# class FooTest < Minitest::Test
# def test_method_should_be_inside_test_class
# end
# end
#
class NonExecutableTestMethod < Base
include MinitestExplorationHelpers
MSG = 'Test method should be defined inside a test class to ensure execution.'
def on_def(node)
return if !test_method?(node) || !use_test_class?
return if node.left_siblings.none? { |sibling| possible_test_class?(sibling) }
add_offense(node)
end
def use_test_class?
root_node = processed_source.ast
root_node.each_descendant(:class).any? { |class_node| test_class?(class_node) }
end
def possible_test_class?(node)
node.is_a?(AST::ClassNode) && test_class?(node) && node.parent_class.source.include?('Test')
end
end
end
end
end