code_analyzer 0.5.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Sexp do
5
+ describe CodeAnalyzer::Node do
6
6
  describe 'line_number' do
7
7
  before :each do
8
8
  content = <<-EOF
@@ -33,75 +33,75 @@ describe Sexp do
33
33
  end
34
34
 
35
35
  it 'should return class line' do
36
- expect(@node.grep_node(sexp_type: :class).line_number).to eq 1
36
+ expect(@node.grep_node(node_type: :class).line_number).to eq 1
37
37
  end
38
38
 
39
39
  it 'should return def line' do
40
- expect(@node.grep_node(sexp_type: :def).line_number).to eq 2
40
+ expect(@node.grep_node(node_type: :def).line_number).to eq 2
41
41
  end
42
42
 
43
43
  it 'should return const line' do
44
- expect(@node.grep_node(sexp_type: :const_ref).line_number).to eq 1
44
+ expect(@node.grep_node(node_type: :const_ref).line_number).to eq 1
45
45
  end
46
46
 
47
47
  it 'should return top const line' do
48
- expect(@node.grep_node(sexp_type: :top_const_ref).line_number).to eq 20
48
+ expect(@node.grep_node(node_type: :top_const_ref).line_number).to eq 20
49
49
  end
50
50
 
51
51
  it 'should return const path line' do
52
- expect(@node.grep_node(sexp_type: :const_path_ref).line_number).to eq 3
52
+ expect(@node.grep_node(node_type: :const_path_ref).line_number).to eq 3
53
53
  end
54
54
 
55
55
  it 'should return alias line' do
56
- expect(@node.grep_node(sexp_type: :alias).line_number).to eq 5
56
+ expect(@node.grep_node(node_type: :alias).line_number).to eq 5
57
57
  end
58
58
 
59
59
  it 'should return hash line' do
60
- expect(@node.grep_node(sexp_type: :hash).line_number).to eq 6
60
+ expect(@node.grep_node(node_type: :hash).line_number).to eq 6
61
61
  end
62
62
 
63
63
  it 'should return massign line' do
64
- expect(@node.grep_node(sexp_type: :massign).line_number).to eq 8
64
+ expect(@node.grep_node(node_type: :massign).line_number).to eq 8
65
65
  end
66
66
 
67
67
  it 'should return opassign line' do
68
- expect(@node.grep_node(sexp_type: :opassign).line_number).to eq 11
68
+ expect(@node.grep_node(node_type: :opassign).line_number).to eq 11
69
69
  end
70
70
 
71
71
  it 'should return if line' do
72
- expect(@node.grep_node(sexp_type: :if).line_number).to eq 14
72
+ expect(@node.grep_node(node_type: :if).line_number).to eq 14
73
73
  end
74
74
 
75
75
  it 'should return elsif line' do
76
- expect(@node.grep_node(sexp_type: :elsif).line_number).to eq 16
76
+ expect(@node.grep_node(node_type: :elsif).line_number).to eq 16
77
77
  end
78
78
 
79
79
  it 'should return if_mod line' do
80
- expect(@node.grep_node(sexp_type: :if_mod).line_number).to eq 15
80
+ expect(@node.grep_node(node_type: :if_mod).line_number).to eq 15
81
81
  end
82
82
 
83
83
  it 'should return unary line' do
84
- expect(@node.grep_node(sexp_type: :unary).line_number).to eq 15
84
+ expect(@node.grep_node(node_type: :unary).line_number).to eq 15
85
85
  end
86
86
 
87
87
  it 'should return assign line' do
88
- expect(@node.grep_node(sexp_type: :assign).line_number).to eq 6
88
+ expect(@node.grep_node(node_type: :assign).line_number).to eq 6
89
89
  end
90
90
 
91
91
  it 'should return paren line' do
92
- expect(@node.grep_node(sexp_type: :paren).line_number).to eq 10
92
+ expect(@node.grep_node(node_type: :paren).line_number).to eq 10
93
93
  end
94
94
 
95
95
  it 'should return dot2 line' do
96
- expect(@node.grep_node(sexp_type: :dot2).line_number).to eq 17
96
+ expect(@node.grep_node(node_type: :dot2).line_number).to eq 17
97
97
  end
98
98
 
99
99
  it 'should return dot3 line' do
100
- expect(@node.grep_node(sexp_type: :dot3).line_number).to eq 18
100
+ expect(@node.grep_node(node_type: :dot3).line_number).to eq 18
101
101
  end
102
102
 
103
103
  it 'should return params line' do
104
- expect(@node.grep_node(sexp_type: :params).line_number).to be_nil
104
+ expect(@node.grep_node(node_type: :params).line_number).to be_nil
105
105
  end
106
106
 
107
107
  it 'should return params line if not empty' do
@@ -110,18 +110,18 @@ describe Sexp do
110
110
  def foo(a, b)
111
111
  end
112
112
  CODE
113
- expect(@node.grep_node(sexp_type: :params).line_number).to eq 2
113
+ expect(@node.grep_node(node_type: :params).line_number).to eq 2
114
114
  end
115
115
 
116
116
  it 'should return stmts_add line' do
117
- expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 13
117
+ expect(@node.grep_node(node_type: :stmts_add).line_number).to eq 13
118
118
  end
119
119
 
120
120
  context 'when a complex code is given' do
121
121
  before :each do
122
122
  @node = parse_content(<<~CODE)
123
123
  def foo(num)
124
- unless (num == 0 ? :zero, :other) || !@opts.right?
124
+ unless (num == 0 ? :zero : :other) || !@opts.right?
125
125
  @bar = {}
126
126
  end
127
127
  end
@@ -129,27 +129,27 @@ describe Sexp do
129
129
  end
