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