code_analyzer 0.5.0 → 0.5.4

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,94 +166,45 @@ 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
90
- if RUBY_VERSION == '1.9.2'
91
- expect(nodes).to eq [
92
- s(
93
- :call,
94
- s(:var_ref, s(:@ident, 'current_user', s(2, 8))),
95
- s(:@period, ".", s(2, 20)),
96
- s(:@ident, 'posts', s(2, 21))
97
- )
98
- ]
99
- else
100
- expect(nodes).to eq [
101
- s(
102
- :call,
103
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
104
- s(:@period, ".", s(2, 20)),
105
- s(:@ident, 'posts', s(2, 21))
106
- )
107
- ]
108
- end
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
+ ]
109
178
  end
110
179
 
111
180
  it 'should get the call nodes with different messages' do
112
181
  nodes = []
113
- @node.grep_nodes(sexp_type: :call, message: %w[posts find]) do |node|
114
- nodes << node
115
- end
116
- if RUBY_VERSION == '1.9.2'
117
- expect(nodes).to eq [
118
- s(
119
- :call,
120
- s(
121
- :call,
122
- s(:var_ref, s(:@ident, 'current_user', s(2, 8))),
123
- s(:@period, ".", s(2, 20)),
124
- s(:@ident, 'posts', s(2, 21))
125
- ),
126
- s(:@period, ".", s(2, 26)),
127
- s(:@ident, 'find', s(2, 27))
128
- ),
129
- s(
130
- :call,
131
- s(:var_ref, s(:@ident, 'current_user', s(2, 8))),
132
- s(:@period, ".", s(2, 20)),
133
- s(:@ident, 'posts', s(2, 21))
134
- )
135
- ]
136
- else
137
- expect(nodes).to eq [
138
- s(
139
- :call,
140
- s(
141
- :call,
142
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
143
- s(:@period, ".", s(2, 20)),
144
- s(:@ident, 'posts', s(2, 21))
145
- ),
146
- s(:@period, ".", s(2, 26)),
147
- s(:@ident, 'find', s(2, 27))
148
- ),
182
+ @node.grep_nodes(sexp_type: :call, message: %w[posts find]) { |node| nodes << node }
183
+ expect(nodes).to eq [
184
+ s(
185
+ :call,
149
186
  s(
150
187
  :call,
151
188
  s(:vcall, s(:@ident, 'current_user', s(2, 8))),
152
- s(:@period, ".", s(2, 20)),
189
+ s(:@period, '.', s(2, 20)),
153
190
  s(:@ident, 'posts', s(2, 21))
154
- )
155
- ]
156
- end
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
+ )
201
+ ]
157
202
  end
158
203
 
159
- if RUBY_VERSION == '1.9.2'
160
- it 'should get the var_ref node with to_s' do
161
- nodes = []
162
- @node.grep_nodes(sexp_type: :var_ref, to_s: 'current_user') do |node|
163
- nodes << node
164
- end
165
- expect(nodes).to eq [s(:var_ref, s(:@ident, 'current_user', s(2, 8)))]
166
- end
167
- else
168
- it 'should get the vcall node with to_s' do
169
- nodes = []
170
- @node.grep_nodes(sexp_type: :vcall, to_s: 'current_user') do |node|
171
- nodes << node
172
- end
173
- expect(nodes).to eq [s(:vcall, s(:@ident, 'current_user', s(2, 8)))]
174
- end
204
+ it 'should get the vcall node with to_s' do
205
+ 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)))]
175
208
  end
176
209
  end
177
210
 
@@ -187,21 +220,12 @@ describe Sexp do
187
220
 
188
221
  it 'should get first node with empty argument' do
189
222
  node = @node.grep_node(sexp_type: :call, receiver: 'current_user')
190
- if RUBY_VERSION == '1.9.2'
191
- expect(node).to eq s(
192
- :call,
193
- s(:var_ref, s(:@ident, 'current_user', s(2, 8))),
194
- s(:@period, ".", s(2, 20)),
195
- s(:@ident, 'posts', s(2, 21))
196
- )
197
- else
198
- expect(node).to eq s(
199
- :call,
200
- s(:vcall, s(:@ident, 'current_user', s(2, 8))),
201
- s(:@period, ".", s(2, 20)),
202
- s(:@ident, 'posts', s(2, 21))
203
- )
204
- end
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
229
  end