130
130
 
131
131
  it 'should return unless line' do
132
- expect(@node.grep_node(sexp_type: :unless).line_number).to eq 2
132
+ expect(@node.grep_node(node_type: :unless).line_number).to eq 2
133
133
  end
134
134
 
135
135
  it 'should return paren line' do
136
- expect(@node.grep_node(sexp_type: :paren).line_number).to eq 1
136
+ expect(@node.grep_node(node_type: :paren).line_number).to eq 1
137
137
  end
138
138
 
139
139
  it 'should return stmts_add line' do
140
- expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 2
140
+ expect(@node.grep_node(node_type: :stmts_add).line_number).to eq 2
141
141
  end
142
142
 
143
143
  it 'should return binary line' do
144
- expect(@node.grep_node(sexp_type: :binary).line_number).to eq 2
144
+ expect(@node.grep_node(node_type: :binary).line_number).to eq 2
145
145
  end
146
146
 
147
147
  it 'should return unary line' do
148
- expect(@node.grep_node(sexp_type: :unary).line_number).to eq 2
148
+ expect(@node.grep_node(node_type: :unary).line_number).to eq 2
149
149
  end
150
150
 
151
151
  it 'should return assign line' do
152
- expect(@node.grep_node(sexp_type: :assign).line_number).to eq 3
152
+ expect(@node.grep_node(node_type: :assign).line_number).to eq 3
153
153
  end
154
154
  end
155
155
  end
@@ -166,45 +166,28 @@ describe Sexp do
166
166
 
167
167
  it 'should get the call nodes with receiver current_user' do
168
168
  nodes = []
169
- @node.grep_nodes(sexp_type: :call, receiver: 'current_user') { |node| nodes << node }
170
- expect(nodes).to eq [
171
- s(
172
- :call,
173
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
174
- s(:@period, '.', s(2, 20)),
175
- s(:@ident, 'posts', s(2, 21))
176
- )
177
- ]
169
+ @node.grep_nodes(node_type: :call, receiver: 'current_user') { |node| nodes << node }
170
+ expect(nodes.size).to eq 1
171
+ expect(nodes.first.node_type).to eq :call
172
+ expect(nodes.first.message.to_s).to eq 'posts'
173
+ expect(nodes.first.to_s).to eq 'current_user.posts'
178
174
  end
179
175
 
180
176
  it 'should get the call nodes with different messages' do
181
177
  nodes = []
