forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpending_without_reason.rb
159 lines (141 loc) · 4.43 KB
/
pending_without_reason.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for pending or skipped examples without reason.
#
# @example
# # bad
# pending 'does something' do
# end
#
# # bad
# it 'does something', :pending do
# end
#
# # bad
# it 'does something' do
# pending
# end
#
# # bad
# xdescribe 'something' do
# end
#
# # bad
# skip 'does something' do
# end
#
# # bad
# it 'does something', :skip do
# end
#
# # bad
# it 'does something' do
# skip
# end
#
# # bad
# it 'does something'
#
# # good
# it 'does something' do
# pending 'reason'
# end
#
# # good
# it 'does something' do
# skip 'reason'
# end
#
# # good
# it 'does something', pending: 'reason' do
# end
#
# # good
# it 'does something', skip: 'reason' do
# end
class PendingWithoutReason < Base
MSG = 'Give the reason for pending or skip.'
# @!method skipped_in_example?(node)
def_node_matcher :skipped_in_example?, <<~PATTERN
{
(send nil? ${#Examples.skipped #Examples.pending})
(block (send nil? ${#Examples.skipped}) ...)
(numblock (send nil? ${#Examples.skipped}) ...)
}
PATTERN
# @!method skipped_by_example_method?(node)
def_node_matcher :skipped_by_example_method?, <<~PATTERN
(send nil? ${#Examples.skipped #Examples.pending})
PATTERN
# @!method skipped_by_example_method_with_block?(node)
def_node_matcher :skipped_by_example_method_with_block?, <<~PATTERN
({block numblock} (send nil? ${#Examples.skipped #Examples.pending} ...) ...)
PATTERN
# @!method metadata_without_reason?(node)
def_node_matcher :metadata_without_reason?, <<~PATTERN
(send #rspec?
{#ExampleGroups.all #Examples.all} ...
{
<(sym ${:pending :skip}) ...>
(hash <(pair (sym ${:pending :skip}) true) ...>)
}
)
PATTERN
# @!method skipped_by_example_group_method?(node)
def_node_matcher :skipped_by_example_group_method?, <<~PATTERN
(send #rspec? ${#ExampleGroups.skipped} ...)
PATTERN
# @!method pending_step_without_reason?(node)
def_node_matcher :pending_step_without_reason?, <<~PATTERN
(send nil? {:skip :pending})
PATTERN
def on_send(node)
on_pending_by_metadata(node)
return unless (parent = parent_node(node))
if spec_group?(parent) || block_node_example_group?(node)
on_skipped_by_example_method(node)
on_skipped_by_example_group_method(node)
elsif example?(parent)
on_skipped_by_in_example_method(node)
end
end
private
def parent_node(node)
node_or_block = node.block_node || node
return unless (parent = node_or_block.parent)
parent.begin_type? && parent.parent ? parent.parent : parent
end
def block_node_example_group?(node)
node.block_node &&
example_group?(node.block_node) &&
explicit_rspec?(node.receiver)
end
def on_skipped_by_in_example_method(node)
skipped_in_example?(node) do |pending|
add_offense(node, message: "Give the reason for #{pending}.")
end
end
def on_pending_by_metadata(node)
metadata_without_reason?(node) do |pending|
add_offense(node, message: "Give the reason for #{pending}.")
end
end
def on_skipped_by_example_method(node)
skipped_by_example_method?(node) do |pending|
add_offense(node, message: "Give the reason for #{pending}.")
end
skipped_by_example_method_with_block?(node.parent) do |pending|
add_offense(node, message: "Give the reason for #{pending}.")
end
end
def on_skipped_by_example_group_method(node)
skipped_by_example_group_method?(node) do
add_offense(node, message: 'Give the reason for skip.')
end
end
end
end
end
end