code_analyzer 0.5.1 → 0.5.5

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.
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe Sexp do
4
- describe 'line' do
6
+ describe 'line_number' do
5
7
  before :each do
6
8
  content = <<-EOF
7
9
  class Demo
@@ -18,9 +20,12 @@ describe Sexp do
18
20
  end
19
21
  def condition
20
22
  if success?
21
- puts "unknown" if output?
23
+ puts "unknown" if !output?
22
24
  elsif fail?
25
+ 1..2
26
+ 3...4
23
27
  end
28
+ pp ::Rails.application
24
29
  end
25
30
  end
26
31
  EOF
@@ -39,6 +44,10 @@ describe Sexp do
39
44
  expect(@node.grep_node(sexp_type: :const_ref).line_number).to eq 1
40
45
  end
41
46
 
47
+ it 'should return top const line' do
48
+ expect(@node.grep_node(sexp_type: :top_const_ref).line_number).to eq 20
49
+ end
50
+
42
51
  it 'should return const path line' do
43
52
  expect(@node.grep_node(sexp_type: :const_path_ref).line_number).to eq 3
44
53
  end
@@ -70,6 +79,79 @@ describe Sexp do
70
79
  it 'should return if_mod line' do
71
80
  expect(@node.grep_node(sexp_type: :if_mod).line_number).to eq 15
72
81
  end
82
+
83
+ it 'should return unary line' do
84
+ expect(@node.grep_node(sexp_type: :unary).line_number).to eq 15
85
+ end
86
+
87
+ it 'should return assign line' do
88
+ expect(@node.grep_node(sexp_type: :assign).line_number).to eq 6
89
+ end
90
+
91
+ it 'should return paren line' do
92
+ expect(@node.grep_node(sexp_type: :paren).line_number).to eq 10
93
+ end
94
+
95
+ it 'should return dot2 line' do
96
+ expect(@node.grep_node(sexp_type: :dot2).line_number).to eq 17
97
+ end
98
+
99
+ it 'should return dot3 line' do
100
+ expect(@node.grep_node(sexp_type: :dot3).line_number).to eq 18
101
+ end
102
+
103
+ it 'should return params line' do
104
+ expect(@node.grep_node(sexp_type: :params).line_number).to be_nil
105
+ end
106
+
107
+ it 'should return params line if not empty' do
108
+ @node = parse_content(<<~CODE)
109
+ # @see Foo
110
+ def foo(a, b)
111
+ end
112
+ CODE
113
+ expect(@node.grep_node(sexp_type: :params).line_number).to eq 2
114
+ end
115
+
116
+ it 'should return stmts_add line' do
117
+ expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 13
118
+ end
119
+
120
+ context 'when a complex code is given' do
121
+ before :each do
122
+ @node = parse_content(<<~CODE)
123
+ def foo(num)
124
+ unless (num == 0 ? :zero, :other) || !@opts.right?
125
+ @bar = {}
126
+ end
127
+ end
128
+ CODE
129
+ end
130
+
131
+ it 'should return unless line' do
132
+ expect(@node.grep_node(sexp_type: :unless).line_number).to eq 2
133
+ end
134
+
135
+ it 'should return paren line' do
136
+ expect(@node.grep_node(sexp_type: :paren).line_number).to eq 1
137
+ end
138
+
139
+ it 'should return stmts_add line' do
140
+ expect(@node.grep_node(sexp_type: :stmts_add).line_number).to eq 2
141
+ end
142
+
143
+ it 'should return binary line' do
144
+ expect(@node.grep_node(sexp_type: :binary).line_number).to eq 2
145
+ end
146
+
147
+ it 'should return unary line' do
148
+ expect(@node.grep_node(sexp_type: :unary).line_number).to eq 2
149
+ end
150
+
151
+ it 'should return assign line' do
152
+ expect(@node.grep_node(sexp_type: :assign).line_number).to eq 3
153
+ end
154
+ end
73
155
  end
74
156
 
75
157
  describe 'grep_nodes' do