182
- @node.grep_nodes(sexp_type: :call, message: %w[posts find]) { |node| nodes << node }
183
- expect(nodes).to eq [
184
- s(
185
- :call,
186
- s(
187
- :call,
188
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
189
- s(:@period, '.', s(2, 20)),
190
- s(:@ident, 'posts', s(2, 21))
191
- ),
192
- s(:@period, '.', s(2, 26)),
193
- s(:@ident, 'find', s(2, 27))
194
- ),
195
- s(
196
- :call,
197
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
198
- s(:@period, '.', s(2, 20)),
199
- s(:@ident, 'posts', s(2, 21))
200
- )
178
+ @node.grep_nodes(node_type: :call, message: %w[posts find]) { |node| nodes << node }
179
+ expect(nodes.map { |node| [node.receiver.to_s, node.message.to_s] }).to eq [
180
+ ['current_user.posts', 'find'],
181
+ ['current_user', 'posts']
201
182
  ]
202
183
  end
203
184
 
204
185
  it 'should get the vcall node with to_s' do
205
186
  nodes = []
206
- @node.grep_nodes(sexp_type: :vcall, to_s: 'current_user') { |node| nodes << node }
207
- expect(nodes).to eq [s(:vcall, s(:@ident, 'current_user', s(2, 8)))]
187
+ @node.grep_nodes(node_type: :vcall, to_s: 'current_user') { |node| nodes << node }
188
+ expect(nodes.size).to eq 1
189
+ expect(nodes.first.node_type).to eq :vcall
190
+ expect(nodes.first.to_s).to eq 'current_user'
208
191
  end
209
192
  end
210
193
 
@@ -219,13 +202,9 @@ describe Sexp do
219
202
  end
220
203
 
221
204
  it 'should get first node with empty argument' do
222
- node = @node.grep_node(sexp_type: :call, receiver: 'current_user')
223
- expect(node).to eq s(
224
- :call,
225
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
226
- s(:@period, '.', s(2, 20)),
227
- s(:@ident, 'posts', s(2, 21))
228
- )
205
+ node = @node.grep_node(node_type: :call, receiver: 'current_user')
206
+ expect(node.node_type).to eq :call
207
+ expect(node.to_s).to eq 'current_user.posts'
229
208
  end
230
209
  end
231
210
 
@@ -240,31 +219,31 @@ describe Sexp do
240
219
  end
241
220
 
242
221
  it 'should get the count of call nodes' do
243
- expect(@node.grep_nodes_count(sexp_type: :call)).to eq 2
222
+ expect(@node.grep_nodes_count(node_type: :call)).to eq 2
244
223
  end
245
224
  end
246
225
 
247
226
  describe 'receiver' do
248
227
  it 'should get receiver of assign node' do
249
- node = parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
228
+ node = parse_content('user.name = params[:name]').grep_node(node_type: :assign)
250
229
  receiver = node.receiver
251
- expect(receiver.sexp_type).to eq :field
230
+ expect(receiver.node_type).to eq :field
252
231
  expect(receiver.receiver.to_s).to eq 'user'
253
232
  expect(receiver.message.to_s).to eq 'name'
254
233
  end
255
234
 
256
235
  it 'should get receiver of field node' do
257
- node = parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
236
+ node = parse_content('user.name = params[:name]').grep_node(node_type: :field)
258
237
  expect(node.receiver.to_s).to eq 'user'
259
238
  end
260
239
 
261
240
  it 'should get receiver of call node' do
262
- node = parse_content('user.name').grep_node(sexp_type: :call)
241
+ node = parse_content('user.name').grep_node(node_type: :call)
263
242
  expect(node.receiver.to_s).to eq 'user'
264
243
  end
265
244
 
266
245
  it 'should get receiver of binary' do
267
- node = parse_content("user == 'user_name'").grep_node(sexp_type: :binary)
246
+ node = parse_content("user == 'user_name'").grep_node(node_type: :binary)
268
247
  expect(node.receiver.to_s).to eq 'user'
269
248
  end
270
249
 
@@ -273,298 +252,293 @@ describe Sexp do
273
252
  map.resources :posts do
274
253
  end
275
254
  EOF
276
- node = parse_content(content).grep_node(sexp_type: :command_call)
255
+ node = parse_content(content).grep_node(node_type: :command_call)
277
256
  expect(node.receiver.to_s).to eq 'map'
278
257
  end
279
258
 
280
259
  it 'should get receiver of method_add_arg' do
281
- node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
260
+ node = parse_content('Post.find(:all)').grep_node(node_type: :method_add_arg)
282
261
  expect(node.receiver.to_s).to eq 'Post'
283
262
  end
284
263
 
285
264
  it 'should get receiver of method_add_block' do
286
- node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
265
+ node = parse_content('Post.save do; end').grep_node(node_type: :method_add_block)
287
266
  expect(node.receiver.to_s).to eq 'Post'
288
267
  end
289
268
  end
290
269
 
291
270
  describe 'module_name' do
292
271
  it 'should get module name of module node' do
293
- node = parse_content('module Admin; end').grep_node(sexp_type: :module)
272
+ node = parse_content('module Admin; end').grep_node(node_type: :module)
294
273
  expect(node.module_name.to_s).to eq 'Admin'
295
274
  end
296
275
  end
297
276
 
298
277
  describe 'class_name' do
299
278
  it 'should get class name of class node' do
300
- node = parse_content('class User; end').grep_node(sexp_type: :class)
279
+ node = parse_content('class User; end').grep_node(node_type: :class)
301
280
  expect(node.class_name.to_s).to eq 'User'
302
281
  end
303
282
  end
304
283
 
305
284
  describe 'base_class' do
306
285
  it 'should get base class of class node' do
307
- node = parse_content('class User < ActiveRecord::Base; end').grep_node(sexp_type: :class)
286
+ node = parse_content('class User < ActiveRecord::Base; end').grep_node(node_type: :class)
308
287
  expect(node.base_class.to_s).to eq 'ActiveRecord::Base'
309
288
  end
310
289
  end
311
290
 
312
291
  describe 'left_value' do
313
292
  it 'should get the left value of assign' do
314
- node = parse_content('user = current_user').grep_node(sexp_type: :assign)
293
+ node = parse_content('user = current_user').grep_node(node_type: :assign)
315
294
  expect(node.left_value.to_s).to eq 'user'
316
295
  end
317
296
  end
318
297
 
319
298
  describe 'right_value' do
320
299
  it 'should get the right value of assign' do
321
- node = parse_content('user = current_user').grep_node(sexp_type: :assign)
300
+ node = parse_content('user = current_user').grep_node(node_type: :assign)
322
301
  expect(node.right_value.to_s).to eq 'current_user'
323
302
  end
324
303
  end
325
304
 
326
305
  describe 'message' do
327
306
  it 'should get the message of command' do
328
- node = parse_content('has_many :projects').grep_node(sexp_type: :command)
307
+ node = parse_content('has_many :projects').grep_node(node_type: :command)
329
308
  expect(node.message.to_s).to eq 'has_many'
330
309
  end
331
310
 
332
311
  it 'should get the message of command_call' do
333
- node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
312
+ node = parse_content('map.resources :posts do; end').grep_node(node_type: :command_call)
334
313
  expect(node.message.to_s).to eq 'resources'
335
314
  end
336
315
 
337
316
  it 'should get the message of field' do
338
- node = parse_content("user.name = 'test'").grep_node(sexp_type: :field)
317
+ node = parse_content("user.name = 'test'").grep_node(node_type: :field)
339
318
  expect(node.message.to_s).to eq 'name'
340
319
  end
341
320
 
342
321
  it 'should get the message of call' do
343
- node = parse_content('user.name').grep_node(sexp_type: :call)
322
+ node = parse_content('user.name').grep_node(node_type: :call)
344
323
  expect(node.message.to_s).to eq 'name'
345
324
  end
346
325
 
347
326
  it 'should get the message of binary' do
348
- node = parse_content("user.name == 'test'").grep_node(sexp_type: :binary)
327
+ node = parse_content("user.name == 'test'").grep_node(node_type: :binary)
349
328
  expect(node.message.to_s).to eq '=='
350
329
  end
351
330
 
352
331
  it 'should get the message of fcall' do
353
- node = parse_content("test?('world')").grep_node(sexp_type: :fcall)
332
+ node = parse_content("test?('world')").grep_node(node_type: :fcall)
354
333
  expect(node.message.to_s).to eq 'test?'
355
334
  end
356
335
 
357
336
  it 'should get the message of method_add_arg' do
358
- node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
337
+ node = parse_content('Post.find(:all)').grep_node(node_type: :method_add_arg)
359
338
  expect(node.message.to_s).to eq 'find'
360
339
  end
361
340
 
362
341
  it 'should get the message of method_add_block' do
363
- node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
342
+ node = parse_content('Post.save do; end').grep_node(node_type: :method_add_block)
364
343
  expect(node.message.to_s).to eq 'save'
365
344
  end
366
345
  end
367
346
 
368
347
  describe 'arguments' do
369
348
  it 'should get the arguments of command' do
370
- node = parse_content('resources :posts do; end').grep_node(sexp_type: :command)
371
- expect(node.arguments.sexp_type).to eq :args_add_block
349
+ node = parse_content('resources :posts do; end').grep_node(node_type: :command)
350
+ expect(node.arguments.node_type).to eq :args_add_block
372
351
  end
373
352
 
374
353
  it 'should get the arguments of command_call' do
375
- node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
376
- expect(node.arguments.sexp_type).to eq :args_add_block
354
+ node = parse_content('map.resources :posts do; end').grep_node(node_type: :command_call)
355
+ expect(node.arguments.node_type).to eq :args_add_block
377
356
  end
378
357
 
379
358
  it 'should get the arguments of method_add_arg' do
380
- node = parse_content('User.find(:all)').grep_node(sexp_type: :method_add_arg)
381
- expect(node.arguments.sexp_type).to eq :args_add_block
359
+ node = parse_content('User.find(:all)').grep_node(node_type: :method_add_arg)
360
+ expect(node.arguments.node_type).to eq :args_add_block
382
361
  end
383
362
 
384
363
  it 'should get the arguments of method_add_block' do
385
- node = parse_content('Post.save(false) do; end').grep_node(sexp_type: :method_add_block)
386
- expect(node.arguments.sexp_type).to eq :args_add_block
364
+ node = parse_content('Post.save(false) do; end').grep_node(node_type: :method_add_block)
365
+ expect(node.arguments.node_type).to eq :args_add_block
387
366
  end
388
367
  end
389
368
 
390
369
  describe 'argument' do
391
370
  it 'should get the argument of binary' do
392
- node = parse_content('user == current_user').grep_node(sexp_type: :binary)
371
+ node = parse_content('user == current_user').grep_node(node_type: :binary)
393
372
  expect(node.argument.to_s).to eq 'current_user'
394
373
  end
395
374
  end
396
375
 
397
376
  describe 'all' do
398
377
  it 'should get all arguments' do
399
- node = parse_content("puts 'hello', 'world'").grep_node(sexp_type: :args_add_block)
378
+ node = parse_content("puts 'hello', 'world'").grep_node(node_type: :args_add_block)
400
379
  expect(node.all.map(&:to_s)).to eq %w[hello world]
401
380
  end
402
381
 
403
382
  it 'should get all arguments with &:' do
404
- node = parse_content('user.posts.map(&:title)').grep_node(sexp_type: :args_add_block)
383
+ node = parse_content('user.posts.map(&:title)').grep_node(node_type: :args_add_block)
405
384
  expect(node.all.map(&:to_s)).to eq %w[title]
406
385
  end
407
386
 
408
387
  it 'should get all arguments with command_call node' do
409
- node = parse_content('options_for_select(Account.get_business current_user)').grep_node(sexp_type: :args_add)
410
- expect(node.all).to eq [
411
- s(
412
- :command_call,
413
- s(:var_ref, s(:@const, 'Account', s(1, 19))),
414
- s(:@period, '.', s(1, 26)),
415
- s(:@ident, 'get_business', s(1, 27)),
416
- s(:args_add_block, s(:args_add, s(:args_new), s(:vcall, s(:@ident, 'current_user', s(1, 40)))), false)
417
- )
418
- ]
388
+ node = parse_content('options_for_select(Account.get_business current_user)').grep_node(node_type: :args_add)
389
+ expect(node.all.size).to eq 1
390
+ expect(node.all.first.node_type).to eq :command_call
391
+ expect(node.all.first.message.to_s).to eq 'get_business'
392
+ expect(node.all.first.receiver.to_s).to eq 'Account'
419
393
  end
420
394
 
421
395
  it 'no error for args_add_star' do
422
- node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(sexp_type: :args_add_block)
396
+ node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(node_type: :args_add_block)
423
397
  expect { node.all }.not_to raise_error
424
398
  end
425
399
  end
426
400
 
427
401
  describe 'conditional_statement' do
428
402
  it 'should get conditional statement of if' do
429
- node = parse_content('if true; end').grep_node(sexp_type: :if)
403
+ node = parse_content('if true; end').grep_node(node_type: :if)
430
404
  expect(node.conditional_statement.to_s).to eq 'true'
431
405
  end
432
406
 
433
407
  it 'should get conditional statement of unless' do
434
- node = parse_content('unless false; end').grep_node(sexp_type: :unless)
408
+ node = parse_content('unless false; end').grep_node(node_type: :unless)
435
409
  expect(node.conditional_statement.to_s).to eq 'false'
436
410
  end
437
411
 
438
412
  it 'should get conditional statement of elsif' do
439
- node = parse_content('if true; elsif false; end').grep_node(sexp_type: :elsif)
413
+ node = parse_content('if true; elsif false; end').grep_node(node_type: :elsif)
440
414
  expect(node.conditional_statement.to_s).to eq 'false'
441
415
  end
442
416
 
443
417
  it 'should get conditional statement of if_mod' do
444
- node = parse_content("'OK' if true").grep_node(sexp_type: :if_mod)
418
+ node = parse_content("'OK' if true").grep_node(node_type: :if_mod)
445
419
  expect(node.conditional_statement.to_s).to eq 'true'
446
420
  end
447
421
 
448
422
  it 'should get conditional statement of unless_mod' do
449
- node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
423
+ node = parse_content("'OK' unless false").grep_node(node_type: :unless_mod)
450
424
  expect(node.conditional_statement.to_s).to eq 'false'
451
425
  end
452
426
 
453
427
  it 'should get conditional statement of ifop' do
454
- node = parse_content("true ? 'OK' : 'NO'").grep_node(sexp_type: :ifop)
428
+ node = parse_content("true ? 'OK' : 'NO'").grep_node(node_type: :ifop)
455
429
  expect(node.conditional_statement.to_s).to eq 'true'
456
430
  end
457
431
  end
458
432
 
459
433
  describe 'all_conditions' do
460
434
  it 'should get all conditions' do
461
- node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(sexp_type: :binary)
435
+ node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(node_type: :binary)
462
436
  expect(node.all_conditions.size).to eq 3
463
437
  end
464
438
  end
465
439
 
466
440
  describe 'method_name' do
467
441
  it 'should get the method name of def' do
468
- node = parse_content('def show; end').grep_node(sexp_type: :def)
442
+ node = parse_content('def show; end').grep_node(node_type: :def)
469
443
  expect(node.method_name.to_s).to eq 'show'
470
444
  end
471
445
 
472
446
  it 'should get the method name of defs' do
473
- node = parse_content('def self.find; end').grep_node(sexp_type: :defs)
447
+ node = parse_content('def self.find; end').grep_node(node_type: :defs)
474
448
  expect(node.method_name.to_s).to eq 'find'
475
449
  end
476
450
  end
477
451
 
478
452
  describe 'body' do
479
453
  it 'should get body of class' do
480
- node = parse_content('class User; end').grep_node(sexp_type: :class)
481
- expect(node.body.sexp_type).to eq :bodystmt
454
+ node = parse_content('class User; end').grep_node(node_type: :class)
455
+ expect(node.body.node_type).to eq :bodystmt
482
456
  end
483
457
 
484
458
  it 'should get body of def' do
485
- node = parse_content('def login; end').grep_node(sexp_type: :def)
486
- expect(node.body.sexp_type).to eq :bodystmt
459
+ node = parse_content('def login; end').grep_node(node_type: :def)
460
+ expect(node.body.node_type).to eq :bodystmt
487
461
  end
488
462
 
489
463
  it 'should get body of defs' do
490
- node = parse_content('def self.login; end').grep_node(sexp_type: :defs)
491
- expect(node.body.sexp_type).to eq :bodystmt
464
+ node = parse_content('def self.login; end').grep_node(node_type: :defs)
465
+ expect(node.body.node_type).to eq :bodystmt
492
466
  end
493
467
 
494
468
  it 'should get body of module' do
495
- node = parse_content('module Enumerable; end').grep_node(sexp_type: :module)
496
- expect(node.body.sexp_type).to eq :bodystmt
469
+ node = parse_content('module Enumerable; end').grep_node(node_type: :module)
470
+ expect(node.body.node_type).to eq :bodystmt
497
471
  end
498
472
 
499
473
  it 'should get body of if' do
500
- node = parse_content("if true; 'OK'; end").grep_node(sexp_type: :if)
501
- expect(node.body.sexp_type).to eq :stmts_add
474
+ node = parse_content("if true; 'OK'; end").grep_node(node_type: :if)
475
+ expect(node.body.node_type).to eq :stmts_add
502
476
  end
503
477
 
504
478
  it 'should get body of elsif' do
505
- node = parse_content("if true; elsif true; 'OK'; end").grep_node(sexp_type: :elsif)
506
- expect(node.body.sexp_type).to eq :stmts_add
479
+ node = parse_content("if true; elsif true; 'OK'; end").grep_node(node_type: :elsif)
480
+ expect(node.body.node_type).to eq :stmts_add
507
481
  end
508
482
 
509
483
  it 'should get body of unless' do
510
- node = parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
511
- expect(node.body.sexp_type).to eq :stmts_add
484
+ node = parse_content("unless true; 'OK'; end").grep_node(node_type: :unless)
485
+ expect(node.body.node_type).to eq :stmts_add
512
486
  end
513
487
 
514
488
  it 'should get body of else' do
515
- node = parse_content("if true; else; 'OK'; end").grep_node(sexp_type: :else)
516
- expect(node.body.sexp_type).to eq :stmts_add
489
+ node = parse_content("if true; else; 'OK'; end").grep_node(node_type: :else)
490
+ expect(node.body.node_type).to eq :stmts_add
517
491
  end
518
492
 
519
493
  it 'should get body of if_mod' do
520
- node = parse_content("'OK' if true").grep_node(sexp_type: :if_mod)
494
+ node = parse_content("'OK' if true").grep_node(node_type: :if_mod)
521
495
  expect(node.body.to_s).to eq 'OK'
522
496
  end
523
497
 
524
498
  it 'should get body of unless_mod' do
525
- node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
499
+ node = parse_content("'OK' unless false").grep_node(node_type: :unless_mod)
526
500
  expect(node.body.to_s).to eq 'OK'
527
501
  end
528
502
 
529
503
  it 'should get body of if_op' do
530
- node = parse_content("true ? 'OK' : 'NO'").grep_node(sexp_type: :ifop)
504
+ node = parse_content("true ? 'OK' : 'NO'").grep_node(node_type: :ifop)
531
505
  expect(node.body.to_s).to eq 'OK'
532
506
  end
533
507
  end
534
508
 
535
509
  describe 'block' do
536
510
  it 'sould get block of method_add_block node' do
537
- node = parse_content('resources :posts do; resources :comments; end').grep_node(sexp_type: :method_add_block)
538
- expect(node.block_node.sexp_type).to eq :do_block
511
+ node = parse_content('resources :posts do; resources :comments; end').grep_node(node_type: :method_add_block)
512
+ expect(node.block_node.node_type).to eq :do_block
539
513
  end
540
514
  end
541
515
 
542
516
  describe 'statements' do
543
517
  it 'should get statements of do_block node' do
544
518
  node =
545
- parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(sexp_type: :do_block)
519
+ parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(node_type: :do_block)
546
520
  expect(node.statements.size).to eq 2
547
521
  end
548
522
 
549
523
  it 'should get statements of bodystmt node' do
550
- node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(sexp_type: :bodystmt)
524
+ node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(node_type: :bodystmt)
551
525
  expect(node.statements.size).to eq 2
552
526
  end
553
527
  end
554
528
 
555
529
  describe 'exception_classes' do
556
530
  it 'should get exception classes of rescue node' do
557
- node = parse_content('def test; rescue CustomException; end').grep_node(sexp_type: :rescue)
531
+ node = parse_content('def test; rescue CustomException; end').grep_node(node_type: :rescue)
558
532
  expect(node.exception_classes.first.to_s).to eq 'CustomException'
559
533
  end
560
534
 
561
535
  it 'should get empty of empty rescue node' do
562
- node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
536
+ node = parse_content('def test; rescue; end').grep_node(node_type: :rescue)
563
537
  expect(node.exception_classes.first.to_s).to eq ''
564
538
  end
565
539
 
566
540
  it 'should get exception classes of rescue node for multiple exceptions' do
567
- node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(sexp_type: :rescue)
541
+ node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(node_type: :rescue)
568
542
  expect(node.exception_classes.first.to_s).to eq 'StandardError'
569
543
  expect(node.exception_classes.last.to_s).to eq 'CustomException'
570
544
  end
@@ -572,19 +546,19 @@ describe Sexp do
572
546
 
573
547
  describe 'exception_variable' do
574
548
  it 'should get exception varible of rescue node' do
575
- node = parse_content('def test; rescue => e; end').grep_node(sexp_type: :rescue)
549
+ node = parse_content('def test; rescue => e; end').grep_node(node_type: :rescue)
576
550
  expect(node.exception_variable.to_s).to eq 'e'
577
551
  end
578
552
 
579
553
  it 'should get empty of empty rescue node' do
580
- node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
554
+ node = parse_content('def test; rescue; end').grep_node(node_type: :rescue)
581
555
  expect(node.exception_variable.to_s).to eq ''
582
556
  end
583
557
  end
584
558
 
585
559
  describe 'hash_value' do
586
560
  it 'should get value for hash node' do
587
- node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
561
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
588
562
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
589
563
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
590
564
  end
@@ -592,7 +566,7 @@ describe Sexp do
592
566
  it 'should get value for bare_assoc_hash' do
593
567
  node =
594
568
  parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
595
- sexp_type: :bare_assoc_hash
569
+ node_type: :bare_assoc_hash
596
570
  )
