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