@@ -84,14 +166,12 @@ describe Sexp do
84
166
 
85
167
  it 'should get the call nodes with receiver current_user' do
86
168
  nodes = []
87
- @node.grep_nodes(sexp_type: :call, receiver: 'current_user') do |node|
88
- nodes << node
89
- end
169
+ @node.grep_nodes(sexp_type: :call, receiver: 'current_user') { |node| nodes << node }
90
170
  expect(nodes).to eq [
91
171
  s(
92
172
  :call,
93
173
  s(:vcall, s(:@ident, 'current_user', s(2, 8))),
94
- s(:@period, ".", s(2, 20)),
174
+ s(:@period, '.', s(2, 20)),
95
175
  s(:@ident, 'posts', s(2, 21))
96
176
  )
97
177
  ]
@@ -99,25 +179,23 @@ describe Sexp do
99
179
 
100
180
  it 'should get the call nodes with different messages' do
101
181
  nodes = []
102
- @node.grep_nodes(sexp_type: :call, message: %w[posts find]) do |node|
103
- nodes << node
104
- end
182
+ @node.grep_nodes(sexp_type: :call, message: %w[posts find]) { |node| nodes << node }
105
183
  expect(nodes).to eq [
106
184
  s(
107
185
  :call,
108
186
  s(
109
187
  :call,
110
188
  s(:vcall, s(:@ident, 'current_user', s(2, 8))),
111
- s(:@period, ".", s(2, 20)),
189
+ s(:@period, '.', s(2, 20)),
112
190
  s(:@ident, 'posts', s(2, 21))
113
191
  ),
114
- s(:@period, ".", s(2, 26)),
192
+ s(:@period, '.', s(2, 26)),
115
193
  s(:@ident, 'find', s(2, 27))
116
194
  ),
117
195
  s(
118
196
  :call,
119
197
  s(:vcall, s(:@ident, 'current_user', s(2, 8))),
120
- s(:@period, ".", s(2, 20)),
198
+ s(:@period, '.', s(2, 20)),
121
199
  s(:@ident, 'posts', s(2, 21))
122
200
  )
123
201
  ]
@@ -125,9 +203,7 @@ describe Sexp do
125
203
 
126
204
  it 'should get the vcall node with to_s' do
127
205
  nodes = []
128
- @node.grep_nodes(sexp_type: :vcall, to_s: 'current_user') do |node|
129
- nodes << node
130
- end
206
+ @node.grep_nodes(sexp_type: :vcall, to_s: 'current_user') { |node| nodes << node }
131
207
  expect(nodes).to eq [s(:vcall, s(:@ident, 'current_user', s(2, 8)))]
132
208
  end
133
209
  end
@@ -147,7 +223,7 @@ describe Sexp do
147
223
  expect(node).to eq s(
148
224
  :call,
149
225
  s(:vcall, s(:@ident, 'current_user', s(2, 8))),
150
- s(:@period, ".", s(2, 20)),
226
+ s(:@period, '.', s(2, 20)),
151
227
  s(:@ident, 'posts', s(2, 21))
152
228
  )
153
229
  end
@@ -170,8 +246,7 @@ describe Sexp do
170
246
 
171
247
  describe 'receiver' do
172
248
  it 'should get receiver of assign node' do
173
- node =
174
- parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
249
+ node = parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
175
250
  receiver = node.receiver
176
251
  expect(receiver.sexp_type).to eq :field
177
252
  expect(receiver.receiver.to_s).to eq 'user'
@@ -179,8 +254,7 @@ describe Sexp do
179
254
  end
180
255
 
181
256
  it 'should get receiver of field node' do
182
- node =
183
- parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
257
+ node = parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
184
258
  expect(node.receiver.to_s).to eq 'user'
185
259
  end
186
260
 
@@ -204,16 +278,12 @@ describe Sexp do
204
278
  end
205
279
 
206
280
  it 'should get receiver of method_add_arg' do
207
- node =
208
- parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
281
+ node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
209
282
  expect(node.receiver.to_s).to eq 'Post'
210
283
  end
211
284
 