206
230
  end
207
231
 
@@ -222,8 +246,7 @@ describe Sexp do
222
246
 
223
247
  describe 'receiver' do
224
248
  it 'should get receiver of assign node' do
225
- node =
226
- parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
249
+ node = parse_content('user.name = params[:name]').grep_node(sexp_type: :assign)
227
250
  receiver = node.receiver
228
251
  expect(receiver.sexp_type).to eq :field
229
252
  expect(receiver.receiver.to_s).to eq 'user'
@@ -231,8 +254,7 @@ describe Sexp do
231
254
  end
232
255
 
233
256
  it 'should get receiver of field node' do
234
- node =
235
- parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
257
+ node = parse_content('user.name = params[:name]').grep_node(sexp_type: :field)
236
258
  expect(node.receiver.to_s).to eq 'user'
237
259
  end
238
260
 
@@ -256,16 +278,12 @@ describe Sexp do
256
278
  end
257
279
 
258
280
  it 'should get receiver of method_add_arg' do
259
- node =
260
- 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)
261
282
  expect(node.receiver.to_s).to eq 'Post'
262
283
  end
263
284
 
264
285
  it 'should get receiver of method_add_block' do
265
- node =
266
- parse_content('Post.save do; end').grep_node(
267
- sexp_type: :method_add_block
268
- )
286
+ node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
269
287
  expect(node.receiver.to_s).to eq 'Post'
270
288
  end
271
289
  end
@@ -286,10 +304,7 @@ describe Sexp do
286
304
 
287
305
  describe 'base_class' do
288
306
  it 'should get base class of class node' do
289
- node =
290
- parse_content('class User < ActiveRecord::Base; end').grep_node(
291
- sexp_type: :class
292
- )
307
+ node = parse_content('class User < ActiveRecord::Base; end').grep_node(sexp_type: :class)
293
308
  expect(node.base_class.to_s).to eq 'ActiveRecord::Base'
294
309
  end
295
310
  end
@@ -315,10 +330,7 @@ describe Sexp do
315
330
  end
316
331
 
317
332
  it 'should get the message of command_call' do
318
- node =
319
- parse_content('map.resources :posts do; end').grep_node(
320
- sexp_type: :command_call
321
- )
333
+ node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
322
334
  expect(node.message.to_s).to eq 'resources'
323
335
  end
324
336
 
@@ -343,46 +355,34 @@ describe Sexp do
343
355
  end
344
356
 
345
357
  it 'should get the message of method_add_arg' do
346
- node =
347
- 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)
348
359
  expect(node.message.to_s).to eq 'find'
349
360
  end
350
361
 
351
362
  it 'should get the message of method_add_block' do
352
- node =
353
- parse_content('Post.save do; end').grep_node(
354
- sexp_type: :method_add_block
355
- )
363
+ node = parse_content('Post.save do; end').grep_node(sexp_type: :method_add_block)
356
364
  expect(node.message.to_s).to eq 'save'
357
365
  end
358
366
  end
359
367
 
360
368
  describe 'arguments' do
361
369
  it 'should get the arguments of command' do
362
- node =
363
- parse_content('resources :posts do; end').grep_node(sexp_type: :command)
370
+ node = parse_content('resources :posts do; end').grep_node(sexp_type: :command)
364
371
  expect(node.arguments.sexp_type).to eq :args_add_block
365
372
  end
366
373
 
367
374
  it 'should get the arguments of command_call' do
368
- node =
369
- parse_content('map.resources :posts do; end').grep_node(
370
- sexp_type: :command_call
371
- )
375
+ node = parse_content('map.resources :posts do; end').grep_node(sexp_type: :command_call)
372
376
  expect(node.arguments.sexp_type).to eq :args_add_block
373
377
  end
374
378
 
375
379
  it 'should get the arguments of method_add_arg' do
376
- node =
377
- 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)
378
381
  expect(node.arguments.sexp_type).to eq :args_add_block
379
382
  end
380
383
 
381
384
  it 'should get the arguments of method_add_block' do
