sexp_processor 3.0.5 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4646 @@
1
+ $TESTING = true
2
+
3
+ require 'minitest/unit'
4
+ require 'sexp_processor' # for deep_clone
5
+
6
+ # key:
7
+ # wwtt = what were they thinking?
8
+
9
+ # TODO: <ko1_> 1.8.7 support {|&b|} syntax
10
+
11
+ class Examples
12
+ attr_reader :reader
13
+ attr_writer :writer
14
+
15
+ def a_method(x); x+1; end
16
+ alias an_alias a_method
17
+
18
+ define_method(:bmethod_noargs) do
19
+ x + 1
20
+ end
21
+
22
+ define_method(:unsplatted) do |x|
23
+ x + 1
24
+ end
25
+
26
+ define_method :splatted do |*args|
27
+ y = args.first
28
+ y + 42
29
+ end
30
+
31
+ define_method :dmethod_added, instance_method(:a_method) if
32
+ RUBY_VERSION < "1.9"
33
+ end
34
+
35
+ class ParseTreeTestCase < MiniTest::Unit::TestCase
36
+ attr_accessor :processor # to be defined by subclass
37
+
38
+ def setup
39
+ super
40
+ @processor = nil
41
+ end
42
+
43
+ def after_process_hook klass, node, data, input_name, output_name
44
+ end
45
+
46
+ def before_process_hook klass, node, data, input_name, output_name
47
+ end
48
+
49
+ def self.add_test name, data, klass = self.name[4..-1]
50
+ name = name.to_s
51
+ klass = klass.to_s
52
+
53
+ if testcases.has_key? name then
54
+ if testcases[name].has_key? klass then
55
+ warn "testcase #{klass}##{name} already has data"
56
+ else
57
+ testcases[name][klass] = data
58
+ end
59
+ else
60
+ warn "testcase #{name} does not exist"
61
+ end
62
+ end
63
+
64
+ def self.add_tests name, hash
65
+ name = name.to_s
66
+
67
+ hash.each do |klass, data|
68
+ warn "testcase #{klass}##{name} already has data" if
69
+ testcases[name].has_key? klass
70
+ testcases[name][klass] = data
71
+ end
72
+ end
73
+
74
+ def self.add_18tests name, hash
75
+ return unless RUBY_VERSION >= "1.8" and RUBY_VERSION < "1.9"
76
+ add_tests "#{name}__18", hash
77
+ end
78
+
79
+ def self.add_19tests name, hash
80
+ return unless RUBY_VERSION >= "1.9"
81
+ add_tests "#{name}__19", hash
82
+ end
83
+
84
+ def self.clone_same
85
+ @@testcases.each do |node, data|
86
+ data.each do |key, val|
87
+ if val == :same then
88
+ prev_key = self.previous(key)
89
+ data[key] = data[prev_key].deep_clone
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ def self.copy_test_case nonverbose, klass
96
+ verbose = nonverbose + "_mri_verbose_flag"
97
+ testcases[verbose][klass] = testcases[nonverbose][klass]
98
+ end
99
+
100
+ def self.generate_test klass, node, data, input_name, output_name
101
+ klass.send(:define_method, "test_#{node}".to_sym) do
102
+ flunk "Processor is nil" if processor.nil?
103
+
104
+ assert data.has_key?(input_name), "Unknown input data"
105
+ assert data.has_key?(output_name), "Missing test data"
106
+
107
+ $missing[node] << output_name unless data.has_key? output_name
108
+
109
+ input = data[input_name].deep_clone
110
+ expected = data[output_name].deep_clone
111
+
112
+ case expected
113
+ when :unsupported then
114
+ assert_raises(UnsupportedNodeError) do
115
+ processor.process(input)
116
+ end
117
+ else
118
+ extra_expected = []
119
+ extra_input = []
120
+
121
+ _, expected, extra_expected = *expected if
122
+ Array === expected and expected.first == :defx
123
+ _, input, extra_input = *input if
124
+ Array === input and input.first == :defx
125
+
126
+ # OMG... I can't believe I have to do this this way. these
127
+ # hooks are here instead of refactoring this define_method
128
+ # body into an assertion. It'll allow subclasses to hook in
129
+ # and add behavior before or after the processor does it's
130
+ # thing. If you go the body refactor route, some of the
131
+ # RawParseTree test casese fail for completely bogus reasons.
132
+
133
+ before_process_hook klass, node, data, input_name, output_name
134
+ refute_nil data[input_name], "testcase does not exist?"
135
+ @result = processor.process input
136
+ assert_equal(expected, @result,
137
+ "failed on input: #{data[input_name].inspect}")
138
+ after_process_hook klass, node, data, input_name, output_name
139
+
140
+ extra_input.each do |extra|
141
+ processor.process(extra)
142
+ end
143
+ extra = processor.extra_methods rescue []
144
+ assert_equal extra_expected, extra
145
+ end
146
+ end
147
+ end
148
+
149
+ def self.generate_tests klass
150
+ install_missing_reporter
151
+ clone_same
152
+
153
+ output_name = klass.name.to_s.sub(/^Test/, '')
154
+
155
+ input_name = self.previous(output_name)
156
+
157
+ @@testcases.each do |node, data|
158
+ next if [:skip, :unsupported].include? data[input_name]
159
+ next if data[output_name] == :skip
160
+
161
+ generate_test klass, node, data, input_name, output_name
162
+ end
163
+ end
164
+
165
+ def self.inherited klass
166
+ super
167
+
168
+ generate_tests klass unless klass.name =~ /TestCase/
169
+ end
170
+
171
+ def self.install_missing_reporter
172
+ unless defined? $missing then
173
+ $missing = Hash.new { |h,k| h[k] = [] }
174
+ at_exit {
175
+ at_exit {
176
+ warn ""
177
+ $missing.sort.each do |name, klasses|
178
+ warn "add_tests(#{name.inspect},"
179
+ klasses.map! { |klass| " #{klass.inspect} => :same" }
180
+ warn klasses.join("\n") + ")"
181
+ end
182
+ warn ""
183
+ }
184
+ }
185
+ end
186
+ end
187
+
188
+ def self.previous(key, extra=0) # FIX: remove R2C code
189
+ idx = @@testcase_order.index(key)
190
+
191
+ raise "Unknown class #{key} in @@testcase_order" if idx.nil?
192
+
193
+ case key
194
+ when "RubyToRubyC" then
195
+ idx -= 1
196
+ end
197
+ @@testcase_order[idx - 1 - extra]
198
+ end
199
+
200
+ def self.testcase_order; @@testcase_order; end
201
+ def self.testcases; @@testcases; end
202
+
203
+ def self.unsupported_tests *tests
204
+ tests.flatten.each do |name|
205
+ add_test name, :unsupported
206
+ end
207
+ end
208
+
209
+ ############################################################
210
+ # Shared TestCases:
211
+
212
+ @@testcase_order = %w(Ruby RawParseTree ParseTree)
213
+
214
+ @@testcases = Hash.new { |h,k| h[k] = {} }
215
+
216
+ add_tests("alias",
217
+ "Ruby" => "class X\n alias :y :x\nend",
218
+ "RawParseTree" => [:class, :X, nil,
219
+ [:scope, [:alias, [:lit, :y], [:lit, :x]]]],
220
+ "ParseTree" => s(:class, :X, nil,
221
+ s(:scope, s(:alias, s(:lit, :y), s(:lit, :x)))))
222
+
223
+ add_tests("alias_ugh",
224
+ "Ruby" => "class X\n alias y x\nend",
225
+ "RawParseTree" => [:class, :X, nil,
226
+ [:scope, [:alias, [:lit, :y], [:lit, :x]]]],
227
+ "ParseTree" => s(:class, :X, nil,
228
+ s(:scope, s(:alias, s(:lit, :y), s(:lit, :x)))),
229
+ "Ruby2Ruby" => "class X\n alias :y :x\nend")
230
+
231
+ add_tests("and",
232
+ "Ruby" => "(a and b)",
233
+ "RawParseTree" => [:and, [:vcall, :a], [:vcall, :b]],
234
+ "ParseTree" => s(:and,
235
+ s(:call, nil, :a, s(:arglist)),
236
+ s(:call, nil, :b, s(:arglist))))
237
+
238
+ add_tests("argscat_inside",
239
+ "Ruby" => "a = [b, *c]",
240
+ "RawParseTree" => [:lasgn, :a,
241
+ [:argscat,
242
+ [:array, [:vcall, :b]], [:vcall, :c]]],
243
+ "ParseTree" => s(:lasgn, :a,
244
+ s(:array,
245
+ s(:call, nil, :b, s(:arglist)),
246
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
247
+
248
+ add_tests("argscat_svalue",
249
+ "Ruby" => "a = b, c, *d",
250
+ "RawParseTree" => [:lasgn, :a,
251
+ [:svalue,
252
+ [:argscat,
253
+ [:array, [:vcall, :b], [:vcall, :c]],
254
+ [:vcall, :d]]]],
255
+ "ParseTree" => s(:lasgn, :a,
256
+ s(:svalue,
257
+ s(:array,
258
+ s(:call, nil, :b, s(:arglist)),
259
+ s(:call, nil, :c, s(:arglist)),
260
+ s(:splat,
261
+ s(:call, nil, :d, s(:arglist)))))))
262
+
263
+ add_tests("argspush",
264
+ "Ruby" => "a[*b] = c",
265
+ "RawParseTree" => [:attrasgn,
266
+ [:vcall, :a],
267
+ :[]=,
268
+ [:argspush,
269
+ [:splat,
270
+ [:vcall, :b]],
271
+ [:vcall, :c]]],
272
+ "ParseTree" => s(:attrasgn,
273
+ s(:call, nil, :a, s(:arglist)),
274
+ :[]=,
275
+ s(:arglist,
276
+ s(:splat,
277
+ s(:call, nil, :b, s(:arglist))),
278
+ s(:call, nil, :c, s(:arglist)))))
279
+
280
+ add_tests("array",
281
+ "Ruby" => "[1, :b, \"c\"]",
282
+ "RawParseTree" => [:array, [:lit, 1], [:lit, :b], [:str, "c"]],
283
+ "ParseTree" => s(:array, s(:lit, 1), s(:lit, :b), s(:str, "c")))
284
+
285
+ add_tests("array_pct_W",
286
+ "Ruby" => "%W[a b c]",
287
+ "RawParseTree" => [:array, [:str, "a"], [:str, "b"], [:str, "c"]],
288
+ "ParseTree" => s(:array,
289
+ s(:str, "a"), s(:str, "b"), s(:str, "c")),
290
+ "Ruby2Ruby" => "[\"a\", \"b\", \"c\"]")
291
+
292
+ add_tests("array_pct_W_dstr",
293
+ "Ruby" => "%W[a #\{@b} c]",
294
+ "RawParseTree" => [:array,
295
+ [:str, "a"],
296
+ [:dstr, "", [:evstr, [:ivar, :@b]]],
297
+ [:str, "c"]],
298
+ "ParseTree" => s(:array,
299
+ s(:str, "a"),
300
+ s(:dstr, "", s(:evstr, s(:ivar, :@b))),
301
+ s(:str, "c")),
302
+ "Ruby2Ruby" => "[\"a\", \"#\{@b}\", \"c\"]")
303
+
304
+ add_tests("array_pct_w",
305
+ "Ruby" => "%w[a b c]",
306
+ "RawParseTree" => [:array, [:str, "a"], [:str, "b"], [:str, "c"]],
307
+ "ParseTree" => s(:array,
308
+ s(:str, "a"), s(:str, "b"), s(:str, "c")),
309
+ "Ruby2Ruby" => "[\"a\", \"b\", \"c\"]")
310
+
311
+ add_tests("array_pct_w_dstr",
312
+ "Ruby" => "%w[a #\{@b} c]",
313
+ "RawParseTree" => [:array,
314
+ [:str, "a"],
315
+ [:str, "#\{@b}"],
316
+ [:str, "c"]],
317
+ "ParseTree" => s(:array,
318
+ s(:str, "a"),
319
+ s(:str, "#\{@b}"),
320
+ s(:str, "c")),
321
+ "Ruby2Ruby" => "[\"a\", \"\\\#{@b}\", \"c\"]") # TODO: huh?
322
+
323
+ add_tests("attrasgn",
324
+ "Ruby" => "y = 0\n42.method = y\n",
325
+ "RawParseTree" => [:block,
326
+ [:lasgn, :y, [:lit, 0]],
327
+ [:attrasgn, [:lit, 42], :method=,
328
+ [:array, [:lvar, :y]]]],
329
+ "ParseTree" => s(:block,
330
+ s(:lasgn, :y, s(:lit, 0)),
331
+ s(:attrasgn, s(:lit, 42), :method=,
332
+ s(:arglist, s(:lvar, :y)))))
333
+
334
+ add_tests("attrasgn_index_equals",
335
+ "Ruby" => "a[42] = 24",
336
+ "RawParseTree" => [:attrasgn, [:vcall, :a], :[]=,
337
+ [:array, [:lit, 42], [:lit, 24]]],
338
+ "ParseTree" => s(:attrasgn,
339
+ s(:call, nil, :a, s(:arglist)),
340
+ :[]=,
341
+ s(:arglist, s(:lit, 42), s(:lit, 24))))
342
+
343
+ add_tests("attrasgn_index_equals_space",
344
+ "Ruby" => "a = []; a [42] = 24",
345
+ "RawParseTree" => [:block,
346
+ [:lasgn, :a, [:zarray]],
347
+ [:attrasgn, [:lvar, :a], :[]=,
348
+ [:array, [:lit, 42], [:lit, 24]]]],
349
+ "ParseTree" => s(:block,
350
+ s(:lasgn, :a, s(:array)),
351
+ s(:attrasgn, s(:lvar, :a), :[]=,
352
+ s(:arglist, s(:lit, 42), s(:lit, 24)))),
353
+ "Ruby2Ruby" => "a = []\na[42] = 24\n")
354
+
355
+ add_tests("attrset",
356
+ "Ruby" => [Examples, :writer=],
357
+ "RawParseTree" => [:defn, :writer=, [:attrset, :@writer]],
358
+ "ParseTree" => s(:defn, :writer=,
359
+ s(:args, :arg),
360
+ s(:attrset, :@writer)),
361
+ "Ruby2Ruby" => "attr_writer :writer")
362
+
363
+ add_tests("back_ref",
364
+ "Ruby" => "[$&, $`, $', $+]",
365
+ "RawParseTree" => [:array,
366
+ [:back_ref, :&],
367
+ [:back_ref, :"`"],
368
+ [:back_ref, :"'"],
369
+ [:back_ref, :+]],
370
+ "ParseTree" => s(:array,
371
+ s(:back_ref, :&),
372
+ s(:back_ref, :"`"),
373
+ s(:back_ref, :"'"),
374
+ s(:back_ref, :+)))
375
+
376
+ add_tests("begin",
377
+ "Ruby" => "begin\n (1 + 1)\nend",
378
+ "RawParseTree" => [:call, [:lit, 1], :+, [:array, [:lit, 1]]],
379
+ "ParseTree" => s(:call, s(:lit, 1), :+,
380
+ s(:arglist, s(:lit, 1))),
381
+ "Ruby2Ruby" => "(1 + 1)")
382
+
383
+ add_tests("begin_def",
384
+ "Ruby" => "def m\n begin\n\n end\nend",
385
+ "RawParseTree" => [:defn, :m, [:scope, [:block, [:args], [:nil]]]],
386
+ "ParseTree" => s(:defn, :m, s(:args),
387
+ s(:scope, s(:block, s(:nil)))),
388
+ "Ruby2Ruby" => "def m\n # do nothing\nend")
389
+
390
+ add_tests("begin_rescue_ensure",
391
+ "Ruby" => "begin\n a\nrescue\n # do nothing\nensure\n # do nothing\nend",
392
+ "RawParseTree" => [:ensure,
393
+ [:rescue,
394
+ [:vcall, :a],
395
+ [:resbody, nil]],
396
+ [:nil]],
397
+ "ParseTree" => s(:ensure,
398
+ s(:rescue,
399
+ s(:call, nil, :a, s(:arglist)),
400
+ s(:resbody, s(:array), nil)),
401
+ s(:nil)))
402
+
403
+ add_tests("begin_rescue_ensure_all_empty",
404
+ "Ruby" => "begin\n # do nothing\nrescue\n # do nothing\nensure\n # do nothing\nend",
405
+ "RawParseTree" => [:ensure,
406
+ [:rescue,
407
+ [:resbody, nil]],
408
+ [:nil]],
409
+ "ParseTree" => s(:ensure,
410
+ s(:rescue,
411
+ s(:resbody, s(:array), nil)),
412
+ s(:nil)))
413
+
414
+ add_tests("begin_rescue_twice",
415
+ "Ruby" => "begin\n a\nrescue => mes\n # do nothing\nend\nbegin\n b\nrescue => mes\n # do nothing\nend\n",
416
+ "RawParseTree" => [:block,
417
+ [:rescue,
418
+ [:vcall, :a],
419
+ [:resbody, nil,
420
+ [:lasgn, :mes, [:gvar, :$!]]]],
421
+ [:rescue,
422
+ [:vcall, :b],
423
+ [:resbody, nil,
424
+ [:lasgn, :mes, [:gvar, :$!]]]]],
425
+ "ParseTree" => s(:block,
426
+ s(:rescue,
427
+ s(:call, nil, :a, s(:arglist)),
428
+ s(:resbody,
429
+ s(:array, s(:lasgn, :mes, s(:gvar, :$!))),
430
+ nil)),
431
+ s(:rescue,
432
+ s(:call, nil, :b, s(:arglist)),
433
+ s(:resbody,
434
+ s(:array,
435
+ s(:lasgn, :mes, s(:gvar, :$!))),
436
+ nil))))
437
+
438
+ add_tests("begin_rescue_twice_mri_verbose_flag",
439
+ "RawParseTree" => [:block,
440
+ [:rescue, # no begin
441
+ [:vcall, :a],
442
+ [:resbody, nil,
443
+ [:lasgn, :mes, [:gvar, :$!]]]],
444
+ [:rescue,
445
+ [:vcall, :b],
446
+ [:resbody, nil,
447
+ [:lasgn, :mes, [:gvar, :$!]]]]])
448
+
449
+ copy_test_case "begin_rescue_twice", "Ruby"
450
+ copy_test_case "begin_rescue_twice", "ParseTree"
451
+
452
+ add_tests("block_attrasgn",
453
+ "Ruby" => "def self.setup(ctx)\n bind = allocate\n bind.context = ctx\n return bind\nend",
454
+ "RawParseTree" => [:defs, [:self], :setup,
455
+ [:scope,
456
+ [:block,
457
+ [:args, :ctx],
458
+ [:lasgn, :bind, [:vcall, :allocate]],
459
+ [:attrasgn, [:lvar, :bind], :context=,
460
+ [:array, [:lvar, :ctx]]],
461
+ [:return, [:lvar, :bind]]]]],
462
+ "ParseTree" => s(:defs, s(:self), :setup,
463
+ s(:args, :ctx),
464
+ s(:scope,
465
+ s(:block,
466
+ s(:lasgn, :bind,
467
+ s(:call, nil, :allocate, s(:arglist))),
468
+ s(:attrasgn, s(:lvar, :bind), :context=,
469
+ s(:arglist, s(:lvar, :ctx))),
470
+ s(:return, s(:lvar, :bind))))))
471
+
472
+
473
+ add_tests("block_lasgn",
474
+ "Ruby" => "x = (y = 1\n(y + 2))",
475
+ "RawParseTree" => [:lasgn, :x,
476
+ [:block,
477
+ [:lasgn, :y, [:lit, 1]],
478
+ [:call, [:lvar, :y], :+, [:array, [:lit, 2]]]]],
479
+ "ParseTree" => s(:lasgn, :x,
480
+ s(:block,
481
+ s(:lasgn, :y, s(:lit, 1)),
482
+ s(:call, s(:lvar, :y), :+,
483
+ s(:arglist, s(:lit, 2))))))
484
+
485
+ add_tests("block_mystery_block",
486
+ "Ruby" => "a(b) do\n if b then\n true\n else\n c = false\n d { |x| c = true }\n c\n end\nend",
487
+ "RawParseTree" => [:iter,
488
+ [:fcall, :a, [:array, [:vcall, :b]]],
489
+ nil,
490
+ [:if,
491
+ [:vcall, :b],
492
+ [:true],
493
+ [:block,
494
+ [:dasgn_curr, :c, [:false]],
495
+ [:iter,
496
+ [:fcall, :d],
497
+ [:dasgn_curr, :x],
498
+ [:dasgn, :c, [:true]]],
499
+ [:dvar, :c]]]],
500
+ "ParseTree" => s(:iter,
501
+ s(:call, nil, :a,
502
+ s(:arglist, s(:call, nil, :b, s(:arglist)))),
503
+ nil,
504
+ s(:if,
505
+ s(:call, nil, :b, s(:arglist)),
506
+ s(:true),
507
+ s(:block,
508
+ s(:lasgn, :c, s(:false)),
509
+ s(:iter,
510
+ s(:call, nil, :d, s(:arglist)),
511
+ s(:lasgn, :x),
512
+ s(:lasgn, :c, s(:true))),
513
+ s(:lvar, :c)))))
514
+
515
+ add_tests("block_pass_args_and_splat",
516
+ "Ruby" => "def blah(*args, &block)\n other(42, *args, &block)\nend",
517
+ "RawParseTree" => [:defn, :blah,
518
+ [:scope,
519
+ [:block,
520
+ [:args, :"*args"],
521
+ [:block_arg, :block],
522
+ [:block_pass,
523
+ [:lvar, :block],
524
+ [:fcall, :other,
525
+ [:argscat,
526
+ [:array, [:lit, 42]], [:lvar, :args]]]]]]],
527
+ "ParseTree" => s(:defn, :blah,
528
+ s(:args, :"*args", :"&block"),
529
+ s(:scope,
530
+ s(:block,
531
+ s(:call, nil, :other,
532
+ s(:arglist,
533
+ s(:lit, 42),
534
+ s(:splat, s(:lvar, :args)),
535
+ s(:block_pass, s(:lvar, :block))))))))
536
+
537
+ add_tests("block_pass_call_0",
538
+ "Ruby" => "a.b(&c)",
539
+ "RawParseTree" => [:block_pass,
540
+ [:vcall, :c], [:call, [:vcall, :a], :b]],
541
+ "ParseTree" => s(:call,
542
+ s(:call, nil, :a, s(:arglist)),
543
+ :b,
544
+ s(:arglist,
545
+ s(:block_pass,
546
+ s(:call, nil, :c, s(:arglist))))))
547
+
548
+ add_tests("block_pass_call_1",
549
+ "Ruby" => "a.b(4, &c)",
550
+ "RawParseTree" => [:block_pass,
551
+ [:vcall, :c],
552
+ [:call, [:vcall, :a], :b, [:array, [:lit, 4]]]],
553
+ "ParseTree" => s(:call,
554
+ s(:call, nil, :a, s(:arglist)),
555
+ :b,
556
+ s(:arglist,
557
+ s(:lit, 4),
558
+ s(:block_pass,
559
+ s(:call, nil, :c, s(:arglist))))))
560
+
561
+ add_tests("block_pass_call_n",
562
+ "Ruby" => "a.b(1, 2, 3, &c)",
563
+ "RawParseTree" => [:block_pass,
564
+ [:vcall, :c],
565
+ [:call, [:vcall, :a], :b,
566
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]]],
567
+ "ParseTree" => s(:call,
568
+ s(:call, nil, :a, s(:arglist)),
569
+ :b,
570
+ s(:arglist,
571
+ s(:lit, 1), s(:lit, 2), s(:lit, 3),
572
+ s(:block_pass,
573
+ s(:call, nil, :c, s(:arglist))))))
574
+
575
+ add_tests("block_pass_fcall_0",
576
+ "Ruby" => "a(&b)",
577
+ "RawParseTree" => [:block_pass, [:vcall, :b], [:fcall, :a]],
578
+ "ParseTree" => s(:call, nil, :a,
579
+ s(:arglist,
580
+ s(:block_pass,
581
+ s(:call, nil, :b, s(:arglist))))))
582
+
583
+ add_tests("block_pass_fcall_1",
584
+ "Ruby" => "a(4, &b)",
585
+ "RawParseTree" => [:block_pass,
586
+ [:vcall, :b],
587
+ [:fcall, :a, [:array, [:lit, 4]]]],
588
+ "ParseTree" => s(:call, nil, :a,
589
+ s(:arglist,
590
+ s(:lit, 4),
591
+ s(:block_pass,
592
+ s(:call, nil, :b, s(:arglist))))))
593
+
594
+ add_tests("block_pass_fcall_n",
595
+ "Ruby" => "a(1, 2, 3, &b)",
596
+ "RawParseTree" => [:block_pass,
597
+ [:vcall, :b],
598
+ [:fcall, :a,
599
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]]],
600
+ "ParseTree" => s(:call, nil, :a,
601
+ s(:arglist,
602
+ s(:lit, 1), s(:lit, 2), s(:lit, 3),
603
+ s(:block_pass,
604
+ s(:call, nil, :b, s(:arglist))))))
605
+
606
+ add_tests("block_pass_omgwtf",
607
+ "Ruby" => "define_attr_method(:x, :sequence_name, &Proc.new { |*args| nil })",
608
+ "RawParseTree" => [:block_pass,
609
+ [:iter,
610
+ [:call, [:const, :Proc], :new],
611
+ [:masgn, nil, [:dasgn_curr, :args], nil],
612
+ [:nil]],
613
+ [:fcall, :define_attr_method,
614
+ [:array, [:lit, :x], [:lit, :sequence_name]]]],
615
+ "ParseTree" => s(:call, nil, :define_attr_method,
616
+ s(:arglist,
617
+ s(:lit, :x),
618
+ s(:lit, :sequence_name),
619
+ s(:block_pass,
620
+ s(:iter,
621
+ s(:call, s(:const, :Proc), :new,
622
+ s(:arglist)),
623
+ s(:masgn,
624
+ s(:array, s(:splat, s(:lasgn, :args)))),
625
+ s(:nil))))))
626
+
627
+ add_tests("block_pass_splat",
628
+ "Ruby" => "def blah(*args, &block)\n other(*args, &block)\nend",
629
+ "RawParseTree" => [:defn, :blah,
630
+ [:scope,
631
+ [:block,
632
+ [:args, :"*args"],
633
+ [:block_arg, :block],
634
+ [:block_pass,
635
+ [:lvar, :block],
636
+ [:fcall, :other,
637
+ [:splat, [:lvar, :args]]]]]]],
638
+ "ParseTree" => s(:defn, :blah,
639
+ s(:args, :"*args", :"&block"),
640
+ s(:scope,
641
+ s(:block,
642
+ s(:call, nil, :other,
643
+ s(:arglist,
644
+ s(:splat, s(:lvar, :args)),
645
+ s(:block_pass, s(:lvar, :block))))))))
646
+
647
+ add_tests("block_pass_thingy",
648
+ "Ruby" => "r.read_body(dest, &block)",
649
+ "RawParseTree" => [:block_pass,
650
+ [:vcall, :block],
651
+ [:call, [:vcall, :r], :read_body,
652
+ [:array, [:vcall, :dest]]]],
653
+ "ParseTree" => s(:call,
654
+ s(:call, nil, :r, s(:arglist)),
655
+ :read_body,
656
+ s(:arglist,
657
+ s(:call, nil, :dest, s(:arglist)),
658
+ s(:block_pass,
659
+ s(:call, nil, :block, s(:arglist))))))
660
+
661
+ add_tests("block_stmt_after",
662
+ "Ruby" => "def f\n begin\n b\n rescue\n c\n end\n\n d\nend",
663
+ "RawParseTree" => [:defn, :f,
664
+ [:scope,
665
+ [:block,
666
+ [:args],
667
+ [:rescue,
668
+ [:vcall, :b],
669
+ [:resbody, nil, [:vcall, :c]]],
670
+ [:vcall, :d]]]],
671
+ "ParseTree" => s(:defn, :f,
672
+ s(:args),
673
+ s(:scope,
674
+ s(:block,
675
+ s(:rescue,
676
+ s(:call, nil, :b, s(:arglist)),
677
+ s(:resbody,
678
+ s(:array),
679
+ s(:call, nil, :c, s(:arglist)))),
680
+ s(:call, nil, :d, s(:arglist))))),
681
+ "Ruby2Ruby" => "def f\n b rescue c\n d\nend")
682
+
683
+ add_tests("block_stmt_after_mri_verbose_flag",
684
+ "RawParseTree" => [:defn, :f,
685
+ [:scope,
686
+ [:block,
687
+ [:args],
688
+ [:rescue, # no begin
689
+ [:vcall, :b],
690
+ [:resbody, nil, [:vcall, :c]]],
691
+ [:vcall, :d]]]])
692
+
693
+ copy_test_case "block_stmt_after", "Ruby"
694
+ copy_test_case "block_stmt_after", "ParseTree"
695
+ copy_test_case "block_stmt_after", "Ruby2Ruby"
696
+
697
+ add_tests("block_stmt_before",
698
+ "Ruby" => "def f\n a\n begin\n b\n rescue\n c\n end\nend",
699
+ "RawParseTree" => [:defn, :f,
700
+ [:scope,
701
+ [:block,
702
+ [:args],
703
+ [:vcall, :a],
704
+ [:rescue, [:vcall, :b],
705
+ [:resbody, nil, [:vcall, :c]]]]]],
706
+ "ParseTree" => s(:defn, :f,
707
+ s(:args),
708
+ s(:scope,
709
+ s(:block,
710
+ s(:call, nil, :a, s(:arglist)),
711
+ s(:rescue, s(:call, nil, :b, s(:arglist)),
712
+ s(:resbody,
713
+ s(:array),
714
+ s(:call, nil, :c, s(:arglist))))))),
715
+ "Ruby2Ruby" => "def f\n a\n b rescue c\nend")
716
+
717
+ # oddly... this one doesn't HAVE any differences when verbose... new?
718
+ copy_test_case "block_stmt_before", "Ruby"
719
+ copy_test_case "block_stmt_before", "ParseTree"
720
+ copy_test_case "block_stmt_before", "RawParseTree"
721
+ copy_test_case "block_stmt_before", "Ruby2Ruby"
722
+
723
+ add_tests("block_stmt_both",
724
+ "Ruby" => "def f\n a\n begin\n b\n rescue\n c\n end\n d\nend",
725
+ "RawParseTree" => [:defn, :f,
726
+ [:scope,
727
+ [:block,
728
+ [:args],
729
+ [:vcall, :a],
730
+ [:rescue,
731
+ [:vcall, :b],
732
+ [:resbody,
733
+ nil,
734
+ [:vcall, :c]]],
735
+ [:vcall, :d]]]],
736
+ "ParseTree" => s(:defn, :f, s(:args),
737
+ s(:scope,
738
+ s(:block,
739
+ s(:call, nil, :a, s(:arglist)),
740
+ s(:rescue,
741
+ s(:call, nil, :b, s(:arglist)),
742
+ s(:resbody,
743
+ s(:array),
744
+ s(:call, nil, :c, s(:arglist)))),
745
+ s(:call, nil, :d, s(:arglist))))),
746
+ "Ruby2Ruby" => "def f\n a\n b rescue c\n d\nend")
747
+
748
+ add_tests("block_stmt_both_mri_verbose_flag",
749
+ "RawParseTree" => [:defn, :f,
750
+ [:scope,
751
+ [:block,
752
+ [:args],
753
+ [:vcall, :a],
754
+ [:rescue, # no begin
755
+ [:vcall, :b],
756
+ [:resbody,
757
+ nil,
758
+ [:vcall, :c]]],
759
+ [:vcall, :d]]]])
760
+
761
+ copy_test_case "block_stmt_both", "Ruby"
762
+ copy_test_case "block_stmt_both", "ParseTree"
763
+ copy_test_case "block_stmt_both", "Ruby2Ruby"
764
+
765
+ add_tests("bmethod",
766
+ "Ruby" => [Examples, :unsplatted],
767
+ "RawParseTree" => [:defn, :unsplatted,
768
+ [:bmethod,
769
+ [:dasgn_curr, :x],
770
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]],
771
+ "ParseTree" => s(:defn, :unsplatted,
772
+ s(:args, :x),
773
+ s(:scope,
774
+ s(:block,
775
+ s(:call,
776
+ s(:lvar, :x),
777
+ :+,
778
+ s(:arglist, s(:lit, 1)))))),
779
+ "Ruby2Ruby" => "def unsplatted(x)\n (x + 1)\nend")
780
+
781
+ add_tests("bmethod_noargs",
782
+ "Ruby" => [Examples, :bmethod_noargs],
783
+ "RawParseTree" => [:defn, :bmethod_noargs,
784
+ [:bmethod,
785
+ nil,
786
+ [:call,
787
+ [:vcall, :x], :"+", [:array, [:lit, 1]]]]],
788
+ "ParseTree" => s(:defn, :bmethod_noargs,
789
+ s(:args),
790
+ s(:scope,
791
+ s(:block,
792
+ s(:call,
793
+ s(:call, nil, :x, s(:arglist)),
794
+ :"+",
795
+ s(:arglist, s(:lit, 1)))))),
796
+ "Ruby2Ruby" => "def bmethod_noargs\n (x + 1)\nend")
797
+
798
+ add_tests("bmethod_splat",
799
+ "Ruby" => [Examples, :splatted],
800
+ "RawParseTree" => [:defn, :splatted,
801
+ [:bmethod,
802
+ [:masgn, nil, [:dasgn_curr, :args], nil],
803
+ [:block,
804
+ [:dasgn_curr, :y,
805
+ [:call, [:dvar, :args], :first]],
806
+ [:call, [:dvar, :y], :+,
807
+ [:array, [:lit, 42]]]]]],
808
+ "ParseTree" => s(:defn, :splatted,
809
+ s(:args, :"*args"),
810
+ s(:scope,
811
+ s(:block,
812
+ s(:lasgn, :y,
813
+ s(:call, s(:lvar, :args), :first,
814
+ s(:arglist))),
815
+ s(:call, s(:lvar, :y), :+,
816
+ s(:arglist, s(:lit, 42)))))),
817
+ "Ruby2Ruby" => "def splatted(*args)\n y = args.first\n (y + 42)\nend")
818
+
819
+ add_tests("break",
820
+ "Ruby" => "loop { break if true }",
821
+ "RawParseTree" => [:iter,
822
+ [:fcall, :loop], nil,
823
+ [:if, [:true], [:break], nil]],
824
+ "ParseTree" => s(:iter,
825
+ s(:call, nil, :loop, s(:arglist)), nil,
826
+ s(:if, s(:true), s(:break), nil)))
827
+
828
+ add_tests("break_arg",
829
+ "Ruby" => "loop { break 42 if true }",
830
+ "RawParseTree" => [:iter,
831
+ [:fcall, :loop], nil,
832
+ [:if, [:true], [:break, [:lit, 42]], nil]],
833
+ "ParseTree" => s(:iter,
834
+ s(:call, nil, :loop, s(:arglist)), nil,
835
+ s(:if, s(:true), s(:break, s(:lit, 42)), nil)))
836
+
837
+ add_tests("call",
838
+ "Ruby" => "self.method",
839
+ "RawParseTree" => [:call, [:self], :method],
840
+ "ParseTree" => s(:call, s(:self), :method, s(:arglist)))
841
+
842
+ add_tests("call_arglist",
843
+ "Ruby" => "o.puts(42)",
844
+ "RawParseTree" => [:call, [:vcall, :o], :puts,
845
+ [:array, [:lit, 42]]],
846
+ "ParseTree" => s(:call, s(:call, nil, :o, s(:arglist)), :puts,
847
+ s(:arglist, s(:lit, 42))))
848
+
849
+ add_tests("call_arglist_hash",
850
+ "Ruby" => "o.m(:a => 1, :b => 2)",
851
+ "RawParseTree" => [:call,
852
+ [:vcall, :o], :m,
853
+ [:array,
854
+ [:hash,
855
+ [:lit, :a], [:lit, 1],
856
+ [:lit, :b], [:lit, 2]]]],
857
+ "ParseTree" => s(:call,
858
+ s(:call, nil, :o, s(:arglist)), :m,
859
+ s(:arglist,
860
+ s(:hash,
861
+ s(:lit, :a), s(:lit, 1),
862
+ s(:lit, :b), s(:lit, 2)))))
863
+
864
+ add_tests("call_arglist_norm_hash",
865
+ "Ruby" => "o.m(42, :a => 1, :b => 2)",
866
+ "RawParseTree" => [:call,
867
+ [:vcall, :o], :m,
868
+ [:array,
869
+ [:lit, 42],
870
+ [:hash,
871
+ [:lit, :a], [:lit, 1],
872
+ [:lit, :b], [:lit, 2]]]],
873
+ "ParseTree" => s(:call,
874
+ s(:call, nil, :o, s(:arglist)), :m,
875
+ s(:arglist,
876
+ s(:lit, 42),
877
+ s(:hash,
878
+ s(:lit, :a), s(:lit, 1),
879
+ s(:lit, :b), s(:lit, 2)))))
880
+
881
+ add_tests("call_arglist_norm_hash_splat",
882
+ "Ruby" => "o.m(42, :a => 1, :b => 2, *c)",
883
+ "RawParseTree" => [:call,
884
+ [:vcall, :o], :m,
885
+ [:argscat,
886
+ [:array,
887
+ [:lit, 42],
888
+ [:hash,
889
+ [:lit, :a], [:lit, 1],
890
+ [:lit, :b], [:lit, 2]]],
891
+ [:vcall, :c]]],
892
+ "ParseTree" => s(:call,
893
+ s(:call, nil, :o, s(:arglist)), :m,
894
+ s(:arglist,
895
+ s(:lit, 42),
896
+ s(:hash,
897
+ s(:lit, :a), s(:lit, 1),
898
+ s(:lit, :b), s(:lit, 2)),
899
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
900
+
901
+ add_tests("call_arglist_space",
902
+ "Ruby" => "a (1,2,3)",
903
+ "RawParseTree" => [:fcall, :a,
904
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
905
+ "ParseTree" => s(:call, nil, :a,
906
+ s(:arglist,
907
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
908
+ "Ruby2Ruby" => "a(1, 2, 3)")
909
+
910
+ add_tests("call_command",
911
+ "Ruby" => "1.b(c)",
912
+ "RawParseTree" => [:call, [:lit, 1], :b, [:array, [:vcall, :c]]],
913
+ "ParseTree" => s(:call,
914
+ s(:lit, 1),
915
+ :b,
916
+ s(:arglist, s(:call, nil, :c, s(:arglist)))))
917
+
918
+ add_tests("call_expr",
919
+ "Ruby" => "(v = (1 + 1)).zero?",
920
+ "RawParseTree" => [:call,
921
+ [:lasgn, :v,
922
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
923
+ :zero?],
924
+ "ParseTree" => s(:call,
925
+ s(:lasgn, :v,
926
+ s(:call, s(:lit, 1), :+,
927
+ s(:arglist, s(:lit, 1)))),
928
+ :zero?, s(:arglist)))
929
+
930
+ add_tests("call_index",
931
+ "Ruby" => "a = []\na[42]\n",
932
+ "RawParseTree" => [:block,
933
+ [:lasgn, :a, [:zarray]],
934
+ [:call, [:lvar, :a], :[], [:array, [:lit, 42]]]],
935
+ "ParseTree" => s(:block,
936
+ s(:lasgn, :a, s(:array)),
937
+ s(:call, s(:lvar, :a), :[],
938
+ s(:arglist, s(:lit, 42)))))
939
+
940
+ add_tests("call_index_no_args",
941
+ "Ruby" => "a[]",
942
+ "RawParseTree" => [:call, [:vcall, :a], :[]],
943
+ "ParseTree" => s(:call, s(:call, nil, :a, s(:arglist)),
944
+ :[], s(:arglist)))
945
+
946
+ add_tests("call_index_space",
947
+ "Ruby" => "a = []\na [42]\n",
948
+ "RawParseTree" => [:block,
949
+ [:lasgn, :a, [:zarray]],
950
+ [:call, [:lvar, :a], :[], [:array, [:lit, 42]]]],
951
+ "ParseTree" => s(:block,
952
+ s(:lasgn, :a, s(:array)),
953
+ s(:call, s(:lvar, :a), :[],
954
+ s(:arglist, s(:lit, 42)))),
955
+ "Ruby2Ruby" => "a = []\na[42]\n")
956
+
957
+ add_tests("call_unary_neg",
958
+ "Ruby" => "-2**31",
959
+ "RawParseTree" => [:call,
960
+ [:call, [:lit, 2], :**, [:array, [:lit, 31]]],
961
+ :-@],
962
+ "ParseTree" => s(:call,
963
+ s(:call,
964
+ s(:lit, 2),
965
+ :**,
966
+ s(:arglist, s(:lit, 31))),
967
+ :-@, s(:arglist)),
968
+ "Ruby2Ruby" => "-(2 ** 31)")
969
+
970
+ add_tests("case",
971
+ "Ruby" => "var = 2\nresult = \"\"\ncase var\nwhen 1 then\n puts(\"something\")\n result = \"red\"\nwhen 2, 3 then\n result = \"yellow\"\nwhen 4 then\n # do nothing\nelse\n result = \"green\"\nend\ncase result\nwhen \"red\" then\n var = 1\nwhen \"yellow\" then\n var = 2\nwhen \"green\" then\n var = 3\nelse\n # do nothing\nend\n",
972
+ "RawParseTree" => [:block,
973
+ [:lasgn, :var, [:lit, 2]],
974
+ [:lasgn, :result, [:str, ""]],
975
+ [:case,
976
+ [:lvar, :var],
977
+ [:when,
978
+ [:array, [:lit, 1]],
979
+ [:block,
980
+ [:fcall, :puts,
981
+ [:array, [:str, "something"]]],
982
+ [:lasgn, :result, [:str, "red"]]]],
983
+ [:when,
984
+ [:array, [:lit, 2], [:lit, 3]],
985
+ [:lasgn, :result, [:str, "yellow"]]],
986
+ [:when, [:array, [:lit, 4]], nil],
987
+ [:lasgn, :result, [:str, "green"]]],
988
+ [:case,
989
+ [:lvar, :result],
990
+ [:when, [:array, [:str, "red"]],
991
+ [:lasgn, :var, [:lit, 1]]],
992
+ [:when, [:array, [:str, "yellow"]],
993
+ [:lasgn, :var, [:lit, 2]]],
994
+ [:when, [:array, [:str, "green"]],
995
+ [:lasgn, :var, [:lit, 3]]],
996
+ nil]],
997
+ "ParseTree" => s(:block,
998
+ s(:lasgn, :var, s(:lit, 2)),
999
+ s(:lasgn, :result, s(:str, "")),
1000
+ s(:case,
1001
+ s(:lvar, :var),
1002
+ s(:when,
1003
+ s(:array, s(:lit, 1)),
1004
+ s(:block,
1005
+ s(:call, nil, :puts,
1006
+ s(:arglist, s(:str, "something"))),
1007
+ s(:lasgn, :result, s(:str, "red")))),
1008
+ s(:when,
1009
+ s(:array, s(:lit, 2), s(:lit, 3)),
1010
+ s(:lasgn, :result, s(:str, "yellow"))),
1011
+ s(:when, s(:array, s(:lit, 4)), nil),
1012
+ s(:lasgn, :result, s(:str, "green"))),
1013
+ s(:case,
1014
+ s(:lvar, :result),
1015
+ s(:when, s(:array, s(:str, "red")),
1016
+ s(:lasgn, :var, s(:lit, 1))),
1017
+ s(:when, s(:array, s(:str, "yellow")),
1018
+ s(:lasgn, :var, s(:lit, 2))),
1019
+ s(:when, s(:array, s(:str, "green")),
1020
+ s(:lasgn, :var, s(:lit, 3))),
1021
+ nil)))
1022
+
1023
+ add_tests("case_nested",
1024
+ "Ruby" => "var1 = 1\nvar2 = 2\nresult = nil\ncase var1\nwhen 1 then\n case var2\n when 1 then\n result = 1\n when 2 then\n result = 2\n else\n result = 3\n end\nwhen 2 then\n case var2\n when 1 then\n result = 4\n when 2 then\n result = 5\n else\n result = 6\n end\nelse\n result = 7\nend\n",
1025
+ "RawParseTree" => [:block,
1026
+ [:lasgn, :var1, [:lit, 1]],
1027
+ [:lasgn, :var2, [:lit, 2]],
1028
+ [:lasgn, :result, [:nil]],
1029
+ [:case,
1030
+ [:lvar, :var1],
1031
+ [:when, [:array, [:lit, 1]],
1032
+ [:case,
1033
+ [:lvar, :var2],
1034
+ [:when, [:array, [:lit, 1]],
1035
+ [:lasgn, :result, [:lit, 1]]],
1036
+ [:when, [:array, [:lit, 2]],
1037
+ [:lasgn, :result, [:lit, 2]]],
1038
+ [:lasgn, :result, [:lit, 3]]]],
1039
+ [:when, [:array, [:lit, 2]],
1040
+ [:case,
1041
+ [:lvar, :var2],
1042
+ [:when, [:array, [:lit, 1]],
1043
+ [:lasgn, :result, [:lit, 4]]],
1044
+ [:when, [:array, [:lit, 2]],
1045
+ [:lasgn, :result, [:lit, 5]]],
1046
+ [:lasgn, :result, [:lit, 6]]]],
1047
+ [:lasgn, :result, [:lit, 7]]]],
1048
+ "ParseTree" => s(:block,
1049
+ s(:lasgn, :var1, s(:lit, 1)),
1050
+ s(:lasgn, :var2, s(:lit, 2)),
1051
+ s(:lasgn, :result, s(:nil)),
1052
+ s(:case,
1053
+ s(:lvar, :var1),
1054
+ s(:when, s(:array, s(:lit, 1)),
1055
+ s(:case,
1056
+ s(:lvar, :var2),
1057
+ s(:when, s(:array, s(:lit, 1)),
1058
+ s(:lasgn, :result, s(:lit, 1))),
1059
+ s(:when, s(:array, s(:lit, 2)),
1060
+ s(:lasgn, :result, s(:lit, 2))),
1061
+ s(:lasgn, :result, s(:lit, 3)))),
1062
+ s(:when, s(:array, s(:lit, 2)),
1063
+ s(:case,
1064
+ s(:lvar, :var2),
1065
+ s(:when, s(:array, s(:lit, 1)),
1066
+ s(:lasgn, :result, s(:lit, 4))),
1067
+ s(:when, s(:array, s(:lit, 2)),
1068
+ s(:lasgn, :result, s(:lit, 5))),
1069
+ s(:lasgn, :result, s(:lit, 6)))),
1070
+ s(:lasgn, :result, s(:lit, 7)))))
1071
+
1072
+ add_tests("case_nested_inner_no_expr",
1073
+ "Ruby" => "case a\nwhen b then\n case\n when (d and e) then\n f\n else\n # do nothing\n end\nelse\n # do nothing\nend",
1074
+ "RawParseTree" => [:case, [:vcall, :a],
1075
+ [:when, [:array, [:vcall, :b]],
1076
+ [:case, nil,
1077
+ [:when,
1078
+ [:array, [:and, [:vcall, :d], [:vcall, :e]]],
1079
+ [:vcall, :f]],
1080
+ nil]],
1081
+ nil],
1082
+ "ParseTree" => s(:case, s(:call, nil, :a, s(:arglist)),
1083
+ s(:when,
1084
+ s(:array, s(:call, nil, :b, s(:arglist))),
1085
+ s(:case, nil,
1086
+ s(:when,
1087
+ s(:array,
1088
+ s(:and,
1089
+ s(:call, nil, :d, s(:arglist)),
1090
+ s(:call, nil, :e, s(:arglist)))),
1091
+ s(:call, nil, :f, s(:arglist))),
1092
+ nil)),
1093
+ nil))
1094
+
1095
+ add_tests("case_no_expr",
1096
+ "Ruby" => "case\nwhen (a == 1) then\n :a\nwhen (a == 2) then\n :b\nelse\n :c\nend",
1097
+ "RawParseTree" => [:case, nil,
1098
+ [:when,
1099
+ [:array,
1100
+ [:call, [:vcall, :a], :==,
1101
+ [:array, [:lit, 1]]]],
1102
+ [:lit, :a]],
1103
+ [:when,
1104
+ [:array,
1105
+ [:call, [:vcall, :a], :==,
1106
+ [:array, [:lit, 2]]]],
1107
+ [:lit, :b]],
1108
+ [:lit, :c]],
1109
+ "ParseTree" => s(:case, nil,
1110
+ s(:when,
1111
+ s(:array,
1112
+ s(:call,
1113
+ s(:call, nil, :a, s(:arglist)),
1114
+ :==,
1115
+ s(:arglist, s(:lit, 1)))),
1116
+ s(:lit, :a)),
1117
+ s(:when,
1118
+ s(:array,
1119
+ s(:call,
1120
+ s(:call, nil, :a, s(:arglist)),
1121
+ :==,
1122
+ s(:arglist, s(:lit, 2)))),
1123
+ s(:lit, :b)),
1124
+ s(:lit, :c)))
1125
+
1126
+ add_tests("case_splat",
1127
+ "Ruby" => "case a\nwhen :b, *c then\n d\nelse\n e\nend",
1128
+ "RawParseTree" => [:case, [:vcall, :a],
1129
+ [:when,
1130
+ [:array,
1131
+ [:lit, :b], [:when, [:vcall, :c], nil]], # wtf?
1132
+ [:vcall, :d]],
1133
+ [:vcall, :e]],
1134
+ "ParseTree" => s(:case, s(:call, nil, :a, s(:arglist)),
1135
+ s(:when,
1136
+ s(:array,
1137
+ s(:lit, :b),
1138
+ s(:when,
1139
+ s(:call, nil, :c, s(:arglist)),
1140
+ nil)), # wtf?
1141
+ s(:call, nil, :d, s(:arglist))),
1142
+ s(:call, nil, :e, s(:arglist))))
1143
+
1144
+ add_tests("cdecl",
1145
+ "Ruby" => "X = 42",
1146
+ "RawParseTree" => [:cdecl, :X, [:lit, 42]],
1147
+ "ParseTree" => s(:cdecl, :X, s(:lit, 42)))
1148
+
1149
+ add_tests("class_plain",
1150
+ "Ruby" => "class X\n puts((1 + 1))\n def blah\n puts(\"hello\")\n end\nend",
1151
+ "RawParseTree" => [:class,
1152
+ :X,
1153
+ nil,
1154
+ [:scope,
1155
+ [:block,
1156
+ [:fcall, :puts,
1157
+ [:array,
1158
+ [:call, [:lit, 1], :+,
1159
+ [:array, [:lit, 1]]]]],
1160
+ [:defn, :blah,
1161
+ [:scope,
1162
+ [:block,
1163
+ [:args],
1164
+ [:fcall, :puts,
1165
+ [:array, [:str, "hello"]]]]]]]]],
1166
+ "ParseTree" => s(:class,
1167
+ :X,
1168
+ nil,
1169
+ s(:scope,
1170
+ s(:block,
1171
+ s(:call, nil, :puts,
1172
+ s(:arglist,
1173
+ s(:call, s(:lit, 1), :+,
1174
+ s(:arglist, s(:lit, 1))))),
1175
+ s(:defn, :blah,
1176
+ s(:args),
1177
+ s(:scope,
1178
+ s(:block,
1179
+ s(:call, nil, :puts,
1180
+ s(:arglist,
1181
+ s(:str, "hello"))))))))))
1182
+
1183
+ add_tests("class_scoped",
1184
+ "Ruby" => "class X::Y\n c\nend",
1185
+ "RawParseTree" => [:class, [:colon2, [:const, :X], :Y], nil,
1186
+ [:scope, [:vcall, :c]]],
1187
+ "ParseTree" => s(:class, s(:colon2, s(:const, :X), :Y), nil,
1188
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
1189
+
1190
+ add_tests("class_scoped3",
1191
+ "Ruby" => "class ::Y\n c\nend",
1192
+ "RawParseTree" => [:class, [:colon3, :Y], nil,
1193
+ [:scope, [:vcall, :c]]],
1194
+ "ParseTree" => s(:class, s(:colon3, :Y), nil,
1195
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
1196
+
1197
+ add_tests("class_super_array",
1198
+ "Ruby" => "class X < Array\nend",
1199
+ "RawParseTree" => [:class,
1200
+ :X,
1201
+ [:const, :Array],
1202
+ [:scope]],
1203
+ "ParseTree" => s(:class,
1204
+ :X,
1205
+ s(:const, :Array),
1206
+ s(:scope)))
1207
+
1208
+ add_tests("class_super_expr",
1209
+ "Ruby" => "class X < expr\nend",
1210
+ "RawParseTree" => [:class,
1211
+ :X,
1212
+ [:vcall, :expr],
1213
+ [:scope]],
1214
+ "ParseTree" => s(:class,
1215
+ :X,
1216
+ s(:call, nil, :expr, s(:arglist)),
1217
+ s(:scope)))
1218
+
1219
+ add_tests("class_super_object",
1220
+ "Ruby" => "class X < Object\nend",
1221
+ "RawParseTree" => [:class,
1222
+ :X,
1223
+ [:const, :Object],
1224
+ [:scope]],
1225
+ "ParseTree" => s(:class,
1226
+ :X,
1227
+ s(:const, :Object),
1228
+ s(:scope)))
1229
+
1230
+ add_tests("colon2",
1231
+ "Ruby" => "X::Y",
1232
+ "RawParseTree" => [:colon2, [:const, :X], :Y],
1233
+ "ParseTree" => s(:colon2, s(:const, :X), :Y))
1234
+
1235
+ add_tests("colon3",
1236
+ "Ruby" => "::X",
1237
+ "RawParseTree" => [:colon3, :X],
1238
+ "ParseTree" => s(:colon3, :X))
1239
+
1240
+ add_tests("const",
1241
+ "Ruby" => "X",
1242
+ "RawParseTree" => [:const, :X],
1243
+ "ParseTree" => s(:const, :X))
1244
+
1245
+ add_tests("constX",
1246
+ "Ruby" => "X = 1",
1247
+ "RawParseTree" => [:cdecl, :X, [:lit, 1]],
1248
+ "ParseTree" => s(:cdecl, :X, s(:lit, 1)))
1249
+
1250
+ add_tests("constY",
1251
+ "Ruby" => "::X = 1",
1252
+ "RawParseTree" => [:cdecl, [:colon3, :X], [:lit, 1]],
1253
+ "ParseTree" => s(:cdecl, s(:colon3, :X), s(:lit, 1)))
1254
+
1255
+ add_tests("constZ",
1256
+ "Ruby" => "X::Y = 1",
1257
+ "RawParseTree" => [:cdecl, [:colon2, [:const, :X], :Y], [:lit, 1]],
1258
+ "ParseTree" => s(:cdecl,
1259
+ s(:colon2, s(:const, :X), :Y),
1260
+ s(:lit, 1)))
1261
+
1262
+ add_tests("cvar",
1263
+ "Ruby" => "@@x",
1264
+ "RawParseTree" => [:cvar, :@@x],
1265
+ "ParseTree" => s(:cvar, :@@x))
1266
+
1267
+ add_tests("cvasgn",
1268
+ "Ruby" => "def x\n @@blah = 1\nend",
1269
+ "RawParseTree" => [:defn, :x,
1270
+ [:scope,
1271
+ [:block, [:args],
1272
+ [:cvasgn, :@@blah, [:lit, 1]]]]],
1273
+ "ParseTree" => s(:defn, :x,
1274
+ s(:args),
1275
+ s(:scope,
1276
+ s(:block,
1277
+ s(:cvasgn, :@@blah, s(:lit, 1))))))
1278
+
1279
+ add_tests("cvasgn_cls_method",
1280
+ "Ruby" => "def self.quiet_mode=(boolean)\n @@quiet_mode = boolean\nend",
1281
+ "RawParseTree" => [:defs, [:self], :quiet_mode=,
1282
+ [:scope,
1283
+ [:block,
1284
+ [:args, :boolean],
1285
+ [:cvasgn, :@@quiet_mode, [:lvar, :boolean]]]]],
1286
+ "ParseTree" => s(:defs, s(:self), :quiet_mode=,
1287
+ s(:args, :boolean),
1288
+ s(:scope,
1289
+ s(:block,
1290
+ s(:cvasgn, :@@quiet_mode,
1291
+ s(:lvar, :boolean))))))
1292
+
1293
+ add_tests("cvdecl",
1294
+ "Ruby" => "class X\n @@blah = 1\nend",
1295
+ "RawParseTree" => [:class, :X, nil,
1296
+ [:scope, [:cvdecl, :@@blah, [:lit, 1]]]],
1297
+ "ParseTree" => s(:class, :X, nil,
1298
+ s(:scope, s(:cvdecl, :@@blah, s(:lit, 1)))))
1299
+
1300
+ add_tests("dasgn_0",
1301
+ "Ruby" => "a.each { |x| b.each { |y| x = (x + 1) } if true }",
1302
+ "RawParseTree" => [:iter,
1303
+ [:call, [:vcall, :a], :each],
1304
+ [:dasgn_curr, :x],
1305
+ [:if, [:true],
1306
+ [:iter,
1307
+ [:call, [:vcall, :b], :each],
1308
+ [:dasgn_curr, :y],
1309
+ [:dasgn, :x,
1310
+ [:call, [:dvar, :x], :+,
1311
+ [:array, [:lit, 1]]]]],
1312
+ nil]],
1313
+ "ParseTree" => s(:iter,
1314
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1315
+ s(:arglist)),
1316
+ s(:lasgn, :x),
1317
+ s(:if, s(:true),
1318
+ s(:iter,
1319
+ s(:call, s(:call, nil, :b, s(:arglist)),
1320
+ :each,
1321
+ s(:arglist)),
1322
+ s(:lasgn, :y),
1323
+ s(:lasgn, :x,
1324
+ s(:call, s(:lvar, :x), :+,
1325
+ s(:arglist, s(:lit, 1))))),
1326
+ nil)))
1327
+
1328
+ add_tests("dasgn_1",
1329
+ "Ruby" => "a.each { |x| b.each { |y| c = (c + 1) } if true }",
1330
+ "RawParseTree" => [:iter,
1331
+ [:call, [:vcall, :a], :each],
1332
+ [:dasgn_curr, :x],
1333
+ [:if, [:true],
1334
+ [:iter,
1335
+ [:call, [:vcall, :b], :each],
1336
+ [:dasgn_curr, :y],
1337
+ [:dasgn_curr, :c,
1338
+ [:call, [:dvar, :c], :+,
1339
+ [:array, [:lit, 1]]]]],
1340
+ nil]],
1341
+ "ParseTree" => s(:iter,
1342
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1343
+ s(:arglist)),
1344
+ s(:lasgn, :x),
1345
+ s(:if, s(:true),
1346
+ s(:iter,
1347
+ s(:call, s(:call, nil, :b, s(:arglist)),
1348
+ :each,
1349
+ s(:arglist)),
1350
+ s(:lasgn, :y),
1351
+ s(:lasgn, :c,
1352
+ s(:call, s(:lvar, :c), :+,
1353
+ s(:arglist, s(:lit, 1))))),
1354
+ nil)))
1355
+
1356
+ add_tests("dasgn_2",
1357
+ "Ruby" => "a.each do |x|\n if true then\n c = 0\n b.each { |y| c = (c + 1) }\n end\nend", # FIX: hate that extra newline!
1358
+ "RawParseTree" => [:iter,
1359
+ [:call, [:vcall, :a], :each],
1360
+ [:dasgn_curr, :x],
1361
+ [:if, [:true],
1362
+ [:block,
1363
+ [:dasgn_curr, :c, [:lit, 0]],
1364
+ [:iter,
1365
+ [:call, [:vcall, :b], :each],
1366
+ [:dasgn_curr, :y],
1367
+ [:dasgn, :c,
1368
+ [:call, [:dvar, :c], :+,
1369
+ [:array, [:lit, 1]]]]]],
1370
+ nil]],
1371
+ "ParseTree" => s(:iter,
1372
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1373
+ s(:arglist)),
1374
+ s(:lasgn, :x),
1375
+ s(:if, s(:true),
1376
+ s(:block,
1377
+ s(:lasgn, :c, s(:lit, 0)),
1378
+ s(:iter,
1379
+ s(:call, s(:call, nil, :b, s(:arglist)),
1380
+ :each,
1381
+ s(:arglist)),
1382
+ s(:lasgn, :y),
1383
+ s(:lasgn, :c,
1384
+ s(:call, s(:lvar, :c), :+,
1385
+ s(:arglist, s(:lit, 1)))))),
1386
+ nil)))
1387
+
1388
+ add_tests("dasgn_curr",
1389
+ "Ruby" => "data.each do |x, y|\n a = 1\n b = a\n b = a = x\nend",
1390
+ "RawParseTree" => [:iter,
1391
+ [:call, [:vcall, :data], :each],
1392
+ [:masgn,
1393
+ [:array, [:dasgn_curr, :x], [:dasgn_curr, :y]],
1394
+ nil, nil],
1395
+ [:block,
1396
+ [:dasgn_curr, :a, [:lit, 1]],
1397
+ [:dasgn_curr, :b, [:dvar, :a]],
1398
+ [:dasgn_curr, :b,
1399
+ [:dasgn_curr, :a, [:dvar, :x]]]]],
1400
+ "ParseTree" => s(:iter,
1401
+ s(:call, s(:call, nil, :data,
1402
+ s(:arglist)), :each, s(:arglist)),
1403
+ s(:masgn,
1404
+ s(:array, s(:lasgn, :x), s(:lasgn, :y))),
1405
+ s(:block,
1406
+ s(:lasgn, :a, s(:lit, 1)),
1407
+ s(:lasgn, :b, s(:lvar, :a)),
1408
+ s(:lasgn, :b, s(:lasgn, :a, s(:lvar, :x))))))
1409
+
1410
+ add_tests("dasgn_icky",
1411
+ "Ruby" => "a do\n v = nil\n assert_block(full_message) do\n begin\n yield\n rescue Exception => v\n break\n end\n end\nend",
1412
+ "RawParseTree" => [:iter,
1413
+ [:fcall, :a],
1414
+ nil,
1415
+ [:block,
1416
+ [:dasgn_curr, :v, [:nil]],
1417
+ [:iter,
1418
+ [:fcall, :assert_block,
1419
+ [:array, [:vcall, :full_message]]],
1420
+ nil,
1421
+ [:rescue,
1422
+ [:yield],
1423
+ [:resbody,
1424
+ [:array, [:const, :Exception]],
1425
+ [:block,
1426
+ [:dasgn, :v,
1427
+ [:gvar, :$!]], [:break]]]]]]],
1428
+ "ParseTree" => s(:iter,
1429
+ s(:call, nil, :a, s(:arglist)),
1430
+ nil,
1431
+ s(:block,
1432
+ s(:lasgn, :v, s(:nil)),
1433
+ s(:iter,
1434
+ s(:call, nil, :assert_block,
1435
+ s(:arglist,
1436
+ s(:call, nil, :full_message,
1437
+ s(:arglist)))),
1438
+ nil,
1439
+ s(:rescue,
1440
+ s(:yield),
1441
+ s(:resbody,
1442
+ s(:array,
1443
+ s(:const, :Exception),
1444
+ s(:lasgn, :v, s(:gvar, :$!))),
1445
+ s(:break)))))))
1446
+
1447
+ add_tests("dasgn_mixed",
1448
+ "Ruby" => "t = 0\nns.each { |n| t += n }\n",
1449
+ "RawParseTree" => [:block,
1450
+ [:lasgn, :t, [:lit, 0]],
1451
+ [:iter,
1452
+ [:call, [:vcall, :ns], :each],
1453
+ [:dasgn_curr, :n],
1454
+ [:lasgn, :t,
1455
+ [:call, [:lvar, :t], :+,
1456
+ [:array, [:dvar, :n]]]]]],
1457
+ "ParseTree" => s(:block,
1458
+ s(:lasgn, :t, s(:lit, 0)),
1459
+ s(:iter,
1460
+ s(:call, s(:call, nil, :ns,
1461
+ s(:arglist)), :each, s(:arglist)),
1462
+ s(:lasgn, :n),
1463
+ s(:lasgn, :t,
1464
+ s(:call, s(:lvar, :t), :+,
1465
+ s(:arglist, s(:lvar, :n)))))),
1466
+ "Ruby2Ruby" => "t = 0\nns.each { |n| t = (t + n) }\n")
1467
+
1468
+ add_tests("defined",
1469
+ "Ruby" => "defined? $x",
1470
+ "RawParseTree" => [:defined, [:gvar, :$x]],
1471
+ "ParseTree" => s(:defined, s(:gvar, :$x)))
1472
+
1473
+ # TODO: make all the defn_args* p their arglist
1474
+ add_tests("defn_args_block",
1475
+ "Ruby" => "def f(&block)\n # do nothing\nend",
1476
+ "RawParseTree" => [:defn, :f,
1477
+ [:scope,
1478
+ [:block,
1479
+ [:args],
1480
+ [:block_arg, :block],
1481
+ [:nil]]]],
1482
+ "ParseTree" => s(:defn, :f,
1483
+ s(:args, :"&block"),
1484
+ s(:scope, s(:block, s(:nil)))))
1485
+
1486
+ add_tests("defn_args_mand",
1487
+ "Ruby" => "def f(mand)\n # do nothing\nend",
1488
+ "RawParseTree" => [:defn, :f,
1489
+ [:scope,
1490
+ [:block,
1491
+ [:args, :mand],
1492
+ [:nil]]]],
1493
+ "ParseTree" => s(:defn, :f,
1494
+ s(:args, :mand),
1495
+ s(:scope, s(:block, s(:nil)))))
1496
+
1497
+ add_tests("defn_args_mand_block",
1498
+ "Ruby" => "def f(mand, &block)\n # do nothing\nend",
1499
+ "RawParseTree" => [:defn, :f,
1500
+ [:scope,
1501
+ [:block,
1502
+ [:args, :mand],
1503
+ [:block_arg, :block],
1504
+ [:nil]]]],
1505
+ "ParseTree" => s(:defn, :f,
1506
+ s(:args, :mand, :"&block"),
1507
+ s(:scope, s(:block, s(:nil)))))
1508
+
1509
+ add_tests("defn_args_mand_opt",
1510
+ "Ruby" => "def f(mand, opt = 42)\n # do nothing\nend",
1511
+ "RawParseTree" => [:defn, :f,
1512
+ [:scope,
1513
+ [:block,
1514
+ [:args, :mand, :opt,
1515
+ [:block,
1516
+ [:lasgn, :opt, [:lit, 42]]]],
1517
+ [:nil]]]],
1518
+ "ParseTree" => s(:defn, :f,
1519
+ s(:args, :mand, :opt,
1520
+ s(:block,
1521
+ s(:lasgn, :opt, s(:lit, 42)))),
1522
+ s(:scope, s(:block, s(:nil)))))
1523
+
1524
+ add_tests("defn_args_mand_opt_block",
1525
+ "Ruby" => "def f(mand, opt = 42, &block)\n # do nothing\nend",
1526
+ "RawParseTree" => [:defn, :f,
1527
+ [:scope,
1528
+ [:block,
1529
+ [:args, :mand, :opt,
1530
+ [:block,
1531
+ [:lasgn, :opt, [:lit, 42]]]],
1532
+ [:block_arg, :block],
1533
+ [:nil]]]],
1534
+ "ParseTree" => s(:defn, :f,
1535
+ s(:args, :mand, :opt, :"&block",
1536
+ s(:block,
1537
+ s(:lasgn, :opt, s(:lit, 42)))),
1538
+ s(:scope, s(:block, s(:nil)))))
1539
+
1540
+ add_tests("defn_args_mand_opt_splat",
1541
+ "Ruby" => "def f(mand, opt = 42, *rest)\n # do nothing\nend",
1542
+ "RawParseTree" => [:defn, :f,
1543
+ [:scope,
1544
+ [:block,
1545
+ [:args, :mand, :opt, :"*rest",
1546
+ [:block,
1547
+ [:lasgn, :opt, [:lit, 42]]]],
1548
+ [:nil]]]],
1549
+ "ParseTree" => s(:defn, :f,
1550
+ s(:args, :mand, :opt, :"*rest",
1551
+ s(:block,
1552
+ s(:lasgn, :opt, s(:lit, 42)))),
1553
+ s(:scope, s(:block, s(:nil)))))
1554
+
1555
+ add_tests("defn_args_mand_opt_splat_block",
1556
+ "Ruby" => "def f(mand, opt = 42, *rest, &block)\n # do nothing\nend",
1557
+ "RawParseTree" => [:defn, :f,
1558
+ [:scope,
1559
+ [:block,
1560
+ [:args, :mand, :opt, :"*rest",
1561
+ [:block,
1562
+ [:lasgn, :opt, [:lit, 42]]]],
1563
+ [:block_arg, :block],
1564
+ [:nil]]]],
1565
+ "ParseTree" => s(:defn, :f,
1566
+ s(:args, :mand, :opt, :"*rest", :"&block",
1567
+ s(:block,
1568
+ s(:lasgn, :opt, s(:lit, 42)))),
1569
+ s(:scope, s(:block, s(:nil)))))
1570
+
1571
+ add_tests("defn_args_mand_opt_splat_no_name",
1572
+ "Ruby" => "def x(a, b = 42, *)\n # do nothing\nend",
1573
+ "RawParseTree" => [:defn, :x,
1574
+ [:scope,
1575
+ [:block,
1576
+ [:args, :a, :b, :"*",
1577
+ [:block, [:lasgn, :b, [:lit, 42]]]],
1578
+ [:nil]]]],
1579
+ "ParseTree" => s(:defn, :x,
1580
+ s(:args, :a, :b, :"*",
1581
+ s(:block, s(:lasgn, :b, s(:lit, 42)))),
1582
+ s(:scope,
1583
+ s(:block,
1584
+ s(:nil)))))
1585
+
1586
+ add_tests("defn_args_mand_splat",
1587
+ "Ruby" => "def f(mand, *rest)\n # do nothing\nend",
1588
+ "RawParseTree" => [:defn, :f,
1589
+ [:scope,
1590
+ [:block,
1591
+ [:args, :mand, :"*rest"],
1592
+ [:nil]]]],
1593
+ "ParseTree" => s(:defn, :f,
1594
+ s(:args, :mand, :"*rest"),
1595
+ s(:scope, s(:block, s(:nil)))))
1596
+
1597
+ add_tests("defn_args_mand_splat_block",
1598
+ "Ruby" => "def f(mand, *rest, &block)\n # do nothing\nend",
1599
+ "RawParseTree" => [:defn, :f,
1600
+ [:scope,
1601
+ [:block,
1602
+ [:args, :mand, :"*rest"],
1603
+ [:block_arg, :block],
1604
+ [:nil]]]],
1605
+ "ParseTree" => s(:defn, :f,
1606
+ s(:args, :mand, :"*rest", :"&block"),
1607
+ s(:scope, s(:block, s(:nil)))))
1608
+
1609
+ add_tests("defn_args_mand_splat_no_name",
1610
+ "Ruby" => "def x(a, *args)\n p(a, args)\nend",
1611
+ "RawParseTree" => [:defn, :x,
1612
+ [:scope,
1613
+ [:block,
1614
+ [:args, :a, :"*args"],
1615
+ [:fcall, :p,
1616
+ [:array, [:lvar, :a], [:lvar, :args]]]]]],
1617
+ "ParseTree" => s(:defn, :x,
1618
+ s(:args, :a, :"*args"),
1619
+ s(:scope,
1620
+ s(:block,
1621
+ s(:call, nil, :p,
1622
+ s(:arglist, s(:lvar, :a), s(:lvar, :args)))))))
1623
+
1624
+ add_tests("defn_args_none",
1625
+ "Ruby" => "def empty\n # do nothing\nend",
1626
+ "RawParseTree" => [:defn, :empty,
1627
+ [:scope, [:block, [:args], [:nil]]]],
1628
+ "ParseTree" => s(:defn, :empty,
1629
+ s(:args),
1630
+ s(:scope, s(:block, s(:nil)))))
1631
+
1632
+ add_tests("defn_args_opt",
1633
+ "Ruby" => "def f(opt = 42)\n # do nothing\nend",
1634
+ "RawParseTree" => [:defn, :f,
1635
+ [:scope,
1636
+ [:block,
1637
+ [:args, :opt,
1638
+ [:block,
1639
+ [:lasgn, :opt, [:lit, 42]]]],
1640
+ [:nil]]]],
1641
+ "ParseTree" => s(:defn, :f,
1642
+ s(:args, :opt,
1643
+ s(:block,
1644
+ s(:lasgn, :opt, s(:lit, 42)))),
1645
+ s(:scope, s(:block, s(:nil)))))
1646
+
1647
+ add_tests("defn_args_opt_block",
1648
+ "Ruby" => "def f(opt = 42, &block)\n # do nothing\nend",
1649
+ "RawParseTree" => [:defn, :f,
1650
+ [:scope,
1651
+ [:block,
1652
+ [:args, :opt,
1653
+ [:block,
1654
+ [:lasgn, :opt, [:lit, 42]]]],
1655
+ [:block_arg, :block],
1656
+ [:nil]]]],
1657
+ "ParseTree" => s(:defn, :f,
1658
+ s(:args, :opt, :"&block",
1659
+ s(:block,
1660
+ s(:lasgn, :opt, s(:lit, 42)))),
1661
+ s(:scope, s(:block, s(:nil)))))
1662
+
1663
+ add_tests("defn_args_opt_splat",
1664
+ "Ruby" => "def f(opt = 42, *rest)\n # do nothing\nend",
1665
+ "RawParseTree" => [:defn, :f,
1666
+ [:scope,
1667
+ [:block,
1668
+ [:args, :opt, :"*rest",
1669
+ [:block,
1670
+ [:lasgn, :opt, [:lit, 42]]]],
1671
+ [:nil]]]],
1672
+ "ParseTree" => s(:defn, :f,
1673
+ s(:args, :opt, :"*rest",
1674
+ s(:block,
1675
+ s(:lasgn, :opt, s(:lit, 42)))),
1676
+ s(:scope, s(:block, s(:nil)))))
1677
+
1678
+ add_tests("defn_args_opt_splat_block",
1679
+ "Ruby" => "def f(opt = 42, *rest, &block)\n # do nothing\nend",
1680
+ "RawParseTree" => [:defn, :f,
1681
+ [:scope,
1682
+ [:block,
1683
+ [:args, :opt, :"*rest",
1684
+ [:block,
1685
+ [:lasgn, :opt, [:lit, 42]]]],
1686
+ [:block_arg, :block],
1687
+ [:nil]]]],
1688
+ "ParseTree" => s(:defn, :f,
1689
+ s(:args, :opt, :"*rest", :"&block",
1690
+ s(:block,
1691
+ s(:lasgn, :opt, s(:lit, 42)))),
1692
+ s(:scope, s(:block, s(:nil)))))
1693
+
1694
+ add_tests("defn_args_opt_splat_no_name",
1695
+ "Ruby" => "def x(b = 42, *)\n # do nothing\nend",
1696
+ "RawParseTree" => [:defn, :x,
1697
+ [:scope,
1698
+ [:block,
1699
+ [:args, :b, :"*",
1700
+ [:block, [:lasgn, :b, [:lit, 42]]]],
1701
+ [:nil]]]],
1702
+ "ParseTree" => s(:defn, :x,
1703
+ s(:args, :b, :"*",
1704
+ s(:block, s(:lasgn, :b, s(:lit, 42)))),
1705
+ s(:scope,
1706
+ s(:block,
1707
+ s(:nil)))))
1708
+
1709
+ add_tests("defn_args_splat",
1710
+ "Ruby" => "def f(*rest)\n # do nothing\nend",
1711
+ "RawParseTree" => [:defn, :f,
1712
+ [:scope,
1713
+ [:block,
1714
+ [:args, :"*rest"],
1715
+ [:nil]]]],
1716
+ "ParseTree" => s(:defn, :f,
1717
+ s(:args, :"*rest"),
1718
+ s(:scope, s(:block, s(:nil)))))
1719
+
1720
+ add_tests("defn_args_splat_no_name",
1721
+ "Ruby" => "def x(*)\n # do nothing\nend",
1722
+ "RawParseTree" => [:defn, :x,
1723
+ [:scope,
1724
+ [:block,
1725
+ [:args, :"*"],
1726
+ [:nil]]]],
1727
+ "ParseTree" => s(:defn, :x,
1728
+ s(:args, :"*"),
1729
+ s(:scope,
1730
+ s(:block,
1731
+ s(:nil)))))
1732
+
1733
+ add_tests("defn_or",
1734
+ "Ruby" => "def |(o)\n # do nothing\nend",
1735
+ "RawParseTree" => [:defn, :|,
1736
+ [:scope, [:block, [:args, :o], [:nil]]]],
1737
+ "ParseTree" => s(:defn, :|,
1738
+ s(:args, :o),
1739
+ s(:scope, s(:block, s(:nil)))))
1740
+
1741
+ add_tests("defn_rescue",
1742
+ "Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid)\nrescue\n false\nend",
1743
+ "RawParseTree" => [:defn, :eql?,
1744
+ [:scope,
1745
+ [:block,
1746
+ [:args, :resource],
1747
+ [:rescue,
1748
+ [:call,
1749
+ [:call, [:self], :uuid],
1750
+ :==,
1751
+ [:array,
1752
+ [:call, [:lvar, :resource], :uuid]]],
1753
+ [:resbody, nil, [:false]]]]]],
1754
+ "ParseTree" => s(:defn, :eql?,
1755
+ s(:args, :resource),
1756
+ s(:scope,
1757
+ s(:block,
1758
+ s(:rescue,
1759
+ s(:call,
1760
+ s(:call, s(:self), :uuid, s(:arglist)),
1761
+ :==,
1762
+ s(:arglist,
1763
+ s(:call, s(:lvar, :resource),
1764
+ :uuid, s(:arglist)))),
1765
+ s(:resbody, s(:array), s(:false)))))),
1766
+ "Ruby2Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid) rescue false\nend")
1767
+
1768
+ add_tests("defn_rescue_mri_verbose_flag",
1769
+ "Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid)\nrescue\n false\nend",
1770
+ "RawParseTree" => [:defn, :eql?,
1771
+ [:scope,
1772
+ [:block,
1773
+ [:args, :resource],
1774
+ [:rescue,
1775
+ [:call,
1776
+ [:call, [:self], :uuid],
1777
+ :==,
1778
+ [:array,
1779
+ [:call, [:lvar, :resource], :uuid]]],
1780
+ [:resbody, nil, [:false]]]]]],
1781
+ "ParseTree" => s(:defn, :eql?,
1782
+ s(:args, :resource),
1783
+ s(:scope,
1784
+ s(:block,
1785
+ s(:rescue,
1786
+ s(:call,
1787
+ s(:call, s(:self), :uuid, s(:arglist)),
1788
+ :==,
1789
+ s(:arglist,
1790
+ s(:call, s(:lvar, :resource),
1791
+ :uuid, s(:arglist)))),
1792
+ s(:resbody, s(:array), s(:false)))))),
1793
+ "Ruby2Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid) rescue false\nend")
1794
+
1795
+ add_tests("defn_something_eh",
1796
+ "Ruby" => "def something?\n # do nothing\nend",
1797
+ "RawParseTree" => [:defn, :something?,
1798
+ [:scope, [:block, [:args], [:nil]]]],
1799
+ "ParseTree" => s(:defn, :something?,
1800
+ s(:args),
1801
+ s(:scope, s(:block, s(:nil)))))
1802
+
1803
+ add_tests("defn_splat_no_name",
1804
+ "Ruby" => "def x(a, *)\n p(a)\nend",
1805
+ "RawParseTree" => [:defn, :x,
1806
+ [:scope,
1807
+ [:block,
1808
+ [:args, :a, :"*"],
1809
+ [:fcall, :p,
1810
+ [:array, [:lvar, :a]]]]]],
1811
+ "ParseTree" => s(:defn, :x,
1812
+ s(:args, :a, :"*"),
1813
+ s(:scope,
1814
+ s(:block,
1815
+ s(:call, nil, :p,
1816
+ s(:arglist, s(:lvar, :a)))))))
1817
+
1818
+ add_tests("defn_zarray",
1819
+ "Ruby" => "def zarray\n a = []\n return a\nend",
1820
+ "RawParseTree" => [:defn, :zarray,
1821
+ [:scope,
1822
+ [:block, [:args],
1823
+ [:lasgn, :a, [:zarray]],
1824
+ [:return, [:lvar, :a]]]]],
1825
+ "ParseTree" => s(:defn, :zarray,
1826
+ s(:args),
1827
+ s(:scope,
1828
+ s(:block,
1829
+ s(:lasgn, :a, s(:array)),
1830
+ s(:return, s(:lvar, :a))))))
1831
+
1832
+ add_tests("defs",
1833
+ "Ruby" => "def self.x(y)\n (y + 1)\nend",
1834
+ "RawParseTree" => [:defs, [:self], :x,
1835
+ [:scope,
1836
+ [:block,
1837
+ [:args, :y],
1838
+ [:call, [:lvar, :y], :+,
1839
+ [:array, [:lit, 1]]]]]],
1840
+ "ParseTree" => s(:defs, s(:self), :x,
1841
+ s(:args, :y),
1842
+ s(:scope,
1843
+ s(:block,
1844
+ s(:call, s(:lvar, :y), :+,
1845
+ s(:arglist, s(:lit, 1)))))))
1846
+
1847
+ add_tests("defs_empty",
1848
+ "Ruby" => "def self.empty\n # do nothing\nend",
1849
+ "RawParseTree" => [:defs, [:self], :empty,
1850
+ [:scope, [:args]]],
1851
+ "ParseTree" => s(:defs, s(:self), :empty,
1852
+ s(:args),
1853
+ s(:scope, s(:block))))
1854
+
1855
+ add_tests("defs_empty_args",
1856
+ "Ruby" => "def self.empty(*)\n # do nothing\nend",
1857
+ "RawParseTree" => [:defs, [:self], :empty,
1858
+ [:scope, [:args, :*]]],
1859
+ "ParseTree" => s(:defs, s(:self), :empty,
1860
+ s(:args, :*),
1861
+ s(:scope, s(:block))))
1862
+
1863
+ add_tests("defs_expr_wtf",
1864
+ "Ruby" => "def (a.b).empty(*)\n # do nothing\nend",
1865
+ "RawParseTree" => [:defs,
1866
+ [:call, [:vcall, :a], :b],
1867
+ :empty,
1868
+ [:scope, [:args, :*]]],
1869
+ "ParseTree" => s(:defs,
1870
+ s(:call,
1871
+ s(:call, nil, :a, s(:arglist)),
1872
+ :b, s(:arglist)),
1873
+ :empty,
1874
+ s(:args, :*),
1875
+ s(:scope, s(:block))))
1876
+
1877
+ add_tests("dmethod",
1878
+ "Ruby" => [Examples, :dmethod_added],
1879
+ "RawParseTree" => [:defn, :dmethod_added,
1880
+ [:dmethod,
1881
+ :a_method,
1882
+ [:scope,
1883
+ [:block,
1884
+ [:args, :x],
1885
+ [:call, [:lvar, :x], :+,
1886
+ [:array, [:lit, 1]]]]]]],
1887
+ "ParseTree" => s(:defn, :dmethod_added,
1888
+ s(:args, :x),
1889
+ s(:scope,
1890
+ s(:block,
1891
+ s(:call, s(:lvar, :x), :+,
1892
+ s(:arglist, s(:lit, 1)))))),
1893
+ "Ruby2Ruby" => "def dmethod_added(x)\n (x + 1)\nend")
1894
+
1895
+ add_tests("dot2",
1896
+ "Ruby" => "(a..b)",
1897
+ "RawParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
1898
+ "ParseTree" => s(:dot2,
1899
+ s(:call, nil, :a, s(:arglist)),
1900
+ s(:call, nil, :b, s(:arglist))))
1901
+
1902
+ add_tests("dot3",
1903
+ "Ruby" => "(a...b)",
1904
+ "RawParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
1905
+ "ParseTree" => s(:dot3,
1906
+ s(:call, nil, :a, s(:arglist)),
1907
+ s(:call, nil, :b, s(:arglist))))
1908
+
1909
+ add_tests("dregx",
1910
+ "Ruby" => "/x#\{(1 + 1)}y/",
1911
+ "RawParseTree" => [:dregx, "x",
1912
+ [:evstr,
1913
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
1914
+ [:str, "y"]],
1915
+ "ParseTree" => s(:dregx, "x",
1916
+ s(:evstr,
1917
+ s(:call, s(:lit, 1), :+,
1918
+ s(:arglist, s(:lit, 1)))),
1919
+ s(:str, "y")))
1920
+
1921
+ add_tests("dregx_interp",
1922
+ "Ruby" => "/#\{@rakefile}/",
1923
+ "RawParseTree" => [:dregx, '', [:evstr, [:ivar, :@rakefile]]],
1924
+ "ParseTree" => s(:dregx, '', s(:evstr, s(:ivar, :@rakefile))))
1925
+
1926
+ add_tests("dregx_interp_empty",
1927
+ "Ruby" => "/a#\{}b/",
1928
+ "RawParseTree" => [:dregx, 'a', [:evstr], [:str, "b"]],
1929
+ "ParseTree" => s(:dregx, 'a', s(:evstr), s(:str, "b")))
1930
+
1931
+ add_tests("dregx_n",
1932
+ "Ruby" => '/#{1}/n',
1933
+ "RawParseTree" => [:dregx, '', [:evstr, [:lit, 1]], /x/n.options],
1934
+ "ParseTree" => s(:dregx, '',
1935
+ s(:evstr, s(:lit, 1)), /x/n.options),
1936
+ "Ruby2Ruby" => "/#\{1}/") # HACK - need to support regexp flag
1937
+
1938
+ add_tests("dregx_once",
1939
+ "Ruby" => "/x#\{(1 + 1)}y/o",
1940
+ "RawParseTree" => [:dregx_once, "x",
1941
+ [:evstr,
1942
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
1943
+ [:str, "y"]],
1944
+ "ParseTree" => s(:dregx_once, "x",
1945
+ s(:evstr,
1946
+ s(:call, s(:lit, 1), :+,
1947
+ s(:arglist, s(:lit, 1)))),
1948
+ s(:str, "y")))
1949
+
1950
+ add_tests("dregx_once_n_interp",
1951
+ "Ruby" => "/#\{IAC}#\{SB}/no",
1952
+ "RawParseTree" => [:dregx_once, '',
1953
+ [:evstr, [:const, :IAC]],
1954
+ [:evstr, [:const, :SB]], /x/n.options],
1955
+ "ParseTree" => s(:dregx_once, '',
1956
+ s(:evstr, s(:const, :IAC)),
1957
+ s(:evstr, s(:const, :SB)), /x/n.options),
1958
+ "Ruby2Ruby" => "/#\{IAC}#\{SB}/o") # HACK
1959
+
1960
+ add_tests("dstr",
1961
+ "Ruby" => "argl = 1\n\"x#\{argl}y\"\n",
1962
+ "RawParseTree" => [:block,
1963
+ [:lasgn, :argl, [:lit, 1]],
1964
+ [:dstr, "x", [:evstr, [:lvar, :argl]],
1965
+ [:str, "y"]]],
1966
+ "ParseTree" => s(:block,
1967
+ s(:lasgn, :argl, s(:lit, 1)),
1968
+ s(:dstr, "x", s(:evstr, s(:lvar, :argl)),
1969
+ s(:str, "y"))))
1970
+
1971
+ add_tests("dstr_2",
1972
+ "Ruby" => "argl = 1\n\"x#\{(\"%.2f\" % 3.14159)}y\"\n",
1973
+ "RawParseTree" => [:block,
1974
+ [:lasgn, :argl, [:lit, 1]],
1975
+ [:dstr,
1976
+ "x",
1977
+ [:evstr,
1978
+ [:call, [:str, "%.2f"], :%,
1979
+ [:array, [:lit, 3.14159]]]],
1980
+ [:str, "y"]]],
1981
+ "ParseTree" => s(:block,
1982
+ s(:lasgn, :argl, s(:lit, 1)),
1983
+ s(:dstr,
1984
+ "x",
1985
+ s(:evstr,
1986
+ s(:call, s(:str, "%.2f"), :%,
1987
+ s(:arglist, s(:lit, 3.14159)))),
1988
+ s(:str, "y"))))
1989
+
1990
+ add_tests("dstr_3",
1991
+ "Ruby" => "max = 2\nargl = 1\n\"x#\{(\"%.#\{max}f\" % 3.14159)}y\"\n",
1992
+ "RawParseTree" => [:block,
1993
+ [:lasgn, :max, [:lit, 2]],
1994
+ [:lasgn, :argl, [:lit, 1]],
1995
+ [:dstr, "x",
1996
+ [:evstr,
1997
+ [:call,
1998
+ [:dstr, "%.",
1999
+ [:evstr, [:lvar, :max]],
2000
+ [:str, "f"]],
2001
+ :%,
2002
+ [:array, [:lit, 3.14159]]]],
2003
+ [:str, "y"]]],
2004
+ "ParseTree" => s(:block,
2005
+ s(:lasgn, :max, s(:lit, 2)),
2006
+ s(:lasgn, :argl, s(:lit, 1)),
2007
+ s(:dstr, "x",
2008
+ s(:evstr,
2009
+ s(:call,
2010
+ s(:dstr, "%.",
2011
+ s(:evstr, s(:lvar, :max)),
2012
+ s(:str, "f")),
2013
+ :%,
2014
+ s(:arglist, s(:lit, 3.14159)))),
2015
+ s(:str, "y"))))
2016
+
2017
+ add_tests("dstr_concat",
2018
+ "Ruby" => '"#{22}aa" "cd#{44}" "55" "#{66}"',
2019
+ "RawParseTree" => [:dstr,
2020
+ "",
2021
+ [:evstr, [:lit, 22]],
2022
+ [:str, "aa"],
2023
+ [:str, "cd"],
2024
+ [:evstr, [:lit, 44]],
2025
+ [:str, "55"],
2026
+ [:evstr, [:lit, 66]]],
2027
+ "ParseTree" => s(:dstr,
2028
+ "",
2029
+ s(:evstr, s(:lit, 22)),
2030
+ s(:str, "aa"),
2031
+ s(:str, "cd"),
2032
+ s(:evstr, s(:lit, 44)),
2033
+ s(:str, "55"),
2034
+ s(:evstr, s(:lit, 66))),
2035
+ "Ruby2Ruby" => '"#{22}aacd#{44}55#{66}"')
2036
+
2037
+ add_tests("dstr_gross",
2038
+ "Ruby" => '"a #$global b #@ivar c #@@cvar d"',
2039
+ "RawParseTree" => [:dstr, "a ",
2040
+ [:evstr, [:gvar, :$global]],
2041
+ [:str, " b "],
2042
+ [:evstr, [:ivar, :@ivar]],
2043
+ [:str, " c "],
2044
+ [:evstr, [:cvar, :@@cvar]],
2045
+ [:str, " d"]],
2046
+ "ParseTree" => s(:dstr, "a ",
2047
+ s(:evstr, s(:gvar, :$global)),
2048
+ s(:str, " b "),
2049
+ s(:evstr, s(:ivar, :@ivar)),
2050
+ s(:str, " c "),
2051
+ s(:evstr, s(:cvar, :@@cvar)),
2052
+ s(:str, " d")),
2053
+ "Ruby2Ruby" => '"a #{$global} b #{@ivar} c #{@@cvar} d"')
2054
+
2055
+ add_tests("dstr_heredoc_expand",
2056
+ "Ruby" => "<<EOM\n blah\n#\{1 + 1}blah\nEOM\n",
2057
+ "RawParseTree" => [:dstr, " blah\n",
2058
+ [:evstr, [:call, [:lit, 1], :+,
2059
+ [:array, [:lit, 1]]]],
2060
+ [:str, "blah\n"]],
2061
+ "ParseTree" => s(:dstr, " blah\n",
2062
+ s(:evstr, s(:call, s(:lit, 1), :+,
2063
+ s(:arglist, s(:lit, 1)))),
2064
+ s(:str, "blah\n")),
2065
+ "Ruby2Ruby" => "\" blah\\n#\{(1 + 1)}blah\\n\"")
2066
+
2067
+ add_tests("dstr_heredoc_windoze_sucks",
2068
+ "Ruby" => "<<-EOF\r\ndef test_#\{action}_valid_feed\r\n EOF\r\n",
2069
+ "RawParseTree" => [:dstr,
2070
+ 'def test_',
2071
+ [:evstr, [:vcall, :action]],
2072
+ [:str, "_valid_feed\n"]],
2073
+ "ParseTree" => s(:dstr,
2074
+ 'def test_',
2075
+ s(:evstr, s(:call, nil, :action, s(:arglist))),
2076
+ s(:str, "_valid_feed\n")),
2077
+ "Ruby2Ruby" => "\"def test_#\{action}_valid_feed\\n\"")
2078
+
2079
+ add_tests("dstr_heredoc_yet_again",
2080
+ "Ruby" => "<<-EOF\ns1 '#\{RUBY_PLATFORM}' s2\n#\{__FILE__}\n EOF\n",
2081
+ "RawParseTree" => [:dstr, "s1 '",
2082
+ [:evstr, [:const, :RUBY_PLATFORM]],
2083
+ [:str, "' s2\n"],
2084
+ [:str, "(string)"],
2085
+ [:str, "\n"]],
2086
+ "ParseTree" => s(:dstr, "s1 '",
2087
+ s(:evstr, s(:const, :RUBY_PLATFORM)),
2088
+ s(:str, "' s2\n"),
2089
+ s(:str, "(string)"),
2090
+ s(:str, "\n")),
2091
+ "Ruby2Ruby" => "\"s1 '#\{RUBY_PLATFORM}' s2\\n(string)\\n\"")
2092
+
2093
+ add_tests("dstr_nest",
2094
+ "Ruby" => "%Q[before [#\{nest}] after]",
2095
+ "RawParseTree" => [:dstr, "before [",
2096
+ [:evstr, [:vcall, :nest]], [:str, "] after"]],
2097
+ "ParseTree" => s(:dstr, "before [",
2098
+ s(:evstr, s(:call, nil, :nest, s(:arglist))),
2099
+ s(:str, "] after")),
2100
+ "Ruby2Ruby" => "\"before [#\{nest}] after\"")
2101
+
2102
+ add_tests("dstr_str_lit_start",
2103
+ "Ruby" => '"#{"blah"}#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})"',
2104
+ "RawParseTree" => [:dstr,
2105
+ "blah(string):",
2106
+ [:evstr, [:lit, 1]],
2107
+ [:str, ": warning: "],
2108
+ [:evstr, [:call, [:gvar, :$!], :message]],
2109
+ [:str, " ("],
2110
+ [:evstr, [:call, [:gvar, :$!], :class]],
2111
+ [:str, ")"]],
2112
+ "ParseTree" => s(:dstr,
2113
+ "blah(string):",
2114
+ s(:evstr, s(:lit, 1)),
2115
+ s(:str, ": warning: "),
2116
+ s(:evstr, s(:call, s(:gvar, :$!), :message,
2117
+ s(:arglist))),
2118
+ s(:str, " ("),
2119
+ s(:evstr, s(:call, s(:gvar, :$!), :class,
2120
+ s(:arglist))),
2121
+ s(:str, ")")),
2122
+ "Ruby2Ruby" => '"blah(string):#{1}: warning: #{$!.message} (#{$!.class})"')
2123
+
2124
+ add_tests("dstr_the_revenge",
2125
+ "Ruby" => '"before #{from} middle #{to} (#{__FILE__}:#{__LINE__})"',
2126
+ "RawParseTree" => [:dstr,
2127
+ "before ",
2128
+ [:evstr, [:vcall, :from]],
2129
+ [:str, " middle "],
2130
+ [:evstr, [:vcall, :to]],
2131
+ [:str, " ("],
2132
+ [:str, "(string)"],
2133
+ [:str, ":"],
2134
+ [:evstr, [:lit, 1]],
2135
+ [:str, ")"]],
2136
+ "ParseTree" => s(:dstr,
2137
+ "before ",
2138
+ s(:evstr, s(:call, nil, :from, s(:arglist))),
2139
+ s(:str, " middle "),
2140
+ s(:evstr, s(:call, nil, :to, s(:arglist))),
2141
+ s(:str, " ("),
2142
+ s(:str, "(string)"),
2143
+ s(:str, ":"),
2144
+ s(:evstr, s(:lit, 1)),
2145
+ s(:str, ")")),
2146
+ "Ruby2Ruby" => '"before #{from} middle #{to} ((string):#{1})"')
2147
+
2148
+ add_tests("dsym",
2149
+ "Ruby" => ":\"x#\{(1 + 1)}y\"",
2150
+ "RawParseTree" => [:dsym, "x",
2151
+ [:evstr, [:call, [:lit, 1], :+,
2152
+ [:array, [:lit, 1]]]], [:str, "y"]],
2153
+ "ParseTree" => s(:dsym, "x",
2154
+ s(:evstr, s(:call, s(:lit, 1), :+,
2155
+ s(:arglist, s(:lit, 1)))), s(:str, "y")))
2156
+
2157
+ add_tests("dxstr",
2158
+ "Ruby" => "t = 5\n`touch #\{t}`\n",
2159
+ "RawParseTree" => [:block,
2160
+ [:lasgn, :t, [:lit, 5]],
2161
+ [:dxstr, 'touch ', [:evstr, [:lvar, :t]]]],
2162
+ "ParseTree" => s(:block,
2163
+ s(:lasgn, :t, s(:lit, 5)),
2164
+ s(:dxstr, 'touch ', s(:evstr, s(:lvar, :t)))))
2165
+
2166
+ add_tests("ensure",
2167
+ "Ruby" => "begin\n (1 + 1)\nrescue SyntaxError => e1\n 2\nrescue Exception => e2\n 3\nelse\n 4\nensure\n 5\nend",
2168
+ "RawParseTree" => [:ensure,
2169
+ [:rescue,
2170
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]],
2171
+ [:resbody,
2172
+ [:array, [:const, :SyntaxError]],
2173
+ [:block,
2174
+ [:lasgn, :e1, [:gvar, :$!]], [:lit, 2]],
2175
+ [:resbody,
2176
+ [:array, [:const, :Exception]],
2177
+ [:block,
2178
+ [:lasgn, :e2, [:gvar, :$!]], [:lit, 3]]]],
2179
+ [:lit, 4]],
2180
+ [:lit, 5]],
2181
+ "ParseTree" => s(:ensure,
2182
+ s(:rescue,
2183
+ s(:call, s(:lit, 1), :+,
2184
+ s(:arglist, s(:lit, 1))),
2185
+ s(:resbody,
2186
+ s(:array,
2187
+ s(:const, :SyntaxError),
2188
+ s(:lasgn, :e1, s(:gvar, :$!))),
2189
+ s(:lit, 2)),
2190
+ s(:resbody,
2191
+ s(:array,
2192
+ s(:const, :Exception),
2193
+ s(:lasgn, :e2, s(:gvar, :$!))),
2194
+ s(:lit, 3)),
2195
+ s(:lit, 4)),
2196
+ s(:lit, 5)))
2197
+
2198
+ add_tests("false",
2199
+ "Ruby" => "false",
2200
+ "RawParseTree" => [:false],
2201
+ "ParseTree" => s(:false))
2202
+
2203
+ add_tests("fbody",
2204
+ "Ruby" => [Examples, :an_alias],
2205
+ "RawParseTree" => [:defn, :an_alias,
2206
+ [:fbody,
2207
+ [:scope,
2208
+ [:block,
2209
+ [:args, :x],
2210
+ [:call, [:lvar, :x], :+,
2211
+ [:array, [:lit, 1]]]]]]],
2212
+ "ParseTree" => s(:defn, :an_alias,
2213
+ s(:args, :x),
2214
+ s(:scope,
2215
+ s(:block,
2216
+ s(:call, s(:lvar, :x), :+,
2217
+ s(:arglist, s(:lit, 1)))))),
2218
+ "Ruby2Ruby" => "def an_alias(x)\n (x + 1)\nend")
2219
+
2220
+ add_tests("fcall_arglist",
2221
+ "Ruby" => "m(42)",
2222
+ "RawParseTree" => [:fcall, :m, [:array, [:lit, 42]]],
2223
+ "ParseTree" => s(:call, nil, :m, s(:arglist, s(:lit, 42))))
2224
+
2225
+ add_tests("fcall_arglist_hash",
2226
+ "Ruby" => "m(:a => 1, :b => 2)",
2227
+ "RawParseTree" => [:fcall, :m,
2228
+ [:array,
2229
+ [:hash,
2230
+ [:lit, :a], [:lit, 1],
2231
+ [:lit, :b], [:lit, 2]]]],
2232
+ "ParseTree" => s(:call, nil, :m,
2233
+ s(:arglist,
2234
+ s(:hash,
2235
+ s(:lit, :a), s(:lit, 1),
2236
+ s(:lit, :b), s(:lit, 2)))))
2237
+
2238
+ add_tests("fcall_arglist_norm_hash",
2239
+ "Ruby" => "m(42, :a => 1, :b => 2)",
2240
+ "RawParseTree" => [:fcall, :m,
2241
+ [:array,
2242
+ [:lit, 42],
2243
+ [:hash,
2244
+ [:lit, :a], [:lit, 1],
2245
+ [:lit, :b], [:lit, 2]]]],
2246
+ "ParseTree" => s(:call, nil, :m,
2247
+ s(:arglist,
2248
+ s(:lit, 42),
2249
+ s(:hash,
2250
+ s(:lit, :a), s(:lit, 1),
2251
+ s(:lit, :b), s(:lit, 2)))))
2252
+
2253
+ add_tests("fcall_arglist_norm_hash_splat",
2254
+ "Ruby" => "m(42, :a => 1, :b => 2, *c)",
2255
+ "RawParseTree" => [:fcall, :m,
2256
+ [:argscat,
2257
+ [:array,
2258
+ [:lit, 42],
2259
+ [:hash,
2260
+ [:lit, :a], [:lit, 1],
2261
+ [:lit, :b], [:lit, 2]]],
2262
+ [:vcall, :c]]],
2263
+ "ParseTree" => s(:call, nil, :m,
2264
+ s(:arglist,
2265
+ s(:lit, 42),
2266
+ s(:hash,
2267
+ s(:lit, :a), s(:lit, 1),
2268
+ s(:lit, :b), s(:lit, 2)),
2269
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
2270
+
2271
+ add_tests("fcall_block",
2272
+ "Ruby" => "a(:b) { :c }",
2273
+ "RawParseTree" => [:iter,
2274
+ [:fcall, :a, [:array, [:lit, :b]]], nil,
2275
+ [:lit, :c]],
2276
+ "ParseTree" => s(:iter,
2277
+ s(:call, nil, :a,
2278
+ s(:arglist, s(:lit, :b))), nil,
2279
+ s(:lit, :c)))
2280
+
2281
+ add_tests("fcall_index_space",
2282
+ "Ruby" => "a [42]",
2283
+ "RawParseTree" => [:fcall, :a, [:array, [:array, [:lit, 42]]]],
2284
+ "ParseTree" => s(:call, nil, :a,
2285
+ s(:arglist, s(:array, s(:lit, 42)))),
2286
+ "Ruby2Ruby" => "a([42])")
2287
+
2288
+ add_tests("fcall_keyword",
2289
+ "Ruby" => "42 if block_given?",
2290
+ "RawParseTree" => [:if, [:fcall, :block_given?], [:lit, 42], nil],
2291
+ "ParseTree" => s(:if,
2292
+ s(:call, nil, :block_given?, s(:arglist)),
2293
+ s(:lit, 42), nil))
2294
+
2295
+ add_tests("flip2",
2296
+ "Ruby" => "x = if ((i % 4) == 0)..((i % 3) == 0) then\n i\nelse\n nil\nend",
2297
+ "RawParseTree" => [:lasgn,
2298
+ :x,
2299
+ [:if,
2300
+ [:flip2,
2301
+ [:call,
2302
+ [:call, [:vcall, :i], :%,
2303
+ [:array, [:lit, 4]]],
2304
+ :==,
2305
+ [:array, [:lit, 0]]],
2306
+ [:call,
2307
+ [:call, [:vcall, :i], :%,
2308
+ [:array, [:lit, 3]]],
2309
+ :==,
2310
+ [:array, [:lit, 0]]]],
2311
+ [:vcall, :i],
2312
+ [:nil]]],
2313
+ "ParseTree" => s(:lasgn,
2314
+ :x,
2315
+ s(:if,
2316
+ s(:flip2,
2317
+ s(:call,
2318
+ s(:call, s(:call, nil, :i, s(:arglist)),
2319
+ :%,
2320
+ s(:arglist, s(:lit, 4))),
2321
+ :==,
2322
+ s(:arglist, s(:lit, 0))),
2323
+ s(:call,
2324
+ s(:call, s(:call, nil, :i, s(:arglist)),
2325
+ :%,
2326
+ s(:arglist, s(:lit, 3))),
2327
+ :==,
2328
+ s(:arglist, s(:lit, 0)))),
2329
+ s(:call, nil, :i, s(:arglist)),
2330
+ s(:nil))))
2331
+
2332
+ add_tests("flip2_method",
2333
+ "Ruby" => "if 1..2.a?(b) then\n nil\nend",
2334
+ "RawParseTree" => [:if,
2335
+ [:flip2,
2336
+ [:call, [:lit, 1], :==,
2337
+ [:array, [:gvar, :$.]]],
2338
+ [:call, [:lit, 2], :a?,
2339
+ [:array, [:vcall, :b]]]],
2340
+ [:nil],
2341
+ nil],
2342
+ "ParseTree" => s(:if,
2343
+ s(:flip2,
2344
+ s(:lit, 1),
2345
+ s(:call, s(:lit, 2), :a?,
2346
+ s(:arglist,
2347
+ s(:call, nil, :b, s(:arglist))))),
2348
+ s(:nil),
2349
+ nil))
2350
+
2351
+ add_tests("flip3",
2352
+ "Ruby" => "x = if ((i % 4) == 0)...((i % 3) == 0) then\n i\nelse\n nil\nend",
2353
+ "RawParseTree" => [:lasgn,
2354
+ :x,
2355
+ [:if,
2356
+ [:flip3,
2357
+ [:call,
2358
+ [:call, [:vcall, :i], :%,
2359
+ [:array, [:lit, 4]]],
2360
+ :==,
2361
+ [:array, [:lit, 0]]],
2362
+ [:call,
2363
+ [:call, [:vcall, :i], :%,
2364
+ [:array, [:lit, 3]]],
2365
+ :==,
2366
+ [:array, [:lit, 0]]]],
2367
+ [:vcall, :i],
2368
+ [:nil]]],
2369
+ "ParseTree" => s(:lasgn,
2370
+ :x,
2371
+ s(:if,
2372
+ s(:flip3,
2373
+ s(:call,
2374
+ s(:call, s(:call, nil, :i, s(:arglist)),
2375
+ :%,
2376
+ s(:arglist, s(:lit, 4))),
2377
+ :==,
2378
+ s(:arglist, s(:lit, 0))),
2379
+ s(:call,
2380
+ s(:call, s(:call, nil, :i, s(:arglist)),
2381
+ :%,
2382
+ s(:arglist, s(:lit, 3))),
2383
+ :==,
2384
+ s(:arglist, s(:lit, 0)))),
2385
+ s(:call, nil, :i, s(:arglist)),
2386
+ s(:nil))))
2387
+
2388
+ add_tests("for",
2389
+ "Ruby" => "for o in ary do\n puts(o)\nend",
2390
+ "RawParseTree" => [:for,
2391
+ [:vcall, :ary],
2392
+ [:lasgn, :o],
2393
+ [:fcall, :puts, [:array, [:lvar, :o]]]],
2394
+ "ParseTree" => s(:for,
2395
+ s(:call, nil, :ary, s(:arglist)),
2396
+ s(:lasgn, :o),
2397
+ s(:call, nil, :puts,
2398
+ s(:arglist, s(:lvar, :o)))))
2399
+
2400
+ add_tests("for_no_body",
2401
+ "Ruby" => "for i in (0..max) do\n # do nothing\nend",
2402
+ "RawParseTree" => [:for,
2403
+ [:dot2, [:lit, 0], [:vcall, :max]],
2404
+ [:lasgn, :i]],
2405
+ "ParseTree" => s(:for,
2406
+ s(:dot2,
2407
+ s(:lit, 0),
2408
+ s(:call, nil, :max, s(:arglist))),
2409
+ s(:lasgn, :i)))
2410
+
2411
+ add_tests("gasgn",
2412
+ "Ruby" => "$x = 42",
2413
+ "RawParseTree" => [:gasgn, :$x, [:lit, 42]],
2414
+ "ParseTree" => s(:gasgn, :$x, s(:lit, 42)))
2415
+
2416
+ add_tests("global",
2417
+ "Ruby" => "$stderr",
2418
+ "RawParseTree" => [:gvar, :$stderr],
2419
+ "ParseTree" => s(:gvar, :$stderr))
2420
+
2421
+ add_tests("gvar",
2422
+ "Ruby" => "$x",
2423
+ "RawParseTree" => [:gvar, :$x],
2424
+ "ParseTree" => s(:gvar, :$x))
2425
+
2426
+ add_tests("gvar_underscore",
2427
+ "Ruby" => "$_",
2428
+ "RawParseTree" => [:gvar, :$_],
2429
+ "ParseTree" => s(:gvar, :$_))
2430
+
2431
+ add_tests("gvar_underscore_blah",
2432
+ "Ruby" => "$__blah",
2433
+ "RawParseTree" => [:gvar, :$__blah],
2434
+ "ParseTree" => s(:gvar, :$__blah))
2435
+
2436
+ add_tests("hash",
2437
+ "Ruby" => "{ 1 => 2, 3 => 4 }",
2438
+ "RawParseTree" => [:hash,
2439
+ [:lit, 1], [:lit, 2],
2440
+ [:lit, 3], [:lit, 4]],
2441
+ "ParseTree" => s(:hash,
2442
+ s(:lit, 1), s(:lit, 2),
2443
+ s(:lit, 3), s(:lit, 4)))
2444
+
2445
+ add_tests("hash_rescue",
2446
+ "Ruby" => "{ 1 => (2 rescue 3) }",
2447
+ "RawParseTree" => [:hash,
2448
+ [:lit, 1],
2449
+ [:rescue,
2450
+ [:lit, 2],
2451
+ [:resbody, nil, [:lit, 3]]]],
2452
+ "ParseTree" => s(:hash,
2453
+ s(:lit, 1),
2454
+ s(:rescue,
2455
+ s(:lit, 2),
2456
+ s(:resbody, s(:array), s(:lit, 3)))))
2457
+
2458
+ add_tests("iasgn",
2459
+ "Ruby" => "@a = 4",
2460
+ "RawParseTree" => [:iasgn, :@a, [:lit, 4]],
2461
+ "ParseTree" => s(:iasgn, :@a, s(:lit, 4)))
2462
+
2463
+ add_tests("if_block_condition",
2464
+ "Ruby" => "if (x = 5\n(x + 1)) then\n nil\nend",
2465
+ "RawParseTree" => [:if,
2466
+ [:block,
2467
+ [:lasgn, :x, [:lit, 5]],
2468
+ [:call,
2469
+ [:lvar, :x],
2470
+ :+,
2471
+ [:array, [:lit, 1]]]],
2472
+ [:nil],
2473
+ nil],
2474
+ "ParseTree" => s(:if,
2475
+ s(:block,
2476
+ s(:lasgn, :x, s(:lit, 5)),
2477
+ s(:call,
2478
+ s(:lvar, :x),
2479
+ :+,
2480
+ s(:arglist, s(:lit, 1)))),
2481
+ s(:nil),
2482
+ nil))
2483
+
2484
+ add_tests("if_lasgn_short",
2485
+ "Ruby" => "if x = obj.x then\n x.do_it\nend",
2486
+ "RawParseTree" => [:if,
2487
+ [:lasgn, :x,
2488
+ [:call, [:vcall, :obj], :x]],
2489
+ [:call,
2490
+ [:lvar, :x], :do_it],
2491
+ nil],
2492
+ "ParseTree" => s(:if,
2493
+ s(:lasgn, :x,
2494
+ s(:call,
2495
+ s(:call, nil, :obj, s(:arglist)),
2496
+ :x, s(:arglist))),
2497
+ s(:call, s(:lvar, :x), :do_it, s(:arglist)),
2498
+ nil))
2499
+
2500
+ add_tests("if_nested",
2501
+ "Ruby" => "return if false unless true",
2502
+ "RawParseTree" => [:if, [:true], nil,
2503
+ [:if, [:false], [:return], nil]],
2504
+ "ParseTree" => s(:if, s(:true), nil,
2505
+ s(:if, s(:false), s(:return), nil)))
2506
+
2507
+ add_tests("if_post",
2508
+ "Ruby" => "a if b",
2509
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
2510
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
2511
+ s(:call, nil, :a, s(:arglist)), nil))
2512
+
2513
+ add_tests("if_post_not",
2514
+ "Ruby" => "a if not b",
2515
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
2516
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
2517
+ s(:call, nil, :a, s(:arglist))),
2518
+ "Ruby2Ruby" => "a unless b")
2519
+
2520
+ add_tests("if_pre",
2521
+ "Ruby" => "if b then a end",
2522
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
2523
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
2524
+ s(:call, nil, :a, s(:arglist)), nil),
2525
+ "Ruby2Ruby" => "a if b")
2526
+
2527
+ add_tests("if_pre_not",
2528
+ "Ruby" => "if not b then a end",
2529
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
2530
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
2531
+ s(:call, nil, :a, s(:arglist))),
2532
+ "Ruby2Ruby" => "a unless b")
2533
+
2534
+ add_tests("iter_call_arglist_space",
2535
+ "Ruby" => "a (1) {|c|d}",
2536
+ "RawParseTree" => [:iter,
2537
+ [:fcall, :a, [:array, [:lit, 1]]],
2538
+ [:dasgn_curr, :c],
2539
+ [:vcall, :d]],
2540
+ "ParseTree" => s(:iter,
2541
+ s(:call, nil, :a, s(:arglist, s(:lit, 1))),
2542
+ s(:lasgn, :c),
2543
+ s(:call, nil, :d, s(:arglist))),
2544
+ "Ruby2Ruby" => "a(1) { |c| d }")
2545
+
2546
+ add_tests("iter_dasgn_curr_dasgn_madness",
2547
+ "Ruby" => "as.each { |a|\n b += a.b(false) }",
2548
+ "RawParseTree" => [:iter,
2549
+ [:call, [:vcall, :as], :each],
2550
+ [:dasgn_curr, :a],
2551
+ [:dasgn_curr,
2552
+ :b,
2553
+ [:call,
2554
+ [:dvar, :b],
2555
+ :+,
2556
+ [:array,
2557
+ [:call, [:dvar, :a], :b,
2558
+ [:array, [:false]]]]]]],
2559
+ "ParseTree" => s(:iter,
2560
+ s(:call,
2561
+ s(:call, nil, :as, s(:arglist)),
2562
+ :each, s(:arglist)),
2563
+ s(:lasgn, :a),
2564
+ s(:lasgn, :b,
2565
+ s(:call,
2566
+ s(:lvar, :b),
2567
+ :+,
2568
+ s(:arglist,
2569
+ s(:call, s(:lvar, :a), :b,
2570
+ s(:arglist, s(:false))))))),
2571
+ "Ruby2Ruby" => "as.each { |a| b = (b + a.b(false)) }")
2572
+
2573
+ add_tests("iter_downto",
2574
+ "Ruby" => "3.downto(1) { |n| puts(n.to_s) }",
2575
+ "RawParseTree" => [:iter,
2576
+ [:call, [:lit, 3], :downto, [:array, [:lit, 1]]],
2577
+ [:dasgn_curr, :n],
2578
+ [:fcall, :puts,
2579
+ [:array, [:call, [:dvar, :n], :to_s]]]],
2580
+ "ParseTree" => s(:iter,
2581
+ s(:call, s(:lit, 3), :downto,
2582
+ s(:arglist, s(:lit, 1))),
2583
+ s(:lasgn, :n),
2584
+ s(:call, nil, :puts,
2585
+ s(:arglist,
2586
+ s(:call, s(:lvar, :n),
2587
+ :to_s, s(:arglist))))))
2588
+
2589
+ add_tests("iter_each_lvar",
2590
+ "Ruby" => "array = [1, 2, 3]\narray.each { |x| puts(x.to_s) }\n",
2591
+ "RawParseTree" => [:block,
2592
+ [:lasgn, :array,
2593
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
2594
+ [:iter,
2595
+ [:call, [:lvar, :array], :each],
2596
+ [:dasgn_curr, :x],
2597
+ [:fcall, :puts,
2598
+ [:array, [:call, [:dvar, :x], :to_s]]]]],
2599
+ "ParseTree" => s(:block,
2600
+ s(:lasgn, :array,
2601
+ s(:array,
2602
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
2603
+ s(:iter,
2604
+ s(:call, s(:lvar, :array), :each,
2605
+ s(:arglist)),
2606
+ s(:lasgn, :x),
2607
+ s(:call, nil, :puts,
2608
+ s(:arglist, s(:call, s(:lvar, :x),
2609
+ :to_s, s(:arglist)))))))
2610
+
2611
+ add_tests("iter_each_nested",
2612
+ "Ruby" => "array1 = [1, 2, 3]\narray2 = [4, 5, 6, 7]\narray1.each do |x|\n array2.each do |y|\n puts(x.to_s)\n puts(y.to_s)\n end\nend\n",
2613
+ "RawParseTree" => [:block,
2614
+ [:lasgn, :array1,
2615
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
2616
+ [:lasgn, :array2,
2617
+ [:array,
2618
+ [:lit, 4], [:lit, 5], [:lit, 6], [:lit, 7]]],
2619
+ [:iter,
2620
+ [:call,
2621
+ [:lvar, :array1], :each],
2622
+ [:dasgn_curr, :x],
2623
+ [:iter,
2624
+ [:call,
2625
+ [:lvar, :array2], :each],
2626
+ [:dasgn_curr, :y],
2627
+ [:block,
2628
+ [:fcall, :puts,
2629
+ [:array, [:call, [:dvar, :x], :to_s]]],
2630
+ [:fcall, :puts,
2631
+ [:array, [:call, [:dvar, :y], :to_s]]]]]]],
2632
+ "ParseTree" => s(:block,
2633
+ s(:lasgn, :array1,
2634
+ s(:array,
2635
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
2636
+ s(:lasgn, :array2,
2637
+ s(:array,
2638
+ s(:lit, 4), s(:lit, 5),
2639
+ s(:lit, 6), s(:lit, 7))),
2640
+ s(:iter,
2641
+ s(:call,
2642
+ s(:lvar, :array1), :each, s(:arglist)),
2643
+ s(:lasgn, :x),
2644
+ s(:iter,
2645
+ s(:call,
2646
+ s(:lvar, :array2), :each, s(:arglist)),
2647
+ s(:lasgn, :y),
2648
+ s(:block,
2649
+ s(:call, nil, :puts,
2650
+ s(:arglist,
2651
+ s(:call, s(:lvar, :x),
2652
+ :to_s, s(:arglist)))),
2653
+ s(:call, nil, :puts,
2654
+ s(:arglist,
2655
+ s(:call, s(:lvar, :y),
2656
+ :to_s, s(:arglist)))))))))
2657
+
2658
+ add_tests("iter_loop_empty",
2659
+ "Ruby" => "loop { }",
2660
+ "RawParseTree" => [:iter, [:fcall, :loop], nil],
2661
+ "ParseTree" => s(:iter, s(:call, nil, :loop, s(:arglist)), nil))
2662
+
2663
+ add_tests("iter_masgn_2",
2664
+ "Ruby" => "a { |b, c| p(c) }",
2665
+ "RawParseTree" => [:iter,
2666
+ [:fcall, :a],
2667
+ [:masgn,
2668
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2669
+ nil, nil],
2670
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2671
+ "ParseTree" => s(:iter,
2672
+ s(:call, nil, :a, s(:arglist)),
2673
+ s(:masgn,
2674
+ s(:array, s(:lasgn, :b), s(:lasgn, :c))),
2675
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2676
+
2677
+ add_tests("iter_masgn_args_splat",
2678
+ "Ruby" => "a { |b, c, *d| p(c) }",
2679
+ "RawParseTree" => [:iter,
2680
+ [:fcall, :a],
2681
+ [:masgn,
2682
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2683
+ [:dasgn_curr, :d], nil],
2684
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2685
+ "ParseTree" => s(:iter,
2686
+ s(:call, nil, :a, s(:arglist)),
2687
+ s(:masgn,
2688
+ s(:array,
2689
+ s(:lasgn, :b),
2690
+ s(:lasgn, :c),
2691
+ s(:splat, s(:lasgn, :d)))),
2692
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2693
+
2694
+ add_tests("iter_masgn_args_splat_no_name",
2695
+ "Ruby" => "a { |b, c, *| p(c) }",
2696
+ "RawParseTree" => [:iter,
2697
+ [:fcall, :a],
2698
+ [:masgn,
2699
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2700
+ [:splat], nil],
2701
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2702
+ "ParseTree" => s(:iter,
2703
+ s(:call, nil, :a, s(:arglist)),
2704
+ s(:masgn,
2705
+ s(:array,
2706
+ s(:lasgn, :b),
2707
+ s(:lasgn, :c),
2708
+ s(:splat))),
2709
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2710
+
2711
+ add_tests("iter_masgn_splat",
2712
+ "Ruby" => "a { |*c| p(c) }",
2713
+ "RawParseTree" => [:iter,
2714
+ [:fcall, :a],
2715
+ [:masgn, nil, [:dasgn_curr, :c], nil],
2716
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2717
+ "ParseTree" => s(:iter,
2718
+ s(:call, nil, :a, s(:arglist)),
2719
+ s(:masgn, s(:array, s(:splat, s(:lasgn, :c)))),
2720
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2721
+
2722
+ add_tests("iter_masgn_splat_no_name",
2723
+ "Ruby" => "a { |*| p(c) }",
2724
+ "RawParseTree" => [:iter,
2725
+ [:fcall, :a],
2726
+ [:masgn, nil, [:splat], nil],
2727
+ [:fcall, :p, [:array, [:vcall, :c]]]],
2728
+ "ParseTree" => s(:iter,
2729
+ s(:call, nil, :a, s(:arglist)),
2730
+ s(:masgn, s(:array, s(:splat))),
2731
+ s(:call, nil, :p,
2732
+ s(:arglist, s(:call, nil, :c, s(:arglist))))))
2733
+
2734
+ add_tests("iter_shadowed_var",
2735
+ "Ruby" => "a do |x|\n b do |x|\n puts x\n end\nend",
2736
+ "RawParseTree" => [:iter,
2737
+ [:fcall, :a],
2738
+ [:dasgn_curr, :x],
2739
+ [:iter,
2740
+ [:fcall, :b],
2741
+ [:dasgn, :x],
2742
+ [:fcall, :puts, [:array, [:dvar, :x]]]]],
2743
+ "ParseTree" => s(:iter,
2744
+ s(:call, nil, :a, s(:arglist)),
2745
+ s(:lasgn, :x),
2746
+ s(:iter,
2747
+ s(:call, nil, :b, s(:arglist)),
2748
+ s(:lasgn, :x),
2749
+ s(:call, nil, :puts,
2750
+ s(:arglist, s(:lvar, :x))))),
2751
+ "Ruby2Ruby" => "a { |x| b { |x| puts(x) } }")
2752
+
2753
+ add_tests("iter_upto",
2754
+ "Ruby" => "1.upto(3) { |n| puts(n.to_s) }",
2755
+ "RawParseTree" => [:iter,
2756
+ [:call, [:lit, 1], :upto, [:array, [:lit, 3]]],
2757
+ [:dasgn_curr, :n],
2758
+ [:fcall, :puts,
2759
+ [:array, [:call, [:dvar, :n], :to_s]]]],
2760
+ "ParseTree" => s(:iter,
2761
+ s(:call, s(:lit, 1), :upto,
2762
+ s(:arglist, s(:lit, 3))),
2763
+ s(:lasgn, :n),
2764
+ s(:call, nil, :puts,
2765
+ s(:arglist,
2766
+ s(:call, s(:lvar, :n), :to_s,
2767
+ s(:arglist))))))
2768
+
2769
+ add_tests("iter_while",
2770
+ "Ruby" => "argl = 10\nwhile (argl >= 1) do\n puts(\"hello\")\n argl = (argl - 1)\nend\n",
2771
+ "RawParseTree" => [:block,
2772
+ [:lasgn, :argl, [:lit, 10]],
2773
+ [:while,
2774
+ [:call, [:lvar, :argl], :">=",
2775
+ [:array, [:lit, 1]]],
2776
+ [:block,
2777
+ [:fcall, :puts, [:array, [:str, "hello"]]],
2778
+ [:lasgn,
2779
+ :argl,
2780
+ [:call, [:lvar, :argl],
2781
+ :"-", [:array, [:lit, 1]]]]], true]],
2782
+ "ParseTree" => s(:block,
2783
+ s(:lasgn, :argl, s(:lit, 10)),
2784
+ s(:while,
2785
+ s(:call, s(:lvar, :argl), :">=",
2786
+ s(:arglist, s(:lit, 1))),
2787
+ s(:block,
2788
+ s(:call, nil, :puts,
2789
+ s(:arglist, s(:str, "hello"))),
2790
+ s(:lasgn,
2791
+ :argl,
2792
+ s(:call, s(:lvar, :argl), :"-",
2793
+ s(:arglist, s(:lit, 1))))), true)))
2794
+
2795
+ add_tests("ivar",
2796
+ "Ruby" => [Examples, :reader],
2797
+ "RawParseTree" => [:defn, :reader, [:ivar, :@reader]],
2798
+ "ParseTree" => s(:defn, :reader, # FIX should be unified?
2799
+ s(:args),
2800
+ s(:ivar, :@reader)),
2801
+ "Ruby2Ruby" => "attr_reader :reader")
2802
+
2803
+ add_tests("lasgn_array",
2804
+ "Ruby" => "var = [\"foo\", \"bar\"]",
2805
+ "RawParseTree" => [:lasgn, :var,
2806
+ [:array,
2807
+ [:str, "foo"],
2808
+ [:str, "bar"]]],
2809
+ "ParseTree" => s(:lasgn, :var,
2810
+ s(:array,
2811
+ s(:str, "foo"),
2812
+ s(:str, "bar"))))
2813
+
2814
+ add_tests("lasgn_call",
2815
+ "Ruby" => "c = (2 + 3)",
2816
+ "RawParseTree" => [:lasgn, :c, [:call, [:lit, 2], :+,
2817
+ [:array, [:lit, 3]]]],
2818
+ "ParseTree" => s(:lasgn, :c, s(:call, s(:lit, 2), :+,
2819
+ s(:arglist, s(:lit, 3)))))
2820
+
2821
+ add_tests("lit_bool_false",
2822
+ "Ruby" => "false",
2823
+ "RawParseTree" => [:false],
2824
+ "ParseTree" => s(:false))
2825
+
2826
+ add_tests("lit_bool_true",
2827
+ "Ruby" => "true",
2828
+ "RawParseTree" => [:true],
2829
+ "ParseTree" => s(:true))
2830
+
2831
+ add_tests("lit_float",
2832
+ "Ruby" => "1.1",
2833
+ "RawParseTree" => [:lit, 1.1],
2834
+ "ParseTree" => s(:lit, 1.1))
2835
+
2836
+ add_tests("lit_long",
2837
+ "Ruby" => "1",
2838
+ "RawParseTree" => [:lit, 1],
2839
+ "ParseTree" => s(:lit, 1))
2840
+
2841
+ add_tests("lit_long_negative",
2842
+ "Ruby" => "-1",
2843
+ "RawParseTree" => [:lit, -1],
2844
+ "ParseTree" => s(:lit, -1))
2845
+
2846
+ add_tests("lit_range2",
2847
+ "Ruby" => "(1..10)",
2848
+ "RawParseTree" => [:lit, 1..10],
2849
+ "ParseTree" => s(:lit, 1..10))
2850
+
2851
+ add_tests("lit_range3",
2852
+ "Ruby" => "(1...10)",
2853
+ "RawParseTree" => [:lit, 1...10],
2854
+ "ParseTree" => s(:lit, 1...10))
2855
+
2856
+ # TODO: discuss and decide which lit we like
2857
+ # it "converts a regexp to an sexp" do
2858
+ # "/blah/".to_sexp.should == s(:regex, "blah", 0)
2859
+ # "/blah/i".to_sexp.should == s(:regex, "blah", 1)
2860
+ # "/blah/u".to_sexp.should == s(:regex, "blah", 64)
2861
+ # end
2862
+
2863
+ add_tests("lit_regexp",
2864
+ "Ruby" => "/x/",
2865
+ "RawParseTree" => [:lit, /x/],
2866
+ "ParseTree" => s(:lit, /x/))
2867
+
2868
+ add_tests("lit_regexp_i_wwtt",
2869
+ "Ruby" => 'str.split(//i)',
2870
+ "RawParseTree" => [:call, [:vcall, :str], :split,
2871
+ [:array, [:lit, //i]]],
2872
+ "ParseTree" => s(:call, s(:call, nil, :str, s(:arglist)), :split,
2873
+ s(:arglist, s(:lit, //i))))
2874
+
2875
+ add_tests("lit_regexp_n",
2876
+ "Ruby" => "/x/n", # HACK differs on 1.9 - this is easiest
2877
+ "RawParseTree" => [:lit, /x/n],
2878
+ "ParseTree" => s(:lit, /x/n),
2879
+ "Ruby2Ruby" => /x/n.inspect)
2880
+
2881
+ add_tests("lit_regexp_once",
2882
+ "Ruby" => "/x/o",
2883
+ "RawParseTree" => [:lit, /x/],
2884
+ "ParseTree" => s(:lit, /x/),
2885
+ "Ruby2Ruby" => "/x/")
2886
+
2887
+ add_tests("lit_sym",
2888
+ "Ruby" => ":x",
2889
+ "RawParseTree" => [:lit, :x],
2890
+ "ParseTree" => s(:lit, :x))
2891
+
2892
+ add_tests("lit_sym_splat",
2893
+ "Ruby" => ":\"*args\"",
2894
+ "RawParseTree" => [:lit, :"*args"],
2895
+ "ParseTree" => s(:lit, :"*args"))
2896
+
2897
+ add_tests("lvar_def_boundary",
2898
+ "Ruby" => "b = 42\ndef a\n c do\n begin\n do_stuff\n rescue RuntimeError => b\n puts(b)\n end\n end\nend\n",
2899
+ "RawParseTree" => [:block,
2900
+ [:lasgn, :b, [:lit, 42]],
2901
+ [:defn, :a,
2902
+ [:scope,
2903
+ [:block,
2904
+ [:args],
2905
+ [:iter,
2906
+ [:fcall, :c],
2907
+ nil,
2908
+ [:rescue,
2909
+ [:vcall, :do_stuff],
2910
+ [:resbody,
2911
+ [:array, [:const, :RuntimeError]],
2912
+ [:block,
2913
+ [:dasgn_curr, :b, [:gvar, :$!]],
2914
+ [:fcall, :puts,
2915
+ [:array, [:dvar, :b]]]]]]]]]]],
2916
+ "ParseTree" => s(:block,
2917
+ s(:lasgn, :b, s(:lit, 42)),
2918
+ s(:defn, :a,
2919
+ s(:args),
2920
+ s(:scope,
2921
+ s(:block,
2922
+ s(:iter,
2923
+ s(:call, nil, :c, s(:arglist)),
2924
+ nil,
2925
+ s(:rescue,
2926
+ s(:call, nil, :do_stuff, s(:arglist)),
2927
+ s(:resbody,
2928
+ s(:array,
2929
+ s(:const, :RuntimeError),
2930
+ s(:lasgn, :b, s(:gvar, :$!))),
2931
+ s(:call, nil, :puts,
2932
+ s(:arglist,
2933
+ s(:lvar, :b)))))))))))
2934
+
2935
+ add_tests("masgn",
2936
+ "Ruby" => "a, b = c, d",
2937
+ "RawParseTree" => [:masgn,
2938
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
2939
+ [:array, [:vcall, :c], [:vcall, :d]]],
2940
+ "ParseTree" => s(:masgn,
2941
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
2942
+ s(:array, s(:call, nil, :c, s(:arglist)),
2943
+ s(:call, nil, :d, s(:arglist)))))
2944
+
2945
+ add_tests("masgn_argscat",
2946
+ "Ruby" => "a, b, *c = 1, 2, *[3, 4]",
2947
+ "RawParseTree" => [:masgn,
2948
+ [:array, [:lasgn, :a], [:lasgn, :b]],
2949
+ [:lasgn, :c],
2950
+ [:argscat,
2951
+ [:array, [:lit, 1], [:lit, 2]],
2952
+ [:array, [:lit, 3], [:lit, 4]]]],
2953
+ "ParseTree" => s(:masgn,
2954
+ s(:array,
2955
+ s(:lasgn, :a),
2956
+ s(:lasgn, :b),
2957
+ s(:splat, s(:lasgn, :c))),
2958
+ s(:array,
2959
+ s(:lit, 1), s(:lit, 2),
2960
+ s(:splat,
2961
+ s(:array, s(:lit, 3), s(:lit, 4))))))
2962
+
2963
+ add_tests("masgn_attrasgn",
2964
+ "Ruby" => "a, b.c = d, e",
2965
+ "RawParseTree" => [:masgn,
2966
+ [:array, [:lasgn, :a],
2967
+ [:attrasgn, [:vcall, :b], :c=]], nil,
2968
+ [:array, [:vcall, :d], [:vcall, :e]]],
2969
+ "ParseTree" => s(:masgn,
2970
+ s(:array,
2971
+ s(:lasgn, :a),
2972
+ s(:attrasgn,
2973
+ s(:call, nil, :b, s(:arglist)),
2974
+ :c=, s(:arglist))),
2975
+ s(:array,
2976
+ s(:call, nil, :d, s(:arglist)),
2977
+ s(:call, nil, :e, s(:arglist)))))
2978
+
2979
+ add_tests("masgn_attrasgn_array_rhs",
2980
+ "Ruby" => "a.b, a.c, _ = q",
2981
+ "RawParseTree" => [:masgn,
2982
+ [:array,
2983
+ [:attrasgn, [:vcall, :a], :b=],
2984
+ [:attrasgn, [:vcall, :a], :c=],
2985
+ [:lasgn, :_]], nil,
2986
+ [:to_ary, [:vcall, :q]]],
2987
+ "ParseTree" => s(:masgn,
2988
+ s(:array,
2989
+ s(:attrasgn,
2990
+ s(:call, nil, :a, s(:arglist)),
2991
+ :b=, s(:arglist)),
2992
+ s(:attrasgn,
2993
+ s(:call, nil, :a, s(:arglist)),
2994
+ :c=, s(:arglist)),
2995
+ s(:lasgn, :_)),
2996
+ s(:to_ary,
2997
+ s(:call, nil, :q, s(:arglist)))))
2998
+
2999
+ add_tests("masgn_attrasgn_idx",
3000
+ "Ruby" => "a, i, j = [], 1, 2\na[i], a[j] = a[j], a[i]\n",
3001
+ "RawParseTree" => [:block,
3002
+ [:masgn,
3003
+ [:array,
3004
+ [:lasgn, :a], [:lasgn, :i], [:lasgn, :j]], nil,
3005
+ [:array, [:zarray], [:lit, 1], [:lit, 2]]],
3006
+ [:masgn,
3007
+ [:array,
3008
+ [:attrasgn,
3009
+ [:lvar, :a], :[]=, [:array, [:lvar, :i]]],
3010
+ [:attrasgn,
3011
+ [:lvar, :a], :[]=, [:array, [:lvar, :j]]]],
3012
+ nil,
3013
+ [:array,
3014
+ [:call, [:lvar, :a], :[],
3015
+ [:array, [:lvar, :j]]],
3016
+ [:call, [:lvar, :a], :[],
3017
+ [:array, [:lvar, :i]]]]]],
3018
+ "ParseTree" => s(:block,
3019
+ s(:masgn,
3020
+ s(:array,
3021
+ s(:lasgn, :a),
3022
+ s(:lasgn, :i), s(:lasgn, :j)),
3023
+ s(:array, s(:array), s(:lit, 1), s(:lit, 2))),
3024
+ s(:masgn,
3025
+ s(:array,
3026
+ s(:attrasgn, s(:lvar, :a), :[]=,
3027
+ s(:arglist, s(:lvar, :i))),
3028
+ s(:attrasgn, s(:lvar, :a), :[]=,
3029
+ s(:arglist, s(:lvar, :j)))),
3030
+ s(:array,
3031
+ s(:call, s(:lvar, :a), :[],
3032
+ s(:arglist, s(:lvar, :j))),
3033
+ s(:call, s(:lvar, :a), :[],
3034
+ s(:arglist, s(:lvar, :i)))))))
3035
+
3036
+ add_tests("masgn_cdecl",
3037
+ "Ruby" => "A, B, C = 1, 2, 3",
3038
+ "RawParseTree" => [:masgn,
3039
+ [:array, [:cdecl, :A], [:cdecl, :B],
3040
+ [:cdecl, :C]], nil,
3041
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
3042
+ "ParseTree" => s(:masgn,
3043
+ s(:array, s(:cdecl, :A), s(:cdecl, :B),
3044
+ s(:cdecl, :C)),
3045
+ s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))))
3046
+
3047
+
3048
+ add_tests("masgn_iasgn",
3049
+ "Ruby" => "a, @b = c, d",
3050
+ "RawParseTree" => [:masgn,
3051
+ [:array, [:lasgn, :a], [:iasgn, :"@b"]], nil,
3052
+ [:array, [:vcall, :c], [:vcall, :d]]],
3053
+ "ParseTree" => s(:masgn,
3054
+ s(:array, s(:lasgn, :a), s(:iasgn, :"@b")),
3055
+ s(:array,
3056
+ s(:call, nil, :c, s(:arglist)),
3057
+ s(:call, nil, :d, s(:arglist)))))
3058
+
3059
+ add_tests("masgn_masgn",
3060
+ "Ruby" => "a, (b, c) = [1, [2, 3]]",
3061
+ "RawParseTree" => [:masgn,
3062
+ [:array,
3063
+ [:lasgn, :a],
3064
+ [:masgn,
3065
+ [:array,
3066
+ [:lasgn, :b],
3067
+ [:lasgn, :c]], nil, nil]],
3068
+ nil,
3069
+ [:to_ary,
3070
+ [:array,
3071
+ [:lit, 1],
3072
+ [:array,
3073
+ [:lit, 2],
3074
+ [:lit, 3]]]]],
3075
+ "ParseTree" => s(:masgn,
3076
+ s(:array,
3077
+ s(:lasgn, :a),
3078
+ s(:masgn,
3079
+ s(:array,
3080
+ s(:lasgn, :b),
3081
+ s(:lasgn, :c)))),
3082
+ s(:to_ary,
3083
+ s(:array,
3084
+ s(:lit, 1),
3085
+ s(:array,
3086
+ s(:lit, 2),
3087
+ s(:lit, 3))))))
3088
+
3089
+ add_tests("masgn_splat_lhs",
3090
+ "Ruby" => "a, b, *c = d, e, f, g",
3091
+ "RawParseTree" => [:masgn,
3092
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3093
+ [:lasgn, :c],
3094
+ [:array,
3095
+ [:vcall, :d], [:vcall, :e],
3096
+ [:vcall, :f], [:vcall, :g]]],
3097
+ "ParseTree" => s(:masgn,
3098
+ s(:array,
3099
+ s(:lasgn, :a),
3100
+ s(:lasgn, :b),
3101
+ s(:splat, s(:lasgn, :c))),
3102
+ s(:array,
3103
+ s(:call, nil, :d, s(:arglist)),
3104
+ s(:call, nil, :e, s(:arglist)),
3105
+ s(:call, nil, :f, s(:arglist)),
3106
+ s(:call, nil, :g, s(:arglist)))))
3107
+
3108
+ add_tests("masgn_splat_rhs_1",
3109
+ "Ruby" => "a, b = *c",
3110
+ "RawParseTree" => [:masgn,
3111
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3112
+ nil,
3113
+ [:splat, [:vcall, :c]]],
3114
+ "ParseTree" => s(:masgn,
3115
+ s(:array,
3116
+ s(:lasgn, :a),
3117
+ s(:lasgn, :b)),
3118
+ s(:splat, s(:call, nil, :c, s(:arglist)))))
3119
+
3120
+ add_tests("masgn_splat_rhs_n",
3121
+ "Ruby" => "a, b = c, d, *e",
3122
+ "RawParseTree" => [:masgn,
3123
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3124
+ nil,
3125
+ [:argscat,
3126
+ [:array, [:vcall, :c], [:vcall, :d]],
3127
+ [:vcall, :e]]],
3128
+ "ParseTree" => s(:masgn,
3129
+ s(:array,
3130
+ s(:lasgn, :a),
3131
+ s(:lasgn, :b)),
3132
+ s(:array,
3133
+ s(:call, nil, :c, s(:arglist)),
3134
+ s(:call, nil, :d, s(:arglist)),
3135
+ s(:splat, s(:call, nil, :e, s(:arglist))))))
3136
+
3137
+ add_tests("masgn_splat_no_name_to_ary",
3138
+ "Ruby" => "a, b, * = c",
3139
+ "RawParseTree" => [:masgn,
3140
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3141
+ [:splat],
3142
+ [:to_ary, [:vcall, :c]]],
3143
+ "ParseTree" => s(:masgn,
3144
+ s(:array,
3145
+ s(:lasgn, :a),
3146
+ s(:lasgn, :b),
3147
+ s(:splat)),
3148
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))))
3149
+
3150
+ add_tests("masgn_splat_no_name_trailing",
3151
+ "Ruby" => "a, b, = c",
3152
+ "RawParseTree" => [:masgn,
3153
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
3154
+ [:to_ary, [:vcall, :c]]],
3155
+ "ParseTree" => s(:masgn,
3156
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
3157
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))),
3158
+ "Ruby2Ruby" => "a, b = c") # TODO: check this is right
3159
+
3160
+ add_tests("masgn_splat_to_ary",
3161
+ "Ruby" => "a, b, *c = d",
3162
+ "RawParseTree" => [:masgn,
3163
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3164
+ [:lasgn, :c],
3165
+ [:to_ary, [:vcall, :d]]],
3166
+ "ParseTree" => s(:masgn,
3167
+ s(:array,
3168
+ s(:lasgn, :a),
3169
+ s(:lasgn, :b),
3170
+ s(:splat, s(:lasgn, :c))),
3171
+ s(:to_ary, s(:call, nil, :d, s(:arglist)))))
3172
+
3173
+ add_tests("masgn_splat_to_ary2",
3174
+ "Ruby" => "a, b, *c = d.e(\"f\")",
3175
+ "RawParseTree" => [:masgn,
3176
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3177
+ [:lasgn, :c],
3178
+ [:to_ary,
3179
+ [:call, [:vcall, :d], :e,
3180
+ [:array, [:str, 'f']]]]],
3181
+ "ParseTree" => s(:masgn,
3182
+ s(:array,
3183
+ s(:lasgn, :a),
3184
+ s(:lasgn, :b),
3185
+ s(:splat, s(:lasgn, :c))),
3186
+ s(:to_ary,
3187
+ s(:call,
3188
+ s(:call, nil, :d, s(:arglist)),
3189
+ :e,
3190
+ s(:arglist, s(:str, 'f'))))))
3191
+
3192
+ add_tests("match",
3193
+ "Ruby" => "1 if /x/",
3194
+ "RawParseTree" => [:if, [:match, [:lit, /x/]], [:lit, 1], nil],
3195
+ "ParseTree" => s(:if, s(:match, s(:lit, /x/)), s(:lit, 1), nil))
3196
+
3197
+ add_tests("match2",
3198
+ "Ruby" => "/x/ =~ \"blah\"",
3199
+ "RawParseTree" => [:match2, [:lit, /x/], [:str, "blah"]],
3200
+ "ParseTree" => s(:match2, s(:lit, /x/), s(:str, "blah")))
3201
+
3202
+ add_tests("match3",
3203
+ "Ruby" => "\"blah\" =~ /x/",
3204
+ "RawParseTree" => [:match3, [:lit, /x/], [:str, "blah"]],
3205
+ "ParseTree" => s(:match3, s(:lit, /x/), s(:str, "blah")))
3206
+
3207
+ add_tests("module",
3208
+ "Ruby" => "module X\n def y\n # do nothing\n end\nend",
3209
+ "RawParseTree" => [:module, :X,
3210
+ [:scope,
3211
+ [:defn, :y,
3212
+ [:scope, [:block, [:args], [:nil]]]]]],
3213
+ "ParseTree" => s(:module, :X,
3214
+ s(:scope,
3215
+ s(:defn, :y,
3216
+ s(:args),
3217
+ s(:scope, s(:block, s(:nil)))))))
3218
+
3219
+ add_tests("module_scoped",
3220
+ "Ruby" => "module X::Y\n c\nend",
3221
+ "RawParseTree" => [:module, [:colon2, [:const, :X], :Y],
3222
+ [:scope, [:vcall, :c]]],
3223
+ "ParseTree" => s(:module, s(:colon2, s(:const, :X), :Y),
3224
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
3225
+
3226
+ add_tests("module_scoped3",
3227
+ "Ruby" => "module ::Y\n c\nend",
3228
+ "RawParseTree" => [:module, [:colon3, :Y], [:scope, [:vcall, :c]]],
3229
+ "ParseTree" => s(:module,
3230
+ s(:colon3, :Y),
3231
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
3232
+
3233
+ add_tests("next",
3234
+ "Ruby" => "loop { next if false }",
3235
+ "RawParseTree" => [:iter,
3236
+ [:fcall, :loop],
3237
+ nil,
3238
+ [:if, [:false], [:next], nil]],
3239
+ "ParseTree" => s(:iter,
3240
+ s(:call, nil, :loop, s(:arglist)),
3241
+ nil,
3242
+ s(:if, s(:false), s(:next), nil)))
3243
+
3244
+ add_tests("next_arg",
3245
+ "Ruby" => "loop { next 42 if false }",
3246
+ "RawParseTree" => [:iter,
3247
+ [:fcall, :loop],
3248
+ nil,
3249
+ [:if, [:false], [:next, [:lit, 42]], nil]],
3250
+ "ParseTree" => s(:iter,
3251
+ s(:call, nil, :loop, s(:arglist)),
3252
+ nil,
3253
+ s(:if, s(:false), s(:next, s(:lit, 42)), nil)))
3254
+
3255
+ add_tests("not",
3256
+ "Ruby" => "(not true)",
3257
+ "RawParseTree" => [:not, [:true]],
3258
+ "ParseTree" => s(:not, s(:true)))
3259
+
3260
+ add_tests("nth_ref",
3261
+ "Ruby" => "$1",
3262
+ "RawParseTree" => [:nth_ref, 1],
3263
+ "ParseTree" => s(:nth_ref, 1))
3264
+
3265
+ add_tests("op_asgn1",
3266
+ "Ruby" => "b = []\nb[1] ||= 10\nb[2] &&= 11\nb[3] += 12\n",
3267
+ "RawParseTree" => [:block,
3268
+ [:lasgn, :b, [:zarray]],
3269
+ [:op_asgn1, [:lvar, :b],
3270
+ [:array, [:lit, 1]], :"||", [:lit, 10]],
3271
+ [:op_asgn1, [:lvar, :b],
3272
+ [:array, [:lit, 2]], :"&&", [:lit, 11]],
3273
+ [:op_asgn1, [:lvar, :b],
3274
+ [:array, [:lit, 3]], :+, [:lit, 12]]],
3275
+ "ParseTree" => s(:block,
3276
+ s(:lasgn, :b, s(:array)),
3277
+ s(:op_asgn1, s(:lvar, :b),
3278
+ s(:arglist, s(:lit, 1)), :"||", s(:lit, 10)),
3279
+ s(:op_asgn1, s(:lvar, :b),
3280
+ s(:arglist, s(:lit, 2)), :"&&", s(:lit, 11)),
3281
+ s(:op_asgn1, s(:lvar, :b),
3282
+ s(:arglist, s(:lit, 3)), :+, s(:lit, 12))))
3283
+
3284
+ add_tests("op_asgn1_ivar",
3285
+ "Ruby" => "@b = []\n@b[1] ||= 10\n@b[2] &&= 11\n@b[3] += 12\n",
3286
+ "RawParseTree" => [:block,
3287
+ [:iasgn, :@b, [:zarray]],
3288
+ [:op_asgn1, [:ivar, :@b],
3289
+ [:array, [:lit, 1]], :"||", [:lit, 10]],
3290
+ [:op_asgn1, [:ivar, :@b],
3291
+ [:array, [:lit, 2]], :"&&", [:lit, 11]],
3292
+ [:op_asgn1, [:ivar, :@b],
3293
+ [:array, [:lit, 3]], :+, [:lit, 12]]],
3294
+ "ParseTree" => s(:block,
3295
+ s(:iasgn, :@b, s(:array)),
3296
+ s(:op_asgn1, s(:ivar, :@b),
3297
+ s(:arglist, s(:lit, 1)), :"||", s(:lit, 10)),
3298
+ s(:op_asgn1, s(:ivar, :@b),
3299
+ s(:arglist, s(:lit, 2)), :"&&", s(:lit, 11)),
3300
+ s(:op_asgn1, s(:ivar, :@b),
3301
+ s(:arglist, s(:lit, 3)), :+, s(:lit, 12))))
3302
+
3303
+ add_tests("op_asgn2",
3304
+ "Ruby" => "s = Struct.new(:var)\nc = s.new(nil)\nc.var ||= 20\nc.var &&= 21\nc.var += 22\nc.d.e.f ||= 42\n",
3305
+ "RawParseTree" => [:block,
3306
+ [:lasgn, :s,
3307
+ [:call, [:const, :Struct],
3308
+ :new, [:array, [:lit, :var]]]],
3309
+ [:lasgn, :c,
3310
+ [:call, [:lvar, :s], :new, [:array, [:nil]]]],
3311
+
3312
+ [:op_asgn2, [:lvar, :c], :var=, :"||",
3313
+ [:lit, 20]],
3314
+ [:op_asgn2, [:lvar, :c], :var=, :"&&",
3315
+ [:lit, 21]],
3316
+ [:op_asgn2, [:lvar, :c], :var=, :+, [:lit, 22]],
3317
+
3318
+ [:op_asgn2,
3319
+ [:call,
3320
+ [:call, [:lvar, :c], :d], :e], :f=, :"||",
3321
+ [:lit, 42]]],
3322
+ "ParseTree" => s(:block,
3323
+ s(:lasgn, :s,
3324
+ s(:call, s(:const, :Struct),
3325
+ :new, s(:arglist, s(:lit, :var)))),
3326
+ s(:lasgn, :c,
3327
+ s(:call, s(:lvar, :s),
3328
+ :new, s(:arglist, s(:nil)))),
3329
+ s(:op_asgn2, s(:lvar, :c),
3330
+ :var=, :"||", s(:lit, 20)),
3331
+ s(:op_asgn2, s(:lvar, :c),
3332
+ :var=, :"&&", s(:lit, 21)),
3333
+ s(:op_asgn2, s(:lvar, :c),
3334
+ :var=, :+, s(:lit, 22)),
3335
+ s(:op_asgn2,
3336
+ s(:call,
3337
+ s(:call, s(:lvar, :c), :d, s(:arglist)),
3338
+ :e, s(:arglist)),
3339
+ :f=, :"||", s(:lit, 42))))
3340
+
3341
+ add_tests("op_asgn2_self",
3342
+ "Ruby" => "self.Bag ||= Bag.new",
3343
+ "RawParseTree" => [:op_asgn2, [:self], :"Bag=", :"||",
3344
+ [:call, [:const, :Bag], :new]],
3345
+ "ParseTree" => s(:op_asgn2, s(:self), :"Bag=", :"||",
3346
+ s(:call, s(:const, :Bag), :new, s(:arglist))))
3347
+
3348
+ add_tests("op_asgn_and",
3349
+ "Ruby" => "a = 0\na &&= 2\n",
3350
+ "RawParseTree" => [:block,
3351
+ [:lasgn, :a, [:lit, 0]],
3352
+ [:op_asgn_and,
3353
+ [:lvar, :a], [:lasgn, :a, [:lit, 2]]]],
3354
+ "ParseTree" => s(:block,
3355
+ s(:lasgn, :a, s(:lit, 0)),
3356
+ s(:op_asgn_and,
3357
+ s(:lvar, :a), s(:lasgn, :a, s(:lit, 2)))))
3358
+
3359
+ add_tests("op_asgn_and_ivar2",
3360
+ "Ruby" => "@fetcher &&= new(Gem.configuration[:http_proxy])",
3361
+ "RawParseTree" => [:op_asgn_and,
3362
+ [:ivar, :@fetcher],
3363
+ [:iasgn,
3364
+ :@fetcher,
3365
+ [:fcall,
3366
+ :new,
3367
+ [:array,
3368
+ [:call,
3369
+ [:call, [:const, :Gem], :configuration],
3370
+ :[],
3371
+ [:array, [:lit, :http_proxy]]]]]]],
3372
+ "ParseTree" => s(:op_asgn_and,
3373
+ s(:ivar, :@fetcher),
3374
+ s(:iasgn,
3375
+ :@fetcher,
3376
+ s(:call, nil,
3377
+ :new,
3378
+ s(:arglist,
3379
+ s(:call,
3380
+ s(:call, s(:const, :Gem),
3381
+ :configuration,
3382
+ s(:arglist)),
3383
+ :[],
3384
+ s(:arglist, s(:lit, :http_proxy))))))))
3385
+
3386
+ add_tests("op_asgn_or",
3387
+ "Ruby" => "a = 0\na ||= 1\n",
3388
+ "RawParseTree" => [:block,
3389
+ [:lasgn, :a, [:lit, 0]],
3390
+ [:op_asgn_or,
3391
+ [:lvar, :a], [:lasgn, :a, [:lit, 1]]]],
3392
+ "ParseTree" => s(:block,
3393
+ s(:lasgn, :a, s(:lit, 0)),
3394
+ s(:op_asgn_or,
3395
+ s(:lvar, :a), s(:lasgn, :a, s(:lit, 1)))))
3396
+
3397
+ add_tests("op_asgn_or_block",
3398
+ "Ruby" => "a ||= begin\n b\n rescue\n c\n end",
3399
+ "RawParseTree" => [:op_asgn_or,
3400
+ [:lvar, :a],
3401
+ [:lasgn, :a,
3402
+ [:rescue,
3403
+ [:vcall, :b],
3404
+ [:resbody, nil, [:vcall, :c]]]]],
3405
+ "ParseTree" => s(:op_asgn_or,
3406
+ s(:lvar, :a),
3407
+ s(:lasgn, :a,
3408
+ s(:rescue,
3409
+ s(:call, nil, :b, s(:arglist)),
3410
+ s(:resbody, s(:array),
3411
+ s(:call, nil, :c, s(:arglist)))))),
3412
+ "Ruby2Ruby" => "a ||= b rescue c")
3413
+
3414
+ add_tests("op_asgn_or_ivar",
3415
+ "Ruby" => "@v ||= { }",
3416
+ "RawParseTree" => [:op_asgn_or,
3417
+ [:ivar, :@v],
3418
+ [:iasgn, :@v, [:hash]]],
3419
+ "ParseTree" => s(:op_asgn_or,
3420
+ s(:ivar, :@v),
3421
+ s(:iasgn, :@v, s(:hash))))
3422
+
3423
+ add_tests("op_asgn_or_ivar2",
3424
+ "Ruby" => "@fetcher ||= new(Gem.configuration[:http_proxy])",
3425
+ "RawParseTree" => [:op_asgn_or,
3426
+ [:ivar, :@fetcher],
3427
+ [:iasgn,
3428
+ :@fetcher,
3429
+ [:fcall,
3430
+ :new,
3431
+ [:array,
3432
+ [:call,
3433
+ [:call, [:const, :Gem], :configuration],
3434
+ :[],
3435
+ [:array, [:lit, :http_proxy]]]]]]],
3436
+ "ParseTree" => s(:op_asgn_or,
3437
+ s(:ivar, :@fetcher),
3438
+ s(:iasgn,
3439
+ :@fetcher,
3440
+ s(:call, nil, :new,
3441
+ s(:arglist,
3442
+ s(:call,
3443
+ s(:call, s(:const, :Gem),
3444
+ :configuration,
3445
+ s(:arglist)),
3446
+ :[],
3447
+ s(:arglist, s(:lit, :http_proxy))))))))
3448
+
3449
+ add_tests("or",
3450
+ "Ruby" => "(a or b)",
3451
+ "RawParseTree" => [:or, [:vcall, :a], [:vcall, :b]],
3452
+ "ParseTree" => s(:or,
3453
+ s(:call, nil, :a, s(:arglist)),
3454
+ s(:call, nil, :b, s(:arglist))))
3455
+
3456
+ add_tests("or_big",
3457
+ "Ruby" => "((a or b) or (c and d))",
3458
+ "RawParseTree" => [:or,
3459
+ [:or, [:vcall, :a], [:vcall, :b]],
3460
+ [:and, [:vcall, :c], [:vcall, :d]]],
3461
+ "ParseTree" => s(:or,
3462
+ s(:or,
3463
+ s(:call, nil, :a, s(:arglist)),
3464
+ s(:call, nil, :b, s(:arglist))),
3465
+ s(:and,
3466
+ s(:call, nil, :c, s(:arglist)),
3467
+ s(:call, nil, :d, s(:arglist)))))
3468
+
3469
+ add_tests("or_big2",
3470
+ "Ruby" => "((a || b) || (c && d))",
3471
+ "RawParseTree" => [:or,
3472
+ [:or, [:vcall, :a], [:vcall, :b]],
3473
+ [:and, [:vcall, :c], [:vcall, :d]]],
3474
+ "ParseTree" => s(:or,
3475
+ s(:or,
3476
+ s(:call, nil, :a, s(:arglist)),
3477
+ s(:call, nil, :b, s(:arglist))),
3478
+ s(:and,
3479
+ s(:call, nil, :c, s(:arglist)),
3480
+ s(:call, nil, :d, s(:arglist)))),
3481
+ "Ruby2Ruby" => "((a or b) or (c and d))")
3482
+
3483
+ add_tests("parse_floats_as_args",
3484
+ "Ruby" => "def x(a=0.0,b=0.0)\n a+b\nend",
3485
+ "RawParseTree" => [:defn, :x,
3486
+ [:scope,
3487
+ [:block,
3488
+ [:args, :a, :b,
3489
+ [:block,
3490
+ [:lasgn, :a, [:lit, 0.0]],
3491
+ [:lasgn, :b, [:lit, 0.0]]]],
3492
+ [:call, [:lvar, :a], :+,
3493
+ [:array, [:lvar, :b]]]]]],
3494
+ "ParseTree" => s(:defn, :x,
3495
+ s(:args, :a, :b,
3496
+ s(:block,
3497
+ s(:lasgn, :a, s(:lit, 0.0)),
3498
+ s(:lasgn, :b, s(:lit, 0.0)))),
3499
+ s(:scope,
3500
+ s(:block,
3501
+ s(:call, s(:lvar, :a), :+,
3502
+ s(:arglist, s(:lvar, :b)))))),
3503
+ "Ruby2Ruby" => "def x(a = 0.0, b = 0.0)\n (a + b)\nend")
3504
+
3505
+ add_tests("postexe",
3506
+ "Ruby" => "END { 1 }",
3507
+ "RawParseTree" => [:iter, [:postexe], nil, [:lit, 1]],
3508
+ "ParseTree" => s(:iter, s(:postexe), nil, s(:lit, 1)))
3509
+
3510
+ add_tests("proc_args_0",
3511
+ "Ruby" => "proc { || (x + 1) }",
3512
+ "RawParseTree" => [:iter,
3513
+ [:fcall, :proc],
3514
+ 0,
3515
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
3516
+ "ParseTree" => s(:iter,
3517
+ s(:call, nil, :proc, s(:arglist)),
3518
+ 0,
3519
+ s(:call,
3520
+ s(:call, nil, :x, s(:arglist)),
3521
+ :+,
3522
+ s(:arglist, s(:lit, 1)))))
3523
+
3524
+ add_tests("proc_args_1",
3525
+ "Ruby" => "proc { |x| (x + 1) }",
3526
+ "RawParseTree" => [:iter,
3527
+ [:fcall, :proc],
3528
+ [:dasgn_curr, :x],
3529
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]],
3530
+ "ParseTree" => s(:iter,
3531
+ s(:call, nil, :proc, s(:arglist)),
3532
+ s(:lasgn, :x),
3533
+ s(:call, s(:lvar, :x), :+,
3534
+ s(:arglist, s(:lit, 1)))))
3535
+
3536
+ add_tests("proc_args_2",
3537
+ "Ruby" => "proc { |x, y| (x + y) }",
3538
+ "RawParseTree" => [:iter,
3539
+ [:fcall, :proc],
3540
+ [:masgn, [:array,
3541
+ [:dasgn_curr, :x],
3542
+ [:dasgn_curr, :y]], nil, nil],
3543
+ [:call, [:dvar, :x], :+, [:array, [:dvar, :y]]]],
3544
+ "ParseTree" => s(:iter,
3545
+ s(:call, nil, :proc, s(:arglist)),
3546
+ s(:masgn,
3547
+ s(:array,
3548
+ s(:lasgn, :x),
3549
+ s(:lasgn, :y))),
3550
+ s(:call, s(:lvar, :x), :+,
3551
+ s(:arglist, s(:lvar, :y)))))
3552
+
3553
+ add_tests("proc_args_no",
3554
+ "Ruby" => "proc { (x + 1) }",
3555
+ "RawParseTree" => [:iter,
3556
+ [:fcall, :proc],
3557
+ nil,
3558
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
3559
+ "ParseTree" => s(:iter,
3560
+ s(:call, nil, :proc, s(:arglist)),
3561
+ nil,
3562
+ s(:call, s(:call, nil, :x, s(:arglist)),
3563
+ :+, s(:arglist, s(:lit, 1)))))
3564
+
3565
+ add_tests("redo",
3566
+ "Ruby" => "loop { redo if false }",
3567
+ "RawParseTree" => [:iter,
3568
+ [:fcall, :loop], nil,
3569
+ [:if, [:false], [:redo], nil]],
3570
+ "ParseTree" => s(:iter,
3571
+ s(:call, nil, :loop, s(:arglist)),
3572
+ nil,
3573
+ s(:if, s(:false), s(:redo), nil)))
3574
+
3575
+ # TODO: need a resbody w/ multiple classes and a splat
3576
+
3577
+ add_tests("rescue",
3578
+ "Ruby" => "blah rescue nil",
3579
+ "RawParseTree" => [:rescue,
3580
+ [:vcall, :blah], [:resbody, nil, [:nil]]],
3581
+ "ParseTree" => s(:rescue,
3582
+ s(:call, nil, :blah, s(:arglist)),
3583
+ s(:resbody, s(:array), s(:nil))))
3584
+
3585
+ add_tests("rescue_block_body",
3586
+ "Ruby" => "begin\n a\nrescue => e\n c\n d\nend",
3587
+ "RawParseTree" => [:rescue,
3588
+ [:vcall, :a],
3589
+ [:resbody, nil,
3590
+ [:block,
3591
+ [:lasgn, :e, [:gvar, :$!]],
3592
+ [:vcall, :c],
3593
+ [:vcall, :d]]]],
3594
+ "ParseTree" => s(:rescue,
3595
+ s(:call, nil, :a, s(:arglist)),
3596
+ s(:resbody,
3597
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3598
+ s(:block,
3599
+ s(:call, nil, :c, s(:arglist)),
3600
+ s(:call, nil, :d, s(:arglist))))))
3601
+
3602
+ add_tests("rescue_block_body_3",
3603
+ "Ruby" => "begin\n a\nrescue A\n b\nrescue B\n c\nrescue C\n d\nend",
3604
+ "RawParseTree" => [:rescue,
3605
+ [:vcall, :a],
3606
+ [:resbody, [:array, [:const, :A]],
3607
+ [:vcall, :b],
3608
+ [:resbody, [:array, [:const, :B]],
3609
+ [:vcall, :c],
3610
+ [:resbody, [:array, [:const, :C]],
3611
+ [:vcall, :d]]]]],
3612
+ "ParseTree" => s(:rescue,
3613
+ s(:call, nil, :a, s(:arglist)),
3614
+ s(:resbody, s(:array, s(:const, :A)),
3615
+ s(:call, nil, :b, s(:arglist))),
3616
+ s(:resbody, s(:array, s(:const, :B)),
3617
+ s(:call, nil, :c, s(:arglist))),
3618
+ s(:resbody, s(:array, s(:const, :C)),
3619
+ s(:call, nil, :d, s(:arglist)))))
3620
+
3621
+ add_tests("rescue_block_body_ivar",
3622
+ "Ruby" => "begin\n a\nrescue => @e\n c\n d\nend",
3623
+ "RawParseTree" => [:rescue,
3624
+ [:vcall, :a],
3625
+ [:resbody, nil,
3626
+ [:block,
3627
+ [:iasgn, :@e, [:gvar, :$!]],
3628
+ [:vcall, :c],
3629
+ [:vcall, :d]]]],
3630
+ "ParseTree" => s(:rescue,
3631
+ s(:call, nil, :a, s(:arglist)),
3632
+ s(:resbody,
3633
+ s(:array, s(:iasgn, :@e, s(:gvar, :$!))),
3634
+ s(:block,
3635
+ s(:call, nil, :c, s(:arglist)),
3636
+ s(:call, nil, :d, s(:arglist))))))
3637
+
3638
+ add_tests("rescue_block_nada",
3639
+ "Ruby" => "begin\n blah\nrescue\n # do nothing\nend",
3640
+ "RawParseTree" => [:rescue, [:vcall, :blah], [:resbody, nil]],
3641
+ "ParseTree" => s(:rescue,
3642
+ s(:call, nil, :blah, s(:arglist)),
3643
+ s(:resbody, s(:array), nil)))
3644
+
3645
+ add_tests("rescue_exceptions",
3646
+ "Ruby" => "begin\n blah\nrescue RuntimeError => r\n # do nothing\nend",
3647
+ "RawParseTree" => [:rescue,
3648
+ [:vcall, :blah],
3649
+ [:resbody,
3650
+ [:array, [:const, :RuntimeError]],
3651
+ [:lasgn, :r, [:gvar, :$!]]]],
3652
+ "ParseTree" => s(:rescue,
3653
+ s(:call, nil, :blah, s(:arglist)),
3654
+ s(:resbody,
3655
+ s(:array,
3656
+ s(:const, :RuntimeError),
3657
+ s(:lasgn, :r, s(:gvar, :$!))),
3658
+ nil)))
3659
+
3660
+
3661
+ add_tests("rescue_iasgn_var_empty",
3662
+ "Ruby" => "begin\n 1\nrescue => @e\n # do nothing\nend",
3663
+ "RawParseTree" => [:rescue,
3664
+ [:lit, 1],
3665
+ [:resbody, nil, [:iasgn, :@e, [:gvar, :$!]]]],
3666
+ "ParseTree" => s(:rescue,
3667
+ s(:lit, 1),
3668
+ s(:resbody,
3669
+ s(:array, s(:iasgn, :@e, s(:gvar, :$!))),
3670
+ nil)))
3671
+
3672
+ add_tests("rescue_lasgn",
3673
+ "Ruby" => "begin\n 1\nrescue\n var = 2\nend",
3674
+ "RawParseTree" => [:rescue,
3675
+ [:lit, 1],
3676
+ [:resbody, nil, [:lasgn, :var, [:lit, 2]]]],
3677
+ "ParseTree" => s(:rescue,
3678
+ s(:lit, 1),
3679
+ s(:resbody,
3680
+ s(:array),
3681
+ s(:lasgn, :var, s(:lit, 2)))),
3682
+ "Ruby2Ruby" => "1 rescue var = 2")
3683
+
3684
+ add_tests("rescue_lasgn_var",
3685
+ "Ruby" => "begin\n 1\nrescue => e\n var = 2\nend",
3686
+ "RawParseTree" => [:rescue,
3687
+ [:lit, 1],
3688
+ [:resbody, nil,
3689
+ [:block,
3690
+ [:lasgn, :e, [:gvar, :$!]],
3691
+ [:lasgn, :var, [:lit, 2]]]]],
3692
+ "ParseTree" => s(:rescue,
3693
+ s(:lit, 1),
3694
+ s(:resbody,
3695
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3696
+ s(:lasgn, :var, s(:lit, 2)))))
3697
+
3698
+ add_tests("rescue_lasgn_var_empty",
3699
+ "Ruby" => "begin\n 1\nrescue => e\n # do nothing\nend",
3700
+ "RawParseTree" => [:rescue,
3701
+ [:lit, 1],
3702
+ [:resbody, nil, [:lasgn, :e, [:gvar, :$!]]]],
3703
+ "ParseTree" => s(:rescue,
3704
+ s(:lit, 1),
3705
+ s(:resbody,
3706
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3707
+ nil)))
3708
+
3709
+ add_tests("retry",
3710
+ "Ruby" => "retry",
3711
+ "RawParseTree" => [:retry],
3712
+ "ParseTree" => s(:retry))
3713
+
3714
+ add_tests("return_0",
3715
+ "Ruby" => "return",
3716
+ "RawParseTree" => [:return],
3717
+ "ParseTree" => s(:return))
3718
+
3719
+ add_tests("return_1",
3720
+ "Ruby" => "return 1",
3721
+ "RawParseTree" => [:return, [:lit, 1]],
3722
+ "ParseTree" => s(:return, s(:lit, 1)))
3723
+
3724
+ add_tests("return_1_splatted",
3725
+ "Ruby" => "return *1",
3726
+ "RawParseTree" => [:return, [:svalue, [:splat, [:lit, 1]]]],
3727
+ "ParseTree" => s(:return, s(:svalue, s(:splat, s(:lit, 1)))))
3728
+
3729
+ add_tests("return_n",
3730
+ "Ruby" => "return 1, 2, 3",
3731
+ "RawParseTree" => [:return, [:array,
3732
+ [:lit, 1], [:lit, 2], [:lit, 3]]],
3733
+ "ParseTree" => s(:return, s(:array,
3734
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
3735
+ "Ruby2Ruby" => "return [1, 2, 3]")
3736
+
3737
+ add_tests("sclass",
3738
+ "Ruby" => "class << self\n 42\nend",
3739
+ "RawParseTree" => [:sclass, [:self], [:scope, [:lit, 42]]],
3740
+ "ParseTree" => s(:sclass, s(:self), s(:scope, s(:lit, 42))))
3741
+
3742
+ add_tests("sclass_trailing_class",
3743
+ "Ruby" => "class A\n class << self\n a\n end\n class B\n end\nend",
3744
+ "RawParseTree" => [:class, :A, nil,
3745
+ [:scope,
3746
+ [:block,
3747
+ [:sclass, [:self], [:scope, [:vcall, :a]]],
3748
+ [:class, :B, nil, [:scope]]]]],
3749
+ "ParseTree" => s(:class, :A, nil,
3750
+ s(:scope,
3751
+ s(:block,
3752
+ s(:sclass, s(:self),
3753
+ s(:scope,
3754
+ s(:call, nil, :a, s(:arglist)))),
3755
+ s(:class, :B, nil, s(:scope))))))
3756
+
3757
+ add_tests("splat",
3758
+ "Ruby" => "def x(*b)\n a(*b)\nend",
3759
+ "RawParseTree" => [:defn, :x,
3760
+ [:scope,
3761
+ [:block,
3762
+ [:args, :"*b"],
3763
+ [:fcall, :a, [:splat, [:lvar, :b]]]]]],
3764
+ "ParseTree" => s(:defn, :x,
3765
+ s(:args, :"*b"),
3766
+ s(:scope,
3767
+ s(:block,
3768
+ s(:call, nil, :a,
3769
+ s(:arglist, s(:splat, s(:lvar, :b))))))))
3770
+
3771
+ add_tests("splat_array",
3772
+ "Ruby" => "[*[1]]",
3773
+ "RawParseTree" => [:splat, [:array, [:lit, 1]]],
3774
+ "ParseTree" => s(:array, s(:splat, s(:array, s(:lit, 1)))))
3775
+
3776
+ add_tests("splat_break",
3777
+ "Ruby" => "break *[1]",
3778
+ "RawParseTree" => [:break, [:svalue, [:splat, [:array, [:lit, 1]]]]],
3779
+ "ParseTree" => s(:break, s(:svalue, s(:splat, s(:array, s(:lit, 1))))))
3780
+
3781
+ add_tests("splat_break_array",
3782
+ "Ruby" => "break [*[1]]",
3783
+ "RawParseTree" => [:break, [:splat, [:array, [:lit, 1]]]],
3784
+ "ParseTree" => s(:break, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3785
+
3786
+ add_tests("splat_fcall",
3787
+ "Ruby" => "meth(*[1])",
3788
+ "RawParseTree" => [:fcall, :meth,
3789
+ [:splat, [:array, [:lit, 1]]]],
3790
+ "ParseTree" => s(:call, nil, :meth,
3791
+ s(:arglist, s(:splat, s(:array, s(:lit, 1))))))
3792
+
3793
+ add_tests("splat_fcall_array",
3794
+ "Ruby" => "meth([*[1]])",
3795
+ "RawParseTree" => [:fcall, :meth,
3796
+ [:array, [:splat, [:array, [:lit, 1]]]]],
3797
+ "ParseTree" => s(:call, nil, :meth,
3798
+ s(:arglist,
3799
+ s(:array, s(:splat, s(:array, s(:lit, 1)))))))
3800
+
3801
+ add_tests("splat_lasgn",
3802
+ "Ruby" => "x = *[1]",
3803
+ "RawParseTree" => [:lasgn, :x, [:svalue, [:splat, [:array, [:lit, 1]]]]],
3804
+ "ParseTree" => s(:lasgn, :x, s(:svalue, s(:splat, s(:array, s(:lit, 1))))))
3805
+
3806
+ add_tests("splat_lasgn_array",
3807
+ "Ruby" => "x = [*[1]]",
3808
+ "RawParseTree" => [:lasgn, :x, [:splat, [:array, [:lit, 1]]]],
3809
+ "ParseTree" => s(:lasgn, :x, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3810
+
3811
+ add_tests("splat_lit_1",
3812
+ "Ruby" => "[*1]",
3813
+ "RawParseTree" => [:splat, [:lit, 1]], # UGH - damn MRI
3814
+ "ParseTree" => s(:array, s(:splat, s(:lit, 1))))
3815
+
3816
+ add_tests("splat_lit_n",
3817
+ "Ruby" => "[1, *2]",
3818
+ "RawParseTree" => [:argscat, [:array, [:lit, 1]], [:lit, 2]],
3819
+ "ParseTree" => s(:array, s(:lit, 1), s(:splat, s(:lit, 2))))
3820
+
3821
+ add_tests("splat_next",
3822
+ "Ruby" => "next *[1]",
3823
+ "RawParseTree" => [:next, [:svalue, [:splat, [:array, [:lit, 1]]]]],
3824
+ "ParseTree" => s(:next, s(:svalue, s(:splat, s(:array, s(:lit, 1))))))
3825
+
3826
+ add_tests("splat_next_array",
3827
+ "Ruby" => "next [*[1]]",
3828
+ "RawParseTree" => [:next, [:splat, [:array, [:lit, 1]]]],
3829
+ "ParseTree" => s(:next, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3830
+
3831
+ add_tests("splat_return",
3832
+ "Ruby" => "return *[1]",
3833
+ "RawParseTree" => [:return, [:svalue, [:splat, [:array, [:lit, 1]]]]],
3834
+ "ParseTree" => s(:return, s(:svalue, s(:splat, s(:array, s(:lit, 1))))))
3835
+
3836
+ add_tests("splat_return_array",
3837
+ "Ruby" => "return [*[1]]",
3838
+ "RawParseTree" => [:return, [:splat, [:array, [:lit, 1]]]],
3839
+ "ParseTree" => s(:return, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3840
+
3841
+ add_tests("splat_super",
3842
+ "Ruby" => "super(*[1])",
3843
+ "RawParseTree" => [:super, [:splat, [:array, [:lit, 1]]]],
3844
+ "ParseTree" => s(:super, s(:splat, s(:array, s(:lit, 1)))))
3845
+
3846
+ add_tests("splat_super_array",
3847
+ "Ruby" => "super([*[1]])",
3848
+ "RawParseTree" => [:super, [:array, [:splat, [:array, [:lit, 1]]]]],
3849
+ "ParseTree" => s(:super, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3850
+
3851
+ add_tests("splat_yield",
3852
+ "Ruby" => "yield(*[1])",
3853
+ "RawParseTree" => [:yield, [:splat, [:array, [:lit, 1]]]],
3854
+ "ParseTree" => s(:yield, s(:splat, s(:array, s(:lit, 1)))))
3855
+
3856
+ add_tests("splat_yield_array",
3857
+ "Ruby" => "yield([*[1]])",
3858
+ "RawParseTree" => [:yield, [:splat, [:array, [:lit, 1]]], true],
3859
+ "ParseTree" => s(:yield, s(:array, s(:splat, s(:array, s(:lit, 1))))))
3860
+
3861
+ add_tests("str",
3862
+ "Ruby" => '"x"',
3863
+ "RawParseTree" => [:str, "x"],
3864
+ "ParseTree" => s(:str, "x"))
3865
+
3866
+ add_tests("str_concat_newline", # FIX? make prettier? possible?
3867
+ "Ruby" => '"before" \\
3868
+ " after"',
3869
+ "RawParseTree" => [:str, "before after"],
3870
+ "ParseTree" => s(:str, "before after"),
3871
+ "Ruby2Ruby" => '"before after"')
3872
+
3873
+ add_tests("str_concat_space",
3874
+ "Ruby" => '"before" " after"',
3875
+ "RawParseTree" => [:str, "before after"],
3876
+ "ParseTree" => s(:str, "before after"),
3877
+ "Ruby2Ruby" => '"before after"')
3878
+
3879
+ add_tests("str_heredoc",
3880
+ "Ruby" => "<<'EOM'\n blah\nblah\nEOM",
3881
+ "RawParseTree" => [:str, " blah\nblah\n"],
3882
+ "ParseTree" => s(:str, " blah\nblah\n"),
3883
+ "Ruby2Ruby" => "\" blah\\nblah\\n\"")
3884
+
3885
+ add_tests("str_heredoc_call",
3886
+ "Ruby" => "<<'EOM'.strip\n blah\nblah\nEOM",
3887
+ "RawParseTree" => [:call, [:str, " blah\nblah\n"], :strip],
3888
+ "ParseTree" => s(:call, s(:str, " blah\nblah\n"),
3889
+ :strip, s(:arglist)),
3890
+ "Ruby2Ruby" => "\" blah\\nblah\\n\".strip")
3891
+
3892
+ add_tests("str_heredoc_double",
3893
+ "Ruby" => "a += <<-H1 + b + <<-H2\n first\nH1\n second\nH2",
3894
+ "RawParseTree" => [:lasgn, :a,
3895
+ [:call,
3896
+ [:lvar, :a],
3897
+ :+,
3898
+ [:array,
3899
+ [:call,
3900
+ [:call, [:str, " first\n"], :+,
3901
+ [:array, [:vcall, :b]]],
3902
+ :+,
3903
+ [:array, [:str, " second\n"]]]]]],
3904
+ "ParseTree" => s(:lasgn, :a,
3905
+ s(:call,
3906
+ s(:lvar, :a),
3907
+ :+,
3908
+ s(:arglist,
3909
+ s(:call,
3910
+ s(:call, s(:str, " first\n"), :+,
3911
+ s(:arglist,
3912
+ s(:call, nil, :b, s(:arglist)))),
3913
+ :+,
3914
+ s(:arglist, s(:str, " second\n")))))),
3915
+ "Ruby2Ruby" => "a = (a + ((\" first\\n\" + b) + \" second\\n\"))")
3916
+
3917
+ add_tests("str_heredoc_empty", # yes... tarded
3918
+ "Ruby" => "<<'EOM'\nEOM",
3919
+ "RawParseTree" => [:str, ""],
3920
+ "ParseTree" => s(:str, ""),
3921
+ "Ruby2Ruby" => '""')
3922
+
3923
+ add_tests("str_heredoc_indent",
3924
+ "Ruby" => "<<-EOM\n blah\nblah\n\n EOM",
3925
+ "RawParseTree" => [:str, " blah\nblah\n\n"],
3926
+ "ParseTree" => s(:str, " blah\nblah\n\n"),
3927
+ "Ruby2Ruby" => "\" blah\\nblah\\n\\n\"")
3928
+
3929
+ add_tests("str_interp_file",
3930
+ "Ruby" => '"file = #{__FILE__}\n"',
3931
+ "RawParseTree" => [:str, "file = (string)\n"],
3932
+ "ParseTree" => s(:str, "file = (string)\n"),
3933
+ "Ruby2Ruby" => '"file = (string)\\n"')
3934
+
3935
+ add_tests("structure_extra_block_for_dvar_scoping",
3936
+ "Ruby" => "a.b do |c, d|\n unless e.f(c) then\n g = false\n d.h { |x, i| g = true }\n end\nend",
3937
+ "RawParseTree" => [:iter,
3938
+ [:call, [:vcall, :a], :b],
3939
+ [:masgn, [:array,
3940
+ [:dasgn_curr, :c],
3941
+ [:dasgn_curr, :d]], nil, nil],
3942
+ [:if,
3943
+ [:call, [:vcall, :e], :f,
3944
+ [:array, [:dvar, :c]]],
3945
+ nil,
3946
+ [:block,
3947
+ [:dasgn_curr, :g, [:false]],
3948
+ [:iter,
3949
+ [:call, [:dvar, :d], :h],
3950
+ [:masgn, [:array,
3951
+ [:dasgn_curr, :x],
3952
+ [:dasgn_curr, :i]], nil, nil],
3953
+ [:dasgn, :g, [:true]]]]]],
3954
+ "ParseTree" => s(:iter,
3955
+ s(:call,
3956
+ s(:call, nil, :a, s(:arglist)),
3957
+ :b, s(:arglist)),
3958
+ s(:masgn, s(:array,
3959
+ s(:lasgn, :c),
3960
+ s(:lasgn, :d))),
3961
+ s(:if,
3962
+ s(:call, s(:call, nil, :e, s(:arglist)), :f,
3963
+ s(:arglist, s(:lvar, :c))),
3964
+ nil,
3965
+ s(:block,
3966
+ s(:lasgn, :g, s(:false)),
3967
+ s(:iter,
3968
+ s(:call, s(:lvar, :d), :h, s(:arglist)),
3969
+ s(:masgn, s(:array,
3970
+ s(:lasgn, :x),
3971
+ s(:lasgn, :i))),
3972
+ s(:lasgn, :g, s(:true)))))))
3973
+
3974
+ add_tests("structure_remove_begin_1",
3975
+ "Ruby" => "a << begin\n b\n rescue\n c\n end",
3976
+ "RawParseTree" => [:call, [:vcall, :a], :<<,
3977
+ [:array, [:rescue, [:vcall, :b],
3978
+ [:resbody, nil, [:vcall, :c]]]]],
3979
+ "ParseTree" => s(:call, s(:call, nil, :a, s(:arglist)), :<<,
3980
+ s(:arglist,
3981
+ s(:rescue,
3982
+ s(:call, nil, :b, s(:arglist)),
3983
+ s(:resbody, s(:array),
3984
+ s(:call, nil, :c, s(:arglist)))))),
3985
+ "Ruby2Ruby" => "(a << b rescue c)")
3986
+
3987
+ add_tests("structure_remove_begin_2",
3988
+ "Ruby" => "a = if c\n begin\n b\n rescue\n nil\n end\n end\na",
3989
+ "RawParseTree" => [:block,
3990
+ [:lasgn,
3991
+ :a,
3992
+ [:if, [:vcall, :c],
3993
+ [:rescue,
3994
+ [:vcall, :b],
3995
+ [:resbody, nil, [:nil]]],
3996
+ nil]],
3997
+ [:lvar, :a]],
3998
+ "ParseTree" => s(:block,
3999
+ s(:lasgn,
4000
+ :a,
4001
+ s(:if, s(:call, nil, :c, s(:arglist)),
4002
+ s(:rescue, s(:call, nil, :b, s(:arglist)),
4003
+ s(:resbody,
4004
+ s(:array), s(:nil))),
4005
+ nil)),
4006
+ s(:lvar, :a)),
4007
+ "Ruby2Ruby" => "a = b rescue nil if c\na\n") # OMG that's awesome
4008
+
4009
+ add_tests("structure_unused_literal_wwtt",
4010
+ "Ruby" => "\"prevent the above from infecting rdoc\"\n\nmodule Graffle\nend",
4011
+ "RawParseTree" => [:module, :Graffle, [:scope]],
4012
+ "ParseTree" => s(:module, :Graffle, s(:scope)),
4013
+ "Ruby2Ruby" => "module Graffle\nend")
4014
+
4015
+ add_tests("super_0",
4016
+ "Ruby" => "def x\n super()\nend",
4017
+ "RawParseTree" => [:defn, :x,
4018
+ [:scope,
4019
+ [:block, [:args], [:super]]]],
4020
+ "ParseTree" => s(:defn, :x,
4021
+ s(:args),
4022
+ s(:scope, s(:block, s(:super)))))
4023
+
4024
+ add_tests("super_1",
4025
+ "Ruby" => "def x\n super(4)\nend",
4026
+ "RawParseTree" => [:defn, :x,
4027
+ [:scope,
4028
+ [:block,
4029
+ [:args],
4030
+ [:super, [:array, [:lit, 4]]]]]],
4031
+ "ParseTree" => s(:defn, :x,
4032
+ s(:args),
4033
+ s(:scope,
4034
+ s(:block,
4035
+ s(:super, s(:lit, 4))))))
4036
+
4037
+ add_tests("super_1_array",
4038
+ "Ruby" => "def x\n super([24, 42])\nend",
4039
+ "RawParseTree" => [:defn, :x,
4040
+ [:scope,
4041
+ [:block,
4042
+ [:args],
4043
+ [:super,
4044
+ [:array,
4045
+ [:array, [:lit, 24], [:lit, 42]]]]]]],
4046
+ "ParseTree" => s(:defn, :x,
4047
+ s(:args),
4048
+ s(:scope,
4049
+ s(:block,
4050
+ s(:super, s(:array,
4051
+ s(:lit, 24),
4052
+ s(:lit, 42)))))))
4053
+
4054
+ add_tests("super_block_pass",
4055
+ "Ruby" => "super(a, &b)",
4056
+ "RawParseTree" => [:block_pass,
4057
+ [:vcall, :b], [:super, [:array, [:vcall, :a]]]],
4058
+ "ParseTree" => s(:super,
4059
+ s(:call, nil, :a, s(:arglist)),
4060
+ s(:block_pass,
4061
+ s(:call, nil, :b, s(:arglist)))))
4062
+
4063
+ add_tests("super_block_splat",
4064
+ "Ruby" => "super(a, *b)",
4065
+ "RawParseTree" => [:super,
4066
+ [:argscat,
4067
+ [:array, [:vcall, :a]],
4068
+ [:vcall, :b]]],
4069
+ "ParseTree" => s(:super,
4070
+ s(:call, nil, :a, s(:arglist)),
4071
+ s(:splat, s(:call, nil, :b, s(:arglist)))))
4072
+
4073
+ add_tests("super_n",
4074
+ "Ruby" => "def x\n super(24, 42)\nend",
4075
+ "RawParseTree" => [:defn, :x,
4076
+ [:scope,
4077
+ [:block,
4078
+ [:args],
4079
+ [:super, [:array, [:lit, 24], [:lit, 42]]]]]],
4080
+ "ParseTree" => s(:defn, :x,
4081
+ s(:args),
4082
+ s(:scope,
4083
+ s(:block,
4084
+ s(:super, s(:lit, 24), s(:lit, 42))))))
4085
+
4086
+ add_tests("svalue",
4087
+ "Ruby" => "a = *b",
4088
+ "RawParseTree" => [:lasgn, :a, [:svalue, [:splat, [:vcall, :b]]]],
4089
+ "ParseTree" => s(:lasgn, :a,
4090
+ s(:svalue,
4091
+ s(:splat, s(:call, nil, :b, s(:arglist))))))
4092
+
4093
+ add_tests("to_ary",
4094
+ "Ruby" => "a, b = c",
4095
+ "RawParseTree" => [:masgn,
4096
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
4097
+ [:to_ary, [:vcall, :c]]],
4098
+ "ParseTree" => s(:masgn,
4099
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
4100
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))))
4101
+
4102
+ add_tests("true",
4103
+ "Ruby" => "true",
4104
+ "RawParseTree" => [:true],
4105
+ "ParseTree" => s(:true))
4106
+
4107
+ add_tests("undef",
4108
+ "Ruby" => "undef :x",
4109
+ "RawParseTree" => [:undef, [:lit, :x]],
4110
+ "ParseTree" => s(:undef, s(:lit, :x)))
4111
+
4112
+ add_tests("undef_2",
4113
+ "Ruby" => "undef :x, :y",
4114
+ "RawParseTree" => [:block,
4115
+ [:undef, [:lit, :x]],
4116
+ [:undef, [:lit, :y]]],
4117
+ "ParseTree" => s(:block,
4118
+ s(:undef, s(:lit, :x)),
4119
+ s(:undef, s(:lit, :y))),
4120
+ "Ruby2Ruby" => "undef :x\nundef :y\n")
4121
+
4122
+ add_tests("undef_3",
4123
+ "Ruby" => "undef :x, :y, :z",
4124
+ "RawParseTree" => [:block,
4125
+ [:undef, [:lit, :x]],
4126
+ [:undef, [:lit, :y]],
4127
+ [:undef, [:lit, :z]]],
4128
+ "ParseTree" => s(:block,
4129
+ s(:undef, s(:lit, :x)),
4130
+ s(:undef, s(:lit, :y)),
4131
+ s(:undef, s(:lit, :z))),
4132
+ "Ruby2Ruby" => "undef :x\nundef :y\nundef :z\n")
4133
+
4134
+ add_tests("undef_block_1",
4135
+ "Ruby" => "f1\nundef :x\n", # TODO: don't like the extra return
4136
+ "RawParseTree" => [:block,
4137
+ [:vcall, :f1],
4138
+ [:undef, [:lit, :x]]],
4139
+ "ParseTree" => s(:block,
4140
+ s(:call, nil, :f1, s(:arglist)),
4141
+ s(:undef, s(:lit, :x))))
4142
+
4143
+ add_tests("undef_block_2",
4144
+ "Ruby" => "f1\nundef :x, :y",
4145
+ "RawParseTree" => [:block,
4146
+ [:vcall, :f1],
4147
+ [:block,
4148
+ [:undef, [:lit, :x]],
4149
+ [:undef, [:lit, :y]],
4150
+ ]],
4151
+ "ParseTree" => s(:block,
4152
+ s(:call, nil, :f1, s(:arglist)),
4153
+ s(:block,
4154
+ s(:undef, s(:lit, :x)),
4155
+ s(:undef, s(:lit, :y)))),
4156
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y)\n")
4157
+
4158
+ add_tests("undef_block_3",
4159
+ "Ruby" => "f1\nundef :x, :y, :z",
4160
+ "RawParseTree" => [:block,
4161
+ [:vcall, :f1],
4162
+ [:block,
4163
+ [:undef, [:lit, :x]],
4164
+ [:undef, [:lit, :y]],
4165
+ [:undef, [:lit, :z]],
4166
+ ]],
4167
+ "ParseTree" => s(:block,
4168
+ s(:call, nil, :f1, s(:arglist)),
4169
+ s(:block,
4170
+ s(:undef, s(:lit, :x)),
4171
+ s(:undef, s(:lit, :y)),
4172
+ s(:undef, s(:lit, :z)))),
4173
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y\nundef :z)\n")
4174
+
4175
+ add_tests("undef_block_3_post",
4176
+ "Ruby" => "undef :x, :y, :z\nf2",
4177
+ "RawParseTree" => [:block,
4178
+ [:undef, [:lit, :x]],
4179
+ [:undef, [:lit, :y]],
4180
+ [:undef, [:lit, :z]],
4181
+ [:vcall, :f2]],
4182
+ "ParseTree" => s(:block,
4183
+ s(:undef, s(:lit, :x)),
4184
+ s(:undef, s(:lit, :y)),
4185
+ s(:undef, s(:lit, :z)),
4186
+ s(:call, nil, :f2, s(:arglist))),
4187
+ "Ruby2Ruby" => "undef :x\nundef :y\nundef :z\nf2\n")
4188
+
4189
+ add_tests("undef_block_wtf",
4190
+ "Ruby" => "f1\nundef :x, :y, :z\nf2",
4191
+ "RawParseTree" => [:block,
4192
+ [:vcall, :f1],
4193
+ [:block,
4194
+ [:undef, [:lit, :x]],
4195
+ [:undef, [:lit, :y]],
4196
+ [:undef, [:lit, :z]]],
4197
+ [:vcall, :f2]],
4198
+ "ParseTree" => s(:block,
4199
+ s(:call, nil, :f1, s(:arglist)),
4200
+ s(:block,
4201
+ s(:undef, s(:lit, :x)),
4202
+ s(:undef, s(:lit, :y)),
4203
+ s(:undef, s(:lit, :z))),
4204
+ s(:call, nil, :f2, s(:arglist))),
4205
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y\nundef :z)\nf2\n")
4206
+
4207
+
4208
+ add_tests("unless_post",
4209
+ "Ruby" => "a unless b",
4210
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
4211
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
4212
+ s(:call, nil, :a, s(:arglist))))
4213
+
4214
+ add_tests("unless_post_not",
4215
+ "Ruby" => "a unless not b",
4216
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
4217
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
4218
+ s(:call, nil, :a, s(:arglist)), nil),
4219
+ "Ruby2Ruby" => "a if b")
4220
+
4221
+ add_tests("unless_pre",
4222
+ "Ruby" => "unless b then a end",
4223
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
4224
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
4225
+ s(:call, nil, :a, s(:arglist))),
4226
+ "Ruby2Ruby" => "a unless b")
4227
+
4228
+ add_tests("unless_pre_not",
4229
+ "Ruby" => "unless not b then a end",
4230
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
4231
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
4232
+ s(:call, nil, :a, s(:arglist)), nil),
4233
+ "Ruby2Ruby" => "a if b")
4234
+
4235
+ add_tests("until_post",
4236
+ "Ruby" => "begin\n (1 + 1)\nend until false",
4237
+ "RawParseTree" => [:until, [:false],
4238
+ [:call, [:lit, 1], :+,
4239
+ [:array, [:lit, 1]]], false],
4240
+ "ParseTree" => s(:until, s(:false),
4241
+ s(:call, s(:lit, 1), :+,
4242
+ s(:arglist, s(:lit, 1))), false))
4243
+
4244
+ add_tests("until_post_not",
4245
+ "Ruby" => "begin\n (1 + 1)\nend until not true",
4246
+ "RawParseTree" => [:while, [:true],
4247
+ [:call, [:lit, 1], :+,
4248
+ [:array, [:lit, 1]]], false],
4249
+ "ParseTree" => s(:while, s(:true),
4250
+ s(:call, s(:lit, 1), :+,
4251
+ s(:arglist, s(:lit, 1))), false),
4252
+ "Ruby2Ruby" => "begin\n (1 + 1)\nend while true")
4253
+
4254
+ add_tests("until_pre",
4255
+ "Ruby" => "until false do\n (1 + 1)\nend",
4256
+ "RawParseTree" => [:until, [:false],
4257
+ [:call, [:lit, 1], :+,
4258
+ [:array, [:lit, 1]]], true],
4259
+ "ParseTree" => s(:until, s(:false),
4260
+ s(:call, s(:lit, 1), :+,
4261
+ s(:arglist, s(:lit, 1))), true))
4262
+
4263
+ add_tests("until_pre_mod",
4264
+ "Ruby" => "(1 + 1) until false",
4265
+ "RawParseTree" => [:until, [:false],
4266
+ [:call, [:lit, 1], :+,
4267
+ [:array, [:lit, 1]]], true],
4268
+ "ParseTree" => s(:until, s(:false),
4269
+ s(:call, s(:lit, 1), :+,
4270
+ s(:arglist, s(:lit, 1))), true),
4271
+ "Ruby2Ruby" => "until false do\n (1 + 1)\nend")
4272
+
4273
+ add_tests("until_pre_not",
4274
+ "Ruby" => "until not true do\n (1 + 1)\nend",
4275
+ "RawParseTree" => [:while, [:true],
4276
+ [:call, [:lit, 1], :+,
4277
+ [:array, [:lit, 1]]], true],
4278
+ "ParseTree" => s(:while, s(:true),
4279
+ s(:call, s(:lit, 1), :+,
4280
+ s(:arglist, s(:lit, 1))), true),
4281
+ "Ruby2Ruby" => "while true do\n (1 + 1)\nend")
4282
+
4283
+ add_tests("until_pre_not_mod",
4284
+ "Ruby" => "(1 + 1) until not true",
4285
+ "RawParseTree" => [:while, [:true],
4286
+ [:call, [:lit, 1], :+,
4287
+ [:array, [:lit, 1]]], true],
4288
+ "ParseTree" => s(:while, s(:true),
4289
+ s(:call, s(:lit, 1), :+,
4290
+ s(:arglist, s(:lit, 1))), true),
4291
+ "Ruby2Ruby" => "while true do\n (1 + 1)\nend")
4292
+
4293
+ add_tests("valias",
4294
+ "Ruby" => "alias $y $x",
4295
+ "RawParseTree" => [:valias, :$y, :$x],
4296
+ "ParseTree" => s(:valias, :$y, :$x))
4297
+
4298
+ add_tests("vcall",
4299
+ "Ruby" => "method",
4300
+ "RawParseTree" => [:vcall, :method],
4301
+ "ParseTree" => s(:call, nil, :method, s(:arglist)))
4302
+
4303
+ add_tests("while_post",
4304
+ "Ruby" => "begin\n (1 + 1)\nend while false",
4305
+ "RawParseTree" => [:while, [:false],
4306
+ [:call, [:lit, 1], :+,
4307
+ [:array, [:lit, 1]]], false],
4308
+ "ParseTree" => s(:while, s(:false),
4309
+ s(:call, s(:lit, 1), :+,
4310
+ s(:arglist, s(:lit, 1))), false))
4311
+
4312
+ add_tests("while_post2",
4313
+ "Ruby" => "begin\n (1 + 2)\n (3 + 4)\nend while false",
4314
+ "RawParseTree" => [:while, [:false],
4315
+ [:block,
4316
+ [:call, [:lit, 1], :+, [:array, [:lit, 2]]],
4317
+ [:call, [:lit, 3], :+, [:array, [:lit, 4]]]],
4318
+ false],
4319
+ "ParseTree" => s(:while, s(:false),
4320
+ s(:block,
4321
+ s(:call, s(:lit, 1), :+,
4322
+ s(:arglist, s(:lit, 2))),
4323
+ s(:call, s(:lit, 3), :+,
4324
+ s(:arglist, s(:lit, 4)))),
4325
+ false))
4326
+
4327
+ add_tests("while_post_not",
4328
+ "Ruby" => "begin\n (1 + 1)\nend while not true",
4329
+ "RawParseTree" => [:until, [:true],
4330
+ [:call, [:lit, 1], :+,
4331
+ [:array, [:lit, 1]]], false],
4332
+ "ParseTree" => s(:until, s(:true),
4333
+ s(:call, s(:lit, 1), :+,
4334
+ s(:arglist, s(:lit, 1))), false),
4335
+ "Ruby2Ruby" => "begin\n (1 + 1)\nend until true")
4336
+
4337
+ add_tests("while_pre",
4338
+ "Ruby" => "while false do\n (1 + 1)\nend",
4339
+ "RawParseTree" => [:while, [:false],
4340
+ [:call, [:lit, 1], :+,
4341
+ [:array, [:lit, 1]]], true],
4342
+ "ParseTree" => s(:while, s(:false),
4343
+ s(:call, s(:lit, 1), :+,
4344
+ s(:arglist, s(:lit, 1))), true))
4345
+
4346
+ add_tests("while_pre_mod",
4347
+ "Ruby" => "(1 + 1) while false",
4348
+ "RawParseTree" => [:while, [:false],
4349
+ [:call, [:lit, 1], :+,
4350
+ [:array, [:lit, 1]]], true],
4351
+ "ParseTree" => s(:while, s(:false),
4352
+ s(:call, s(:lit, 1), :+,
4353
+ s(:arglist, s(:lit, 1))), true),
4354
+ "Ruby2Ruby" => "while false do\n (1 + 1)\nend") # FIX can be one liner
4355
+
4356
+ add_tests("while_pre_nil",
4357
+ "Ruby" => "while false do\nend",
4358
+ "RawParseTree" => [:while, [:false], nil, true],
4359
+ "ParseTree" => s(:while, s(:false), nil, true))
4360
+
4361
+ add_tests("while_pre_not",
4362
+ "Ruby" => "while not true do\n (1 + 1)\nend",
4363
+ "RawParseTree" => [:until, [:true],
4364
+ [:call, [:lit, 1], :+,
4365
+ [:array, [:lit, 1]]], true],
4366
+ "ParseTree" => s(:until, s(:true),
4367
+ s(:call, s(:lit, 1), :+,
4368
+ s(:arglist, s(:lit, 1))), true),
4369
+ "Ruby2Ruby" => "until true do\n (1 + 1)\nend")
4370
+
4371
+ add_tests("while_pre_not_mod",
4372
+ "Ruby" => "(1 + 1) while not true",
4373
+ "RawParseTree" => [:until, [:true],
4374
+ [:call, [:lit, 1], :+,
4375
+ [:array, [:lit, 1]]], true],
4376
+ "ParseTree" => s(:until, s(:true),
4377
+ s(:call, s(:lit, 1), :+,
4378
+ s(:arglist, s(:lit, 1))), true),
4379
+ "Ruby2Ruby" => "until true do\n (1 + 1)\nend") # FIX
4380
+
4381
+ add_tests("xstr",
4382
+ "Ruby" => "`touch 5`",
4383
+ "RawParseTree" => [:xstr, 'touch 5'],
4384
+ "ParseTree" => s(:xstr, 'touch 5'))
4385
+
4386
+ add_tests("yield_0",
4387
+ "Ruby" => "yield",
4388
+ "RawParseTree" => [:yield],
4389
+ "ParseTree" => s(:yield))
4390
+
4391
+ add_tests("yield_1",
4392
+ "Ruby" => "yield(42)",
4393
+ "RawParseTree" => [:yield, [:lit, 42]],
4394
+ "ParseTree" => s(:yield, s(:lit, 42)))
4395
+
4396
+ add_tests("yield_array_0",
4397
+ "Ruby" => "yield([])",
4398
+ "RawParseTree" => [:yield, [:zarray], true],
4399
+ "ParseTree" => s(:yield, s(:array)))
4400
+
4401
+ add_tests("yield_array_1",
4402
+ "Ruby" => "yield([42])",
4403
+ "RawParseTree" => [:yield, [:array, [:lit, 42]], true],
4404
+ "ParseTree" => s(:yield, s(:array, s(:lit, 42))))
4405
+
4406
+ add_tests("yield_array_n",
4407
+ "Ruby" => "yield([42, 24])",
4408
+ "RawParseTree" => [:yield, [:array, [:lit, 42], [:lit, 24]], true],
4409
+ "ParseTree" => s(:yield, s(:array, s(:lit, 42), s(:lit, 24))))
4410
+
4411
+ add_tests("yield_n",
4412
+ "Ruby" => "yield(42, 24)",
4413
+ "RawParseTree" => [:yield, [:array, [:lit, 42], [:lit, 24]]],
4414
+ "ParseTree" => s(:yield, s(:lit, 42), s(:lit, 24)))
4415
+
4416
+ add_tests("zarray",
4417
+ "Ruby" => "a = []",
4418
+ "RawParseTree" => [:lasgn, :a, [:zarray]],
4419
+ "ParseTree" => s(:lasgn, :a, s(:array)))
4420
+
4421
+ add_tests("zsuper",
4422
+ "Ruby" => "def x\n super\nend",
4423
+ "RawParseTree" => [:defn, :x,
4424
+ [:scope, [:block, [:args], [:zsuper]]]],
4425
+ "ParseTree" => s(:defn, :x, s(:args),
4426
+ s(:scope, s(:block, s(:zsuper)))))
4427
+
4428
+ add_18tests("iter_args_ivar",
4429
+ "Ruby" => "a { |@a| 42 }",
4430
+ "RawParseTree" => [:iter,
4431
+ [:fcall, :a],
4432
+ [:iasgn, :@a],
4433
+ [:lit, 42]],
4434
+ "ParseTree" => s(:iter,
4435
+ s(:call, nil, :a, s(:arglist)),
4436
+ s(:iasgn, :@a),
4437
+ s(:lit, 42)))
4438
+
4439
+ add_18tests("iter_masgn_args_ivar",
4440
+ "Ruby" => "a { |a, @b| 42 }",
4441
+ "RawParseTree" => [:iter,
4442
+ [:fcall, :a],
4443
+ [:masgn,
4444
+ [:array, [:dasgn_curr, :a], [:iasgn, :@b]],
4445
+ nil, nil],
4446
+ [:lit, 42]],
4447
+ "ParseTree" => s(:iter,
4448
+ s(:call, nil, :a, s(:arglist)),
4449
+ s(:masgn,
4450
+ s(:array, s(:lasgn, :a), s(:iasgn, :@b))),
4451
+ s(:lit, 42)))
4452
+
4453
+ add_18tests("str_question_control",
4454
+ "Ruby" => '?\M-\C-a',
4455
+ "RawParseTree" => [:lit, 129],
4456
+ "ParseTree" => s(:lit, 129),
4457
+ "Ruby2Ruby" => "129")
4458
+
4459
+ add_18tests("str_question_escape",
4460
+ "Ruby" => '?\n',
4461
+ "RawParseTree" => [:lit, 10],
4462
+ "ParseTree" => s(:lit, 10),
4463
+ "Ruby2Ruby" => "10")
4464
+
4465
+ add_18tests("str_question_literal",
4466
+ "Ruby" => '?a',
4467
+ "RawParseTree" => [:lit, 97],
4468
+ "ParseTree" => s(:lit, 97),
4469
+ "Ruby2Ruby" => "97")
4470
+
4471
+ add_19tests("call_arglist_norm_hash_colons",
4472
+ "Ruby" => "o.m(42, a: 1, b: 2)",
4473
+ "RawParseTree" => [:call,
4474
+ [:vcall, :o], :m,
4475
+ [:array,
4476
+ [:lit, 42],
4477
+ [:hash,
4478
+ [:lit, :a], [:lit, 1],
4479
+ [:lit, :b], [:lit, 2]]]],
4480
+ "ParseTree" => s(:call,
4481
+ s(:call, nil, :o, s(:arglist)),
4482
+ :m,
4483
+ s(:arglist,
4484
+ s(:lit, 42),
4485
+ s(:hash,
4486
+ s(:lit, :a), s(:lit, 1),
4487
+ s(:lit, :b), s(:lit, 2)))))
4488
+
4489
+ add_19tests("call_not_equal",
4490
+ "Ruby" => "a != b",
4491
+ "RawParseTree" => [:call, [:vcall, :a], :"!=",
4492
+ [:array, [:vcall, :b]]],
4493
+ "ParseTree" => s(:call,
4494
+ s(:call, nil, :a, s(:arglist)),
4495
+ :"!=",
4496
+ s(:arglist, s(:call, nil, :b, s(:arglist)))))
4497
+
4498
+ add_19tests("call_unary_not",
4499
+ "Ruby" => "!a",
4500
+ "RawParseTree" => [:call, [:vcall, :a], :"!@"],
4501
+ "ParseTree" => s(:call,
4502
+ s(:call, nil, :a, s(:arglist)),
4503
+ :"!@", s(:arglist)))
4504
+
4505
+ add_19tests("defn_args_splat_mand",
4506
+ "Ruby" => "def f(*rest, mand)\n # do nothing\nend",
4507
+ "ParseTree" => s(:defn, :f,
4508
+ s(:args, :"*rest", :mand),
4509
+ s(:scope, s(:block, s(:nil)))))
4510
+
4511
+ add_19tests("defn_args_splat_middle",
4512
+ "Ruby" => "def f(first, *middle, last)\n # do nothing\nend",
4513
+ "RawParseTree" => [:defn, :f,
4514
+ [:scope,
4515
+ [:block,
4516
+ [:args, :first, :"*middle", :last],
4517
+ [:nil]]]],
4518
+ "ParseTree" => s(:defn, :f,
4519
+ s(:args, :first, :"*middle", :last),
4520
+ s(:scope, s(:block, s(:nil)))))
4521
+
4522
+ add_19tests("fcall_arglist_hash_colons",
4523
+ "Ruby" => "m(a: 1, b: 2)",
4524
+ "RawParseTree" => [:fcall, :m,
4525
+ [:array,
4526
+ [:hash,
4527
+ [:lit, :a], [:lit, 1],
4528
+ [:lit, :b], [:lit, 2]]]],
4529
+ "ParseTree" => s(:call, nil, :m,
4530
+ s(:arglist,
4531
+ s(:hash,
4532
+ s(:lit, :a), s(:lit, 1),
4533
+ s(:lit, :b), s(:lit, 2)))))
4534
+
4535
+ add_19tests("hash_new",
4536
+ "Ruby" => "{ a: 1, b: 2 }",
4537
+ "RawParseTree" => [:hash,
4538
+ [:lit, :a], [:lit, 1],
4539
+ [:lit, :b], [:lit, 2]],
4540
+ "ParseTree" => s(:hash,
4541
+ s(:lit, :a), s(:lit, 1),
4542
+ s(:lit, :b), s(:lit, 2)))
4543
+
4544
+ add_19tests("lambda_args_0",
4545
+ "Ruby" => "->(){ (x + 1) }",
4546
+ "RawParseTree" => [:iter,
4547
+ [:fcall, :lambda],
4548
+ 0,
4549
+ [:call, [:vcall, :x], :+,
4550
+ [:array, [:lit, 1]]]],
4551
+ "ParseTree" => s(:iter,
4552
+ s(:call, nil, :lambda, s(:arglist)),
4553
+ 0,
4554
+ s(:call,
4555
+ s(:call, nil, :x, s(:arglist)),
4556
+ :+,
4557
+ s(:arglist, s(:lit, 1)))))
4558
+
4559
+ add_19tests("lambda_args_1",
4560
+ "Ruby" => "-> (x) { (x + 1) }",
4561
+ "RawParseTree" => [:iter,
4562
+ [:fcall, :lambda],
4563
+ [:dasgn_curr, :x],
4564
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]],
4565
+ "ParseTree" => s(:iter,
4566
+ s(:call, nil, :lambda, s(:arglist)),
4567
+ s(:lasgn, :x),
4568
+ s(:call, s(:lvar, :x), :+,
4569
+ s(:arglist, s(:lit, 1)))))
4570
+
4571
+ add_19tests("lambda_args_2",
4572
+ "Ruby" => "-> (x, y) { (x + y) }",
4573
+ "RawParseTree" => [:iter,
4574
+ [:fcall, :lambda],
4575
+ [:masgn, [:array,
4576
+ [:dasgn_curr, :x],
4577
+ [:dasgn_curr, :y]], nil, nil],
4578
+ [:call, [:dvar, :x], :+,
4579
+ [:array, [:dvar, :y]]]],
4580
+ "ParseTree" => s(:iter,
4581
+ s(:call, nil, :lambda, s(:arglist)),
4582
+ s(:masgn,
4583
+ s(:array,
4584
+ s(:lasgn, :x),
4585
+ s(:lasgn, :y))),
4586
+ s(:call, s(:lvar, :x), :+,
4587
+ s(:arglist, s(:lvar, :y)))))
4588
+
4589
+ add_19tests("lambda_args_2_no_parens",
4590
+ "Ruby" => "-> x, y { (x + y) }",
4591
+ "RawParseTree" => [:iter,
4592
+ [:fcall, :lambda],
4593
+ [:masgn, [:array,
4594
+ [:dasgn_curr, :x],
4595
+ [:dasgn_curr, :y]], nil, nil],
4596
+ [:call, [:dvar, :x], :+,
4597
+ [:array, [:dvar, :y]]]],
4598
+ "ParseTree" => s(:iter,
4599
+ s(:call, nil, :lambda, s(:arglist)),
4600
+ s(:masgn,
4601
+ s(:array,
4602
+ s(:lasgn, :x),
4603
+ s(:lasgn, :y))),
4604
+ s(:call, s(:lvar, :x), :+,
4605
+ s(:arglist, s(:lvar, :y)))))
4606
+
4607
+ add_19tests("lambda_args_no",
4608
+ "Ruby" => "-> { (x + 1) }",
4609
+ "RawParseTree" => [:iter,
4610
+ [:fcall, :lambda],
4611
+ nil,
4612
+ [:call, [:vcall, :x], :+,
4613
+ [:array, [:lit, 1]]]],
4614
+ "ParseTree" => s(:iter,
4615
+ s(:call, nil, :lambda, s(:arglist)),
4616
+ nil,
4617
+ s(:call, s(:call, nil, :x, s(:arglist)),
4618
+ :+, s(:arglist, s(:lit, 1)))))
4619
+
4620
+ add_19tests("splat_fcall_middle",
4621
+ "Ruby" => "meth(1, *[2], 3)",
4622
+ "RawParseTree" => [:fcall, :meth,
4623
+ [:lit, 1],
4624
+ [:splat, [:array, [:lit, 2]]],
4625
+ [:lit, 3]],
4626
+ "ParseTree" => s(:call, nil, :meth,
4627
+ s(:arglist,
4628
+ s(:lit, 1),
4629
+ s(:splat, s(:array, s(:lit, 2))),
4630
+ s(:lit, 3))))
4631
+
4632
+ add_19tests("str_question_control",
4633
+ "Ruby" => '?\M-\C-a',
4634
+ "RawParseTree" => [:str, "\x81"],
4635
+ "ParseTree" => s(:str, "\x81"))
4636
+
4637
+ add_19tests("str_question_escape",
4638
+ "Ruby" => '?\n',
4639
+ "RawParseTree" => [:str, "\n"],
4640
+ "ParseTree" => s(:str, "\n"))
4641
+
4642
+ add_19tests("str_question_literal",
4643
+ "Ruby" => '?a',
4644
+ "RawParseTree" => [:str, "a"],
4645
+ "ParseTree" => s(:str, "a"))
4646
+ end