212
285
  it 'should get receiver of method_add_block' do
213
- node =
214
- parse_content('Post.save do; end').grep_node(
215
- sexp_type: :method_add_block
216
- )
286
+ node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
217
287
  expect(node.receiver.to_s).to eq 'Post'
218
288
  end
219
289
  end
@@ -234,10 +304,7 @@ describe Sexp do
234
304
 
235
305
  describe 'base_class' do
236
306
  it 'should get base class of class node' do
237
- node =
238
- parse_content('class User < ActiveRecord::Base; end').grep_node(
239
- sexp_type: :class
240
- )
307
+ node = parse_content('class User < ActiveRecord::Base; end').grep_node(sexp_type: :class)
241
308
  expect(node.base_class.to_s).to eq 'ActiveRecord::Base'
242
309
  end
243
310
  end
@@ -263,10 +330,7 @@ describe Sexp do
263
330
  end
264
331
 
265
332
  it 'should get the message of command_call' do
266
- node =
267
- parse_content('map.resources :posts do; end').grep_node(
268
- sexp_type: :command_call
269
- )
333
+ node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
270
334
  expect(node.message.to_s).to eq 'resources'
271
335
  end
272
336
 
@@ -291,46 +355,34 @@ describe Sexp do
291
355
  end
292
356
 
293
357
  it 'should get the message of method_add_arg' do
294
- node =
295
- parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
358
+ node = parse_content('Post.find(:all)').grep_node(sexp_type: :method_add_arg)
296
359
  expect(node.message.to_s).to eq 'find'
297
360
  end
298
361
 
299
362
  it 'should get the message of method_add_block' do
300
- node =
301
- parse_content('Post.save do; end').grep_node(
302
- sexp_type: :method_add_block
303
- )
363
+ node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
304
364
  expect(node.message.to_s).to eq 'save'
305
365
  end
306
366
  end
307
367
 
308
368
  describe 'arguments' do
309
369
  it 'should get the arguments of command' do
310
- node =
311
- parse_content('resources :posts do; end').grep_node(sexp_type: :command)
370
+ node = parse_content('resources :posts do; end').grep_node(sexp_type: :command)
312
371
  expect(node.arguments.sexp_type).to eq :args_add_block
313
372
  end
314
373
 
315
374
  it 'should get the arguments of command_call' do
316
- node =
317
- parse_content('map.resources :posts do; end').grep_node(
318
- sexp_type: :command_call
319
- )
375
+ node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
320
376
  expect(node.arguments.sexp_type).to eq :args_add_block
321
377
  end
322
378
 
323
379
  it 'should get the arguments of method_add_arg' do
324
- node =
325
- parse_content('User.find(:all)').grep_node(sexp_type: :method_add_arg)
380
+ node = parse_content('User.find(:all)').grep_node(sexp_type: :method_add_arg)
326
381
  expect(node.arguments.sexp_type).to eq :args_add_block
327
382
  end
328
383
 
329
384
  it 'should get the arguments of method_add_block' do
330
- node =
331
- parse_content('Post.save(false) do; end').grep_node(
332
- sexp_type: :method_add_block
333
- )
385
+ node = parse_content('Post.save(false) do; end').grep_node(sexp_type: :method_add_block)
334
386
  expect(node.arguments.sexp_type).to eq :args_add_block
335
387
  end
336
388
  end
@@ -344,49 +396,30 @@ describe Sexp do
344
396
 
345
397
  describe 'all' do
346
398
  it 'should get all arguments' do
347
- node =
348
- parse_content("puts 'hello', 'world'").grep_node(
349
- sexp_type: :args_add_block
350
- )
399
+ node = parse_content("puts 'hello', 'world'").grep_node(sexp_type: :args_add_block)
351
400
  expect(node.all.map(&:to_s)).to eq %w[hello world]
352
401
  end
353
402
 
354
403
  it 'should get all arguments with &:' do
