ripper_ruby_parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,309 @@
1
+ require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
2
+
3
+ class TestProcessor < RipperRubyParser::SexpProcessor
4
+ def process_foo exp
5
+ exp.shift
6
+ s(:foo_p)
7
+ end
8
+
9
+ def process_bar exp
10
+ exp.shift
11
+ s(:bar_p)
12
+ end
13
+
14
+ def process_baz exp
15
+ exp.shift
16
+ s(:baz_p)
17
+ end
18
+ end
19
+
20
+ describe RipperRubyParser::SexpProcessor do
21
+ let :processor do
22
+ TestProcessor.new
23
+ end
24
+
25
+ describe "#process" do
26
+ it "can handle s(s()) constructs" do
27
+ sexp = s(s())
28
+ processor.process sexp
29
+ end
30
+
31
+ it "can handle line number information constructs" do
32
+ sexp = s(1, 6)
33
+ processor.process sexp
34
+ end
35
+
36
+ describe "for a :program sexp" do
37
+ it "strips off the outer :program node" do
38
+ sexp = s(:program, s(s(:foo)))
39
+ result = processor.process sexp
40
+ result.must_equal s(:foo_p)
41
+ end
42
+
43
+ it "transforms a multi-statement :program into a :block sexp" do
44
+ sexp = s(:program, s(s(:foo), s(:bar)))
45
+ result = processor.process sexp
46
+ result.must_equal s(:block, s(:foo_p), s(:bar_p))
47
+ end
48
+ end
49
+
50
+ describe "for a :string_literal sexp" do
51
+ it "transforms a simple sexp to :str" do
52
+ sexp = s(:string_literal, s(:string_content, s(:@tstring_content, "foo")))
53
+ result = processor.process sexp
54
+ result.must_equal s(:str, "foo")
55
+ end
56
+ end
57
+
58
+ describe "for an :args_add_block sexp" do
59
+ it "transforms a one-argument sexp to an :arglist" do
60
+ sexp = s(:args_add_block, s(s(:foo)), false)
61
+ result = processor.process sexp
62
+ result.must_equal s(:arglist, s(:foo_p))
63
+ end
64
+
65
+ it "transforms a multi-argument sexp to an :arglist" do
66
+ sexp = s(:args_add_block, s(s(:foo), s(:bar)), false)
67
+ result = processor.process sexp
68
+ result.must_equal s(:arglist, s(:foo_p), s(:bar_p))
69
+ end
70
+ end
71
+
72
+ describe "for a :command sexp" do
73
+ it "transforms a sexp to a :call" do
74
+ sexp = s(:command, s(:@ident, "foo", s(1, 0)), s(:foo))
75
+ result = processor.process sexp
76
+ result.must_equal s(:call, nil, :foo, s(:foo_p))
77
+ end
78
+ end
79
+
80
+ describe "for a :var_ref sexp" do
81
+ it "transforms the sexp to a :lvar sexp" do
82
+ sexp = s(:var_ref, s(:@ident, "bar", s(1, 4)))
83
+ result = processor.process sexp
84
+ result.must_equal s(:lvar, :bar)
85
+ end
86
+ end
87
+
88
+ describe "for a :vcall sexp" do
89
+ it "transforms the sexp to a :call sexp" do
90
+ sexp = s(:vcall, s(:@ident, "bar", s(1, 4)))
91
+ result = processor.process sexp
92
+ result.must_equal s(:call, nil, :bar, s(:arglist))
93
+ end
94
+ end
95
+
96
+ describe "for a :module sexp" do
97
+ it "does not create a nested :block sexp for an empty definition" do
98
+ sexp = s(:module,
99
+ s(:const_ref, s(:@const, "Foo", s(1, 13))),
100
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil))
101
+ result = processor.process sexp
102
+ result.must_equal s(:module, :Foo, s(:scope))
103
+ end
104
+
105
+ it "does not create a nested :block sexp for a definition with one statement" do
106
+ sexp = s(:module,
107
+ s(:const_ref, s(:@const, "Foo", s(1, 13))),
108
+ s(:bodystmt, s(s(:foo)), nil, nil, nil))
109
+ result = processor.process sexp
110
+ result.must_equal s(:module, :Foo, s(:scope, s(:foo_p)))
111
+ end
112
+
113
+ it "creates a nested :block sexp for a definition with more than one statement" do
114
+ sexp = s(:module,
115
+ s(:const_ref, s(:@const, "Foo", s(1, 13))),
116
+ s(:bodystmt, s(s(:foo), s(:bar)), nil, nil, nil))
117
+ result = processor.process sexp
118
+ result.must_equal s(:module, :Foo,
119
+ s(:scope, s(:block, s(:foo_p), s(:bar_p))))
120
+ end
121
+ end
122
+
123
+ describe "for a :class sexp" do
124
+ it "does not create a nested :block sexp for an empty definition" do
125
+ sexp = s(:class,
126
+ s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
127
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil))
128
+ result = processor.process sexp
129
+ result.must_equal s(:class, :Foo, nil, s(:scope))
130
+ end
131
+
132
+ it "does not create a nested :block sexp for a definition with one statement" do
133
+ sexp = s(:class,
134
+ s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
135
+ s(:bodystmt, s(s(:foo)), nil, nil, nil))
136
+ result = processor.process sexp
137
+ result.must_equal s(:class, :Foo, nil, s(:scope, s(:foo_p)))
138
+ end
139
+
140
+ it "creates a nested :block sexp for a definition with more than one statement" do
141
+ sexp = s(:class,
142
+ s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
143
+ s(:bodystmt, s(s(:foo), s(:bar)), nil, nil, nil))
144
+ result = processor.process sexp
145
+ result.must_equal s(:class,
146
+ :Foo, nil,
147
+ s(:scope, s(:block, s(:foo_p), s(:bar_p))))
148
+ end
149
+
150
+ it "passes on the given ancestor" do
151
+ sexp = s(:class,
152
+ s(:const_ref, s(:@const, "Foo", s(1, 13))),
153
+ s(:var_ref, s(:@const, "Bar", s(1, 12))),
154
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil))
155
+ result = processor.process sexp
156
+ result.must_equal s(:class, :Foo, s(:const, :Bar), s(:scope))
157
+ end
158
+ end
159
+
160
+ describe "for a :bodystmt sexp" do
161
+ it "creates a :scope sexp with nested :block" do
162
+ sexp = s(:bodystmt, s(s(:foo), s(:bar)), nil, nil, nil)
163
+ result = processor.process sexp
164
+ result.must_equal s(:scope, s(:block, s(:foo_p), s(:bar_p)))
165
+ end
166
+
167
+ it "removes nested :void_stmt sexps" do
168
+ sexp = s(:bodystmt, s(s(:void_stmt), s(:foo)), nil, nil, nil)
169
+ result = processor.process sexp
170
+ result.must_equal s(:scope, s(:block, s(:foo_p)))
171
+ end
172
+ end
173
+
174
+ describe "for a :def sexp" do
175
+ it "transforms the sexp for a basic function definition" do
176
+ sexp = s(:def,
177
+ s(:@ident, "foo", s(1, 4)),
178
+ s(:params, nil, nil, nil, nil, nil),
179
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil))
180
+ result = processor.process sexp
181
+ result.must_equal s(:defn,
182
+ :foo, s(:args), s(:scope, s(:block, s(:nil))))
183
+
184
+ end
185
+ end
186
+
187
+ describe "for a :params sexp" do
188
+ describe "with a normal argument" do
189
+ it "uses the bare argument names" do
190
+ sexp = s(:params, s(s(:@ident, "bar", s(1, 8))), nil, nil, nil, nil)
191
+ result = processor.process sexp
192
+ result.must_equal s(:args, :bar)
193
+ end
194
+ end
195
+ end
196
+
197
+ describe "for an :assign sexp" do
198
+ it "creates a :lasgn sexp" do
199
+ sexp = s(:assign,
200
+ s(:var_field, s(:@ident, "a", s(1, 0))),
201
+ s(:@int, "1", s(1, 4)))
202
+ result = processor.process sexp
203
+ result.must_equal s(:lasgn, :a, s(:lit, 1))
204
+ end
205
+ end
206
+
207
+ describe "for a :binary sexp" do
208
+ it "creates a :call sexp with an :arglist" do
209
+ sexp = s(:binary, s(:bar), :==, s(:foo))
210
+ result = processor.process sexp
211
+ result.must_equal s(:call, s(:bar_p), :==, s(:arglist, s(:foo_p)))
212
+ end
213
+ end
214
+
215
+ describe "for a :method_add_block sexp" do
216
+ it "creates an :iter sexp" do
217
+ sexp = s(:method_add_block,
218
+ s(:call, s(:foo), :".", s(:@ident, "baz", s(1, 2))),
219
+ s(:brace_block, nil, s(s(:bar))))
220
+ result = processor.process sexp
221
+ result.must_equal s(:iter,
222
+ s(:call, s(:foo_p), :baz, s(:arglist)), nil,
223
+ s(:bar_p))
224
+ end
225
+
226
+ describe "with a block parameter" do
227
+ it "creates an :iter sexp with a :lasgn sexp for the block parameter" do
228
+ sexp = s(:method_add_block,
229
+ s(:call, s(:foo), :".", s(:@ident, "baz", s(1, 2))),
230
+ s(:brace_block,
231
+ s(:block_var,
232
+ s(:params, s(s(:@ident, "i", s(1, 6))), nil, nil, nil, nil),
233
+ nil),
234
+ s(s(:bar))))
235
+ result = processor.process sexp
236
+ result.must_equal s(:iter,
237
+ s(:call, s(:foo_p), :baz, s(:arglist)),
238
+ s(:lasgn, :i),
239
+ s(:bar_p))
240
+ end
241
+ end
242
+ end
243
+
244
+ describe "for an :if sexp" do
245
+ describe "with a single statement in the if body" do
246
+ it "uses the statement sexp as the body" do
247
+ sexp = s(:if, s(:foo), s(s(:bar)), nil)
248
+ result = processor.process sexp
249
+ result.must_equal s(:if, s(:foo_p), s(:bar_p), nil)
250
+ end
251
+ end
252
+
253
+ describe "with multiple statements in the if body" do
254
+ it "uses a block containing the statement sexps as the body" do
255
+ sexp = s(:if, s(:foo), s(s(:bar), s(:baz)), nil)
256
+ result = processor.process sexp
257
+ result.must_equal s(:if, s(:foo_p), s(:block, s(:bar_p), s(:baz_p)), nil)
258
+ end
259
+ end
260
+ end
261
+
262
+ describe "for an :array sexp" do
263
+ it "pulls up the element sexps" do
264
+ sexp = s(:array, s(s(:foo), s(:bar), s(:baz)))
265
+ result = processor.process sexp
266
+ result.must_equal s(:array, s(:foo_p), s(:bar_p), s(:baz_p))
267
+ end
268
+ end
269
+
270
+ describe "for a :const_path_ref sexp" do
271
+ it "returns a :colon2 sexp" do
272
+ sexp = s(:const_path_ref,
273
+ s(:var_ref, s(:@const, "Foo", s(1, 0))),
274
+ s(:@const, "Bar", s(1, 5)))
275
+ result = processor.process sexp
276
+ result.must_equal s(:colon2, s(:const, :Foo), :Bar)
277
+ end
278
+ end
279
+
280
+ describe "for a :when sexp" do
281
+ it "turns nested :when clauses into a list" do
282
+ sexp = s(:when, s(s(:foo)), s(s(:bar)),
283
+ s(:when, s(s(:foo)), s(s(:bar)),
284
+ s(:when, s(s(:foo)), s(s(:bar)), nil)))
285
+ result = processor.process sexp
286
+ result.must_equal s(s(:when, s(:array, s(:foo_p)), s(:bar_p)),
287
+ s(:when, s(:array, s(:foo_p)), s(:bar_p)),
288
+ s(:when, s(:array, s(:foo_p)), s(:bar_p)),
289
+ nil)
290
+ end
291
+ end
292
+ end
293
+
294
+ describe "#identifier_node_to_symbol" do
295
+ it "processes an identifier sexp to a bare symbol" do
296
+ sexp = s(:@ident, "foo", s(1, 0))
297
+ result = processor.send :identifier_node_to_symbol, sexp
298
+ result.must_equal :foo
299
+ end
300
+ end
301
+
302
+ describe "#const_node_to_symbol" do
303
+ it "processes a const sexp to a bare symbol" do
304
+ sexp = s(:@const, "Foo", s(1, 0))
305
+ result = processor.send :const_node_to_symbol, sexp
306
+ result.must_equal :Foo
307
+ end
308
+ end
309
+ end
@@ -0,0 +1,5 @@
1
+ describe RipperRubyParser do
2
+ it "knows its own version" do
3
+ RipperRubyParser::VERSION.wont_be_nil
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripper_ruby_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matijs van Zuijlen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sexp_processor
16
+ requirement: &22169420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.10
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *22169420
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &22168900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.11.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *22168900
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &22221020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.2
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *22221020
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby_parser
49
+ requirement: &22220540 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.1
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *22220540
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &22220160 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *22220160
69
+ description:
70
+ email:
71
+ - matijs@matijs.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - README.rdoc
76
+ files:
77
+ - lib/ripper_ruby_parser.rb
78
+ - lib/ripper_ruby_parser/sexp_ext.rb
79
+ - lib/ripper_ruby_parser/sexp_handlers/operators.rb
80
+ - lib/ripper_ruby_parser/sexp_handlers/methods.rb
81
+ - lib/ripper_ruby_parser/sexp_handlers/arrays.rb
82
+ - lib/ripper_ruby_parser/sexp_handlers/method_calls.rb
83
+ - lib/ripper_ruby_parser/sexp_handlers/loops.rb
84
+ - lib/ripper_ruby_parser/sexp_handlers/conditionals.rb
85
+ - lib/ripper_ruby_parser/sexp_handlers/blocks.rb
86
+ - lib/ripper_ruby_parser/sexp_handlers/literals.rb
87
+ - lib/ripper_ruby_parser/sexp_handlers/assignment.rb
88
+ - lib/ripper_ruby_parser/sexp_handlers/arguments.rb
89
+ - lib/ripper_ruby_parser/sexp_handlers/hashes.rb
90
+ - lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb
91
+ - lib/ripper_ruby_parser/parser.rb
92
+ - lib/ripper_ruby_parser/version.rb
93
+ - lib/ripper_ruby_parser/sexp_handlers.rb
94
+ - lib/ripper_ruby_parser/sexp_processor.rb
95
+ - test/test_helper.rb
96
+ - test/end_to_end/lib_comparison_test.rb
97
+ - test/end_to_end/comparison_test.rb
98
+ - test/end_to_end/test_comparison_test.rb
99
+ - test/unit/parser_test.rb
100
+ - test/unit/sexp_processor_test.rb
101
+ - test/unit/version_test.rb
102
+ - README.rdoc
103
+ - Rakefile
104
+ homepage: http://www.github.com/mvz/ripper_ruby_parser
105
+ licenses: []
106
+ post_install_message:
107
+ rdoc_options:
108
+ - --main
109
+ - README.rdoc
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.9.3
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.11
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Parse with Ripper, produce sexps that are compatible with RubyParser.
130
+ test_files:
131
+ - test/end_to_end/comparison_test.rb
132
+ - test/end_to_end/lib_comparison_test.rb
133
+ - test/end_to_end/test_comparison_test.rb
134
+ - test/test_helper.rb
135
+ - test/unit/parser_test.rb
136
+ - test/unit/sexp_processor_test.rb
137
+ - test/unit/version_test.rb