382
- node =
383
- parse_content('Post.save(false) do; end').grep_node(
384
- sexp_type: :method_add_block
385
- )
385
+ node = parse_content('Post.save(false) do; end').grep_node(sexp_type: :method_add_block)
386
386
  expect(node.arguments.sexp_type).to eq :args_add_block
387
387
  end
388
388
  end
@@ -396,69 +396,30 @@ describe Sexp do
396
396
 
397
397
  describe 'all' do
398
398
  it 'should get all arguments' do
399
- node =
400
- parse_content("puts 'hello', 'world'").grep_node(
401
- sexp_type: :args_add_block
402
- )
399
+ node = parse_content("puts 'hello', 'world'").grep_node(sexp_type: :args_add_block)
403
400
  expect(node.all.map(&:to_s)).to eq %w[hello world]
404
401
  end
405
402
 
406
403
  it 'should get all arguments with &:' do
407
- node =
408
- parse_content('user.posts.map(&:title)').grep_node(
409
- sexp_type: :args_add_block
410
- )
404
+ node = parse_content('user.posts.map(&:title)').grep_node(sexp_type: :args_add_block)
411
405
  expect(node.all.map(&:to_s)).to eq %w[title]
412
406
  end
413
407
 
414
408
  it 'should get all arguments with command_call node' do
415
- node =
416
- parse_content('options_for_select(Account.get_business current_user)')
417
- .grep_node(sexp_type: :args_add)
418
- if RUBY_VERSION == '1.9.2'
419
- expect(node.all).to eq [
420
- s(
421
- :command_call,
422
- s(:var_ref, s(:@const, 'Account', s(1, 19))),
423
- :".",
424
- s(:@ident, 'get_business', s(1, 27)),
425
- s(
426
- :args_add_block,
427
- s(
428
- :args_add,
429
- s(:args_new),
430
- s(:var_ref, s(:@ident, 'current_user', s(1, 40)))
431
- ),
432
- false
433
- )
434
- )
435
- ]
436
- else
437
- expect(node.all).to eq [
438
- s(
439
- :command_call,
440
- s(:var_ref, s(:@const, 'Account', s(1, 19))),
441
- s(:@period, ".", s(1, 26)),
442
- s(:@ident, 'get_business', s(1, 27)),
443
- s(
444
- :args_add_block,
445
- s(
446
- :args_add,
447
- s(:args_new),
448
- s(:vcall, s(:@ident, 'current_user', s(1, 40)))
449
- ),
450
- false
451
- )
452
- )
453
- ]
454
- end
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
+ ]
455
419
  end
456
420
 
457
421
  it 'no error for args_add_star' do
458
- node =
459
- parse_content("send(:\"\#{route}_url\", *args)").grep_node(
460
- sexp_type: :args_add_block
461
- )
422
+ node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(sexp_type: :args_add_block)
462
423
  expect { node.all }.not_to raise_error
463
424
  end
464
425
  end
@@ -475,8 +436,7 @@ describe Sexp do
475
436
  end
476
437
 
477
438
  it 'should get conditional statement of elsif' do
478
- node =
479
- 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)
480
440
  expect(node.conditional_statement.to_s).to eq 'false'
481
441
  end
482
442
 
@@ -486,8 +446,7 @@ describe Sexp do
486
446
  end
487
447
 
488
448
  it 'should get conditional statement of unless_mod' do
489
- node =
490
- parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
449
+ node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
491
450
  expect(node.conditional_statement.to_s).to eq 'false'
492
451
  end
493
452
 
@@ -499,9 +458,7 @@ describe Sexp do
499
458
 
500
459
  describe 'all_conditions' do
501
460
  it 'should get all conditions' do
502
- node =
503
- parse_content('user == current_user && user.valid? || user.admin?')
504
- .grep_node(sexp_type: :binary)
461
+ node = parse_content('user == current_user && user.valid? || user.admin?').grep_node(sexp_type: :binary)
505
462
  expect(node.all_conditions.size).to eq 3
506
463
  end
507
464
  end
@@ -535,8 +492,7 @@ describe Sexp do
535
492
  end
536
493
 
537
494
  it 'should get body of module' do
538
- node =
539
- parse_content('module Enumerable; end').grep_node(sexp_type: :module)
495
+ node = parse_content('module Enumerable; end').grep_node(sexp_type: :module)
540
496
  expect(node.body.sexp_type).to eq :bodystmt