355
- node =
356
- parse_content('user.posts.map(&:title)').grep_node(
357
- sexp_type: :args_add_block
358
- )
404
+ node = parse_content('user.posts.map(&:title)').grep_node(sexp_type: :args_add_block)
359
405
  expect(node.all.map(&:to_s)).to eq %w[title]
360
406
  end
361
407
 
362
408
  it 'should get all arguments with command_call node' do
363
- node =
364
- parse_content('options_for_select(Account.get_business current_user)')
365
- .grep_node(sexp_type: :args_add)
409
+ node = parse_content('options_for_select(Account.get_business current_user)').grep_node(sexp_type: :args_add)
366
410
  expect(node.all).to eq [
367
411
  s(
368
412
  :command_call,
369
413
  s(:var_ref, s(:@const, 'Account', s(1, 19))),
370
- s(:@period, ".", s(1, 26)),
414
+ s(:@period, '.', s(1, 26)),
371
415
  s(:@ident, 'get_business', s(1, 27)),
372
- s(
373
- :args_add_block,
374
- s(
375
- :args_add,
376
- s(:args_new),
377
- s(:vcall, s(:@ident, 'current_user', s(1, 40)))
378
- ),
379
- false
380
- )
416
+ s(:args_add_block, s(:args_add, s(:args_new), s(:vcall, s(:@ident, 'current_user', s(1, 40)))), false)
381
417
  )
382
418
  ]
383
419
  end
384
420
 
385
421
  it 'no error for args_add_star' do
386
- node =
387
- parse_content("send(:\"\#{route}_url\", *args)").grep_node(
388
- sexp_type: :args_add_block
389
- )
422
+ node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(sexp_type: :args_add_block)
390
423
  expect { node.all }.not_to raise_error
391
424
  end
392
425
  end
@@ -403,8 +436,7 @@ describe Sexp do
403
436
  end
404
437
 
405
438
  it 'should get conditional statement of elsif' do
406
- node =
407
- parse_content('if true; elsif false; end').grep_node(sexp_type: :elsif)
439
+ node = parse_content('if true; elsif false; end').grep_node(sexp_type: :elsif)
408
440
  expect(node.conditional_statement.to_s).to eq 'false'
409
441
  end
410
442
 
@@ -414,8 +446,7 @@ describe Sexp do
414
446
  end
415
447
 
416
448
  it 'should get conditional statement of unless_mod' do
417
- node =
418
- parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
449
+ node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
419
450
  expect(node.conditional_statement.to_s).to eq 'false'
420
451
  end
421
452
 
@@ -427,9 +458,7 @@ describe Sexp do
427
458
 
428
459
  describe 'all_conditions' do
429
460
  it 'should get all conditions' do
430
- node =
431
- parse_content('user == current_user && user.valid? || user.admin?')
432
- .grep_node(sexp_type: :binary)
461
+ node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(sexp_type: :binary)
433
462
  expect(node.all_conditions.size).to eq 3
434
463
  end
435
464
  end
@@ -463,8 +492,7 @@ describe Sexp do
463
492
  end
464
493
 
465
494
  it 'should get body of module' do
466
- node =
467
- parse_content('module Enumerable; end').grep_node(sexp_type: :module)
495
+ node = parse_content('module Enumerable; end').grep_node(sexp_type: :module)
468
496
  expect(node.body.sexp_type).to eq :bodystmt
469
497
  end
470
498
 
@@ -474,22 +502,17 @@ describe Sexp do
474
502
  end
475
503
 
476
504
  it 'should get body of elsif' do
477
- node =
478
- parse_content("if true; elsif true; 'OK'; end").grep_node(
479
- sexp_type: :elsif
480
- )
505
+ node = parse_content("if true; elsif true; 'OK'; end").grep_node(sexp_type: :elsif)
481
506
  expect(node.body.sexp_type).to eq :stmts_add
482
507
  end
483
508
 
484
509
  it 'should get body of unless' do
485
- node =
486
- parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
510
+ node = parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
487
511
  expect(node.body.sexp_type).to eq :stmts_add
488
512
  end
489
513
 
490
514
  it 'should get body of else' do