597
571
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
598
572
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
@@ -601,14 +575,14 @@ describe Sexp do
601
575
 
602
576
  describe 'hash_size' do
603
577
  it 'should get value for hash node' do
604
- node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
578
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
605
579
  expect(node.hash_size).to eq 2
606
580
  end
607
581
 
608
582
  it 'should get value for bare_assoc_hash' do
609
583
  node =
610
584
  parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
611
- sexp_type: :bare_assoc_hash
585
+ node_type: :bare_assoc_hash
612
586
  )
613
587
  expect(node.hash_size).to eq 2
614
588
  end
@@ -616,14 +590,14 @@ describe Sexp do
616
590
 
617
591
  describe 'hash_keys' do
618
592
  it 'should get hash_keys for hash node' do
619
- node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
593
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
620
594
  expect(node.hash_keys).to eq %w[first_name last_name]
621
595
  end
622
596
 
623
597
  it 'should get hash_keys for bare_assoc_hash' do
624
598
  node =
625
599
  parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
626
- sexp_type: :bare_assoc_hash
600
+ node_type: :bare_assoc_hash
627
601
  )
628
602
  expect(node.hash_keys).to eq %w[first_name last_name]
629
603
  end
@@ -631,14 +605,14 @@ describe Sexp do
631
605
 
