summaryrefslogtreecommitdiff
path: root/test/ripper/test_parser_events.rb
blob: 6b37ff4da97ae36b1473348af54b56b9dd5e718e (plain)
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
begin
  require_relative 'dummyparser'
  require_relative '../ruby/envutil'
  require 'test/unit'
  ripper_test = true
rescue LoadError
end

class TestRipper_ParserEvents < Test::Unit::TestCase

  # should be enabled
=begin
  def test_event_coverage
    dispatched = Ripper::PARSER_EVENTS.map {|event,*| event }
    dispatched.each do |e|
      assert_equal true, respond_to?("test_#{e}", true),
                   "event not tested: #{e.inspect}"
    end
  end
=end

  def parse(str, nm = nil, &bl)
    dp = DummyParser.new(str)
    dp.hook(nm, &bl) if nm
    dp.parse.to_s
  end

  def test_program
    thru_program = false
    assert_equal '[void()]', parse('', :on_program) {thru_program = true}
    assert_equal true, thru_program
  end

  def test_stmts_new
    assert_equal '[void()]', parse('')
  end

  def test_stmts_add
    assert_equal '[ref(nil)]', parse('nil')
    assert_equal '[ref(nil),ref(nil)]', parse('nil;nil')
    assert_equal '[ref(nil),ref(nil),ref(nil)]', parse('nil;nil;nil')
  end

  def test_void_stmt
    assert_equal '[void()]', parse('')
    assert_equal '[void()]', parse('; ;')
  end

  def test_var_ref
    assert_equal '[ref(a)]', parse('a')
    assert_equal '[ref(nil)]', parse('nil')
    assert_equal '[ref(true)]', parse('true')
  end

  def test_BEGIN
    assert_equal '[BEGIN([void()])]', parse('BEGIN{}')
    assert_equal '[BEGIN([ref(nil)])]', parse('BEGIN{nil}')
  end

  def test_END
    assert_equal '[END([void()])]', parse('END{}')
    assert_equal '[END([ref(nil)])]', parse('END{nil}')
  end

  def test_alias
    assert_equal '[alias(symbol_literal(a),symbol_literal(b))]', parse('alias a b')
  end

  def test_var_alias
    assert_equal '[valias($a,$g)]', parse('alias $a $g')
  end

  def test_alias_error
    assert_equal '[aliaserr(valias($a,$1))]', parse('alias $a $1')
  end

  def test_arglist
    assert_equal '[fcall(m,[])]', parse('m()')
    assert_equal '[fcall(m,[1])]', parse('m(1)')
    assert_equal '[fcall(m,[1,2])]', parse('m(1,2)')
    assert_equal '[fcall(m,[*ref(r)])]', parse('m(*r)')
    assert_equal '[fcall(m,[1,*ref(r)])]', parse('m(1,*r)')
    assert_equal '[fcall(m,[1,2,*ref(r)])]', parse('m(1,2,*r)')
    assert_equal '[fcall(m,[&ref(r)])]', parse('m(&r)')
    assert_equal '[fcall(m,[1,&ref(r)])]', parse('m(1,&r)')
    assert_equal '[fcall(m,[1,2,&ref(r)])]', parse('m(1,2,&r)')
    assert_equal '[fcall(m,[*ref(a),&ref(b)])]', parse('m(*a,&b)')
    assert_equal '[fcall(m,[1,*ref(a),&ref(b)])]', parse('m(1,*a,&b)')
    assert_equal '[fcall(m,[1,2,*ref(a),&ref(b)])]', parse('m(1,2,*a,&b)')
  end

  def test_arg_paren
    # FIXME
  end

  def test_aref
    assert_equal '[aref(ref(v),[1])]', parse('v[1]')
    assert_equal '[aref(ref(v),[1,2])]', parse('v[1,2]')
  end

  def test_assocs
    assert_equal '[fcall(m,[assocs(assoc(1,2))])]', parse('m(1=>2)')
    assert_equal '[fcall(m,[assocs(assoc(1,2),assoc(3,4))])]', parse('m(1=>2,3=>4)')
    assert_equal '[fcall(m,[3,assocs(assoc(1,2))])]', parse('m(3,1=>2)')
  end

  def test_aref_field
    assert_equal '[assign(aref_field(ref(a),[1]),2)]', parse('a[1]=2')
  end

  def test_arg_ambiguous
    thru_arg_ambiguous = false
    parse('m //', :on_arg_ambiguous) {thru_arg_ambiguous = true}
    assert_equal true, thru_arg_ambiguous
  end

  def test_array   # array literal
    assert_equal '[array([1,2,3])]', parse('[1,2,3]')
  end

  def test_assign   # generic assignment
    assert_equal '[assign(var_field(v),1)]', parse('v=1')
  end

  def test_assign_error
    thru_assign_error = false
    parse('$` = 1', :on_assign_error) {thru_assign_error = true}
    assert_equal true, thru_assign_error
    thru_assign_error = false
    parse('$`, _ = 1', :on_assign_error) {thru_assign_error = true}
    assert_equal true, thru_assign_error

    thru_assign_error = false
    parse('self::X = 1', :on_assign_error) {thru_assign_error = true}
    assert_equal false, thru_assign_error
    parse('def m\n self::X = 1\nend', :on_assign_error) {thru_assign_error = true}
    assert_equal true, thru_assign_error

    thru_assign_error = false
    parse('X = 1', :on_assign_error) {thru_assign_error = true}
    assert_equal false, thru_assign_error
    parse('def m\n X = 1\nend', :on_assign_error) {thru_assign_error = true}
    assert_equal true, thru_assign_error

    thru_assign_error = false
    parse('::X = 1', :on_assign_error) {thru_assign_error = true}
    assert_equal false, thru_assign_error
    parse('def m\n ::X = 1\nend', :on_assign_error) {thru_assign_error = true}
    assert_equal true, thru_assign_error
  end

  def test_begin
    thru_begin = false
    parse('begin end', :on_begin) {thru_begin = true}
    assert_equal true, thru_begin
  end

  def test_binary
    thru_binary = nil
    %w"and or + - * / % ** | ^ & <=> > >= < <= == === != =~ !~ << >> && ||".each do |op|
      thru_binary = false
      parse("a #{op} b", :on_binary) {thru_binary = true}
      assert_equal true, thru_binary
    end
  end

  def test_block_var
    thru_block_var = false
    parse("proc{||}", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc{| |}", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc{|x|}", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc{|;y|}", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc{|x;y|}", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var

    thru_block_var = false
    parse("proc do || end", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc do | | end", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc do |x| end", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc do |;y| end", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
    thru_block_var = false
    parse("proc do |x;y| end", :on_block_var) {thru_block_var = true}
    assert_equal true, thru_block_var
  end

  def test_bodystmt
    thru_bodystmt = false
    parse("class X\nend", :on_bodystmt) {thru_bodystmt = true}
    assert_equal true, thru_bodystmt
  end

  def test_call
    bug2233 = '[ruby-core:26165]'
    tree = nil

    thru_call = false
    assert_nothing_raised {
      tree = parse("self.foo", :on_call) {thru_call = true}
    }
    assert_equal true, thru_call
    assert_equal "[call(ref(self),.,foo)]", tree
    thru_call = false
    assert_nothing_raised(bug2233) {
      tree = parse("foo.()", :on_call) {thru_call = true}
    }
    assert_equal true, thru_call
    assert_equal "[call(ref(foo),.,call,[])]", tree
  end

  def test_heredoc
    bug1921 = '[ruby-core:24855]'
    thru_heredoc_beg = false
    tree = parse("<<EOS\nheredoc\nEOS\n", :on_heredoc_beg) {thru_heredoc_beg = true}
    assert_equal true, thru_heredoc_beg
    assert_match(/string_content\(\),heredoc\n/, tree, bug1921)
    heredoc = nil
    parse("<<EOS\nheredoc1\nheredoc2\nEOS\n", :on_string_add) {|n, s| heredoc = s}
    assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
    heredoc = nil
    parse("<<-EOS\nheredoc1\nheredoc2\n\tEOS\n", :on_string_add) {|n, s| heredoc = s}
    assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
  end

  def test_massign
    thru_massign = false
    parse("a, b = 1, 2", :on_massign) {thru_massign = true}
    assert_equal true, thru_massign
  end

  def test_mlhs_add
    thru_mlhs_add = false
    parse("a, b = 1, 2", :on_mlhs_add) {thru_mlhs_add = true}
    assert_equal true, thru_mlhs_add
  end

  def test_mlhs_add_star
    bug2232 = '[ruby-core:26163]'

    thru_mlhs_add_star = false
    tree = parse("a, *b = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
    assert_equal true, thru_mlhs_add_star
    assert_match /mlhs_add_star\(mlhs_add\(mlhs_new\(\),a\),b\)/, tree
    thru_mlhs_add_star = false
    tree = parse("a, *b, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
    assert_equal true, thru_mlhs_add_star
    assert_match /mlhs_add\(mlhs_add_star\(mlhs_add\(mlhs_new\(\),a\),b\),mlhs_add\(mlhs_new\(\),c\)\)/, tree, bug2232
  end

  def test_mlhs_new
    thru_mlhs_new = false
    parse("a, b = 1, 2", :on_mlhs_new) {thru_mlhs_new = true}
    assert_equal true, thru_mlhs_new
  end

  def test_mlhs_paren
    thru_mlhs_paren = false
    parse("a, b = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
    assert_equal false, thru_mlhs_paren
    thru_mlhs_paren = false
    parse("(a, b) = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
    assert_equal true, thru_mlhs_paren
  end

=begin
  def test_brace_block
    assert_equal true, $thru__brace_block
  end

  def test_break
    assert_equal true, $thru__break
  end

  def test_case
    assert_equal true, $thru__case
  end

  def test_class
    assert_equal true, $thru__class
  end

  def test_class_name_error
    assert_equal true, $thru__class_name_error
  end

  def test_command
    assert_equal true, $thru__command
  end

  def test_command_call
    assert_equal true, $thru__command_call
  end

  def test_const_ref
    assert_equal true, $thru__const_ref
  end

  def test_constpath_field
    assert_equal true, $thru__constpath_field
  end

  def test_constpath_ref
    assert_equal true, $thru__constpath_ref
  end

  def test_def
    assert_equal true, $thru__def
  end

  def test_defined
    assert_equal true, $thru__defined
  end

  def test_defs
    assert_equal true, $thru__defs
  end

  def test_do_block
    assert_equal true, $thru__do_block
  end

  def test_dot2
    assert_equal true, $thru__dot2
  end

  def test_dot3
    assert_equal true, $thru__dot3
  end

  def test_dyna_symbol
    assert_equal true, $thru__dyna_symbol
  end

  def test_else
    assert_equal true, $thru__else
  end

  def test_elsif
    assert_equal true, $thru__elsif
  end

  def test_ensure
    assert_equal true, $thru__ensure
  end

  def test_fcall
    assert_equal true, $thru__fcall
  end

  def test_field
    assert_equal true, $thru__field
  end

  def test_for
    assert_equal true, $thru__for
  end

  def test_hash
    assert_equal true, $thru__hash
  end

  def test_if
    assert_equal true, $thru__if
  end

  def test_if_mod
    assert_equal true, $thru__if_mod
  end

  def test_ifop
    assert_equal true, $thru__ifop
  end

  def test_iter_block
    assert_equal true, $thru__iter_block
  end

  def test_method_add_arg
    assert_equal true, $thru__method_add_arg
  end

  def test_module
    assert_equal true, $thru__module
  end

  def test_mrhs_add
    assert_equal true, $thru__mrhs_add
  end

  def test_mrhs_add_star
    assert_equal true, $thru__mrhs_add_star
  end

  def test_mrhs_new
    assert_equal true, $thru__mrhs_new
  end

  def test_mrhs_new_from_arglist
    assert_equal true, $thru__mrhs_new_from_arglist
  end

  def test_next
    assert_equal true, $thru__next
  end

  def test_opassign
    assert_equal true, $thru__opassign
  end

  def test_param_error
    assert_equal true, $thru__param_error
  end

  def test_params
    assert_equal true, $thru__params
  end

  def test_paren
    assert_equal true, $thru__paren
  end

  def test_parse_error
    assert_equal true, $thru__parse_error
  end

  def test_qwords_add
    assert_equal true, $thru__qwords_add
  end

  def test_qwords_new
    assert_equal true, $thru__qwords_new
  end

  def test_redo
    assert_equal true, $thru__redo
  end

  def test_regexp_literal
    assert_equal true, $thru__regexp_literal
  end

  def test_rescue
    assert_equal true, $thru__rescue
  end

  def test_rescue_mod
    assert_equal true, $thru__rescue_mod
  end

  def test_restparam
    assert_equal true, $thru__restparam
  end

  def test_retry
    assert_equal true, $thru__retry
  end

  def test_return
    assert_equal true, $thru__return
  end

  def test_return0
    assert_equal true, $thru__return0
  end

  def test_sclass
    assert_equal true, $thru__sclass
  end

  def test_space
    assert_equal true, $thru__space
  end

  def test_string_add
    assert_equal true, $thru__string_add
  end

  def test_string_concat
    assert_equal true, $thru__string_concat
  end

  def test_string_content
    assert_equal true, $thru__string_content
  end

  def test_string_dvar
    assert_equal true, $thru__string_dvar
  end

  def test_string_embexpr
    assert_equal true, $thru__string_embexpr
  end

  def test_string_literal
    assert_equal true, $thru__string_literal
  end

  def test_super
    assert_equal true, $thru__super
  end

  def test_symbol
    assert_equal true, $thru__symbol
  end

  def test_symbol_literal
    assert_equal true, $thru__symbol_literal
  end

  def test_topconst_field
    assert_equal true, $thru__topconst_field
  end

  def test_topconst_ref
    assert_equal true, $thru__topconst_ref
  end

  def test_unary
    assert_equal true, $thru__unary
  end

  def test_undef
    assert_equal true, $thru__undef
  end

  def test_unless
    assert_equal true, $thru__unless
  end

  def test_unless_mod
    assert_equal true, $thru__unless_mod
  end

  def test_until_mod
    assert_equal true, $thru__until_mod
  end

  def test_var_field
    assert_equal true, $thru__var_field
  end

  def test_when
    assert_equal true, $thru__when
  end

  def test_while
    assert_equal true, $thru__while
  end

  def test_while_mod
    assert_equal true, $thru__while_mod
  end

  def test_word_add
    assert_equal true, $thru__word_add
  end

  def test_word_new
    assert_equal true, $thru__word_new
  end

  def test_words_add
    assert_equal true, $thru__words_add
  end

  def test_words_new
    assert_equal true, $thru__words_new
  end

  def test_xstring_add
    assert_equal true, $thru__xstring_add
  end

  def test_xstring_literal
    assert_equal true, $thru__xstring_literal
  end

  def test_xstring_new
    assert_equal true, $thru__xstring_new
  end

  def test_yield
    assert_equal true, $thru__yield
  end

  def test_yield0
    assert_equal true, $thru__yield0
  end

  def test_zsuper
    assert_equal true, $thru__zsuper
  end
=end

  def test_local_variables
    cmd = 'command(w,[regexp_literal(xstring_add(xstring_new(),25 # ),/)])'
    div = 'binary(ref(w),/,25)'
    var = '[w]'
    bug1939 = '[ruby-core:24923]'

    assert_equal("[#{cmd}]", parse('w /25 # /'), bug1939)
    assert_equal("[assign(var_field(w),1),#{div}]", parse("w = 1; w /25 # /"), bug1939)
    assert_equal("[fcall(p,[],&block([w],[#{div}]))]", parse("p{|w|w /25 # /\n}"), bug1939)
    assert_equal("[def(p,[w],bodystmt([#{div}]))]", parse("def p(w)\nw /25 # /\nend"), bug1939)
  end

  def test_block_variables
    assert_equal("[fcall(proc,[],&block([],[void()]))]", parse("proc{|;y|}"))
    if defined?(Process::RLIMIT_AS)
      assert_in_out_err(["-I#{File.dirname(__FILE__)}", "-rdummyparser"],
                        'Process.setrlimit(Process::RLIMIT_AS,102400); puts DummyParser.new("proc{|;y|}").parse',
                        ["[fcall(proc,[],&block([],[void()]))]"], [], '[ruby-dev:39423]')
    end
  end
end if ripper_test