491
- node =
492
- parse_content("if true; else; 'OK'; end").grep_node(sexp_type: :else)
515
+ node = parse_content("if true; else; 'OK'; end").grep_node(sexp_type: :else)
493
516
  expect(node.body.sexp_type).to eq :stmts_add
494
517
  end
495
518
 
@@ -499,8 +522,7 @@ describe Sexp do
499
522
  end
500
523
 
501
524
  it 'should get body of unless_mod' do
502
- node =
503
- parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
525
+ node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
504
526
  expect(node.body.to_s).to eq 'OK'
505
527
  end
506
528
 
@@ -512,9 +534,7 @@ describe Sexp do
512
534
 
513
535
  describe 'block' do
514
536
  it 'sould get block of method_add_block node' do
515
- node =
516
- parse_content('resources :posts do; resources :comments; end')
517
- .grep_node(sexp_type: :method_add_block)
537
+ node = parse_content('resources :posts do; resources :comments; end').grep_node(sexp_type: :method_add_block)
518
538
  expect(node.block_node.sexp_type).to eq :do_block
519
539
  end
520
540
  end
@@ -522,40 +542,29 @@ describe Sexp do
522
542
  describe 'statements' do
523
543
  it 'should get statements of do_block node' do
524
544
  node =
525
- parse_content(
526
- 'resources :posts do; resources :comments; resources :like; end'
527
- )
528
- .grep_node(sexp_type: :do_block)
545
+ parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(sexp_type: :do_block)
529
546
  expect(node.statements.size).to eq 2
530
547
  end
531
548
 
532
549
  it 'should get statements of bodystmt node' do
533
- node =
534
- parse_content('class User; def login?; end; def admin?; end; end')
535
- .grep_node(sexp_type: :bodystmt)
550
+ node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(sexp_type: :bodystmt)
536
551
  expect(node.statements.size).to eq 2
537
552
  end
538
553
  end
539
554
 
540
555
  describe 'exception_classes' do
541
556
  it 'should get exception classes of rescue node' do
542
- node =
543
- parse_content('def test; rescue CustomException; end').grep_node(
544
- sexp_type: :rescue
545
- )
557
+ node = parse_content('def test; rescue CustomException; end').grep_node(sexp_type: :rescue)
546
558
  expect(node.exception_classes.first.to_s).to eq 'CustomException'
547
559
  end
548
560
 
549
561
  it 'should get empty of empty rescue node' do
550
- node =
551
- parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
562
+ node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
552
563
  expect(node.exception_classes.first.to_s).to eq ''
553
564
  end
554
565
 
555
566
  it 'should get exception classes of rescue node for multiple exceptions' do
556
- node =
557
- parse_content('def test; rescue StandardError, CustomException; end')
558
- .grep_node(sexp_type: :rescue)
567
+ node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(sexp_type: :rescue)
559
568
  expect(node.exception_classes.first.to_s).to eq 'StandardError'
560
569
  expect(node.exception_classes.last.to_s).to eq 'CustomException'
561
570
  end
@@ -563,36 +572,28 @@ describe Sexp do
563
572
 
564
573
  describe 'exception_variable' do
565
574
  it 'should get exception varible of rescue node' do
566
- node =
567
- parse_content('def test; rescue => e; end').grep_node(
568
- sexp_type: :rescue
569
- )
575
+ node = parse_content('def test; rescue => e; end').grep_node(sexp_type: :rescue)
570
576
  expect(node.exception_variable.to_s).to eq 'e'
571
577
  end
572
578
 
573
579
  it 'should get empty of empty rescue node' do
574
- node =
575
- parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
580
+ node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
576
581
  expect(node.exception_variable.to_s).to eq ''
577
582
  end
578
583
  end
579
584
 
580
585
  describe 'hash_value' do
581
586
  it 'should get value for hash node' do
582
- node =
583
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
584
- sexp_type: :hash
585
- )
587
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
586
588
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
587
589
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
588
590
  end
589
591
 
590
592
  it 'should get value for bare_assoc_hash' do
591
593
  node =