632
606
  describe 'hash_values' do
633
607
  it 'should get hash_values for hash node' do
634
- node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
608
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(node_type: :hash)
635
609
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
636
610
  end
637
611
 
638
612
  it 'should get hash_values for bare_assoc_hash' do
639
613
  node =
640
614
  parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
641
- sexp_type: :bare_assoc_hash
615
+ node_type: :bare_assoc_hash
642
616
  )
643
617
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
644
618
  end
@@ -648,7 +622,7 @@ describe Sexp do
648
622
  it 'should get key for assoc_new' do
649
623
  node =
650
624
  parse_content("add_user :user, first_name: 'Richard'").grep_node(
651
- sexp_type: :assoc_new
625
+ node_type: :assoc_new
652
626
  )
653
627
  expect(node.key).to eq 'first_name'
654
628
  end
@@ -658,23 +632,25 @@ describe Sexp do
658
632
  it 'should get value for assoc_new' do
659
633
  node =
660
634
  parse_content("add_user :user, first_name: 'Richard'").grep_node(
661
- sexp_type: :assoc_new
635
+ node_type: :assoc_new
662
636
  )
663
637
  expect(node.value).to eq 'Richard'
664
638
  end
665
639
 
666
- it 'should get value for assoc_new' do
667
- node =
668
- parse_content("add_user :user, first_name:").grep_node(
669
- sexp_type: :assoc_new
670
- )
671
- expect(node.value).to be_nil
640
+ if RUBY_VERSION.to_f >= 3.1
641
+ it 'should get value for assoc_new' do
642
+ node =
643
+ parse_content("add_user :user, first_name:").grep_node(
644
+ node_type: :assoc_new
645
+ )
646
+ expect(node.value).to be_nil
647
+ end
672
648
  end
