-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
node_matcher_directive_spec.rb
226 lines (193 loc) · 6.7 KB
/
node_matcher_directive_spec.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::InternalAffairs::NodeMatcherDirective, :config do
%i[def_node_matcher def_node_search].each do |method|
it 'does not register an offense if the node matcher already has a directive' do
expect_no_offenses(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, '(str)'
RUBY
end
it 'does not register an offense if the directive is in a comment block' do
expect_no_offenses(<<~RUBY)
# @!method foo?(node)
# foo? matcher
#{method} :foo?, '(str)'
RUBY
end
it 'registers an offense if the matcher does not have a directive' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, '(str)'
RUBY
end
it 'registers an offense if the matcher does not have a directive and a method call is used for a pattern argument' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, format(PATTERN, type: 'const')
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, format(PATTERN, type: 'const')
RUBY
end
it 'registers an offense if the matcher does not have a directive but has preceeding comments' do
expect_offense(<<~RUBY, method: method)
# foo
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
# foo bar baz
# foo bar baz
# foo bar baz
#{method} :bar?, '(sym)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# foo
# @!method foo?(node)
#{method} :foo?, '(str)'
# foo bar baz
# foo bar baz
# foo bar baz
# @!method bar?(node)
#{method} :bar?, '(sym)'
RUBY
end
it 'registers an offense if the directive name does not match the actual name' do
expect_offense(<<~RUBY, method: method)
# @!method bar?(node)
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ `@!method` YARD directive has invalid method name, use `foo?` instead of `bar?`.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, '(str)'
RUBY
end
it 'registers an offense if the matcher has multiple directives' do
expect_offense(<<~RUBY, method: method)
# @!method foo?(node)
# @!method foo?(node)
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ Multiple `@!method` YARD directives found for this matcher.
RUBY
expect_no_corrections
end
it 'autocorrects with the right arguments if the pattern includes arguments' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, '(str %1)'
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node, arg1)
#{method} :foo?, '(str %1)'
RUBY
end
it 'autocorrects with the right arguments if the pattern references a non-contiguous argument' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, '(str %4)'
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node, arg1, arg2, arg3, arg4)
#{method} :foo?, '(str %4)'
RUBY
end
it 'does not register an offense if called with a dynamic method name' do
expect_no_offenses(<<~'RUBY')
#{method} matcher_name, '(str)'
#{method} "#{matcher_name}", '(str)'
RUBY
end
it 'retains indentation properly when inserting' do
expect_offense(<<~RUBY, method: method)
class MyCop
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
end
RUBY
expect_correction(<<~RUBY)
class MyCop
# @!method foo?(node)
#{method} :foo?, '(str)'
end
RUBY
end
it 'retains indentation properly when correcting' do
expect_offense(<<~RUBY, method: method)
class MyCop
# @!method bar?(node)
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ `@!method` YARD directive has invalid method name, use `foo?` instead of `bar?`.
end
RUBY
expect_correction(<<~RUBY)
class MyCop
# @!method foo?(node)
#{method} :foo?, '(str)'
end
RUBY
end
it 'inserts a blank line between multiple pattern matchers' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, '(str)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
#{method} :bar?, '(str)'
^{method}^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, '(str)'
# @!method bar?(node)
#{method} :bar?, '(str)'
RUBY
end
it 'inserts a blank line between multiple multi-line pattern matchers' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, <<~PATTERN
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
(str)
PATTERN
#{method} :bar?, <<~PATTERN
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
(str)
PATTERN
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, <<~PATTERN
(str)
PATTERN
# @!method bar?(node)
#{method} :bar?, <<~PATTERN
(str)
PATTERN
RUBY
end
it 'does not insert a blank line if one already exists' do
expect_offense(<<~RUBY, method: method)
#{method} :foo?, <<~PATTERN
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
(str)
PATTERN
#{method} :bar?, <<~PATTERN
^{method}^^^^^^^^^^^^^^^^^^ Preceed `#{method}` with a `@!method` YARD directive.
(str)
PATTERN
RUBY
expect_correction(<<~RUBY)
# @!method foo?(node)
#{method} :foo?, <<~PATTERN
(str)
PATTERN
# @!method bar?(node)
#{method} :bar?, <<~PATTERN
(str)
PATTERN
RUBY
end
end
end