592
- parse_content(
593
- "add_user :user, first_name: 'Richard', last_name: 'Huang'"
594
+ parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
595
+ sexp_type: :bare_assoc_hash
594
596
  )
595
- .grep_node(sexp_type: :bare_assoc_hash)
596
597
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
597
598
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
598
599
  end
@@ -600,67 +601,88 @@ describe Sexp do
600
601
 
601
602
  describe 'hash_size' do
602
603
  it 'should get value for hash node' do
603
- node =
604
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
605
- sexp_type: :hash
606
- )
604
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
607
605
  expect(node.hash_size).to eq 2
608
606
  end
609
607
 
610
608
  it 'should get value for bare_assoc_hash' do
611
609
  node =
612
- parse_content(
613
- "add_user :user, first_name: 'Richard', last_name: 'Huang'"
610
+ parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
611
+ sexp_type: :bare_assoc_hash
614
612
  )
615
- .grep_node(sexp_type: :bare_assoc_hash)
616
613
  expect(node.hash_size).to eq 2
617
614
  end
618
615
  end
619
616
 
620
617
  describe 'hash_keys' do
621
618
  it 'should get hash_keys for hash node' do
622
- node =
623
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
624
- sexp_type: :hash
625
- )
619
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
626
620
  expect(node.hash_keys).to eq %w[first_name last_name]
627
621
  end
628
622
 
629
623
  it 'should get hash_keys for bare_assoc_hash' do
630
624
  node =
631
- parse_content(
632
- "add_user :user, first_name: 'Richard', last_name: 'Huang'"
625
+ parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
626
+ sexp_type: :bare_assoc_hash
633
627
  )
634
- .grep_node(sexp_type: :bare_assoc_hash)
635
628
  expect(node.hash_keys).to eq %w[first_name last_name]
636
629
  end
637
630
  end
638
631
 
639
632
  describe 'hash_values' do
640
633
  it 'should get hash_values for hash node' do
641
- node =
642
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
643
- sexp_type: :hash
644
- )
634
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
645
635
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
646
636
  end
647
637
 
648
638
  it 'should get hash_values for bare_assoc_hash' do
649
639
  node =
