code_analyzer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module CodeAnalyzer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module CodeAnalyzer
3
+ # Warning is the violation.
4
+ #
5
+ # it indicates the filenname, line number and error message for the violation.
6
+ class Warning
7
+ attr_reader :filename, :line_number, :message
8
+
9
+ def initialize(options={})
10
+ @filename = options[:filename]
11
+ @line_number = options[:line_number].to_s
12
+ @message = options[:message]
13
+ end
14
+
15
+ def to_s
16
+ "#{@filename}:#{@line_number} - #{@message}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ module CodeAnalyzer
4
+ describe Checker do
5
+ let(:checker) { Checker.new }
6
+
7
+ context "interesting_nodes" do
8
+ it "should get empty interesting nodes" do
9
+ checker.interesting_nodes.should == []
10
+ end
11
+
12
+ it "should add interesting nodes" do
13
+ Checker.interesting_nodes :class, :def
14
+ checker.interesting_nodes.should == [:class, :def]
15
+ end
16
+ end
17
+
18
+ context "interesting_files" do
19
+ it "should match none of interesting files" do
20
+ checker.interesting_files.should == []
21
+ end
22
+
23
+ it "should add interesting files" do
24
+ Checker.interesting_files /lib/, /spec/
25
+ checker.interesting_files.should == [/lib/, /spec/]
26
+ end
27
+ end
28
+
29
+ context "#parse_file?" do
30
+ it "should return true if node_file matches pattern" do
31
+ checker.stub(:interesting_files).and_return([/spec\/.*\.rb/, /lib\/.*\.rb/])
32
+ checker.parse_file?("lib/code_analyzer.rb").should be_true
33
+ end
34
+
35
+ it "should return false if node_file doesn't match pattern" do
36
+ checker.stub(:interesting_files).and_return([/spec\/.*\.rb/])
37
+ checker.parse_file?("lib/code_analyzer.rb").should be_false
38
+ end
39
+ end
40
+
41
+ context "callback" do
42
+ it "should add callback to start_call" do
43
+ block = Proc.new {}
44
+ Checker.add_callback(:start_call, &block)
45
+ Checker.get_callbacks(:start_call).should == [block]
46
+ end
47
+
48
+ it "should add callback to both start_class and end_class" do
49
+ block = Proc.new {}
50
+ Checker.add_callback(:start_class, :end_class, &block)
51
+ Checker.get_callbacks(:start_class).should == [block]
52
+ Checker.get_callbacks(:end_class).should == [block]
53
+ end
54
+
55
+ it "should add multiple callbacks to end_call" do
56
+ block1 = Proc.new {}
57
+ block2 = Proc.new {}
58
+ Checker.add_callback(:end_call, &block1)
59
+ Checker.add_callback(:end_call, &block2)
60
+ Checker.get_callbacks(:end_call).should == [block1, block2]
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ module CodeAnalyzer::CheckingVisitor
4
+ describe Default do
5
+ let(:checker1) { mock(:checker, interesting_nodes: [:class, :def]) }
6
+ let(:checker2) { mock(:checker, interesting_nodes: [:def, :call]) }
7
+ let(:visitor) { Default.new(checkers: [checker1, checker2]) }
8
+
9
+ it "should check def node by all checkers" do
10
+ filename = "filename"
11
+ content = "def test; end"
12
+ checker1.stub(:parse_file?).with(filename).and_return(true)
13
+ checker2.stub(:parse_file?).with(filename).and_return(true)
14
+ checker1.should_receive(:node_start)
15
+ checker1.should_receive(:node_end)
16
+ checker2.should_receive(:node_start)
17
+ checker2.should_receive(:node_end)
18
+
19
+ visitor.check(filename, content)
20
+ end
21
+
22
+ it "should check class node by only checker1" do
23
+ filename = "filename"
24
+ content = "class Test; end"
25
+ checker1.stub(:parse_file?).with(filename).and_return(true)
26
+ checker1.should_receive(:node_start)
27
+ checker1.should_receive(:node_end)
28
+
29
+ visitor.check(filename, content)
30
+ end
31
+
32
+ it "should check call node by only checker2" do
33
+ filename = "filename"
34
+ content = "obj.message"
35
+ checker2.stub(:parse_file?).with(filename).and_return(true)
36
+ checker2.should_receive(:node_start)
37
+ checker2.should_receive(:node_end)
38
+
39
+ visitor.check(filename, content)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ module CodeAnalyzer::CheckingVisitor
4
+ describe Plain do
5
+ let(:checker1) { mock(:checker) }
6
+ let(:checker2) { mock(:checker) }
7
+ let(:visitor) { Plain.new(checkers: [checker1, checker2]) }
8
+
9
+ it "should check by all checkers" do
10
+ filename = "filename"
11
+ content = "content"
12
+ checker1.should_receive(:check).with(filename, content)
13
+ checker2.should_receive(:check).with(filename, content)
14
+
15
+ visitor.check(filename, content)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module CodeAnalyzer
4
+ describe Nil do
5
+ let(:core_nil) { Nil.new }
6
+
7
+ context "to_s" do
8
+ it "should return self" do
9
+ core_nil.to_s.should == core_nil
10
+ end
11
+ end
12
+
13
+ context "hash_size" do
14
+ it "should return 0" do
15
+ core_nil.hash_size.should == 0
16
+ end
17
+ end
18
+
19
+ context "method_missing" do
20
+ it "should return self" do
21
+ core_nil.undefined.should == core_nil
22
+ end
23
+ end
24
+
25
+ context "present?" do
26
+ it "should return false" do
27
+ core_nil.should_not be_present
28
+ end
29
+ end
30
+
31
+ context "blank?" do
32
+ it "should return true" do
33
+ core_nil.should be_blank
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,622 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sexp do
4
+ describe "line" do
5
+ before :each do
6
+ content = <<-EOF
7
+ class Demo
8
+ def test
9
+ ActiveRecord::Base.connection
10
+ end
11
+ alias :test_new :test
12
+ CONST = { foo: :bar }
13
+ end
14
+ EOF
15
+ @node = parse_content(content)
16
+ end
17
+
18
+ it "should return class line" do
19
+ @node.grep_node(sexp_type: :class).line.should == 1
20
+ end
21
+
22
+ it "should return def line" do
23
+ @node.grep_node(sexp_type: :def).line.should == 2
24
+ end
25
+
26
+ it "should return const line" do
27
+ @node.grep_node(sexp_type: :const_ref).line.should == 1
28
+ end
29
+
30
+ it "should return const path line" do
31
+ @node.grep_node(sexp_type: :const_path_ref).line.should == 3
32
+ end
33
+
34
+ it "should return alias line" do
35
+ @node.grep_node(sexp_type: :alias).line.should == 5
36
+ end
37
+
38
+ it "should return hash line" do
39
+ @node.grep_node(sexp_type: :hash).line.should == 6
40
+ end
41
+ end
42
+
43
+ describe "grep_nodes" do
44
+ before :each do
45
+ content = <<-EOF
46
+ def show
47
+ current_user.posts.find(params[:id])
48
+ end
49
+ EOF
50
+ @node = parse_content(content)
51
+ end
52
+
53
+ it "should get the call nodes with receiver current_user" do
54
+ nodes = []
55
+ @node.grep_nodes(sexp_type: :call, receiver: "current_user") { |node| nodes << node }
56
+ if RUBY_VERSION == "1.9.2"
57
+ nodes.should == [s(:call, s(:var_ref, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))]
58
+ else
59
+ nodes.should == [s(:call, s(:vcall, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))]
60
+ end
61
+ end
62
+
63
+ it "should get the call nodes with different messages" do
64
+ nodes = []
65
+ @node.grep_nodes(sexp_type: :call, message: ["posts", "find"]) { |node| nodes << node }
66
+ if RUBY_VERSION == "1.9.2"
67
+ nodes.should == [s(:call, s(:call, s(:var_ref, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21))), :".", s(:@ident, "find", s(2, 27))), s(:call, s(:var_ref, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))]
68
+ else
69
+ nodes.should == [s(:call, s(:call, s(:vcall, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21))), :".", s(:@ident, "find", s(2, 27))), s(:call, s(:vcall, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))]
70
+ end
71
+ end
72
+
73
+ if RUBY_VERSION == "1.9.2"
74
+ it "should get the var_ref node with to_s" do
75
+ nodes = []
76
+ @node.grep_nodes(sexp_type: :var_ref, to_s: "current_user") { |node| nodes << node }
77
+ nodes.should == [s(:var_ref, s(:@ident, "current_user", s(2, 8)))]
78
+ end
79
+ else
80
+ it "should get the vcall node with to_s" do
81
+ nodes = []
82
+ @node.grep_nodes(sexp_type: :vcall, to_s: "current_user") { |node| nodes << node }
83
+ nodes.should == [s(:vcall, s(:@ident, "current_user", s(2, 8)))]
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "grep_node" do
89
+ before :each do
90
+ content = <<-EOF
91
+ def show
92
+ current_user.posts.find(params[:id])
93
+ end
94
+ EOF
95
+ @node = parse_content(content)
96
+ end
97
+
98
+ it "should get first node with empty argument" do
99
+ node = @node.grep_node(sexp_type: :call, receiver: "current_user")
100
+ if RUBY_VERSION == "1.9.2"
101
+ node.should == s(:call, s(:var_ref, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))
102
+ else
103
+ node.should == s(:call, s(:vcall, s(:@ident, "current_user", s(2, 8))), :".", s(:@ident, "posts", s(2, 21)))
104
+ end
105
+ end
106
+ end
107
+
108
+ describe "grep_nodes_count" do
109
+ before :each do
110
+ content = <<-EOF
111
+ def show
112
+ current_user.posts.find(params[:id])
113
+ end
114
+ EOF
115
+ @node = parse_content(content)
116
+ end
117
+
118
+ it "should get the count of call nodes" do
119
+ @node.grep_nodes_count(sexp_type: :call).should == 2
120
+ end
121
+ end
122
+
123
+ describe "receiver" do
124
+ it "should get receiver of assign node" do
125
+ node = parse_content("user.name = params[:name]").grep_node(sexp_type: :assign)
126
+ receiver = node.receiver
127
+ receiver.sexp_type.should == :field
128
+ receiver.receiver.to_s.should == "user"
129
+ receiver.message.to_s.should == "name"
130
+ end
131
+
132
+ it "should get receiver of field node" do
133
+ node = parse_content("user.name = params[:name]").grep_node(sexp_type: :field)
134
+ node.receiver.to_s.should == "user"
135
+ end
136
+
137
+ it "should get receiver of call node" do
138
+ node = parse_content("user.name").grep_node(sexp_type: :call)
139
+ node.receiver.to_s.should == "user"
140
+ end
141
+
142
+ it "should get receiver of binary" do
143
+ node = parse_content("user == 'user_name'").grep_node(sexp_type: :binary)
144
+ node.receiver.to_s.should == "user"
145
+ end
146
+
147
+ it "should get receiver of command_call" do
148
+ content = <<-EOF
149
+ map.resources :posts do
150
+ end
151
+ EOF
152
+ node = parse_content(content).grep_node(sexp_type: :command_call)
153
+ node.receiver.to_s.should == "map"
154
+ end
155
+
156
+ it "should get receiver of method_add_arg" do
157
+ node = parse_content("Post.find(:all)").grep_node(sexp_type: :method_add_arg)
158
+ node.receiver.to_s.should == "Post"
159
+ end
160
+
161
+ it "should get receiver of method_add_block" do
162
+ node = parse_content("Post.save do; end").grep_node(sexp_type: :method_add_block)
163
+ node.receiver.to_s.should == "Post"
164
+ end
165
+ end
166
+
167
+ describe "module_name" do
168
+ it "should get module name of module node" do
169
+ node = parse_content("module Admin; end").grep_node(sexp_type: :module)
170
+ node.module_name.to_s.should == "Admin"
171
+ end
172
+ end
173
+
174
+ describe "class_name" do
175
+ it "should get class name of class node" do
176
+ node = parse_content("class User; end").grep_node(sexp_type: :class)
177
+ node.class_name.to_s.should == "User"
178
+ end
179
+ end
180
+
181
+ describe "base_class" do
182
+ it "should get base class of class node" do
183
+ node = parse_content("class User < ActiveRecord::Base; end").grep_node(sexp_type: :class)
184
+ node.base_class.to_s.should == "ActiveRecord::Base"
185
+ end
186
+ end
187
+
188
+ describe "left_value" do
189
+ it "should get the left value of assign" do
190
+ node = parse_content("user = current_user").grep_node(sexp_type: :assign)
191
+ node.left_value.to_s.should == "user"
192
+ end
193
+ end
194
+
195
+ describe "right_value" do
196
+ it "should get the right value of assign" do
197
+ node = parse_content("user = current_user").grep_node(sexp_type: :assign)
198
+ node.right_value.to_s.should == "current_user"
199
+ end
200
+ end
201
+
202
+ describe "message" do
203
+ it "should get the message of command" do
204
+ node = parse_content("has_many :projects").grep_node(sexp_type: :command)
205
+ node.message.to_s.should == "has_many"
206
+ end
207
+
208
+ it "should get the message of command_call" do
209
+ node = parse_content("map.resources :posts do; end").grep_node(sexp_type: :command_call)
210
+ node.message.to_s.should == "resources"
211
+ end
212
+
213
+ it "should get the message of field" do
214
+ node = parse_content("user.name = 'test'").grep_node(sexp_type: :field)
215
+ node.message.to_s.should == "name"
216
+ end
217
+
218
+ it "should get the message of call" do
219
+ node = parse_content("user.name").grep_node(sexp_type: :call)
220
+ node.message.to_s.should == "name"
221
+ end
222
+
223
+ it "should get the message of binary" do
224
+ node = parse_content("user.name == 'test'").grep_node(sexp_type: :binary)
225
+ node.message.to_s.should == "=="
226
+ end
227
+
228
+ it "should get the message of fcall" do
229
+ node = parse_content("test?('world')").grep_node(sexp_type: :fcall)
230
+ node.message.to_s.should == "test?"
231
+ end
232
+
233
+ it "should get the message of method_add_arg" do
234
+ node = parse_content("Post.find(:all)").grep_node(sexp_type: :method_add_arg)
235
+ node.message.to_s.should == "find"
236
+ end
237
+
238
+ it "should get the message of method_add_block" do
239
+ node = parse_content("Post.save do; end").grep_node(sexp_type: :method_add_block)
240
+ node.message.to_s.should == "save"
241
+ end
242
+ end
243
+
244
+ describe "arguments" do
245
+ it "should get the arguments of command" do
246
+ node = parse_content("resources :posts do; end").grep_node(sexp_type: :command)
247
+ node.arguments.sexp_type.should == :args_add_block
248
+ end
249
+
250
+ it "should get the arguments of command_call" do
251
+ node = parse_content("map.resources :posts do; end").grep_node(sexp_type: :command_call)
252
+ node.arguments.sexp_type.should == :args_add_block
253
+ end
254
+
255
+ it "should get the arguments of method_add_arg" do
256
+ node = parse_content("User.find(:all)").grep_node(sexp_type: :method_add_arg)
257
+ node.arguments.sexp_type.should == :args_add_block
258
+ end
259
+
260
+ it "should get the arguments of method_add_block" do
261
+ node = parse_content("Post.save(false) do; end").grep_node(sexp_type: :method_add_block)
262
+ node.arguments.sexp_type.should == :args_add_block
263
+ end
264
+ end
265
+
266
+ describe "argument" do
267
+ it "should get the argument of binary" do
268
+ node = parse_content("user == current_user").grep_node(sexp_type: :binary)
269
+ node.argument.to_s.should == "current_user"
270
+ end
271
+ end
272
+
273
+ describe "all" do
274
+ it "should get all arguments" do
275
+ node = parse_content("puts 'hello', 'world'").grep_node(sexp_type: :args_add_block)
276
+ node.all.map(&:to_s).should == ["hello", "world"]
277
+ end
278
+
279
+ it "should get all arguments with &:" do
280
+ node = parse_content("user.posts.map(&:title)").grep_node(sexp_type: :args_add_block)
281
+ node.all.map(&:to_s).should == ["title"]
282
+ end
283
+
284
+ it "should get all arguments with command_call node" do
285
+ node = parse_content("options_for_select(Account.get_business current_user)").grep_node(sexp_type: :args_add)
286
+ if RUBY_VERSION == "1.9.2"
287
+ node.all.should == [s(:command_call, s(:var_ref, s(:@const, "Account", s(1, 19))), :".", s(:@ident, "get_business", s(1, 27)), s(:args_add_block, s(:args_add, s(:args_new), s(:var_ref, s(:@ident, "current_user", s(1, 40)))), false))]
288
+ else
289
+ node.all.should == [s(:command_call, s(:var_ref, s(:@const, "Account", s(1, 19))), :".", s(:@ident, "get_business", s(1, 27)), s(:args_add_block, s(:args_add, s(:args_new), s(:vcall, s(:@ident, "current_user", s(1, 40)))), false))]
290
+ end
291
+ end
292
+
293
+ it "no error for args_add_star" do
294
+ node = parse_content("send(:\"\#{route}_url\", *args)").grep_node(sexp_type: :args_add_block)
295
+ lambda { node.all }.should_not raise_error
296
+ end
297
+ end
298
+
299
+ describe "conditional_statement" do
300
+ it "should get conditional statement of if" do
301
+ node = parse_content("if true; end").grep_node(sexp_type: :if)
302
+ node.conditional_statement.to_s.should == "true"
303
+ end
304
+
305
+ it "should get conditional statement of unless" do
306
+ node = parse_content("unless true; end").grep_node(sexp_type: :unless)
307
+ node.conditional_statement.to_s.should == "true"
308
+ end
309
+
310
+ it "should get conditional statement of elsif" do
311
+ content =<<-EOF
312
+ if true
313
+ elsif false
314
+ end
315
+ EOF
316
+ node = parse_content(content).grep_node(sexp_type: :elsif)
317
+ node.conditional_statement.to_s.should == "false"
318
+ end
319
+
320
+ it "should get conditional statement of ifop" do
321
+ content =<<-EOF
322
+ user ? user.name : nil
323
+ EOF
324
+ node = parse_content(content).grep_node(sexp_type: :ifop)
325
+ node.conditional_statement.to_s.should == "user"
326
+ end
327
+ end
328
+
329
+ describe "all_conditions" do
330
+ it "should get all conditions" do
331
+ node = parse_content("user == current_user && user.valid? || user.admin?").grep_node(sexp_type: :binary)
332
+ node.all_conditions.size.should == 3
333
+ end
334
+ end
335
+
336
+ describe "method_name" do
337
+ it "should get the method name of def" do
338
+ node = parse_content("def show; end").grep_node(sexp_type: :def)
339
+ node.method_name.to_s.should == "show"
340
+ end
341
+
342
+ it "should get the method name of defs" do
343
+ node = parse_content("def self.find; end").grep_node(sexp_type: :defs)
344
+ node.method_name.to_s.should == "find"
345
+ end
346
+ end
347
+
348
+ describe "body" do
349
+ it "should get body of class" do
350
+ node = parse_content("class User; end").grep_node(sexp_type: :class)
351
+ node.body.sexp_type.should == :bodystmt
352
+ end
353
+
354
+ it "should get body of def" do
355
+ node = parse_content("def login; end").grep_node(sexp_type: :def)
356
+ node.body.sexp_type.should == :bodystmt
357
+ end
358
+
359
+ it "should get body of defs" do
360
+ node = parse_content("def self.login; end").grep_node(sexp_type: :defs)
361
+ node.body.sexp_type.should == :bodystmt
362
+ end
363
+
364
+ it "should get body of module" do
365
+ node = parse_content("module Enumerable; end").grep_node(sexp_type: :module)
366
+ node.body.sexp_type.should == :bodystmt
367
+ end
368
+
369
+ it "should get body of if" do
370
+ node = parse_content("if true; puts 'hello world'; end").grep_node(sexp_type: :if)
371
+ node.body.sexp_type.should == :stmts_add
372
+ end
373
+
374
+ it "should get body of elsif" do
375
+ node = parse_content("if true; elsif true; puts 'hello world'; end").grep_node(sexp_type: :elsif)
376
+ node.body.sexp_type.should == :stmts_add
377
+ end
378
+
379
+ it "should get body of unless" do
380
+ node = parse_content("unless true; puts 'hello world'; end").grep_node(sexp_type: :unless)
381
+ node.body.sexp_type.should == :stmts_add
382
+ end
383
+
384
+ it "should get body of else" do
385
+ node = parse_content("if true; else; puts 'hello world'; end").grep_node(sexp_type: :else)
386
+ node.body.sexp_type.should == :stmts_add
387
+ end
388
+ end
389
+
390
+ describe "block" do
391
+ it "sould get block of method_add_block node" do
392
+ node = parse_content("resources :posts do; resources :comments; end").grep_node(sexp_type: :method_add_block)
393
+ node.block.sexp_type.should == :do_block
394
+ end
395
+ end
396
+
397
+ describe "statements" do
398
+ it "should get statements of do_block node" do
399
+ node = parse_content("resources :posts do; resources :comments; resources :like; end").grep_node(sexp_type: :do_block)
400
+ node.statements.size.should == 2
401
+ end
402
+
403
+ it "should get statements of bodystmt node" do
404
+ node = parse_content("class User; def login?; end; def admin?; end; end").grep_node(sexp_type: :bodystmt)
405
+ node.statements.size.should == 2
406
+ end
407
+ end
408
+
409
+ describe "hash_value" do
410
+ it "should get value for hash node" do
411
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
412
+ node.hash_value("first_name").to_s.should == "Richard"
413
+ node.hash_value("last_name").to_s.should == "Huang"
414
+ end
415
+
416
+ it "should get value for bare_assoc_hash" do
417
+ node = parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(sexp_type: :bare_assoc_hash)
418
+ node.hash_value("first_name").to_s.should == "Richard"
419
+ node.hash_value("last_name").to_s.should == "Huang"
420
+ end
421
+ end
422
+
423
+ describe "hash_size" do
424
+ it "should get value for hash node" do
425
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
426
+ node.hash_size.should == 2
427
+ end
428
+
429
+ it "should get value for bare_assoc_hash" do
430
+ node = parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(sexp_type: :bare_assoc_hash)
431
+ node.hash_size.should == 2
432
+ end
433
+ end
434
+
435
+ describe "hash_keys" do
436
+ it "should get hash_keys for hash node" do
437
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
438
+ node.hash_keys.should == ["first_name", "last_name"]
439
+ end
440
+
441
+ it "should get hash_keys for bare_assoc_hash" do
442
+ node = parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(sexp_type: :bare_assoc_hash)
443
+ node.hash_keys.should == ["first_name", "last_name"]
444
+ end
445
+ end
446
+
447
+ describe "hash_values" do
448
+ it "should get hash_values for hash node" do
449
+ node = parse_content("{first_name: 'Richard', last_name: 'Huang'}").grep_node(sexp_type: :hash)
450
+ node.hash_values.map(&:to_s).should == ["Richard", "Huang"]
451
+ end
452
+
453
+ it "should get hash_values for bare_assoc_hash" do
454
+ node = parse_content("add_user :user, first_name: 'Richard', last_name: 'Huang'").grep_node(sexp_type: :bare_assoc_hash)
455
+ node.hash_values.map(&:to_s).should == ["Richard", "Huang"]
456
+ end
457
+ end
458
+
459
+ describe "array_size" do
460
+ it "should get array size" do
461
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
462
+ node.array_size.should == 2
463
+ end
464
+
465
+ it "should get 0" do
466
+ node = parse_content("[]").grep_node(sexp_type: :array)
467
+ node.array_size.should == 0
468
+ end
469
+ end
470
+
471
+ describe "array_values" do
472
+ it "should get array values" do
473
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
474
+ node.array_values.map(&:to_s).should == ["first_name", "last_name"]
475
+ end
476
+
477
+ it "should get empty array values" do
478
+ node = parse_content("[]").grep_node(sexp_type: :array)
479
+ node.array_values.should == []
480
+ end
481
+
482
+ it "should get array value with qwords" do
483
+ node = parse_content("%w(first_name last_name)").grep_node(sexp_type: :qwords_add)
484
+ node.array_values.map(&:to_s).should == ["first_name", "last_name"]
485
+ end
486
+ end
487
+
488
+ describe "alias" do
489
+ context "method" do
490
+ before do
491
+ @node = parse_content("alias new old").grep_node(sexp_type: :alias)
492
+ end
493
+
494
+ it "should get old_method" do
495
+ @node.old_method.to_s.should == "old"
496
+ end
497
+
498
+ it "should get new_method" do
499
+ @node.new_method.to_s.should == "new"
500
+ end
501
+ end
502
+
503
+ context "symbol" do
504
+ before do
505
+ @node = parse_content("alias :new :old").grep_node(sexp_type: :alias)
506
+ end
507
+
508
+ it "should get old_method" do
509
+ @node.old_method.to_s.should == "old"
510
+ end
511
+
512
+ it "should get new_method" do
513
+ @node.new_method.to_s.should == "new"
514
+ end
515
+ end
516
+ end
517
+
518
+ describe "to_object" do
519
+ it "should to array" do
520
+ node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
521
+ node.to_object.should == ["first_name", "last_name"]
522
+ end
523
+
524
+ it "should to array with %w()" do
525
+ node = parse_content("%w(new create)").grep_node(sexp_type: :array)
526
+ node.to_object.should == ["new", "create"]
527
+ end
528
+
529
+ it "should to array with symbols" do
530
+ node = parse_content("[:first_name, :last_name]").grep_node(sexp_type: :array)
531
+ node.to_object.should == ["first_name", "last_name"]
532
+ end
533
+
534
+ it "should to empty array" do
535
+ node = parse_content("[]").grep_node(sexp_type: :array)
536
+ node.to_object.should == []
537
+ end
538
+
539
+ it "should to string" do
540
+ node = parse_content("'richard'").grep_node(sexp_type: :string_literal)
541
+ node.to_object.should == "richard"
542
+ end
543
+ end
544
+
545
+ describe "to_s" do
546
+ it "should get to_s for string" do
547
+ node = parse_content("'user'").grep_node(sexp_type: :string_literal)
548
+ node.to_s.should == "user"
549
+ end
550
+
551
+ it "should get to_s for symbol" do
552
+ node = parse_content(":user").grep_node(sexp_type: :symbol_literal)
553
+ node.to_s.should == "user"
554
+ end
555
+
556
+ it "should get to_s for const" do
557
+ node = parse_content("User").grep_node(sexp_type: :@const)
558
+ node.to_s.should == "User"
559
+ end
560
+
561
+ it "should get to_s for ivar" do
562
+ node = parse_content("@user").grep_node(sexp_type: :@ivar)
563
+ node.to_s.should == "@user"
564
+ end
565
+
566
+ it "should get to_s for class with module" do
567
+ node = parse_content("ActiveRecord::Base").grep_node(sexp_type: :const_path_ref)
568
+ node.to_s.should == "ActiveRecord::Base"
569
+ end
570
+
571
+ it "should get to_s for label" do
572
+ node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
573
+ node.to_s.should == "first_name"
574
+ end
575
+
576
+ it "should get to_s for call" do
577
+ node = parse_content("current_user.post").grep_node(sexp_type: :call)
578
+ node.to_s.should == "current_user.post"
579
+ end
580
+ end
581
+
582
+ describe "const?" do
583
+ it "should return true for const with var_ref" do
584
+ node = parse_content("User.find").grep_node(sexp_type: :var_ref)
585
+ node.should be_const
586
+ end
587
+
588
+ it "should return true for const with @const" do
589
+ node = parse_content("User.find").grep_node(sexp_type: :@const)
590
+ node.should be_const
591
+ end
592
+
593
+ it "should return false for ivar" do
594
+ node = parse_content("@user.find").grep_node(sexp_type: :@ivar)
595
+ node.should_not be_const
596
+ end
597
+ end
598
+
599
+ describe "present?" do
600
+ it "should return true" do
601
+ node = parse_content("hello world")
602
+ node.should be_present
603
+ end
604
+ end
605
+
606
+ describe "blank?" do
607
+ it "should return false" do
608
+ node = parse_content("hello world")
609
+ node.should_not be_blank
610
+ end
611
+ end
612
+
613
+ describe "remove_line_and_column" do
614
+ it "should remove" do
615
+ s(:@ident, "test", s(2, 12)).remove_line_and_column.should_equal s(:@ident, "test")
616
+ end
617
+
618
+ it "should remove child nodes" do
619
+ s(:const_ref, s(:@const, "Demo", s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, "Demo"))
620
+ end
621
+ end
622
+ end