541
497
  end
542
498
 
@@ -546,22 +502,17 @@ describe Sexp do
546
502
  end
547
503
 
548
504
  it 'should get body of elsif' do
549
- node =
550
- parse_content("if true; elsif true; 'OK'; end").grep_node(
551
- sexp_type: :elsif
552
- )
505
+ node = parse_content("if true; elsif true; 'OK'; end").grep_node(sexp_type: :elsif)
553
506
  expect(node.body.sexp_type).to eq :stmts_add
554
507
  end
555
508
 
556
509
  it 'should get body of unless' do
557
- node =
558
- parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
510
+ node = parse_content("unless true; 'OK'; end").grep_node(sexp_type: :unless)
559
511
  expect(node.body.sexp_type).to eq :stmts_add
560
512
  end
561
513
 
562
514
  it 'should get body of else' do
563
- node =
564
- 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)
565
516
  expect(node.body.sexp_type).to eq :stmts_add
566
517
  end
567
518
 
@@ -571,8 +522,7 @@ describe Sexp do
571
522
  end
572
523
 
573
524
  it 'should get body of unless_mod' do
574
- node =
575
- parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
525
+ node = parse_content("'OK' unless false").grep_node(sexp_type: :unless_mod)
576
526
  expect(node.body.to_s).to eq 'OK'
577
527
  end
578
528
 
@@ -584,9 +534,7 @@ describe Sexp do
584
534
 
585
535
  describe 'block' do
586
536
  it 'sould get block of method_add_block node' do
587
- node =
588
- parse_content('resources :posts do; resources :comments; end')
589
- .grep_node(sexp_type: :method_add_block)
537
+ node = parse_content('resources :posts do; resources :comments; end').grep_node(sexp_type: :method_add_block)
590
538
  expect(node.block_node.sexp_type).to eq :do_block
591
539
  end
592
540
  end
@@ -594,40 +542,29 @@ describe Sexp do
594
542
  describe 'statements' do
595
543
  it 'should get statements of do_block node' do
596
544
  node =
597
- parse_content(
598
- 'resources :posts do; resources :comments; resources :like; end'
599
- )
600
- .grep_node(sexp_type: :do_block)
545
+ parse_content('resources :posts do; resources :comments; resources :like; end').grep_node(sexp_type: :do_block)
601
546
  expect(node.statements.size).to eq 2
602
547
  end
603
548
 
604
549
  it 'should get statements of bodystmt node' do
605
- node =
606
- parse_content('class User; def login?; end; def admin?; end; end')
607
- .grep_node(sexp_type: :bodystmt)
550
+ node = parse_content('class User; def login?; end; def admin?; end; end').grep_node(sexp_type: :bodystmt)
608
551
  expect(node.statements.size).to eq 2
609
552
  end
610
553
  end
611
554
 
612
555
  describe 'exception_classes' do
613
556
  it 'should get exception classes of rescue node' do
614
- node =
615
- parse_content('def test; rescue CustomException; end').grep_node(
616
- sexp_type: :rescue
617
- )
557
+ node = parse_content('def test; rescue CustomException; end').grep_node(sexp_type: :rescue)
618
558
  expect(node.exception_classes.first.to_s).to eq 'CustomException'
619
559
  end
620
560
 
621
561
  it 'should get empty of empty rescue node' do
622
- node =
623
- parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
562
+ node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
624
563
  expect(node.exception_classes.first.to_s).to eq ''
625
564
  end
626
565
 
627
566
  it 'should get exception classes of rescue node for multiple exceptions' do
628
- node =
629
- parse_content('def test; rescue StandardError, CustomException; end')
630
- .grep_node(sexp_type: :rescue)
567
+ node = parse_content('def test; rescue StandardError, CustomException; end').grep_node(sexp_type: :rescue)
631
568
  expect(node.exception_classes.first.to_s).to eq 'StandardError'
632
569
  expect(node.exception_classes.last.to_s).to eq 'CustomException'
633
570
  end
@@ -635,36 +572,28 @@ describe Sexp do
635
572
 
636
573
  describe 'exception_variable' do
637
574
  it 'should get exception varible of rescue node' do