673
649
 
674
650
  it 'should get array value for assoc_new' do
675
651
  node =
676
652
  parse_content("add_user :user, name: %w[Richard Huang]").grep_node(
677
- sexp_type: :assoc_new
653
+ node_type: :assoc_new
678
654
  )
679
655
  expect(node.value.array_values.map(&:to_s)).to eq ['Richard', 'Huang']
680
656
  end
@@ -682,65 +658,65 @@ describe Sexp do
682
658
 
683
659
  describe 'array_size' do
684
660
  it 'should get array size' do
685
- node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
661
+ node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
686
662
  expect(node.array_size).to eq 2
687
663
  end
688
664
 
689
665
  it 'should get 0' do
690
- node = parse_content('[]').grep_node(sexp_type: :array)
666
+ node = parse_content('[]').grep_node(node_type: :array)
691
667
  expect(node.array_size).to eq 0
692
668
  end
693
669
  end
694
670
 
695
671
  describe 'array_values' do
696
672
  it 'should get array values' do
697
- node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
673
+ node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
698
674
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
699
675
  end
700
676
 
701
677
  it 'should get empty array values' do
702
- node = parse_content('[]').grep_node(sexp_type: :array)
678
+ node = parse_content('[]').grep_node(node_type: :array)
703
679
  expect(node.array_values).to eq []
