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