forked from seattlerb/ruby_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ruby_parser.rb
executable file
·811 lines (637 loc) · 21.6 KB
/
test_ruby_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/usr/local/bin/ruby
ENV['VERBOSE'] = "1"
require 'rubygems'
gem "minitest"
require 'minitest/autorun'
require 'ruby_parser'
$: << File.expand_path('~/Work/p4/zss/src/ParseTree/dev/test')
require 'pt_testcase'
class Ruby18Parser # FIX
def process input
parse input
end
end
class Ruby19Parser
def process input
parse input
end
end
class RubyParserTestCase < ParseTreeTestCase
attr_accessor :result, :processor
def self.previous key
"Ruby"
end
def self.generate_test klass, node, data, input_name, output_name
return if node.to_s =~ /bmethod|dmethod/
return if Array === data['Ruby']
output_name = "ParseTree"
super
end
def assert_parse rb, pt
self.result = processor.parse rb
assert_equal pt, result
end
def assert_parse_line rb, pt, line
assert_parse rb, pt
assert_equal line, result.line, "call should have line number"
end
end
module TestRubyParser
def test_attrasgn_array_lhs
rb = '[1, 2, 3, 4][from .. to] = ["a", "b", "c"]'
pt = s(:attrasgn,
s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3), s(:lit, 4)),
:[]=,
s(:arglist,
s(:dot2,
s(:call, nil, :from, s(:arglist)),
s(:call, nil, :to, s(:arglist))),
s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c"))))
assert_parse rb, pt
end
def test_block_append
head = s(:args)
tail = s(:zsuper)
expected = s(:block, s(:args), s(:zsuper))
assert_equal expected, processor.block_append(head, tail)
end
def test_block_append_begin_begin
head = s(:begin, s(:args))
tail = s(:begin, s(:args))
expected = s(:block, s(:args), s(:begin, s(:args)))
assert_equal expected, processor.block_append(head, tail)
end
def test_block_append_block
head = s(:block, s(:args))
tail = s(:zsuper)
expected = s(:block, s(:args), s(:zsuper))
assert_equal expected, processor.block_append(head, tail)
end
def test_block_append_nil_head
head = nil
tail = s(:zsuper)
expected = s(:zsuper)
assert_equal expected, processor.block_append(head, tail)
end
def test_block_append_nil_tail
head = s(:args)
tail = nil
expected = s(:args)
assert_equal expected, processor.block_append(head, tail)
end
def test_block_append_tail_block
head = s(:call, nil, :f1, s(:arglist))
tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
expected = s(:block,
s(:call, nil, :f1, s(:arglist)),
s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
assert_equal expected, processor.block_append(head, tail)
end
def test_call_env
processor.env[:a] = :lvar
rb = "a.happy"
pt = s(:call, s(:lvar, :a), :happy, s(:arglist))
assert_parse rb, pt
end
def test_dasgn_icky2
rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
pt = s(:iter,
s(:call, nil, :a, s(:arglist)),
nil,
s(:block,
s(:lasgn, :v, s(:nil)),
s(:rescue,
s(:yield),
s(:resbody,
s(:array, s(:const, :Exception), s(:lasgn, :v, s(:gvar, :$!))),
s(:break)))))
assert_parse rb, pt
end
def test_class_comments
rb = "# blah 1\n# blah 2\n\nclass X\n # blah 3\n def blah\n # blah 4\n end\nend"
pt = s(:class, :X, nil,
s(:scope,
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
assert_equal "# blah 3\n", result.scope.defn.comments
end
def test_module_comments
rb = "# blah 1\n \n # blah 2\n\nmodule X\n # blah 3\n def blah\n # blah 4\n end\nend"
pt = s(:module, :X,
s(:scope,
s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
assert_parse rb, pt
assert_equal "# blah 1\n\n# blah 2\n\n", result.comments
assert_equal "# blah 3\n", result.scope.defn.comments
end
def test_defn_comments
rb = "# blah 1\n# blah 2\n\ndef blah\nend"
pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_defs_comments
rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
# This test currently fails because the node doesn't have any comments attached to it.
# def test_parenless_argless_method_call_comments
# rb = "# blah 1\n# blah 2\n\nfoo"
# pt = s(:call, nil, :foo, s(:arglist))
# assert_parse rb, pt
# assert_equal "# blah 1\n# blah 2\n\n", result.comments
# end
def test_method_call_comments
rb = "# blah 1\n# blah 2\n\nfoo('bar')"
pt = s(:call, nil, :foo, s(:arglist, s(:str, 'bar')))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_consecutive_method_call_comments
rb = "# blah 1\nfoo('bar')\n# blah 2\nfoo('baz')"
pt = s(:block,
s(:call, nil, :foo, s(:arglist, s(:str, 'bar'))),
s(:call, nil, :foo, s(:arglist, s(:str, 'baz'))))
assert_parse rb, pt
assert_equal "# blah 1\n", result[1].comments
assert_equal "# blah 2\n", result[2].comments
end
def test_parenless_method_call_comments
rb = "# blah 1\n# blah 2\n\nfoo 'bar'"
pt = s(:call, nil, :foo, s(:arglist, s(:str, 'bar')))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_parenless_method_call_many_args_comments
rb = "# blah 1\n# blah 2\n\nfoo 'bar', 'baz'"
pt = s(:call, nil, :foo, s(:arglist, s(:str, 'bar'), s(:str, 'baz')))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_super_with_args_comments
rb = "# blah 1\n# blah 2\n\nsuper(1)"
pt = s(:super, s(:lit, 1))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
# def test_super_with_block_comments
# rb = "# blah 1\n# blah 2\n\nsuper {x}"
# pt = s(:iter, s(:zsuper), nil, s(:call, nil, :x, s(:arglist)))
# assert_parse rb, pt
# assert_equal "# blah 1\n# blah 2\n\n", result.comments
# end
def test_plain_super_comments
rb = "# blah 1\n# blah 2\n\nsuper"
pt = s(:zsuper)
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_receiverless_call_comments
rb = "# blah 1\n# blah 2\n\nbar(1)"
pt = s(:call, nil, :bar, s(:arglist, s(:lit, 1)))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_receiver_call_comments
rb = "# blah 1\n# blah 2\n\nfoo.bar(1)"
pt = s(:call, s(:call, nil, :foo, s(:arglist)), :bar, s(:arglist, s(:lit, 1)))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_module_call_comments
rb = "# blah 1\n# blah 2\n\nFoo::bar(1)"
pt = s(:call, s(:const, :Foo), :bar, s(:arglist, s(:lit, 1)))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
def test_method_with_hash
rb = "# blah 1\n# blah 2\n\nbar('string', :key => :value)"
pt = s(:call, nil, :bar, s(:arglist, s(:str, "string"), s(:hash, s(:lit, :key), s(:lit, :value))))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
end
# The comment is currently attached to baz(1), not bar(baz(1)). That happens
# because the most recent comment is attached to the first call to new_call
# that completes (a la LIFO).
# def test_nested_method_call_comments
# rb = "# blah 1\n# blah 2\n\nbar(baz(1))"
# pt = s(:call, nil, :bar, s(:arglist, s(:call, nil, :baz, s(:arglist, s(:lit, 1)))))
# assert_parse rb, pt
# assert_equal "# blah 1\n# blah 2\n\n", result.comments
# end
def test_do_bug # TODO: rename
rb = "a 1\na.b do |c|\n # do nothing\nend"
pt = s(:block,
s(:call, nil, :a, s(:arglist, s(:lit, 1))),
s(:iter,
s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
s(:lasgn, :c)))
assert_parse rb, pt
end
def test_bug_comment_eq_begin
rb = "\n\n#\n=begin\nblah\n=end\n\n"
pt = nil
exp = rb.strip + "\n"
assert_parse rb, pt
assert_equal exp, processor.lexer.comments
end
def test_bug_call_arglist_parens
rb = 'g ( 1), 2'
pt = s(:call, nil, :g, s(:arglist, s(:lit, 1), s(:lit, 2)))
assert_parse rb, pt
rb = <<-CODE
def f
g ( 1), 2
end
CODE
pt = s(:defn, :f, s(:args),
s(:scope,
s(:block,
s(:call, nil, :g,
s(:arglist,
s(:lit, 1), s(:lit, 2))))))
assert_parse rb, pt
rb = <<-CODE
def f()
g (1), 2
end
CODE
assert_parse rb, pt
end
def test_dstr_evstr
rb = "\"#\{'a'}#\{b}\""
pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
assert_parse rb, pt
end
def test_dstr_str
rb = "\"#\{'a'} b\""
pt = s(:str, "a b")
assert_parse rb, pt
end
def test_empty
rb = ""
pt = nil
assert_parse rb, pt
end
def test_evstr_evstr
rb = "\"#\{a}#\{b}\""
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
assert_parse rb, pt
end
def test_evstr_str
rb = "\"#\{a} b\""
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
assert_parse rb, pt
end
def test_lasgn_env
rb = 'a = 42'
pt = s(:lasgn, :a, s(:lit, 42))
expected_env = { :a => :lvar }
assert_parse rb, pt
assert_equal expected_env, processor.env.all
end
def test_list_append
a = s(:lit, 1)
b = s(:lit, 2)
c = s(:lit, 3)
result = processor.list_append(s(:array, b.dup), c.dup)
assert_equal s(:array, b, c), result
result = processor.list_append(b.dup, c.dup)
assert_equal s(:array, b, c), result
result = processor.list_append(result, a.dup)
assert_equal s(:array, b, c, a), result
lhs, rhs = s(:array, s(:lit, :iter)), s(:when, s(:const, :BRANCHING), nil)
expected = s(:array, s(:lit, :iter), s(:when, s(:const, :BRANCHING), nil))
assert_equal expected, processor.list_append(lhs, rhs)
end
def test_list_prepend
a = s(:lit, 1)
b = s(:lit, 2)
c = s(:lit, 3)
result = processor.list_prepend(b.dup, s(:array, c.dup))
assert_equal s(:array, b, c), result
result = processor.list_prepend(b.dup, c.dup)
assert_equal s(:array, b, c), result
result = processor.list_prepend(a.dup, result)
assert_equal s(:array, a, b, c), result
end
def test_literal_concat_dstr_dstr
lhs = s(:dstr, "Failed to download spec ",
s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
s(:str, " from "),
s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
s(:str, ":\n"))
rhs = s(:dstr, "\t",
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
expected = s(:dstr, "Failed to download spec ",
s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
s(:str, " from "),
s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
s(:str, ":\n"),
s(:str, "\t"),
s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
assert_equal expected, processor.literal_concat(lhs, rhs)
end
def test_literal_concat_dstr_evstr
lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b, s(:arglist)))
expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
assert_equal expected, processor.literal_concat(lhs, rhs)
end
def test_literal_concat_evstr_evstr
lhs, rhs = s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2))
expected = s(:dstr, "", s(:evstr, s(:lit, 1)), s(:evstr, s(:lit, 2)))
assert_equal expected, processor.literal_concat(lhs, rhs)
end
def test_literal_concat_str_evstr
lhs, rhs = s(:str, ""), s(:evstr, s(:str, "blah"))
assert_equal s(:str, "blah"), processor.literal_concat(lhs, rhs)
end
def test_logop_12
lhs = s(:lit, 1)
rhs = s(:lit, 2)
exp = s(:and, s(:lit, 1), s(:lit, 2))
assert_equal exp, processor.logop(:and, lhs, rhs)
end
def test_logop_1234_5
lhs = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:and, s(:lit, 3), s(:lit, 4))))
rhs = s(:lit, 5)
exp = s(:and,
s(:lit, 1),
s(:and,
s(:lit, 2),
s(:and,
s(:lit, 3),
s(:and,
s(:lit, 4),
s(:lit, 5)))))
assert_equal exp, processor.logop(:and, lhs, rhs)
end
def test_logop_123_4
lhs = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
rhs = s(:lit, 4)
exp = s(:and,
s(:lit, 1),
s(:and,
s(:lit, 2),
s(:and,
s(:lit, 3),
s(:lit, 4))))
assert_equal exp, processor.logop(:and, lhs, rhs)
end
def test_logop_12_3
lhs = s(:and, s(:lit, 1), s(:lit, 2))
rhs = s(:lit, 3)
exp = s(:and, s(:lit, 1), s(:and, s(:lit, 2), s(:lit, 3)))
assert_equal exp, processor.logop(:and, lhs, rhs)
end
def test_logop_nested_mix
lhs = s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist)))
rhs = s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist)))
exp = s(:or,
s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist))),
s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist))))
lhs.paren = true
rhs.paren = true
assert_equal exp, processor.logop(:or, lhs, rhs)
end
def test_str_evstr
rb = "\"a #\{b}\""
pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
assert_parse rb, pt
end
def test_dsym_to_sym
pt = s(:alias, s(:lit, :<<), s(:lit, :>>))
rb = 'alias :<< :>>'
assert_parse rb, pt
rb = 'alias :"<<" :">>"'
assert_parse rb, pt
end
def test_regexp
regexps = {
"/wtf/" => /wtf/,
"/wtf/n" => /wtf/n,
"/wtf/m" => /wtf/m,
"/wtf/nm" => /wtf/nm,
"/wtf/nmnmnmnm" => /wtf/nm,
}
regexps.each do |rb, lit|
assert_parse rb, s(:lit, lit)
end
# TODO: add more including interpolation etc
end
def test_str_pct_Q_nested
rb = "%Q[before [#\{nest}] after]"
pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
assert_parse rb, pt
end
# def test_str_pct_nested_nested
# rb = "%{ { #\{ \"#\{1}\" } } }"
# assert_equal " { 1 } ", eval(rb)
# pt = s(:dstr, " { ", s(:evstr, s(:lit, 1)), s(:str, " } "))
#
# assert_parse rb, pt
# end
def test_str_str
rb = "\"a #\{'b'}\""
pt = s(:str, "a b")
assert_parse rb, pt
end
def test_str_str_str
rb = "\"a #\{'b'} c\""
pt = s(:str, "a b c")
assert_parse rb, pt
end
STARTING_LINE = {
"case_nested_inner_no_expr" => 2,
"case_no_expr" => 2,
"case_splat" => 2,
"dstr_heredoc_expand" => 1,
"dstr_heredoc_windoze_sucks" => 1,
"dstr_heredoc_yet_again" => 1,
"str_heredoc" => 1,
"str_heredoc_call" => 1,
"str_heredoc_empty" => 1,
"str_heredoc_indent" => 1,
"structure_unused_literal_wwtt" => 3, # yes, 3... odd test
"undef_block_1" => 2,
"undef_block_2" => 2,
"undef_block_3" => 2,
"undef_block_wtf" => 2,
}
def after_process_hook klass, node, data, input_name, output_name
expected = STARTING_LINE[node] || 1
assert_equal expected, @result.line, "should have proper line number"
end
def test_parse_line_block
rb = "a = 42\np a"
pt = s(:block,
s(:lasgn, :a, s(:lit, 42)),
s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
assert_parse_line rb, pt, 1
assert_equal 1, result.lasgn.line, "lasgn should have line number"
assert_equal 2, result.call.line, "call should have line number"
expected = "(string)"
assert_equal expected, result.file
assert_equal expected, result.lasgn.file
assert_equal expected, result.call.file
assert_same result.file, result.lasgn.file
assert_same result.file, result.call.file
end
def test_parse_line_call_no_args
rb = "f do |x, y|\n x + y\nend"
pt = s(:iter,
s(:call, nil, :f, s(:arglist)),
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
assert_parse_line rb, pt, 1
assert_equal 1, result[1].line, "call should have line number"
assert_equal 1, result[2].line, "masgn should have line number"
assert_equal 2, result[3].line, "call should have line number"
end
def test_parse_line_defn_no_parens
pt = s(:defn, :f, s(:args), s(:scope, s(:block, s(:nil))))
rb = "def f\nend"
assert_parse_line rb, pt, 1
rb = "def f\n\nend"
assert_parse_line rb, pt, 1
end
def test_parse_line_defn_complex
rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
pt = s(:defn, :x, s(:args, :y),
s(:scope,
s(:block,
s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
s(:lasgn, :y,
s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
s(:return, s(:lvar, :y)))))
assert_parse_line rb, pt, 1
body = result.scope.block
assert_equal 2, body.call.line, "call should have line number"
assert_equal 3, body.lasgn.line, "lasgn should have line number"
assert_equal 4, body.return.line, "return should have line number"
end
def test_parse_line_iter_call_parens
rb = "f(a) do |x, y|\n x + y\nend"
pt = s(:iter,
s(:call, nil, :f, s(:arglist, s(:call, nil, :a, s(:arglist)))),
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
assert_parse_line rb, pt, 1
assert_equal 1, result[1].line, "call should have line number"
assert_equal 1, result[2].line, "masgn should have line number"
assert_equal 2, result[3].line, "call should have line number"
end
def test_parse_line_iter_call_no_parens
rb = "f a do |x, y|\n x + y\nend"
pt = s(:iter,
s(:call, nil, :f, s(:arglist, s(:call, nil, :a, s(:arglist)))),
s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
assert_parse_line rb, pt, 1
assert_equal 1, result[1].line, "call should have line number"
assert_equal 1, result[2].line, "masgn should have line number"
assert_equal 2, result[3].line, "call should have line number"
end
def test_parse_line_heredoc
rb = <<-CODE
string = <<-HEREDOC
very long string
HEREDOC
puts string
CODE
result = processor.parse rb
assert_equal 1, result.lasgn.line
assert_equal 4, result.call.line
end
def test_parse_line_newlines
rb = "true\n\n"
pt = s(:true)
assert_parse_line rb, pt, 1
end
def test_parse_line_return
rb = <<-RUBY
def blah
if true then
return 42
end
end
RUBY
pt = s(:defn, :blah, s(:args),
s(:scope,
s(:block,
s(:if,
s(:true),
s(:return, s(:lit, 42)),
nil))))
assert_parse_line rb, pt, 1
assert_equal 3, result.scope.block.if.return.line
assert_equal 3, result.scope.block.if.return.lit.line
end
def test_parse_if_not_canonical
rb = "if not var.nil? then 'foo' else 'bar'\nend"
pt = s(:if,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
s(:str, "bar"),
s(:str, "foo"))
assert_parse rb, pt
end
def test_parse_if_not_noncanonical
rb = "if not var.nil? then 'foo' else 'bar'\nend"
pt = s(:if,
s(:not,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
s(:str, "foo"),
s(:str, "bar"))
processor.canonicalize_conditions = false
assert_parse rb, pt
end
def test_parse_while_not_canonical
rb = "while not var.nil?\n 'foo'\nend"
pt = s(:until,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
s(:str, "foo"), true)
assert_parse rb, pt
end
def test_parse_while_not_noncanonical
rb = "while not var.nil?\n 'foo'\nend"
pt = s(:while,
s(:not,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
s(:str, "foo"), true)
processor.canonicalize_conditions = false
assert_parse rb, pt
end
def test_parse_until_not_canonical
rb = "until not var.nil?\n 'foo'\nend"
pt = s(:while,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
s(:str, "foo"), true)
assert_parse rb, pt
end
def test_parse_until_not_noncanonical
rb = "until not var.nil?\n 'foo'\nend"
pt = s(:until,
s(:not,
s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
s(:str, "foo"), true)
processor.canonicalize_conditions = false
assert_parse rb, pt
end
end
class TestRuby18Parser < RubyParserTestCase
include TestRubyParser
def setup
super
self.processor = Ruby18Parser.new
end
end
class TestRuby19Parser < RubyParserTestCase
include TestRubyParser
def setup
super
self.processor = Ruby19Parser.new
end
end