704
680
  end
705
681
 
706
682
  it 'should get array value with array and words_add' do
707
- node = parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
683
+ node = parse_content('%W{day week fortnight}').grep_node(node_type: :array)
708
684
  expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
709
685
  end
710
686
 
711
687
  it 'should get empty array values with array and words_add' do
712
- node = parse_content('%W{}').grep_node(sexp_type: :array)
688
+ node = parse_content('%W{}').grep_node(node_type: :array)
713
689
  expect(node.array_values.map(&:to_s)).to eq []
714
690
  end
715
691
 
716
692
  it 'should get array value with array and qwords_add' do
717
- node = parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
693
+ node = parse_content('%w(first_name last_name)').grep_node(node_type: :array)
718
694
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
719
695
  end
720
696
 
721
697
  it 'should get empty array values with array and qwords_add' do
722
- node = parse_content('%w()').grep_node(sexp_type: :array)
698
+ node = parse_content('%w()').grep_node(node_type: :array)
723
699
  expect(node.array_values.map(&:to_s)).to eq []
724
700
  end
725
701
 
726
702
  if RUBY_VERSION.to_i > 1
727
703
  it 'should get array value with array and symbols_add' do
728
- node = parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
704
+ node = parse_content('%I(first_name last_name)').grep_node(node_type: :array)
729
705
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
730
706
  end
731
707
 
732
708
  it 'should get empty array value with array and symbols_add' do
733
- node = parse_content('%I()').grep_node(sexp_type: :array)
709
+ node = parse_content('%I()').grep_node(node_type: :array)
734
710
  expect(node.array_values.map(&:to_s)).to eq []
735
711
  end
736
712
 
737
713
  it 'should get array value with array and qsymbols_add' do
738
- node = parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
714
+ node = parse_content('%i(first_name last_name)').grep_node(node_type: :array)
739
715
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
740
716
  end
741
717
 
742
718
  it 'should get empty array values with array and qsymbols_new' do
743
- node = parse_content('%i()').grep_node(sexp_type: :array)
719
+ node = parse_content('%i()').grep_node(node_type: :array)
744
720
  expect(node.array_values.map(&:to_s)).to eq []
745
721
  end
746
722
  end
@@ -748,7 +724,7 @@ describe Sexp do
748
724
 
749
725
  describe 'alias' do
750
726
  context 'method' do
751
- before { @node = parse_content('alias new old').grep_node(sexp_type: :alias) }
727
+ before { @node = parse_content('alias new old').grep_node(node_type: :alias) }
752
728
 
753
729
  it 'should get old_method' do
754
730
  expect(@node.old_method.to_s).to eq 'old'
@@ -760,7 +736,7 @@ describe Sexp do
760
736
  end
761
737
 
762
738
  context 'symbol' do
763
- before { @node = parse_content('alias :new :old').grep_node(sexp_type: :alias) }
739
+ before { @node = parse_content('alias :new :old').grep_node(node_type: :alias) }
764
740
 
765
741
  it 'should get old_method' do
766
742
  expect(@node.old_method.to_s).to eq 'old'
@@ -774,91 +750,94 @@ describe Sexp do
774
750
 
775
751
  describe 'to_object' do
776
752
  it 'should to array' do
777
- node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
753
+ node = parse_content("['first_name', 'last_name']").grep_node(node_type: :array)
778
754
  expect(node.to_object).to eq %w[first_name last_name]
779
755
  end
780
756
 
781
757
  it 'should to array with %w()' do
782
- node = parse_content('%w(new create)').grep_node(sexp_type: :array)
758
+ node = parse_content('%w(new create)').grep_node(node_type: :array)
783
759
  expect(node.to_object).to eq %w[new create]
784
760
  end
785
761
 
786
762
  it 'should to array with symbols' do
787
- node = parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
763
+ node = parse_content('[:first_name, :last_name]').grep_node(node_type: :array)
788
764
  expect(node.to_object).to eq %w[first_name last_name]
789
765
  end
790
766
 
791
767
  it 'should to empty array' do
792
- node = parse_content('[]').grep_node(sexp_type: :array)
768
+ node = parse_content('[]').grep_node(node_type: :array)
793
769
  expect(node.to_object).to eq []