638
- node =
639
- parse_content('def test; rescue => e; end').grep_node(
640
- sexp_type: :rescue
641
- )
575
+ node = parse_content('def test; rescue => e; end').grep_node(sexp_type: :rescue)
642
576
  expect(node.exception_variable.to_s).to eq 'e'
643
577
  end
644
578
 
645
579
  it 'should get empty of empty rescue node' do
646
- node =
647
- parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
580
+ node = parse_content('def test; rescue; end').grep_node(sexp_type: :rescue)
648
581
  expect(node.exception_variable.to_s).to eq ''
649
582
  end
650
583
  end
651
584
 
652
585
  describe 'hash_value' do
653
586
  it 'should get value for hash node' do
654
- node =
655
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
656
- sexp_type: :hash
657
- )
587
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
658
588
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
659
589
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
660
590
  end
661
591
 
662
592
  it 'should get value for bare_assoc_hash' do
663
593
  node =
664
- parse_content(
665
- "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
666
596
  )
667
- .grep_node(sexp_type: :bare_assoc_hash)
668
597
  expect(node.hash_value('first_name').to_s).to eq 'Richard'
669
598
  expect(node.hash_value('last_name').to_s).to eq 'Huang'
670
599
  end
@@ -672,67 +601,80 @@ describe Sexp do
672
601
 
673
602
  describe 'hash_size' do
674
603
  it 'should get value for hash node' do
675
- node =
676
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
677
- sexp_type: :hash
678
- )
604
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
679
605
  expect(node.hash_size).to eq 2
680
606
  end
681
607
 
682
608
  it 'should get value for bare_assoc_hash' do
683
609
  node =
684
- parse_content(
685
- "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
686
612
  )
687
- .grep_node(sexp_type: :bare_assoc_hash)
688
613
  expect(node.hash_size).to eq 2
689
614
  end
690
615
  end
691
616
 
692
617
  describe 'hash_keys' do
693
618
  it 'should get hash_keys for hash node' do
694
- node =
695
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
696
- sexp_type: :hash
697
- )
619
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
698
620
  expect(node.hash_keys).to eq %w[first_name last_name]
699
621
  end
700
622
 
701
623
  it 'should get hash_keys for bare_assoc_hash' do
702
624
  node =
703
- parse_content(
704
- "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
705
627
  )
706
- .grep_node(sexp_type: :bare_assoc_hash)
707
628
  expect(node.hash_keys).to eq %w[first_name last_name]
708
629
  end
709
630
  end
710
631
 
711
632
  describe 'hash_values' do
712
633
  it 'should get hash_values for hash node' do
713
- node =
714
- parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(
715
- sexp_type: :hash
716
- )
634
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
717
635
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
718
636
  end
719
637
 
720
638
  it 'should get hash_values for bare_assoc_hash' do
721
639
  node =
722
- parse_content(
723
- "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
724
642
  )
725
- .grep_node(sexp_type: :bare_assoc_hash)
726
643
  expect(node.hash_values.map(&:to_s)).to eq %w[Richard Huang]
727
644
  end
728
645
  end
729
646
 
730
- describe 'array_size' do
731
- 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
732
659
  node =
