code_analyzer 0.4.6 → 0.5.2
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/.travis.yml +2 -2
- data/code_analyzer.gemspec +2 -1
- data/lib/code_analyzer.rb +10 -9
- data/lib/code_analyzer/analyzer_exception.rb +2 -1
- data/lib/code_analyzer/checker.rb +5 -8
- data/lib/code_analyzer/checking_visitor.rb +5 -4
- 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/nil.rb +2 -1
- data/lib/code_analyzer/sexp.rb +111 -112
- data/lib/code_analyzer/version.rb +3 -2
- data/lib/code_analyzer/warning.rb +3 -2
- 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 +463 -328
- data/spec/code_analyzer/warning_spec.rb +6 -6
- data/spec/spec_helper.rb +3 -1
- metadata +6 -7
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module CodeAnalyzer
|
3
4
|
# Warning is the violation.
|
4
5
|
#
|
@@ -6,7 +7,7 @@ module CodeAnalyzer
|
|
6
7
|
class Warning
|
7
8
|
attr_reader :filename, :line_number, :message
|
8
9
|
|
9
|
-
def initialize(options={})
|
10
|
+
def initialize(options = {})
|
10
11
|
@filename = options[:filename]
|
11
12
|
@line_number = options[:line_number].to_s
|
12
13
|
@message = options[:message]
|
@@ -1,58 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer
|
4
6
|
describe Checker do
|
5
7
|
let(:checker) { Checker.new }
|
6
8
|
|
7
|
-
context
|
8
|
-
it
|
9
|
+
context 'interesting_nodes' do
|
10
|
+
it 'should get empty interesting nodes' do
|
9
11
|
expect(checker.interesting_nodes).to eq []
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
14
|
+
it 'should add interesting nodes' do
|
13
15
|
Checker.interesting_nodes :class, :def
|
14
|
-
expect(checker.interesting_nodes).to eq [
|
16
|
+
expect(checker.interesting_nodes).to eq %i[class def]
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
context
|
19
|
-
it
|
20
|
+
context 'interesting_files' do
|
21
|
+
it 'should match none of interesting files' do
|
20
22
|
expect(checker.interesting_files).to eq []
|
21
23
|
end
|
22
24
|
|
23
|
-
it
|
25
|
+
it 'should add interesting files' do
|
24
26
|
Checker.interesting_files /lib/, /spec/
|
25
27
|
expect(checker.interesting_files).to eq [/lib/, /spec/]
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
|
-
context
|
30
|
-
it
|
31
|
-
allow(checker).to receive(:interesting_files).and_return([
|
32
|
-
expect(checker.parse_file?(
|
31
|
+
context '#parse_file?' do
|
32
|
+
it 'should return true if node_file matches pattern' do
|
33
|
+
allow(checker).to receive(:interesting_files).and_return([%r{spec\/.*\.rb}, %r{lib\/.*\.rb}])
|
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
|
-
expect(checker.parse_file?(
|
38
|
+
allow(checker).to receive(:interesting_files).and_return([%r{spec\/.*\.rb}])
|
39
|
+
expect(checker.parse_file?('lib/code_analyzer.rb')).to be false
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
|
-
context
|
42
|
-
it
|
43
|
+
context 'callback' do
|
44
|
+
it 'should add callback to start_call' do
|
43
45
|
block = Proc.new {}
|
44
46
|
Checker.add_callback(:start_call, &block)
|
45
47
|
expect(Checker.get_callbacks(:start_call)).to eq [block]
|
46
48
|
end
|
47
49
|
|
48
|
-
it
|
50
|
+
it 'should add callback to both start_class and end_class' do
|
49
51
|
block = Proc.new {}
|
50
52
|
Checker.add_callback(:start_class, :end_class, &block)
|
51
53
|
expect(Checker.get_callbacks(:start_class)).to eq [block]
|
52
54
|
expect(Checker.get_callbacks(:end_class)).to eq [block]
|
53
55
|
end
|
54
56
|
|
55
|
-
it
|
57
|
+
it 'should add multiple callbacks to end_call' do
|
56
58
|
block1 = Proc.new {}
|
57
59
|
block2 = Proc.new {}
|
58
60
|
Checker.add_callback(:end_call, &block1)
|
@@ -1,14 +1,16 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer::CheckingVisitor
|
4
6
|
describe Default do
|
5
|
-
let(:checker1) { double(:checker, interesting_nodes: [
|
6
|
-
let(:checker2) { double(:checker, interesting_nodes: [
|
7
|
+
let(:checker1) { double(:checker, interesting_nodes: %i[class def]) }
|
8
|
+
let(:checker2) { double(:checker, interesting_nodes: %i[def call]) }
|
7
9
|
let(:visitor) { Default.new(checkers: [checker1, checker2]) }
|
8
10
|
|
9
|
-
it
|
10
|
-
filename =
|
11
|
-
content =
|
11
|
+
it 'should check def node by all checkers' do
|
12
|
+
filename = 'filename'
|
13
|
+
content = 'def test; end'
|
12
14
|
allow(checker1).to receive(:parse_file?).with(filename).and_return(true)
|
13
15
|
allow(checker2).to receive(:parse_file?).with(filename).and_return(true)
|
14
16
|
expect(checker1).to receive(:node_start)
|
@@ -19,9 +21,9 @@ module CodeAnalyzer::CheckingVisitor
|
|
19
21
|
visitor.check(filename, content)
|
20
22
|
end
|
21
23
|
|
22
|
-
it
|
23
|
-
filename =
|
24
|
-
content =
|
24
|
+
it 'should check class node by only checker1' do
|
25
|
+
filename = 'filename'
|
26
|
+
content = 'class Test; end'
|
25
27
|
allow(checker1).to receive(:parse_file?).with(filename).and_return(true)
|
26
28
|
expect(checker1).to receive(:node_start)
|
27
29
|
expect(checker1).to receive(:node_end)
|
@@ -29,9 +31,9 @@ module CodeAnalyzer::CheckingVisitor
|
|
29
31
|
visitor.check(filename, content)
|
30
32
|
end
|
31
33
|
|
32
|
-
it
|
33
|
-
filename =
|
34
|
-
content =
|
34
|
+
it 'should check call node by only checker2' do
|
35
|
+
filename = 'filename'
|
36
|
+
content = 'obj.message'
|
35
37
|
allow(checker2).to receive(:parse_file?).with(filename).and_return(true)
|
36
38
|
expect(checker2).to receive(:node_start)
|
37
39
|
expect(checker2).to receive(:node_end)
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer::CheckingVisitor
|
4
6
|
describe Plain do
|
@@ -6,9 +8,9 @@ module CodeAnalyzer::CheckingVisitor
|
|
6
8
|
let(:checker2) { double(:checker) }
|
7
9
|
let(:visitor) { Plain.new(checkers: [checker1, checker2]) }
|
8
10
|
|
9
|
-
it
|
10
|
-
filename =
|
11
|
-
content =
|
11
|
+
it 'should check by all checkers' do
|
12
|
+
filename = 'filename'
|
13
|
+
content = 'content'
|
12
14
|
expect(checker1).to receive(:parse_file?).and_return(false)
|
13
15
|
expect(checker2).to receive(:parse_file?).and_return(true)
|
14
16
|
expect(checker1).not_to receive(:check).with(filename, content)
|
@@ -1,35 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module CodeAnalyzer
|
4
6
|
describe Nil do
|
5
7
|
let(:core_nil) { Nil.new }
|
6
8
|
|
7
|
-
context
|
8
|
-
it
|
9
|
+
context 'to_s' do
|
10
|
+
it 'should return self' do
|
9
11
|
expect(core_nil.to_s).to eq core_nil
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
13
|
-
context
|
14
|
-
it
|
15
|
+
context 'hash_size' do
|
16
|
+
it 'should return 0' do
|
15
17
|
expect(core_nil.hash_size).to eq 0
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
context
|
20
|
-
it
|
21
|
+
context 'method_missing' do
|
22
|
+
it 'should return self' do
|
21
23
|
expect(core_nil.undefined).to eq core_nil
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
|
-
context
|
26
|
-
it
|
27
|
+
context 'present?' do
|
28
|
+
it 'should return false' do
|
27
29
|
expect(core_nil).not_to be_present
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
|
-
context
|
32
|
-
it
|
33
|
+
context 'blank?' do
|
34
|
+
it 'should return true' do
|
33
35
|
expect(core_nil).to be_blank
|
34
36
|
end
|
35
37
|
end
|
@@ -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,572 +239,615 @@ 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]
|
530
636
|
end
|
531
637
|
|
532
|
-
it
|
533
|
-
node =
|
534
|
-
|
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]
|
535
644
|
end
|
536
645
|
end
|
537
646
|
|
538
|
-
describe
|
539
|
-
it
|
647
|
+
describe 'array_size' do
|
648
|
+
it 'should get array size' do
|
540
649
|
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
541
650
|
expect(node.array_size).to eq 2
|
542
651
|
end
|
543
652
|
|
544
|
-
it
|
545
|
-
node = parse_content(
|
653
|
+
it 'should get 0' do
|
654
|
+
node = parse_content('[]').grep_node(sexp_type: :array)
|
546
655
|
expect(node.array_size).to eq 0
|
547
656
|
end
|
548
657
|
end
|
549
658
|
|
550
|
-
describe
|
551
|
-
it
|
659
|
+
describe 'array_values' do
|
660
|
+
it 'should get array values' do
|
552
661
|
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
553
|
-
expect(node.array_values.map(&:to_s)).to eq [
|
662
|
+
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
554
663
|
end
|
555
664
|
|
556
|
-
it
|
557
|
-
node = parse_content(
|
665
|
+
it 'should get empty array values' do
|
666
|
+
node = parse_content('[]').grep_node(sexp_type: :array)
|
558
667
|
expect(node.array_values).to eq []
|
559
668
|
end
|
560
669
|
|
561
|
-
it
|
562
|
-
node = parse_content(
|
563
|
-
expect(node.array_values.map(&:to_s)).to eq [
|
670
|
+
it 'should get array value with array and words_add' do
|
671
|
+
node = parse_content('%W{day week fortnight}').grep_node(sexp_type: :array)
|
672
|
+
expect(node.array_values.map(&:to_s)).to eq %w[day week fortnight]
|
673
|
+
end
|
674
|
+
|
675
|
+
it 'should get empty array values with array and words_add' do
|
676
|
+
node = parse_content('%W{}').grep_node(sexp_type: :array)
|
677
|
+
expect(node.array_values.map(&:to_s)).to eq []
|
678
|
+
end
|
679
|
+
|
680
|
+
it 'should get array value with array and qwords_add' do
|
681
|
+
node = parse_content('%w(first_name last_name)').grep_node(sexp_type: :array)
|
682
|
+
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
564
683
|
end
|
565
684
|
|
566
|
-
it
|
567
|
-
node = parse_content(
|
568
|
-
expect(node.array_values.map(&:to_s)).to eq [
|
685
|
+
it 'should get empty array values with array and qwords_add' do
|
686
|
+
node = parse_content('%w()').grep_node(sexp_type: :array)
|
687
|
+
expect(node.array_values.map(&:to_s)).to eq []
|
569
688
|
end
|
570
689
|
|
571
690
|
if RUBY_VERSION.to_i > 1
|
572
|
-
it
|
573
|
-
node = parse_content(
|
574
|
-
expect(node.array_values.map(&:to_s)).to eq [
|
691
|
+
it 'should get array value with array and symbols_add' do
|
692
|
+
node = parse_content('%I(first_name last_name)').grep_node(sexp_type: :array)
|
693
|
+
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
575
694
|
end
|
576
|
-
end
|
577
|
-
end
|
578
695
|
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
@node = parse_content("alias new old").grep_node(sexp_type: :alias)
|
696
|
+
it 'should get empty array value with array and symbols_add' do
|
697
|
+
node = parse_content('%I()').grep_node(sexp_type: :array)
|
698
|
+
expect(node.array_values.map(&:to_s)).to eq []
|
583
699
|
end
|
584
700
|
|
585
|
-
it
|
586
|
-
|
701
|
+
it 'should get array value with array and qsymbols_add' do
|
702
|
+
node = parse_content('%i(first_name last_name)').grep_node(sexp_type: :array)
|
703
|
+
expect(node.array_values.map(&:to_s)).to eq %w[first_name last_name]
|
587
704
|
end
|
588
705
|
|
589
|
-
it
|
590
|
-
|
706
|
+
it 'should get empty array values with array and qsymbols_new' do
|
707
|
+
node = parse_content('%i()').grep_node(sexp_type: :array)
|
708
|
+
expect(node.array_values.map(&:to_s)).to eq []
|
591
709
|
end
|
592
710
|
end
|
711
|
+
end
|
593
712
|
|
594
|
-
|
595
|
-
|
596
|
-
|
713
|
+
describe 'alias' do
|
714
|
+
context 'method' do
|
715
|
+
before { @node = parse_content('alias new old').grep_node(sexp_type: :alias) }
|
716
|
+
|
717
|
+
it 'should get old_method' do
|
718
|
+
expect(@node.old_method.to_s).to eq 'old'
|
597
719
|
end
|
598
720
|
|
599
|
-
it
|
600
|
-
expect(@node.
|
721
|
+
it 'should get new_method' do
|
722
|
+
expect(@node.new_method.to_s).to eq 'new'
|
601
723
|
end
|
724
|
+
end
|
602
725
|
|
603
|
-
|
604
|
-
|
726
|
+
context 'symbol' do
|
727
|
+
before { @node = parse_content('alias :new :old').grep_node(sexp_type: :alias) }
|
728
|
+
|
729
|
+
it 'should get old_method' do
|
730
|
+
expect(@node.old_method.to_s).to eq 'old'
|
731
|
+
end
|
732
|
+
|
733
|
+
it 'should get new_method' do
|
734
|
+
expect(@node.new_method.to_s).to eq 'new'
|
605
735
|
end
|
606
736
|
end
|
607
737
|
end
|
608
738
|
|
609
|
-
describe
|
610
|
-
it
|
739
|
+
describe 'to_object' do
|
740
|
+
it 'should to array' do
|
611
741
|
node = parse_content("['first_name', 'last_name']").grep_node(sexp_type: :array)
|
612
|
-
expect(node.to_object).to eq [
|
742
|
+
expect(node.to_object).to eq %w[first_name last_name]
|
613
743
|
end
|
614
744
|
|
615
|
-
it
|
616
|
-
node = parse_content(
|
617
|
-
expect(node.to_object).to eq [
|
745
|
+
it 'should to array with %w()' do
|
746
|
+
node = parse_content('%w(new create)').grep_node(sexp_type: :array)
|
747
|
+
expect(node.to_object).to eq %w[new create]
|
618
748
|
end
|
619
749
|
|
620
|
-
it
|
621
|
-
node = parse_content(
|
622
|
-
expect(node.to_object).to eq [
|
750
|
+
it 'should to array with symbols' do
|
751
|
+
node = parse_content('[:first_name, :last_name]').grep_node(sexp_type: :array)
|
752
|
+
expect(node.to_object).to eq %w[first_name last_name]
|
623
753
|
end
|
624
754
|
|
625
|
-
it
|
626
|
-
node = parse_content(
|
755
|
+
it 'should to empty array' do
|
756
|
+
node = parse_content('[]').grep_node(sexp_type: :array)
|
627
757
|
expect(node.to_object).to eq []
|
628
758
|
end
|
629
759
|
|
630
|
-
it
|
760
|
+
it 'should to string' do
|
631
761
|
node = parse_content("'richard'").grep_node(sexp_type: :string_literal)
|
632
|
-
expect(node.to_object).to eq
|
762
|
+
expect(node.to_object).to eq 'richard'
|
633
763
|
end
|
634
764
|
end
|
635
765
|
|
636
|
-
describe
|
637
|
-
it
|
766
|
+
describe 'to_s' do
|
767
|
+
it 'should get to_s for string' do
|
638
768
|
node = parse_content("'user'").grep_node(sexp_type: :string_literal)
|
639
|
-
expect(node.to_s).to eq
|
769
|
+
expect(node.to_s).to eq 'user'
|
770
|
+
end
|
771
|
+
|
772
|
+
it 'should get to_s for symbol' do
|
773
|
+
node = parse_content(':user').grep_node(sexp_type: :symbol_literal)
|
774
|
+
expect(node.to_s).to eq 'user'
|
640
775
|
end
|
641
776
|
|
642
|
-
it
|
643
|
-
node = parse_content(
|
644
|
-
expect(node.to_s).to eq
|
777
|
+
it 'should get to_s for const' do
|
778
|
+
node = parse_content('User').grep_node(sexp_type: :@const)
|
779
|
+
expect(node.to_s).to eq 'User'
|
645
780
|
end
|
646
781
|
|
647
|
-
it
|
648
|
-
node = parse_content(
|
649
|
-
expect(node.to_s).to eq
|
782
|
+
it 'should get to_s for ivar' do
|
783
|
+
node = parse_content('@user').grep_node(sexp_type: :@ivar)
|
784
|
+
expect(node.to_s).to eq '@user'
|
650
785
|
end
|
651
786
|
|
652
|
-
it
|
653
|
-
node = parse_content(
|
654
|
-
expect(node.to_s).to eq
|
787
|
+
it 'should get to_s for period' do
|
788
|
+
node = parse_content('@user.name').grep_node(sexp_type: :@period)
|
789
|
+
expect(node.to_s).to eq '.'
|
655
790
|
end
|
656
791
|
|
657
|
-
it
|
658
|
-
node = parse_content(
|
659
|
-
expect(node.to_s).to eq
|
792
|
+
it 'should get to_s for class with module' do
|
793
|
+
node = parse_content('ActiveRecord::Base').grep_node(sexp_type: :const_path_ref)
|
794
|
+
expect(node.to_s).to eq 'ActiveRecord::Base'
|
660
795
|
end
|
661
796
|
|
662
|
-
it
|
797
|
+
it 'should get to_s for label' do
|
663
798
|
node = parse_content("{first_name: 'Richard'}").grep_node(sexp_type: :@label)
|
664
|
-
expect(node.to_s).to eq
|
799
|
+
expect(node.to_s).to eq 'first_name'
|
665
800
|
end
|
666
801
|
|
667
|
-
it
|
668
|
-
node = parse_content(
|
669
|
-
expect(node.to_s).to eq
|
802
|
+
it 'should get to_s for call' do
|
803
|
+
node = parse_content('current_user.post').grep_node(sexp_type: :call)
|
804
|
+
expect(node.to_s).to eq 'current_user.post'
|
670
805
|
end
|
671
806
|
|
672
|
-
it
|
673
|
-
node = parse_content(
|
674
|
-
expect(node.to_s).to eq
|
807
|
+
it 'should get to_s for top_const_ref' do
|
808
|
+
node = parse_content('::User').grep_node(sexp_type: :top_const_ref)
|
809
|
+
expect(node.to_s).to eq '::User'
|
675
810
|
end
|
676
811
|
end
|
677
812
|
|
678
|
-
describe
|
679
|
-
it
|
680
|
-
node = parse_content(
|
813
|
+
describe 'const?' do
|
814
|
+
it 'should return true for const with var_ref' do
|
815
|
+
node = parse_content('User.find').grep_node(sexp_type: :var_ref)
|
681
816
|
expect(node).to be_const
|
682
817
|
end
|
683
818
|
|
684
|
-
it
|
685
|
-
node = parse_content(
|
819
|
+
it 'should return true for const with @const' do
|
820
|
+
node = parse_content('User.find').grep_node(sexp_type: :@const)
|
686
821
|
expect(node).to be_const
|
687
822
|
end
|
688
823
|
|
689
|
-
it
|
690
|
-
node = parse_content(
|
824
|
+
it 'should return false for ivar' do
|
825
|
+
node = parse_content('@user.find').grep_node(sexp_type: :@ivar)
|
691
826
|
expect(node).not_to be_const
|
692
827
|
end
|
693
828
|
end
|
694
829
|
|
695
|
-
describe
|
696
|
-
it
|
697
|
-
node = parse_content(
|
830
|
+
describe 'present?' do
|
831
|
+
it 'should return true' do
|
832
|
+
node = parse_content('hello world')
|
698
833
|
expect(node).to be_present
|
699
834
|
end
|
700
835
|
end
|
701
836
|
|
702
|
-
describe
|
703
|
-
it
|
704
|
-
node = parse_content(
|
837
|
+
describe 'blank?' do
|
838
|
+
it 'should return false' do
|
839
|
+
node = parse_content('hello world')
|
705
840
|
expect(node).not_to be_blank
|
706
841
|
end
|
707
842
|
end
|
708
843
|
|
709
|
-
describe
|
710
|
-
it
|
711
|
-
s(:@ident,
|
844
|
+
describe 'remove_line_and_column' do
|
845
|
+
it 'should remove' do
|
846
|
+
s(:@ident, 'test', s(2, 12)).remove_line_and_column.should_equal s(:@ident, 'test')
|
712
847
|
end
|
713
848
|
|
714
|
-
it
|
715
|
-
s(:const_ref, s(:@const,
|
849
|
+
it 'should remove child nodes' do
|
850
|
+
s(:const_ref, s(:@const, 'Demo', s(1, 12))).remove_line_and_column.should_equal s(:const_def, s(:@const, 'Demo'))
|
716
851
|
end
|
717
852
|
end
|
718
853
|
end
|