/
rspec_examples_in_same_group.rb
114 lines (99 loc) · 3.2 KB
/
rspec_examples_in_same_group.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true
module RuboCop
module Cop
module Sevencop
# Combine examples in same group in the time-consuming kinds of specs.
#
# @example
# # bad
# context 'when user is logged in' do
# it 'returns 200' do
# subject
# expect(response).to have_http_status(200)
# end
#
# it 'creates Foo' do
# expect { subject }.to change(Foo, :count).by(1)
# end
# end
#
# # good
# context 'when user is logged in' do
# it 'creates Foo and returns 200' do
# expect { subject }.to change(Foo, :count).by(1)
# expect(response).to have_http_status(200)
# end
# end
#
# # bad - IncludeSharedExamples: true
# context 'when user is logged in' do
# it 'returns 200' do
# subject
# expect(response).to have_http_status(200)
# end
#
# includes_examples 'creates Foo'
# end
class RSpecExamplesInSameGroup < Base
METHOD_NAMES_FOR_REGULAR_EXAMPLE = %i[
example
it
its
scenario
specify
].to_set.freeze
METHOD_NAMES_FOR_SHARED_EXAMPLES = %i[
include_examples
it_behaves_like
it_should_behave_like
].to_set.freeze
MSG = 'Combine examples in the same group in the time-consuming kinds of specs.'
RESTRICT_ON_SEND = [
*METHOD_NAMES_FOR_REGULAR_EXAMPLE,
*METHOD_NAMES_FOR_SHARED_EXAMPLES
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
node = node.block_node || node
return unless example?(node)
previous_sibling_example = previous_sibling_example_of(node)
return unless previous_sibling_example
add_offense(node)
end
private
# @param node [RuboCop::AST::BlockNode, RuboCop::AST::SendNode]
# @return [Boolean]
def example?(node)
if include_shared_examples?
regular_example?(node) || shared_example?(node)
else
regular_example?(node)
end
end
# @return [Boolean]
def include_shared_examples?
cop_config['IncludeSharedExamples']
end
# @param node [RuboCop::AST::BlockNode, RuboCop::AST::SendNode]
# @return [RuboCop::AST::BlockNode, RuboCop::AST::SendNode, nil]
def previous_sibling_example_of(node)
return unless node.parent&.begin_type?
node.left_siblings.find do |sibling|
sibling.is_a?(::RuboCop::AST::Node) && example?(sibling)
end
end
# @param node [RuboCop::AST::BlockNode, RuboCop::AST::SendNode]
# @return [Boolean]
def regular_example?(node)
METHOD_NAMES_FOR_REGULAR_EXAMPLE.include?(node.method_name)
end
# @param node [RuboCop::AST::BlockNode, RuboCop::AST::SendNode]
# @return [Boolean]
def shared_example?(node)
METHOD_NAMES_FOR_SHARED_EXAMPLES.include?(node.method_name)
end
end
end
end
end