794
770
  end
795
771
 
796
772
  it 'should to string' do
797
- node = parse_content("'richard'").grep_node(sexp_type: :string_literal)
773
+ node = parse_content("'richard'").grep_node(node_type: :string_literal)
798
774
  expect(node.to_object).to eq 'richard'
799
775
  end
800
776
  end
801
777
 
802
778
  describe 'to_s' do
803
779
  it 'should get to_s for string' do
804
- node = parse_content("'user'").grep_node(sexp_type: :string_literal)
780
+ node = parse_content("'user'").grep_node(node_type: :string_literal)
805
781
  expect(node.to_s).to eq 'user'
806
782
  end
807
783
 
808
784
  it 'should get to_s for symbol' do
809
- node = parse_content(':user').grep_node(sexp_type: :symbol_literal)
785
+ node = parse_content(':user').grep_node(node_type: :symbol_literal)
810
786
  expect(node.to_s).to eq 'user'
811
787
  end
812
788
 
813
789
  it 'should get to_s for const' do
814
- node = parse_content('User').grep_node(sexp_type: :@const)
790
+ node = parse_content('User').grep_node(node_type: :var_ref)
815
791
  expect(node.to_s).to eq 'User'
792
+ expect(node).to be_const
816
793
  end
817
794
 
818
795
  it 'should get to_s for ivar' do
819
- node = parse_content('@user').grep_node(sexp_type: :@ivar)
796
+ node = parse_content('@user').grep_node(node_type: :var_ref)
820
797
  expect(node.to_s).to eq '@user'
798
+ expect(node).not_to be_const
821
799
  end
822
800
 
823
- it 'should get to_s for period' do
824
- node = parse_content('@user.name').grep_node(sexp_type: :@period)
825
- expect(node.to_s).to eq '.'
801
+ it 'should get to_s for message' do
802
+ node = parse_content('@user.name').grep_node(node_type: :call)
803
+ expect(node.message.to_s).to eq 'name'
826
804
  end
827
805
 
828
806
  it 'should get to_s for class with module' do
829
- node = parse_content('ActiveRecord::Base').grep_node(sexp_type: :const_path_ref)
807
+ node = parse_content('ActiveRecord::Base').grep_node(node_type: :const_path_ref)
830
808
  expect(node.to_s).to eq 'ActiveRecord::Base'
831
809
  end
832
810
 
833
811
  it 'should get to_s for label' do
834
- node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
812
+ node = parse_content("{first_name: 'Richard'}").grep_node(node_type: :@label)
835
813
  expect(node.to_s).to eq 'first_name'
836
814
  end
837
815
 
838
816
  it 'should get to_s for call' do
839
- node = parse_content('current_user.post').grep_node(sexp_type: :call)
817
+ node = parse_content('current_user.post').grep_node(node_type: :call)
840
818
  expect(node.to_s).to eq 'current_user.post'
841
819
  end
842
820
 
843
821
  it 'should get to_s for top_const_ref' do
844
- node = parse_content('::User').grep_node(sexp_type: :top_const_ref)
822
+ node = parse_content('::User').grep_node(node_type: :top_const_ref)
845
823
  expect(node.to_s).to eq '::User'
846
824
  end
847
825
  end
848
826
 
849
827
  describe 'const?' do
850
828
  it 'should return true for const with var_ref' do
851
- node = parse_content('User.find').grep_node(sexp_type: :var_ref)
829
+ node = parse_content('User.find').grep_node(node_type: :var_ref)
852
830
  expect(node).to be_const
853
831
  end
854
832
 
855
- it 'should return true for const with @const' do
856
- node = parse_content('User.find').grep_node(sexp_type: :@const)
833
+ it 'should return true for const node' do
834
+ node = parse_content('User.find').grep_node(node_type: :var_ref)
835
+ expect(node.to_s).to eq 'User'
857
836
  expect(node).to be_const
858
837
  end
859
838
 
860
839
  it 'should return false for ivar' do
861
- node = parse_content('@user.find').grep_node(sexp_type: :@ivar)
840
+ node = parse_content('@user.find').grep_node(node_type: :var_ref)
862
841
  expect(node).not_to be_const
863
842
  end
864
843
  end
@@ -878,12 +857,24 @@ describe Sexp do
878
857
  end
879
858
 
880
859
  describe 'remove_line_and_column' do
881
- it 'should remove' do
882
- s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(:@ident, 'test')
860
+ it 'should keep the node accessible' do
861
+ node = parse_content('test_name').grep_node(node_type: :vcall)
862
+ expect(node.remove_line_and_column.node_type).to eq :vcall
863
+ expect(node.remove_line_and_column.to_s).to eq 'test_name'
864
+ end
865
+
866
+ it 'should consider structurally equal nodes as equal' do
867
+ node1 = parse_content('def a; foo.bar; end').grep_node(node_type: :call)
868
+ node2 = parse_content('def b; foo.bar; end').grep_node(node_type: :call)
869
+ expect(node1.remove_line_and_column).to eq node2.remove_line_and_column
870
+ expect(node1.remove_line_and_column.hash).to eq node2.remove_line_and_column.hash
883
871
  end
884
872
 
885
- it 'should remove child nodes' do
886
- s(:const_ref, s(:@const, 'Demo', s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
873
+ it 'should consider nodes at different positions as different' do
874
+ node1 = parse_content("def a\n foo.bar\nend").grep_node(node_type: :call)
875
+ node2 = parse_content("def b\n foo.bar\nend").grep_node(node_type: :call)
876
+ expect(node1).not_to eq node2
877
+ expect(node1.remove_line_and_column).to eq node2.remove_line_and_column
887
878
  end
888
879
  end
889
880
  end