sexp2ruby 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,496 @@
1
+ # WIP: Converting to rspec. This file is not in a usable state.
2
+ class TestRuby2Ruby
3
+ def setup
4
+ super
5
+ @check_sexp = ENV["CHECK_SEXPS"]
6
+ @processor = Ruby2Ruby.new
7
+ end
8
+
9
+ def do_not_check_sexp!
10
+ @check_sexp = false
11
+ end
12
+
13
+ def processor(opts)
14
+ Ruby2Ruby.new(opts)
15
+ end
16
+
17
+ def test_and_alias
18
+ inn = s(:and, s(:true), s(:alias, s(:lit, :a), s(:lit, :b)))
19
+ out = "true and (alias :a :b)"
20
+ util_compare inn, out
21
+ end
22
+
23
+ def test_attr_reader_diff
24
+ inn = s(:defn, :same, s(:args), s(:ivar, :@diff))
25
+ out = "def same\n @diff\nend"
26
+ util_compare inn, out
27
+ end
28
+
29
+ def test_attr_reader_same
30
+ do_not_check_sexp!
31
+
32
+ inn = s(:defn, :same, s(:args), s(:ivar, :@same))
33
+ out = "attr_reader :same"
34
+ util_compare inn, out
35
+ end
36
+
37
+ def test_attr_reader_double
38
+ inn = s(:defn, :same, s(:args), s(:ivar, :@same), s(:ivar, :@diff))
39
+ out = "def same\n @same\n @diff\nend"
40
+ util_compare inn, out
41
+ end
42
+
43
+ def test_attr_reader_same_name_diff_body
44
+ do_not_check_sexp!
45
+
46
+ inn = s(:defn, :same, s(:args), s(:not, s(:ivar, :@same)))
47
+ out = "def same\n (not @same)\nend"
48
+ util_compare inn, out
49
+ end
50
+
51
+ def test_attr_writer_diff
52
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@diff, s(:lvar, :o)))
53
+ out = "def same=(o)\n @diff = o\nend"
54
+ util_compare inn, out
55
+ end
56
+
57
+ def test_attr_writer_double
58
+ inn = s(:defn, :same=, s(:args, :o),
59
+ s(:iasgn, :@same, s(:lvar, :o)), s(:iasgn, :@diff, s(:lvar, :o)))
60
+ out = "def same=(o)\n @same = o\n @diff = o\nend"
61
+ util_compare inn, out
62
+ end
63
+
64
+ def test_attr_writer_same_name_diff_body
65
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same, s(:lit, 42)))
66
+ out = "def same=(o)\n @same = 42\nend"
67
+ util_compare inn, out
68
+ end
69
+
70
+ def test_attr_writer_same
71
+ do_not_check_sexp!
72
+
73
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same , s(:lvar, :o)))
74
+ out = "attr_writer :same"
75
+ util_compare inn, out
76
+ end
77
+
78
+ def test_dregx_slash
79
+ do_not_check_sexp!
80
+
81
+ inn = util_thingy(:dregx)
82
+ out = '/a"b#{(1 + 1)}c"d\/e/'
83
+ util_compare inn, out, /a"b2c"d\/e/
84
+ end
85
+
86
+ def test_dstr_quote
87
+ inn = util_thingy(:dstr)
88
+ out = '"a\"b#{(1 + 1)}c\"d/e"'
89
+ util_compare inn, out, 'a"b2c"d/e'
90
+ end
91
+
92
+ def test_dsym_quote
93
+ inn = util_thingy(:dsym)
94
+ out = ':"a\"b#{(1 + 1)}c\"d/e"'
95
+ util_compare inn, out, :'a"b2c"d/e'
96
+ end
97
+
98
+ def test_lit_regexp_slash
99
+ do_not_check_sexp! # dunno why on this one
100
+
101
+ util_compare s(:lit, /blah\/blah/), '/blah\/blah/', /blah\/blah/
102
+ end
103
+
104
+ def test_call_kwsplat
105
+ inn = s(:call, nil, :test_splat, s(:hash, s(:kwsplat, s(:call, nil, :testing))))
106
+ out = "test_splat(**testing)"
107
+
108
+ util_compare inn, out
109
+ end
110
+
111
+ def test_call_arg_assoc_kwsplat
112
+ inn = s(:call, nil, :f,
113
+ s(:lit, 1),
114
+ s(:hash, s(:lit, :kw), s(:lit, 2), s(:kwsplat, s(:lit, 3))))
115
+ out = "f(1, :kw => 2, **3)"
116
+
117
+ util_compare inn, out
118
+ end
119
+
120
+ def test_call_kwsplat_x
121
+ inn = s(:call, nil, :a, s(:hash, s(:kwsplat, s(:lit, 1))))
122
+ out = "a(**1)"
123
+
124
+ util_compare inn, out
125
+ end
126
+
127
+ def test_defn_kwargs
128
+ inn = s(:defn, :initialize,
129
+ s(:args, :arg, s(:kwarg, :keyword, s(:nil)), :"**args"),
130
+ s(:nil))
131
+ out = "def initialize(arg, keyword: nil, **args)\n # do nothing\nend"
132
+
133
+ util_compare inn, out
134
+ end
135
+
136
+ def test_defn_kwargs2
137
+ inn = s(:defn, :initialize,
138
+ s(:args, :arg,
139
+ s(:kwarg, :kw1, s(:nil)),
140
+ s(:kwarg, :kw2, s(:nil)),
141
+ :"**args"),
142
+ s(:nil))
143
+ out = "def initialize(arg, kw1: nil, kw2: nil, **args)\n # do nothing\nend"
144
+
145
+ util_compare inn, out
146
+ end
147
+
148
+ def test_call_self_index
149
+ util_compare s(:call, nil, :[], s(:lit, 42)), "self[42]"
150
+ end
151
+
152
+ def test_call_self_index_equals
153
+ util_compare(s(:attrasgn, s(:self), :[]=, s(:lit, 42), s(:lit, 24)),
154
+ "self[42] = 24")
155
+ end
156
+
157
+ def test_call_self_index_equals_array
158
+ util_compare(s(:attrasgn, s(:self), :[]=, s(:lit, 1), s(:lit, 2), s(:lit, 3)),
159
+ "self[1, 2] = 3")
160
+ end
161
+
162
+ def test_call_arglist_hash_first
163
+ inn = s(:call, nil, :method,
164
+ s(:hash, s(:lit, :a), s(:lit, 1)),
165
+ s(:call, nil, :b))
166
+ out = "method({ :a => 1 }, b)"
167
+
168
+ util_compare inn, out
169
+ end
170
+
171
+ def test_call_arglist_hash_first_last
172
+ inn = s(:call, nil, :method,
173
+ s(:hash, s(:lit, :a), s(:lit, 1)),
174
+ s(:call, nil, :b),
175
+ s(:hash, s(:lit, :c), s(:lit, 1)))
176
+ out = "method({ :a => 1 }, b, :c => 1)"
177
+
178
+ util_compare inn, out
179
+ end
180
+
181
+ def test_call_arglist_hash_last
182
+ inn = s(:call, nil, :method,
183
+ s(:call, nil, :b),
184
+ s(:hash, s(:lit, :a), s(:lit, 1)))
185
+ out = "method(b, :a => 1)"
186
+
187
+ util_compare inn, out
188
+ end
189
+
190
+ def test_call_arglist_if
191
+ inn = s(:call,
192
+ s(:call, nil, :a),
193
+ :+,
194
+ s(:if,
195
+ s(:call, nil, :b),
196
+ s(:call, nil, :c),
197
+ s(:call, nil, :d)))
198
+
199
+ out = "(a + (b ? (c) : (d)))"
200
+ util_compare inn, out
201
+ end
202
+
203
+ def test_defn_kwsplat
204
+ inn = s(:defn, :test, s(:args, :"**testing"), s(:nil))
205
+ out = "def test(**testing)\n # do nothing\nend"
206
+ assert_parse inn, out
207
+ end
208
+
209
+ def test_defn_rescue_return
210
+ inn = s(:defn, :blah, s(:args),
211
+ s(:rescue,
212
+ s(:lasgn, :a, s(:lit, 1)),
213
+ s(:resbody, s(:array), s(:return, s(:str, "a")))))
214
+ out = "def blah\n a = 1\nrescue\n return \"a\"\nend"
215
+
216
+ assert_parse inn, out
217
+ end
218
+
219
+ def test_masgn_block_arg
220
+ inn = s(:iter,
221
+ s(:call,
222
+ s(:nil),
223
+ :x),
224
+ s(:args, s(:masgn, :a, :b)),
225
+ s(:dstr, "",
226
+ s(:evstr, s(:lvar, :a)),
227
+ s(:str, "="),
228
+ s(:evstr, s(:lvar, :b))))
229
+ out = 'nil.x { |(a, b)| "#{a}=#{b}" }'
230
+
231
+ util_compare inn, out
232
+ end
233
+
234
+ def test_masgn_wtf
235
+ inn = s(:block,
236
+ s(:masgn,
237
+ s(:array, s(:lasgn, :k), s(:lasgn, :v)),
238
+ s(:splat,
239
+ s(:call,
240
+ s(:call, nil, :line),
241
+ :split,
242
+ s(:lit, /\=/), s(:lit, 2)))),
243
+ s(:attrasgn,
244
+ s(:self),
245
+ :[]=,
246
+ s(:lvar, :k),
247
+ s(:call, s(:lvar, :v), :strip)))
248
+
249
+ out = "k, v = *line.split(/\\=/, 2)\nself[k] = v.strip\n"
250
+
251
+ util_compare inn, out
252
+ end
253
+
254
+ def test_masgn_splat_wtf
255
+ inn = s(:masgn,
256
+ s(:array, s(:lasgn, :k), s(:lasgn, :v)),
257
+ s(:splat,
258
+ s(:call,
259
+ s(:call, nil, :line),
260
+ :split,
261
+ s(:lit, /\=/), s(:lit, 2))))
262
+ out = 'k, v = *line.split(/\\=/, 2)'
263
+ util_compare inn, out
264
+ end
265
+
266
+ def test_match3_asgn
267
+ inn = s(:match3, s(:lit, //), s(:lasgn, :y, s(:call, nil, :x)))
268
+ out = "(y = x) =~ //"
269
+ # "y = x =~ //", which matches on x and assigns to y (not what sexp says).
270
+ util_compare inn, out
271
+ end
272
+
273
+ def test_splat_call
274
+ inn = s(:call, nil, :x,
275
+ s(:splat,
276
+ s(:call,
277
+ s(:call, nil, :line),
278
+ :split,
279
+ s(:lit, /\=/), s(:lit, 2))))
280
+
281
+ out = 'x(*line.split(/\=/, 2))'
282
+ util_compare inn, out
283
+ end
284
+
285
+ def test_resbody_block
286
+ inn = s(:rescue,
287
+ s(:call, nil, :x1),
288
+ s(:resbody,
289
+ s(:array),
290
+ s(:call, nil, :x2),
291
+ s(:call, nil, :x3)))
292
+
293
+ out = "begin\n x1\nrescue\n x2\n x3\nend"
294
+ util_compare inn, out
295
+ end
296
+
297
+ def test_resbody_short_with_begin_end
298
+ # "begin; blah; rescue; []; end"
299
+ inn = s(:rescue,
300
+ s(:call, nil, :blah),
301
+ s(:resbody, s(:array), s(:array)))
302
+ out = "blah rescue []"
303
+ util_compare inn, out
304
+ end
305
+
306
+ def test_resbody_short_with_begin_end_multiple
307
+ # "begin; blah; rescue; []; end"
308
+ inn = s(:rescue,
309
+ s(:call, nil, :blah),
310
+ s(:resbody, s(:array),
311
+ s(:call, nil, :log),
312
+ s(:call, nil, :raise)))
313
+ out = "begin\n blah\nrescue\n log\n raise\nend"
314
+ util_compare inn, out
315
+ end
316
+
317
+ def test_resbody_short_with_defn_multiple
318
+ inn = s(:defn,
319
+ :foo,
320
+ s(:args),
321
+ s(:rescue,
322
+ s(:lasgn, :a, s(:lit, 1)),
323
+ s(:resbody,
324
+ s(:array),
325
+ s(:call, nil, :log),
326
+ s(:call, nil, :raise))))
327
+ out = "def foo\n a = 1\nrescue\n log\n raise\nend"
328
+ util_compare inn, out
329
+ end
330
+
331
+ def test_regexp_options
332
+ inn = s(:match3,
333
+ s(:dregx,
334
+ "abc",
335
+ s(:evstr, s(:call, nil, :x)),
336
+ s(:str, "def"),
337
+ 4),
338
+ s(:str, "a"))
339
+ out = '"a" =~ /abc#{x}def/m'
340
+ util_compare inn, out
341
+ end
342
+
343
+ def test_resbody_short_with_rescue_args
344
+ inn = s(:rescue,
345
+ s(:call, nil, :blah),
346
+ s(:resbody, s(:array, s(:const, :A), s(:const, :B)), s(:array)))
347
+ out = "begin\n blah\nrescue A, B\n []\nend"
348
+ util_compare inn, out
349
+ end
350
+
351
+ def test_call_binary_call_with_hash_arg
352
+ # if 42
353
+ # args << {:key => 24}
354
+ # end
355
+
356
+ inn = s(:if, s(:lit, 42),
357
+ s(:call, s(:call, nil, :args),
358
+ :<<,
359
+ s(:hash, s(:lit, :key), s(:lit, 24))),
360
+ nil)
361
+
362
+ out = "(args << { :key => 24 }) if 42"
363
+
364
+ util_compare inn, out
365
+ end
366
+
367
+ def test_binary_operators
368
+ # (1 > 2)
369
+ Ruby2Ruby::BINARY.each do |op|
370
+ inn = s(:call, s(:lit, 1), op, s(:lit, 2))
371
+ out = "(1 #{op} 2)"
372
+ util_compare inn, out
373
+ end
374
+ end
375
+
376
+ def test_call_empty_hash
377
+ inn = s(:call, nil, :foo, s(:hash))
378
+ out = "foo({})"
379
+ util_compare inn, out
380
+ end
381
+
382
+ def test_if_empty
383
+ inn = s(:if, s(:call, nil, :x), nil, nil)
384
+ out = "if x then\n # do nothing\nend"
385
+ util_compare inn, out
386
+ end
387
+
388
+ def test_interpolation_and_escapes
389
+ # log_entry = " \e[#{message_color}m#{message}\e[0m "
390
+ inn = s(:lasgn, :log_entry,
391
+ s(:dstr, " \e[",
392
+ s(:evstr, s(:call, nil, :message_color)),
393
+ s(:str, "m"),
394
+ s(:evstr, s(:call, nil, :message)),
395
+ s(:str, "\e[0m ")))
396
+ out = "log_entry = \" \e[#\{message_color}m#\{message}\e[0m \""
397
+
398
+ util_compare inn, out
399
+ end
400
+
401
+ def test_class_comments
402
+ inn = s(:class, :Z, nil)
403
+ inn.comments = "# x\n# y\n"
404
+ out = "# x\n# y\nclass Z\nend"
405
+ util_compare inn, out
406
+ end
407
+
408
+ def test_module_comments
409
+ inn = s(:module, :Z)
410
+ inn.comments = "# x\n# y\n"
411
+ out = "# x\n# y\nmodule Z\nend"
412
+ util_compare inn, out
413
+ end
414
+
415
+ def test_method_comments
416
+ inn = s(:defn, :z, s(:args), s(:nil))
417
+ inn.comments = "# x\n# y\n"
418
+ out = "# x\n# y\ndef z\n # do nothing\nend"
419
+ util_compare inn, out
420
+ end
421
+
422
+ def test_basic_ensure
423
+ inn = s(:ensure, s(:lit, 1), s(:lit, 2))
424
+ out = "begin\n 1\nensure\n 2\nend"
425
+ util_compare inn, out
426
+ end
427
+
428
+ def test_nested_ensure
429
+ inn = s(:ensure, s(:lit, 1), s(:ensure, s(:lit, 2), s(:lit, 3)))
430
+ out = "begin\n 1\nensure\n begin\n 2\n ensure\n 3\n end\nend"
431
+ util_compare inn, out
432
+ end
433
+
434
+ def test_nested_rescue
435
+ inn = s(:ensure, s(:lit, 1), s(:rescue, s(:lit, 2), s(:resbody, s(:array), s(:lit, 3))))
436
+ out = "begin\n 1\nensure\n 2 rescue 3\nend"
437
+ util_compare inn, out
438
+ end
439
+
440
+ def test_nested_rescue_exception
441
+ inn = s(:ensure, s(:lit, 1), s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))))
442
+ out = "begin\n 1\nensure\n begin\n 2\n rescue Exception\n 3\n end\nend"
443
+ util_compare inn, out
444
+ end
445
+
446
+ def test_nested_rescue_exception2
447
+ inn = s(:ensure, s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))), s(:lit, 1))
448
+ out = "begin\n 2\nrescue Exception\n 3\nensure\n 1\nend"
449
+ util_compare inn, out
450
+ end
451
+
452
+ def test_rescue_block
453
+ inn = s(:rescue,
454
+ s(:call, nil, :alpha),
455
+ s(:resbody, s(:array),
456
+ s(:call, nil, :beta),
457
+ s(:call, nil, :gamma)))
458
+ out = "begin\n alpha\nrescue\n beta\n gamma\nend"
459
+ util_compare inn, out
460
+ end
461
+
462
+ def test_array_adds_parens_around_rescue
463
+ inn = s(:array,
464
+ s(:call, nil, :a),
465
+ s(:rescue, s(:call, nil, :b), s(:resbody, s(:array), s(:call, nil, :c))))
466
+ out = "[a, (b rescue c)]"
467
+
468
+ util_compare inn, out
469
+ end
470
+
471
+ def test_call_arglist_rescue
472
+ inn = s(:call,
473
+ nil,
474
+ :method,
475
+ s(:rescue,
476
+ s(:call, nil, :a),
477
+ s(:resbody, s(:array), s(:call, nil, :b))))
478
+ out = "method((a rescue b))"
479
+ util_compare inn, out
480
+ end
481
+
482
+ def test_unless_vs_if_not
483
+ rb1 = "a unless b"
484
+ rb2 = "a if (not b)"
485
+ rb3 = "a if ! b"
486
+
487
+ util_compare Ruby18Parser.new.parse(rb1), rb1
488
+ util_compare Ruby19Parser.new.parse(rb1), rb1
489
+
490
+ util_compare Ruby18Parser.new.parse(rb2), rb1
491
+ util_compare Ruby19Parser.new.parse(rb2), rb2
492
+
493
+ util_compare Ruby18Parser.new.parse(rb3), rb1
494
+ util_compare Ruby19Parser.new.parse(rb3), rb2
495
+ end
496
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sexp2ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Davis
8
+ - Jared Beck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sexp_processor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.6'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec-core
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.2'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec-expectations
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec-mocks
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.2'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.2'
70
+ - !ruby/object:Gem::Dependency
71
+ name: ruby_parser
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.7'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.7'
84
+ description: |
85
+ Generates ruby from RubyParser-compatible S-expressions.
86
+ It is a fork of ruby2ruby with slightly different goals.
87
+ email:
88
+ - jared@jaredbeck.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - ".travis.yml"
95
+ - CHANGELOG.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - lib/sexp2ruby.rb
101
+ - lib/sexp2ruby/core_extensions/regexp.rb
102
+ - lib/sexp2ruby/errors.rb
103
+ - lib/sexp2ruby/processor.rb
104
+ - lib/sexp2ruby/version.rb
105
+ - sexp2ruby.gemspec
106
+ - spec/lib/processor_spec.rb
107
+ - spec/spec_helper.rb
108
+ - test/test_ruby2ruby.rb
109
+ homepage: https://github.com/jaredbeck/sexp2ruby
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 1.9.3
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Generates ruby from RubyParser S-expressions
133
+ test_files:
134
+ - spec/lib/processor_spec.rb
135
+ - spec/spec_helper.rb
136
+ - test/test_ruby2ruby.rb
137
+ has_rdoc: