/
test_ruby_parser.rb
executable file
·3766 lines (2855 loc) · 84.5 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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# ENV['VERBOSE'] = "1"
require "minitest/autorun"
require "ruby_parser"
$: << File.expand_path("~/Work/p4/zss/src/sexp_processor/dev/lib")
require "pt_testcase"
class Sexp
alias oldeq2 ==
# TODO: push up to Sexp
def ==(obj) # :nodoc:
if obj.class == self.class then
super and
(self.line.nil? or obj.line.nil? or self.line == obj.line)
else
false
end
end
end
module TestRubyParserShared
def setup
super
# p :test => [self.class, __name__]
end
BLOCK_DUP_MSG = "Both block arg and actual block given."
def test_double_block_error_01
assert_syntax_error "a(1, &b) { }", BLOCK_DUP_MSG
end
def test_double_block_error_02
assert_syntax_error "a(1, &b) do end", BLOCK_DUP_MSG
end
def test_double_block_error_03
assert_syntax_error "a 1, &b do end", BLOCK_DUP_MSG
end
def test_double_block_error_04
assert_syntax_error "m.a(1, &b) { }", BLOCK_DUP_MSG
end
def test_double_block_error_05
assert_syntax_error "m.a(1, &b) do end", BLOCK_DUP_MSG
end
def test_double_block_error_06
assert_syntax_error "m.a 1, &b do end", BLOCK_DUP_MSG
end
def test_double_block_error_07
assert_syntax_error "m::a(1, &b) { }", BLOCK_DUP_MSG
end
def test_double_block_error_08
assert_syntax_error "m::a(1, &b) do end", BLOCK_DUP_MSG
end
def test_double_block_error_09
assert_syntax_error "m::a 1, &b do end", BLOCK_DUP_MSG
end
def test_wtf_7
rb = "a.b (1) {c}"
pt = s(:iter,
s(:call, s(:call, nil, :a), :b, s(:lit, 1)),
0,
s(:call, nil, :c))
assert_parse rb, pt
end
def test_wtf_8
rb = "a::b (1) {c}"
pt = s(:iter,
s(:call, s(:call, nil, :a), :b, s(:lit, 1)),
0,
s(:call, nil, :c))
assert_parse rb, pt
end
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(:dot2,
s(:call, nil, :from),
s(:call, nil, :to)),
s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c")))
assert_parse rb, pt
end
def test_attrasgn_array_arg
rb = "a[[1, 2]] = 3"
pt = s(:attrasgn,
s(:call, nil, :a),
:[]=,
s(:array,
s(:lit, 1),
s(:lit, 2)),
s(:lit, 3))
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)
tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
expected = s(:block,
s(:call, nil, :f1),
s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
assert_equal expected, processor.block_append(head, tail)
end
def test_call_array_arg
rb = "1 == [:b, :c]"
pt = s(:call, s(:lit, 1), :==, s(:array, s(:lit, :b), s(:lit, :c)))
assert_parse rb, pt
end
def test_call_env
processor.env[:a] = :lvar
rb = "a.happy"
pt = s(:call, s(:lvar, :a), :happy)
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),
0,
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(:defn, :blah, s(:args), s(:nil)))
assert_parse rb, pt
assert_equal "# blah 1\n# blah 2\n\n", result.comments
assert_equal "# blah 3\n", result.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(:defn, :blah, s(:args), s(:nil)))
assert_parse rb, pt
assert_equal "# blah 1\n\n# blah 2\n\n", result.comments
assert_equal "# blah 3\n", result.defn.comments
end
def test_defn_comments
rb = "# blah 1\n# blah 2\n\ndef blah\nend"
pt = s(:defn, :blah, s(:args), 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(:nil))
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(:lit, 1)),
s(:iter,
s(:call, s(:call, nil, :a), :b),
s(:args, :c)))
assert_parse rb, pt
end
def test_bug_begin_else
rb = "begin 1; else; 2 end"
pt = s(:block, s(:lit, 1), s(:lit, 2))
assert_parse rb, pt
end
def test_begin_else_return_value
rb = "begin; else 2; end"
pt = s(:lit, 2)
assert_parse rb, pt
end
def test_bug_comment_eq_begin
rb = "\n\n#\n=begin\nblah\n=end\n\n"
exp = rb.strip + "\n"
refute_parse rb
assert_equal exp, processor.lexer.comments
end
def test_eq_begin_why_wont_people_use_their_spacebar?
rb = "h[k]=begin\n 42\n end"
pt = s(:attrasgn, s(:call, nil, :h), :[]=, s(:call, nil, :k), s(:lit, 42))
assert_parse rb, pt
end
def test_eq_begin_line_numbers
rb = "1\n=begin\ncomment\ncomment\n=end\n2"
pt = s(:block,
s(:lit, 1).line(1),
s(:lit, 2).line(6))
assert_parse rb, pt
end
def test_bug_call_arglist_parens
rb = 'g ( 1), 2'
pt = s(:call, nil, :g, 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(:call, nil, :g, 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)))
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
refute_parse ""
end
def test_evstr_evstr
rb = "\"#\{a}#\{b}\""
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), s(:evstr, s(:call, nil, :b)))
assert_parse rb, pt
end
def test_evstr_str
rb = "\"#\{a} b\""
pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), 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_lasgn_ivar_env
rb = "@a = 42"
pt = s(:iasgn, :@a, s(:lit, 42))
assert_parse rb, pt
assert_empty 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(:str, " from "),
s(:evstr, s(:call, nil, :source_uri)),
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(:str, " from "),
s(:evstr, s(:call, nil, :source_uri)),
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))
expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b)))
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_logical_op_12
lhs = s(:lit, 1)
rhs = s(:lit, 2)
exp = s(:and, s(:lit, 1), s(:lit, 2))
assert_equal exp, processor.logical_op(:and, lhs, rhs)
end
def test_logical_op_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.logical_op(:and, lhs, rhs)
end
def test_logical_op_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.logical_op(:and, lhs, rhs)
end
def test_logical_op_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.logical_op(:and, lhs, rhs)
end
def test_logical_op_nested_mix
lhs = s(:or, s(:call, nil, :a), s(:call, nil, :b))
rhs = s(:and, s(:call, nil, :c), s(:call, nil, :d))
exp = s(:or,
s(:or, s(:call, nil, :a), s(:call, nil, :b)),
s(:and, s(:call, nil, :c), s(:call, nil, :d)))
lhs.paren = true
rhs.paren = true
assert_equal exp, processor.logical_op(:or, lhs, rhs)
end
def test_str_evstr
rb = "\"a #\{b}\""
pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b)))
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_label_vs_string
rb = "_buf << ':\n'"
pt = s(:call, s(:call, nil, :_buf), :<<, s(:str, ":\n"))
assert_parse rb, pt
end
def test_str_backslashes
long_string = '\n' * 100
rb = "x '#{long_string}'"
pt = s(:call, nil, :x, s(:str, long_string))
assert_parse rb, pt
end
def test_str_pct_Q_nested
rb = "%Q[before [#\{nest}] after]"
pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest)), s(:str, "] after"))
assert_parse rb, pt
end
def test_str_pct_q
rb = "%q{a b c}"
pt = s(:str, "a b c")
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
def test_str_newline_hash_line_number
rb = "\"\\n\\n\\n\\n#\"\n1"
pt = s(:block, s(:str, "\n\n\n\n#").line(1),
s(:lit, 1).line(2))
assert_parse rb, pt
end
def after_process_hook klass, node, data, input_name, output_name
assert_equal 1, @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(: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_block_inline_comment
rb = "a\nb # comment\nc"
pt = s(:block,
s(:call, nil, :a).line(1),
s(:call, nil, :b).line(2),
s(:call, nil, :c).line(3))
assert_parse rb, pt
end
def test_parse_line_block_inline_multiline_comment
rb = "a\nb # comment\n# another comment\nc"
pt = s(:block,
s(:call, nil, :a).line(1),
s(:call, nil, :b).line(2),
s(:call, nil, :c).line(4)).line(1)
assert_parse rb, pt
end
def test_parse_line_block_inline_comment_leading_newlines
rb = "\n\n\na\nb # comment\n# another comment\nc"
pt = s(:block,
s(:call, nil, :a).line(4),
s(:call, nil, :b).line(5),
s(:call, nil, :c).line(7)).line(4)
assert_parse rb, pt
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(:args, :x, :y),
s(:call, s(:lvar, :x), :+, 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_call_ivar_line_break_paren
rb = "a(@b\n)"
pt = s(:call, nil, :a, s(:ivar, :@b).line(1)).line(1)
assert_parse rb, pt
end
def test_parse_line_call_ivar_arg_no_parens_line_break
rb = "a @b\n"
pt = s(:call, nil, :a, s(:ivar, :@b).line(1)).line(1)
assert_parse rb, pt
end
def test_parse_line_defn_no_parens
pt = s(:defn, :f, s(:args), s(:nil))
rb = "def f\nend"
assert_parse_line rb, pt, 1
processor.reset
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(:call, nil, :p, s(:lvar, :y)),
s(:lasgn, :y, s(:call, s(:lvar, :y), :*, s(:lit, 2))),
s(:return, s(:lvar, :y)))
assert_parse_line rb, pt, 1
body = result
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_evstr_after_break
rb = "\"a\"\\\n\"\#{b}\""
pt = s(:dstr, "a",
s(:evstr,
s(:call, nil, :b).line(2)).line(2)).line(1)
assert_parse rb, pt
end
def test_parse_line_dstr_newline
skip "dstr line numbers are just gonna be screwed for a while..."
rb = <<-'CODE'
"a\n#{
}"
true
CODE
pt = s(:block,
s(:dstr, "a\n",
s(:evstr)).line(1),
s(:true).line(3))
assert_parse rb, pt
end
def test_parse_line_hash_lit
rb = "{\n:s1 => 1,\n}"
pt = s(:hash,
s(:lit, :s1).line(2), s(:lit, 1).line(2),
).line(1)
assert_parse rb, pt
end
def test_parse_line_heredoc_evstr
skip "heredoc line numbers are just gonna be screwed for a while..."
rb = "<<-A\na\n\#{b}\nA"
pt = s(:dstr, "a\n",
s(:evstr,
s(:call, nil, :b).line(3)),
s(:str, "\n")).line(1)
assert_parse rb, pt
end
def test_parse_line_multiline_str
rb = "\"a\nb\"\n1"
pt = s(:block,
s(:str, "a\nb").line(1),
s(:lit, 1).line(3)).line(1)
assert_parse rb, pt
end
def test_parse_line_multiline_str_literal_n
rb = "\"a\\nb\"\n1"
pt = s(:block,
s(:str, "a\nb").line(1),
s(:lit, 1).line(2)).line(1)
assert_parse rb, pt
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(:call, nil, :a)),
s(:args, :x, :y),
s(:call, s(:lvar, :x), :+, 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(:call, nil, :a).line(1)).line(1),
s(:args, :x, :y).line(1),
s(:call, s(:lvar, :x).line(2), :+,
s(:lvar, :y).line(2)).line(2)).line(1)
assert_parse rb, pt
end
def test_parse_line_heredoc
rb = <<-CODE
string = <<-HEREDOC
very long string
HEREDOC
puts string
CODE
pt = s(:block,
s(:lasgn, :string,
s(:str, " very long string\n").line(1)).line(1),
s(:call, nil, :puts, s(:lvar, :string).line(4)).line(4)).line(1)
assert_parse rb, pt
end
def test_parse_line_heredoc_regexp_chars
rb = <<-CODE
string = <<-"^D"
very long string
^D
puts string
CODE
pt = s(:block,
s(:lasgn, :string,
s(:str, " very long string\n").line(1)).line(1),
s(:call, nil, :puts, s(:lvar, :string).line(4)).line(4)).line(1)
assert_parse rb, pt
end
def test_parse_line_newlines
rb = "true\n\n"
pt = s(:true)
assert_parse_line rb, pt, 1
end
def test_parse_line_rescue
rb = "begin\n a\n rescue\n b\n rescue\n c\n end\n"
pt = s(:rescue,
s(:call, nil, :a).line(2),
s(:resbody, s(:array).line(3),
s(:call, nil, :b).line(4)).line(3),
s(:resbody, s(:array).line(5),
s(:call, nil, :c).line(6)).line(5)).line(1)
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(:if, s(:true),
s(:return, s(:lit, 42)),
nil))
assert_parse_line rb, pt, 1
assert_equal 3, result.if.return.line
assert_equal 3, result.if.return.lit.line
end
def test_parse_line_str_with_newline_escape
rb = 'a("\n", true)'
pt = s(:call, nil, :a,
s(:str, "\n").line(1),
s(:true).line(1))
assert_parse rb, pt
end
def test_parse_line_trailing_newlines
rb = "a \nb"
pt = s(:block,
s(:call, nil, :a).line(1),
s(:call, nil, :b).line(2)).line(1)
assert_parse rb, pt
end
def test_bug_and
rb = "true and []"
pt = s(:and, s(:true), s(:array))
assert_parse rb, pt
rb = "true and\ntrue"
pt = s(:and, s(:true), s(:true))
assert_parse rb, pt
end
def test_bug_cond_pct
rb = "case; when %r%blahblah%; end"
pt = s(:case, nil, s(:when, s(:array, s(:lit, /blahblah/)), nil), nil)
assert_parse rb, pt
end
# according to 2.3.1 parser -- added: ON 1.8 only:
# rp.process("f { |(a,b),c| }") == rp.process("f { |((a,b),c)| }")
# ruby18 -e "p lambda { |(a,b)| }.arity" # => 2
# ruby19 -e "p lambda { |(a,b)| }.arity" # => 1
# ruby18 -e "p lambda { |(a,b),c| }.arity" # => 2
# ruby19 -e "p lambda { |(a,b),c| }.arity" # => 2
# ruby18 -e "p lambda { |((a,b),c)| }.arity" # => 2
# ruby19 -e "p lambda { |((a,b),c)| }.arity" # => 1
def test_bug_args_masgn
rb = "f { |(a, b), c| }"
pt = s(:iter,
s(:call, nil, :f),
s(:args, s(:masgn, :a, :b), :c))
assert_parse rb, pt.dup
end
def test_bug_args_masgn2
rb = "f { |((a, b), c), d| }"
pt = s(:iter,
s(:call, nil, :f),
s(:args, s(:masgn, s(:masgn, :a, :b), :c), :d))
assert_parse rb, pt
end
def ruby18
RubyParser::V18 === self.processor
end
def test_bug_comma
val = if ruby18 then
s(:lit, 100)
else
s(:str, "d")
end
rb = "if test ?d, dir then end"
pt = s(:if,
s(:call, nil, :test, val, s(:call, nil, :dir)),
nil,
nil)
assert_parse rb, pt
end
def test_bug_case_when_regexp
rb = "case :x; when /x/ then end"
pt = s(:case, s(:lit, :x),
s(:when, s(:array, s(:lit, /x/)), nil),
nil)
assert_parse rb, pt
end
def test_bug_masgn_right
rb = "f { |a, (b, c)| }"
pt = s(:iter,
s(:call, nil, :f),
s(:args, :a, s(:masgn, :b, :c)))
assert_parse rb, pt
end
def test_when_splat
rb = "case a; when *b then; end"
pt = s(:case, s(:call, nil, :a),
s(:when, s(:array, s(:splat, s(:call, nil, :b))), nil),
nil)
assert_parse rb, pt
end
def test_if_symbol
rb = "if f :x; end"
pt = s(:if, s(:call, nil, :f, s(:lit, :x)), nil, nil)
assert_parse rb, pt
end
def test_bug_not_parens
rb = "not(a)"
pt = if ruby18 then
s(:not, s(:call, nil, :a))
else
s(:call, s(:call, nil, :a), :"!")
end
assert_parse rb, pt
end
def test_pipe_space
rb = "a.b do | | end"
pt = s(:iter, s(:call, s(:call, nil, :a), :b), s(:args))
assert_parse rb, pt
end
def test_cond_unary_minus
rb = "if -1; end"
pt = s(:if, s(:lit, -1), nil, nil)
assert_parse rb, pt
end
def test_bug_op_asgn_rescue
rb = "a ||= b rescue nil"
pt = s(:rescue,