650
- parse_content(
651
- "add_user :user, first_name: 'Richard', last_name: 'Huang'"
640
+ parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(
641
+ sexp_type: :bare_assoc_hash
652
642
  )
653
- .grep_node(sexp_type: :bare_assoc_hash)
654
643
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
655
644
  end
656
645
  end
657
646
 
658
- describe 'array_size' do
659
- it 'should get array size' do
647
+ describe 'key' do
648
+ it 'should get key for assoc_new' do
649
+ node =
650
+ parse_content("add_user :user, first_name: 'Richard'").grep_node(
651
+ sexp_type: :assoc_new
652
+ )
653
+ expect(node.key).to eq 'first_name'
654
+ end
655
+ end
656
+
657
+ describe 'value' do
658
+ it 'should get value for assoc_new' do
659
+ node =
660
+ parse_content("add_user :user, first_name: 'Richard'").grep_node(
661
+ sexp_type: :assoc_new
662
+ )
663
+ expect(node.value).to eq 'Richard'
664
+ end
665
+
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
672
+ end
673
+
674
+ it 'should get array value for assoc_new' do
660
675
  node =
661
- parse_content("['first_name', 'last_name']").grep_node(
662
- sexp_type: :array
676
+ parse_content("add_user :user, name: %w[Richard Huang]").grep_node(
677
+ sexp_type: :assoc_new
663
678
  )
679
+ expect(node.value.array_values.map(&:to_s)).to eq ['Richard', 'Huang']
680
+ end
681
+ end
682
+
683
+ describe 'array_size' do
684
+ it 'should get array size' do
685
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
664
686
  expect(node.array_size).to eq 2
665
687
  end
666
688
 
@@ -672,10 +694,7 @@ describe Sexp do
672
694
 
673
695
  describe 'array_values' do
674
696
  it 'should get array values' do
675
- node =
676
- parse_content("['first_name', 'last_name']").grep_node(
677
- sexp_type: :array
678
- )
697
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
679
698
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
680
699
  end
681
700
 
@@ -685,8 +704,7 @@ describe Sexp do
685
704
  end
686
705
 
687
706
  it 'should get array value with array and words_add' do
688
- node =
689
- parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
707
+ node = parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
690
708
  expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
691
709
  end
692
710
 
@@ -696,8 +714,7 @@ describe Sexp do
696
714
  end
697
715
 
698
716
  it 'should get array value with array and qwords_add' do
699
- node =
700
- parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
717
+ node = parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
701
718
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
702
719
  end
703
720
 
@@ -708,8 +725,7 @@ describe Sexp do
708
725
 
709
726
  if RUBY_VERSION.to_i > 1
710
727
  it 'should get array value with array and symbols_add' do
711
- node =
712
- parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
728
+ node = parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
713
729
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
714
730
  end
715
731
 
@@ -719,8 +735,7 @@ describe Sexp do
719
735
  end
720
736
 
721
737
  it 'should get array value with array and qsymbols_add' do
722
- node =
723
- parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
738
+ node = parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
724
739
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
725
740
  end
726
741
 
@@ -733,9 +748,7 @@ describe Sexp do
733
748
 
734
749
  describe 'alias' do
735
750
  context 'method' do
736
- before do
737
- @node = parse_content('alias new old').grep_node(sexp_type: :alias)
738
- end
751
+ before { @node = parse_content('alias new old').grep_node(sexp_type: :alias) }
739
752
 
740
753
  it 'should get old_method' do
741
754
  expect(@node.old_method.to_s).to eq 'old'
@@ -747,9 +760,7 @@ describe Sexp do
747
760
  end
748
761
 
749
762
  context 'symbol' do
750
- before do
751
- @node = parse_content('alias :new :old').grep_node(sexp_type: :alias)
752
- end
763
+ before { @node = parse_content('alias :new :old').grep_node(sexp_type: :alias) }
753
764
 
754
765
  it 'should get old_method' do
755
766
  expect(@node.old_method.to_s).to eq 'old'
@@ -763,10 +774,7 @@ describe Sexp do
763
774
 
764
775
  describe 'to_object' do
765
776
  it 'should to array' do
766
- node =
767
- parse_content("['first_name', 'last_name']").grep_node(
768
- sexp_type: :array
769
- )
777
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
770
778
  expect(node.to_object).to eq %w[first_name last_name]
771
779
  end
772
780
 
@@ -776,8 +784,7 @@ describe Sexp do
776
784
  end
777
785
 
778
786
  it 'should to array with symbols' do
779
- node =
780
- parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
787
+ node = parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
781
788
  expect(node.to_object).to eq %w[first_name last_name]
782
789
  end
783
790
 
@@ -819,16 +826,12 @@ describe Sexp do
819
826
  end
820
827
 
821
828
  it 'should get to_s for class with module' do
822
- node =
823
- parse_content('ActiveRecord::Base').grep_node(
824
- sexp_type: :const_path_ref
825
- )
829
+ node = parse_content('ActiveRecord::Base').grep_node(sexp_type: :const_path_ref)
826
830
  expect(node.to_s).to eq 'ActiveRecord::Base'
827
831
  end
828
832
 
829
833
  it 'should get to_s for label' do
830
- node =
831
- parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
834
+ node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
832
835
  expect(node.to_s).to eq 'first_name'
833
836
  end
834
837
 
@@ -876,15 +879,11 @@ describe Sexp do
876
879
 
877
880
  describe 'remove_line_and_column' do
878
881
  it 'should remove' do
879
- s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(
880
- :@ident,
881
- 'test'
882
- )
882
+ s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(:@ident, 'test')
883
883
  end
884
884
 
885
885
  it 'should remove child nodes' do
886
- s(:const_ref, s(:@const, 'Demo', s(1, 12)))
887
- .remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
886
+ s(:const_ref, s(:@const, 'Demo', s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
888
887
  end
889
888
  end
890
889
  end