forked from rubocop/rubocop-ast
/
node_spec.rb
376 lines (301 loc) · 9.26 KB
/
node_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# frozen_string_literal: true
RSpec.describe RuboCop::AST::Node do
let(:node) { RuboCop::AST::ProcessedSource.new(src, ruby_version).ast }
describe '#value_used?' do
before :all do
module RuboCop
module AST
class Node
# Let's make our predicate matchers read better
def used?
value_used?
end
end
end
end
end
context 'at the top level' do
let(:src) { 'expr' }
it 'is false' do
expect(node.used?).to be(false)
end
end
context 'within a method call node' do
let(:src) { 'obj.method(arg1, arg2, arg3)' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'at the end of a block' do
let(:src) { 'obj.method { blah; expr }' }
it 'is always true' do
expect(node.children.last.used?).to be(true)
end
end
context 'within a class definition node' do
let(:src) { 'class C < Super; def a; 1; end; self; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within a module definition node' do
let(:src) { 'module M; def method; end; 1; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within a singleton class node' do
let(:src) { 'class << obj; 1; 2; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within an if...else..end node' do
context 'nested in a method call' do
let(:src) { 'obj.method(if a then b else c end)' }
it 'is always true' do
if_node = node.children[2]
expect(if_node.child_nodes).to all(be_used)
end
end
context 'at the top level' do
let(:src) { 'if a then b else c end' }
it 'is true only for the condition' do
expect(node.condition.used?).to be(true)
expect(node.if_branch.used?).to be(false)
expect(node.else_branch.used?).to be(false)
end
end
end
context 'within an array literal' do
context 'assigned to an ivar' do
let(:src) { '@var = [a, b, c]' }
it 'is always true' do
ary_node = node.children[1]
expect(ary_node.child_nodes).to all(be_used)
end
end
context 'at the top level' do
let(:src) { '[a, b, c]' }
it 'is always false' do
expect(node.child_nodes.map(&:used?)).to all(be false)
end
end
end
context 'within a while node' do
let(:src) { 'while a; b; end' }
it 'is true only for the condition' do
expect(node.condition.used?).to be(true)
expect(node.body.used?).to be(false)
end
end
end
describe '#recursive_basic_literal?' do
shared_examples 'literal' do |source|
let(:src) { source }
it "returns true for `#{source}`" do
expect(node.recursive_literal?).to be(true)
end
end
it_behaves_like 'literal', '!true'
it_behaves_like 'literal', '"#{2}"'
it_behaves_like 'literal', '(1)'
it_behaves_like 'literal', '(false && true)'
it_behaves_like 'literal', '(false <=> true)'
it_behaves_like 'literal', '(false or true)'
it_behaves_like 'literal', '[1, 2, 3]'
it_behaves_like 'literal', '{ :a => 1, :b => 2 }'
it_behaves_like 'literal', '{ a: 1, b: 2 }'
it_behaves_like 'literal', '/./'
it_behaves_like 'literal', '%r{abx}ixo'
it_behaves_like 'literal', '1.0'
it_behaves_like 'literal', '1'
it_behaves_like 'literal', 'false'
it_behaves_like 'literal', 'nil'
it_behaves_like 'literal', "'str'"
shared_examples 'non literal' do |source|
let(:src) { source }
it "returns false for `#{source}`" do
expect(node.recursive_literal?).to be(false)
end
end
it_behaves_like 'non literal', '(x && false)'
it_behaves_like 'non literal', '(x == false)'
it_behaves_like 'non literal', '(x or false)'
it_behaves_like 'non literal', '[some_method_call]'
it_behaves_like 'non literal', '{ :sym => some_method_call }'
it_behaves_like 'non literal', '{ some_method_call => :sym }'
it_behaves_like 'non literal', '/.#{some_method_call}/'
it_behaves_like 'non literal', '%r{abx#{foo}}ixo'
it_behaves_like 'non literal', 'some_method_call'
it_behaves_like 'non literal', 'some_method_call(x, y)'
end
describe '#pure?' do
context 'for a method call' do
let(:src) { 'obj.method(arg1, arg2)' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for an integer literal' do
let(:src) { '100' }
it 'returns true' do
expect(node.pure?).to be(true)
end
end
context 'for an array literal' do
context 'with only literal children' do
let(:src) { '[1..100, false, :symbol, "string", 1.0]' }
it 'returns true' do
expect(node.pure?).to be(true)
end
end
context 'which contains a method call' do
let(:src) { '[1, 2, 3, 3 + 4]' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
end
context 'for a hash literal' do
context 'with only literal children' do
let(:src) { '{range: 1..100, bool: false, str: "string", float: 1.0}' }
it 'returns true' do
expect(node.pure?).to be(true)
end
end
context 'which contains a method call' do
let(:src) { '{a: 1, b: 2, c: Kernel.exit}' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
end
context 'for a nested if' do
context 'where the innermost descendants are local vars and literals' do
let(:src) do
['lvar1, lvar2 = method1, method2',
'if $global',
' if @initialized',
' [lvar1, lvar2, true]',
' else',
' :symbol',
' end',
'else',
' lvar1',
'end'].join("\n")
end
it 'returns true' do
if_node = node.children[1]
expect(if_node.type).to be :if
expect(if_node.pure?).to be(true)
end
end
context 'where one branch contains a method call' do
let(:src) { 'if $DEBUG then puts "hello" else nil end' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'where one branch contains an assignment statement' do
let(:src) { 'if @a then 1 else $global = "str" end' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
end
context 'for an ivar assignment' do
let(:src) { '@var = 1' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for a gvar assignment' do
let(:src) { '$var = 1' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for a cvar assignment' do
let(:src) { '@@var = 1' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for an lvar assignment' do
let(:src) { 'var = 1' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for a class definition' do
let(:src) { 'class C < Super; def method; end end' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for a module definition' do
let(:src) { 'module M; def method; end end' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'for a regexp' do
let(:opts) { '' }
let(:body) { '' }
let(:src) { "/#{body}/#{opts}" }
context 'with interpolated segments' do
let(:body) { '#{x}' }
it 'returns false' do
expect(node.pure?).to be(false)
end
end
context 'with no interpolation' do
let(:src) { URI::DEFAULT_PARSER.make_regexp.inspect }
it 'returns true' do
expect(node.pure?).to be(true)
end
end
context 'with options' do
let(:opts) { 'oix' }
it 'returns true' do
expect(node.pure?).to be(true)
end
end
end
end
describe '#sibling_index' do
let(:src) do
[
'def foo; end',
'def bar; end',
'def baz; end'
].join("\n")
end
it 'returns its sibling index' do
(0..2).each do |n|
expect(node.children[n].sibling_index).to eq(n)
end
end
end
describe '#argument_type?' do
context 'block arguments' do
let(:src) { 'bar { |a, b = 42, *c, d: 42, **e| nil }' }
it 'returns true for all argument types' do
node.children[1].children.each do |arg|
expect(arg.argument_type?).to eq(true)
end
expect(node.argument_type?).to eq(false)
end
end
context 'method arguments' do
let(:src) { 'def method_name(a = 0, *b, c: 42, **d); end' }
it 'returns true for all argument types' do
node.children[1].children.each do |arg|
expect(arg.argument_type?).to eq(true)
end
expect(node.argument_type?).to eq(false)
end
end
end
end