733
- parse_content("['first_name', 'last_name']").grep_node(
734
- sexp_type: :array
660
+ parse_content("add_user :user, first_name: 'Richard'").grep_node(
661
+ sexp_type: :assoc_new
735
662
  )
663
+ expect(node.value).to eq 'Richard'
664
+ end
665
+
666
+ it 'should get array value for assoc_new' do
667
+ node =
668
+ parse_content("add_user :user, name: %w[Richard Huang]").grep_node(
669
+ sexp_type: :assoc_new
670
+ )
671
+ expect(node.value.array_values.map(&:to_s)).to eq ['Richard', 'Huang']
672
+ end
673
+ end
674
+
675
+ describe 'array_size' do
676
+ it 'should get array size' do
677
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
736
678
  expect(node.array_size).to eq 2
737
679
  end
738
680
 
@@ -744,10 +686,7 @@ describe Sexp do
744
686
 
745
687
  describe 'array_values' do
746
688
  it 'should get array values' do
747
- node =
748
- parse_content("['first_name', 'last_name']").grep_node(
749
- sexp_type: :array
750
- )
689
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
751
690
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
752
691
  end
753
692
 
@@ -757,8 +696,7 @@ describe Sexp do
757
696
  end
758
697
 
759
698
  it 'should get array value with array and words_add' do
760
- node =
761
- parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
699
+ node = parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
762
700
  expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
763
701
  end
764
702
 
@@ -768,8 +706,7 @@ describe Sexp do
768
706
  end
769
707
 
770
708
  it 'should get array value with array and qwords_add' do
771
- node =
772
- parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
709
+ node = parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
773
710
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
774
711
  end
775
712
 
@@ -780,8 +717,7 @@ describe Sexp do
780
717
 
781
718
  if RUBY_VERSION.to_i > 1
782
719
  it 'should get array value with array and symbols_add' do
783
- node =
784
- parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
720
+ node = parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
785
721
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
786
722
  end
787
723
 
@@ -791,8 +727,7 @@ describe Sexp do
791
727
  end
792
728
 
793
729
  it 'should get array value with array and qsymbols_add' do
794
- node =
795
- parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
730
+ node = parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
796
731
  expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
797
732
  end
798
733
 
@@ -805,9 +740,7 @@ describe Sexp do
805
740
 
806
741
  describe 'alias' do
807
742
  context 'method' do
808
- before do
809
- @node = parse_content('alias new old').grep_node(sexp_type: :alias)
810
- end
743
+ before { @node = parse_content('alias new old').grep_node(sexp_type: :alias) }
811
744
 
812
745
  it 'should get old_method' do
813
746
  expect(@node.old_method.to_s).to eq 'old'
@@ -819,9 +752,7 @@ describe Sexp do
819
752
  end
820
753
 
821
754
  context 'symbol' do
822
- before do
823
- @node = parse_content('alias :new :old').grep_node(sexp_type: :alias)
824
- end
755
+ before { @node = parse_content('alias :new :old').grep_node(sexp_type: :alias) }
825
756
 
826
757
  it 'should get old_method' do
827
758
  expect(@node.old_method.to_s).to eq 'old'
@@ -835,10 +766,7 @@ describe Sexp do
835
766
 
836
767
  describe 'to_object' do
837
768
  it 'should to array' do
838
- node =
839
- parse_content("['first_name', 'last_name']").grep_node(
840
- sexp_type: :array
841
- )
769
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
842
770
  expect(node.to_object).to eq %w[first_name last_name]
843
771
  end
844
772
 
@@ -848,8 +776,7 @@ describe Sexp do
848
776
  end
849
777
 
850
778
  it 'should to array with symbols' do
851
- node =
852
- parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
779
+ node = parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
853
780
  expect(node.to_object).to eq %w[first_name last_name]
854
781
  end
855
782
 
@@ -885,17 +812,18 @@ describe Sexp do
885
812
  expect(node.to_s).to eq '@user'
886
813
  end
887
814
 
815
+ it 'should get to_s for period' do
816
+ node = parse_content('@user.name').grep_node(sexp_type: :@period)
817
+ expect(node.to_s).to eq '.'
818
+ end
819
+
888
820
  it 'should get to_s for class with module' do
889
- node =
890
- parse_content('ActiveRecord::Base').grep_node(
891
- sexp_type: :const_path_ref
892
- )
821
+ node = parse_content('ActiveRecord::Base').grep_node(sexp_type: :const_path_ref)
893
822
  expect(node.to_s).to eq 'ActiveRecord::Base'
894
823
  end
895
824
 
896
825
  it 'should get to_s for label' do
897
- node =
898
- parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
826
+ node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
899
827
  expect(node.to_s).to eq 'first_name'
900
828
  end
901
829
 
@@ -943,15 +871,11 @@ describe Sexp do
943
871
 
944
872
  describe 'remove_line_and_column' do
945
873
  it 'should remove' do
946
- s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(
947
- :@ident,
948
- 'test'
949
- )
874
+ s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(:@ident, 'test')
950
875
  end
951
876
 
952
877
  it 'should remove child nodes' do
953
- s(:const_ref, s(:@const, 'Demo', s(1, 12)))
954
- .remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
878
+ s(:const_ref, s(:@const, 'Demo', s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
955
879
  end
956
880
  end
957
881
  end