antlr3 /runtime/Ruby/test/unit/test-trees.rb

Language Ruby Lines 844
MD5 Hash 141eda1a577ae1a2d90ec1916ca3a518 Estimated Cost $17,659 (why?)
Repository https://bitbucket.org/jwalton/antlr3.git View Raw File View Project SPDX
  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
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3'
require 'test/unit'
require 'spec'

include ANTLR3
include ANTLR3::AST

class TestTreeNodeStream < Test::Unit::TestCase
  def setup
    @adaptor = CommonTreeAdaptor.new
  end
  
  def new_stream(t)
    CommonTreeNodeStream.new(t)
  end
  
  def test_single_node
    t = CommonTree.new(CommonToken.new { |t| t.type = 101 })
    stream = new_stream(t)
    expecting = '101'
    
    found = nodes_only_string(stream)
    
    found.should == expecting
    
    expecting = '<UNKNOWN: 101>'
    found = stream.inspect
    
    found.should == expecting
  end
  
  def test_two_children_of_nil_root
    v = Class.new(CommonTree) do
      def initialize(token = nil, type = nil, x = nil)
        @x = x
        super(token || (CommonToken.new { |t| t.type = type } if type))
      end
      def to_s
        (@token.text rescue '') + '<V>'
      end
    end
    
    root_0 = @adaptor.create_flat_list
    t = v.new(nil, 101, 2)
    u = v.new CommonToken.create(:type => 102, :text => '102')
    @adaptor.add_child(root_0, t)
    @adaptor.add_child(root_0, u)
    
    assert(root_0.parent.nil?)
    root_0.child_index.should == -1
    t.child_index.should == 0
    u.child_index.should == 1
    
  end
  
  def test_4_nodes
    t = CommonTree.new CommonToken[101]
    t.add_child( CommonTree.new CommonToken[102] )
    t.child(0).add_child(CommonTree.new CommonToken[103])
    t.add_child(CommonTree.new CommonToken[104])
    
    stream = new_stream(t)

    expecting = "101 102 103 104"
    found = nodes_only_string(stream)
    found.should == expecting
    
    expecting = "<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> <UP>"
    found = stream.inspect
    found.should == expecting
  end
  
  def test_list
    root = CommonTree.new(nil)
    t = CommonTree.new CommonToken[101]
    t.add_child CommonTree.new(CommonToken[102])
    t.child(0).add_child(CommonTree.new(CommonToken[103]))
    t.add_child(CommonTree.new(CommonToken[104]))
    
    u = CommonTree.new CommonToken[105]
    
    root.add_child(t)
    root.add_child(u)
    
    stream = CommonTreeNodeStream.new(root)
    
    expecting = '101 102 103 104 105'
    found = nodes_only_string(stream)
    found.should == expecting
    
    expecting = "<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> <UP> <UNKNOWN: 105>"
    found = stream.inspect
    found.should == expecting
  end
  
  def test_flat_list
    root = CommonTree.new(nil)
    
    root.add_child CommonTree.new(CommonToken[101])
    root.add_child(CommonTree.new(CommonToken[102]))
    root.add_child(CommonTree.new(CommonToken[103]))
    
    stream = CommonTreeNodeStream.new( root )
    
    expecting = '101 102 103'
    found = nodes_only_string(stream)
    found.should == expecting
    
    expecting = '<UNKNOWN: 101> <UNKNOWN: 102> <UNKNOWN: 103>'
    found = stream.inspect
    found.should == expecting
  end
  
  def test_list_with_one_node
    root = CommonTree.new(nil)
    
    root.add_child(CommonTree.new(CommonToken[101]))
    
    stream = CommonTreeNodeStream.new(root)
    
    expecting = '101'
    found = nodes_only_string(stream)
    found.should == expecting
    
    expecting = "<UNKNOWN: 101>"
    found = stream.inspect
    found.should == expecting
  end
  
  def test_a_over_b
    t = CommonTree.new(CommonToken[101])
    t.add_child(CommonTree.new(CommonToken[102]))
    
    stream = new_stream(t)
    expecting = '101 102'
    found = nodes_only_string(stream)
    found.should == expecting
    
    expecting = '<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <UP>'
    found = stream.inspect
    found.should == expecting
  end
  
  def test_LT
    # ^(101 ^(102 103) 104)
    t = CommonTree.new CommonToken[101]
    t.add_child CommonTree.new(CommonToken[102])
    t.child(0).add_child(CommonTree.new(CommonToken[103]))
    t.add_child(CommonTree.new(CommonToken[104]))
    
    stream = new_stream(t)
    [101, DOWN, 102, DOWN, 103, UP, 104, UP, EOF].each_with_index do |type, index|
      stream.look(index + 1).type.should == type
    end
    stream.look(100).type.should == EOF
  end
  
  def test_mark_rewind_entire
    # ^(101 ^(102 103 ^(106 107)) 104 105)
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r0.add_child(r1)
    r1.add_child(new_node new_token(103))
    r2 = new_node new_token(106)
    r2.add_child new_node( new_token 107 )
    r1.add_child r2
    r0.add_child new_node( new_token 104 )
    r0.add_child new_node( new_token 105 )
    
    stream = CommonTreeNodeStream.new(r0)
    m = stream.mark
    13.times { stream.look(1); stream.consume } # consume until end
    
    stream.look(1).type.should == EOF
    stream.look(-1).type.should == UP
    stream.rewind(m)
    
    13.times { stream.look(1); stream.consume } # consume until end
    
    stream.look(1).type.should == EOF
    stream.look(-1).type.should == UP
  end
  
  def test_mark_rewind_in_middle
    # ^(101 ^(102 103 ^(106 107)) 104 105)
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r0.add_child r1
    r1.add_child new_node( new_token 103 )
    r2 = new_node new_token(106)
    r2.add_child new_node( new_token 107 )
    r1.add_child r2
    r0.add_child new_node( new_token 104 )
    r0.add_child new_node( new_token 105 )
    
    stream = CommonTreeNodeStream.new(r0)
    7.times { stream.consume }
    
    stream.look(1).type.should == 107
    m = stream.mark
    4.times { stream.consume }
    stream.rewind(m)
    
    [107, UP, UP, 104].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    # past rewind position now
    [105, UP].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    stream.look(1).type.should == EOF
    stream.look(-1).type.should == UP
  end
  
  def test_mark_rewind_nested
    # ^(101 ^(102 103 ^(106 107)) 104 105)
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r0.add_child r1
    r1.add_child new_node( new_token 103 )
    r2 = new_node new_token(106)
    r2.add_child new_node( new_token 107 )
    r1.add_child r2
    r0.add_child new_node( new_token 104 )
    r0.add_child new_node( new_token 105 )
    
    stream = CommonTreeNodeStream.new(r0)
    m = stream.mark
    2.times { stream.consume }
    m2 = stream.mark
    4.times { stream.consume }
    stream.rewind(m2)
    stream.look(1).type.should == 102
    stream.consume
    stream.look(1).type.should == DOWN
    stream.consume
    
    stream.rewind(m)
    [101, DOWN, 102].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    stream.look(1).type.should == DOWN
  end
  
  def test_seek
    # ^(101 ^(102 103 ^(106 107) ) 104 105)
    # stream has 7 real + 6 nav nodes
    # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF

    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r0.add_child r1
    r1.add_child new_node( new_token 103 )
    r2 = new_node new_token(106)
    r2.add_child new_node( new_token 107 )
    r1.add_child r2
    r0.add_child new_node( new_token 104 )
    r0.add_child new_node( new_token 105 )
    
    stream = CommonTreeNodeStream.new(r0)
    3.times { stream.consume }
    stream.seek(7)
    stream.look(1).type.should == 107
    3.times { stream.consume }
    stream.look(1).type.should == 104
  end
  
  def test_seek_from_start
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r0.add_child r1
    r1.add_child new_node( new_token 103 )
    r2 = new_node new_token(106)
    r2.add_child new_node( new_token 107 )
    r1.add_child r2
    r0.add_child new_node( new_token 104 )
    r0.add_child new_node( new_token 105 )
    
    stream = CommonTreeNodeStream.new(r0)
    stream.seek(7)
    stream.look(1).type.should == 107
    3.times { stream.consume }
    stream.look(1).type.should == 104
  end
  
  def nodes_only_string(nodes)
    buffer = []
    nodes.size.times do |index|
      t = nodes.look(index + 1)
      type = nodes.tree_adaptor.type_of(t)
      buffer << type.to_s unless type == DOWN or type == UP
    end
    return buffer.join(' ')
  end
  
  def new_token(type, opts = {})
    opts[:type] = type
    CommonToken.create(opts)
  end
  def new_node(token)
    CommonTree.new(token)
  end


end

class TestCommonTreeNodeStream < Test::Unit::TestCase
  def setup
    # before-each-test code
  end
  def teardown
    # after-each-test code
  end
  
  # vvvvvvvv tests vvvvvvvvv
  
  def test_push_pop
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r1.add_child new_node( new_token 103 )
    r0.add_child r1
    r2 = new_node new_token(104)
    r2.add_child new_node( new_token 105 )
    r0.add_child r2
    r3 = new_node new_token(106)
    r3.add_child new_node( new_token 107 )
    r0.add_child r3
    r0.add_child new_node( new_token 108 )
    r0.add_child new_node( new_token 109 )
    
    stream = CommonTreeNodeStream.new(r0)
    expecting = '<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> ' +
                '<DOWN> <UNKNOWN: 105> <UP> <UNKNOWN: 106> <DOWN> <UNKNOWN: 107> <UP> ' +
                '<UNKNOWN: 108> <UNKNOWN: 109> <UP>'
    found = stream.inspect
    found.should == expecting
    
    index_of_102 = 2
    index_of_107 = 12
    index_of_107.times { stream.consume }
    
    stream.look(1).type.should == 107
    stream.push(index_of_102)
    stream.look(1).type.should == 102
    stream.consume
    stream.look(1).type.should == DOWN
    stream.consume
    stream.look(1).type.should == 103
    stream.consume
    stream.look(1).type.should == UP
    stream.pop
    stream.look(1).type.should == 107
  end
  
  def test_nested_push_pop
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r1.add_child new_node( new_token 103 )
    r0.add_child r1
    r2 = new_node new_token(104)
    r2.add_child new_node( new_token 105 )
    r0.add_child r2
    r3 = new_node new_token(106)
    r3.add_child new_node( new_token 107 )
    r0.add_child r3
    r0.add_child new_node( new_token 108 )
    r0.add_child new_node( new_token 109 )
    
    stream = CommonTreeNodeStream.new(r0)
    
    index_of_102 = 2
    index_of_107 = 12
    
    index_of_107.times { stream.consume }
    
    stream.look(1).type.should == 107
    stream.push(index_of_102)
    [102, DOWN, 103].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    
    index_of_104 = 6
    stream.push(index_of_104)
    [104,DOWN,105].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    stream.look(1).type.should == UP
    stream.pop
    
    stream.look(1).type.should == UP
    stream.pop
    stream.look(1).type.should == 107
  end
  
  def test_push_pop_from_eof
    r0 = new_node new_token(101)
    r1 = new_node new_token(102)
    r1.add_child new_node( new_token 103 )
    r0.add_child r1
    r2 = new_node new_token(104)
    r2.add_child new_node( new_token 105 )
    r0.add_child r2
    r3 = new_node new_token(106)
    r3.add_child new_node( new_token 107 )
    r0.add_child r3
    r0.add_child new_node( new_token 108 )
    r0.add_child new_node( new_token 109 )
    
    stream = CommonTreeNodeStream.new(r0)
    stream.consume until stream.peek(1) == EOF
    
    index_of_102 = 2
    index_of_104 = 6
    stream.look(1).type.should == EOF
    
    stream.push(index_of_102)
    [102, DOWN, 103].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    stream.look(1).type.should == UP
    
    stream.pop
    stream.look(1).type.should == EOF
    
    stream.push(index_of_104)
    [104, DOWN, 105].each do |val|
      stream.look(1).type.should == val
      stream.consume
    end
    stream.look(1).type.should == UP
    
    stream.pop
    stream.look(1).type.should == EOF
  end
  
  
  def new_token(type, opts = {})
    opts[:type] = type
    CommonToken.create(opts)
  end
  def new_node(token)
    CommonTree.new(token)
  end
end


class TestCommonTree < Test::Unit::TestCase
  def setup
    @adaptor = CommonTreeAdaptor.new
  end
  def teardown
    # after-each-test code
  end
  
  # vvvvvvvv tests vvvvvvvvv
  
  def test_single_node
    t = new_node( new_token 101 )
    assert_nil t.parent
    t.child_index.should == -1
  end
  
  def test_4_nodes
    # ^(101 ^(102 103) 104)
    r0 = new_node( new_token 101 )
    r0.add_child new_node( new_token 102 )
    r0.child(0).add_child new_node( new_token 103 )
    r0.add_child new_node( new_token 104 )
    
    assert_nil r0.parent
    r0.child_index.should == -1
  end
  
  def test_list
    # ^(nil 101 102 103)
    r0 = CommonTree.new(nil)
    c0 = new_node( new_token 101 )
    r0.add_child c0
    c1 = new_node( new_token 102 )
    r0.add_child c1
    c2 = new_node( new_token 103 )
    r0.add_child c2
    
    assert_nil r0.parent
    r0.child_index.should == -1
    c0.parent.should == r0
    c0.child_index.should == 0
    c1.parent.should == r0
    c1.child_index.should == 1
    c2.parent.should == r0
    c2.child_index.should == 2
  end
  
  def test_list2
    # ^(nil 101 102 103)
    root = new_node( new_token 5 )
    r0 = CommonTree.new(nil)
    c0 = new_node( new_token 101 )
    r0.add_child c0
    c1 = new_node( new_token 102 )
    r0.add_child c1
    c2 = new_node( new_token 103 )
    r0.add_child c2
    
    root.add_child r0
    
    assert_nil root.parent
    root.child_index.should == -1
    c0.parent.should == root
    c0.child_index.should == 0
    c1.parent.should == root            # note -- actual python tests all use c0 here, which i think might be wrong
    c1.child_index.should == 1
    c2.parent.should == root            # note -- actual python tests all use c0 here, which i think might be wrong
    c2.child_index.should == 2
  end
  
  def test_add_list_to_exist_children
    root = new_node( new_token 5 )
    root.add_child new_node( new_token 6 )
    
    r0 = CommonTree.new(nil)
    c0 = new_node( new_token 101 )
    r0.add_child c0
    c1 = new_node( new_token 102 )
    r0.add_child c1
    c2 = new_node( new_token 103 )
    r0.add_child c2
    # ^(nil c0=101 c1=102 c2=103)
    
    root.add_child(r0)
    
    assert_nil root.parent
    root.child_index.should == -1
    c0.parent.should == root
    c0.child_index.should == 1
    c1.parent.should == root
    c1.child_index.should == 2
    c2.parent.should == root
    c2.child_index.should == 3
  end
  
  def test_copy_tree
    r0 = new_node( new_token 101 )
    r1 = new_node( new_token 102 )
    r2 = new_node( new_token 106 )
    r0.add_child( r1 )
    r1.add_child( new_node( new_token 103 ) )
    r2.add_child( new_node( new_token 107 ) )
    r1.add_child( r2 )
    r0.add_child( new_node( new_token 104 ) )
    r0.add_child( new_node( new_token 105 ) )
    
    dup = @adaptor.copy_tree( r0 )
    assert_nil dup.parent
    dup.child_index.should == -1
    dup.sanity_check
  end
  
  def test_become_root
    new_root = new_node( new_token 5 )
    
    old_root = new_node nil
    old_root.add_child( new_node( new_token 101 ) )
    old_root.add_child( new_node( new_token 102 ) )
    old_root.add_child( new_node( new_token 103 ) )
    
    @adaptor.become_root(new_root, old_root)
    new_root.sanity_check
  end
  
  def test_become_root2
    new_root = new_node( new_token 5 )
    
    old_root = new_node( new_token 101 )
    old_root.add_child( new_node( new_token 102 ) )
    old_root.add_child( new_node( new_token 103 ) )
    
    @adaptor.become_root(new_root, old_root)
    new_root.sanity_check
  end
  
  def test_become_root3
    new_root = new_node nil
    new_root.add_child( new_node( new_token 5 ) )
    
    old_root = new_node nil
    old_root.add_child( new_node( new_token 101 ) )
    old_root.add_child( new_node( new_token 102 ) )
    old_root.add_child( new_node( new_token 103 ) )
    
    @adaptor.become_root(new_root, old_root)
    new_root.sanity_check
  end
  
  def test_become_root5
    new_root = new_node nil
    new_root.add_child( new_node( new_token 5 ) )
    
    old_root = new_node( new_token 101 )
    old_root.add_child( new_node( new_token 102 ) )
    old_root.add_child( new_node( new_token 103 ) )
    
    @adaptor.become_root(new_root, old_root)
    new_root.sanity_check
  end
  
  def test_become_root6
    root_0 = @adaptor.create_flat_list
    root_1 = @adaptor.create_flat_list
    root_1 = @adaptor.become_root( new_node( new_token 5 ), root_1 )
    
    @adaptor.add_child( root_1, new_node( new_token 6 ) )
    @adaptor.add_child( root_0, root_1 )
    root_0.sanity_check
  end
  
  def test_replace_with_no_children
    t = new_node( new_token 101 )
    new_child = new_node( new_token 5 )
    error = false
    assert_raise(IndexError) do
      t.replace_children(0, 0, new_child)
    end
  end
  
  def test_replace_with_one_children
    t = new_node( new_token 99, :text => 'a' )
    c0 = new_node( new_token 99, :text => 'b' )
    t.add_child(c0)
    
    new_child = new_node( new_token 99, :text => 'c' )
    t.replace_children(0,0,new_child)
    
    t.inspect.should == '(a c)'
    t.sanity_check

  end
  def test_replace_in_middle
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    t.replace_children(1, 1, new_child)
    t.inspect.should == '(a b x d)'
    t.sanity_check
  end
  
  def test_replace_at_left
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    t.replace_children(0, 0, new_child)
    t.inspect.should == '(a x c d)'
    t.sanity_check
  end
  
  def test_replace_at_left
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    t.replace_children(2, 2, new_child)
    t.inspect.should == '(a b c x)'
    t.sanity_check
  end
  
  def test_replace_one_with_two_at_left
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_children = @adaptor.create_flat_list
    new_children.add_child new_node( new_token 99, :text => 'x' )
    new_children.add_child new_node( new_token 99, :text => 'y' )
    
    t.replace_children(0, 0, new_children)
    t.inspect.should == '(a x y c d)'
    t.sanity_check
  end
  
  def test_replace_one_with_two_at_right
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_children = @adaptor.create_flat_list
    new_children.add_child new_node( new_token 99, :text => 'x' )
    new_children.add_child new_node( new_token 99, :text => 'y' )
    
    t.replace_children(2, 2, new_children)
    t.inspect.should == '(a b c x y)'
    t.sanity_check
  end
  
  def test_replace_one_with_two_in_middle
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_children = @adaptor.create_flat_list
    new_children.add_child new_node( new_token 99, :text => 'x' )
    new_children.add_child new_node( new_token 99, :text => 'y' )
    
    t.replace_children(1, 1, new_children)
    t.inspect.should == '(a b x y d)'
    t.sanity_check
  end
  
  def test_replace_two_with_one_at_left
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    
    t.replace_children(0, 1, new_child)
    t.inspect.should == '(a x d)'
    t.sanity_check
  end
  
  def test_replace_two_with_one_at_right
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    
    t.replace_children(1, 2, new_child)
    t.inspect.should == '(a b x)'
    t.sanity_check
  end
  
  def test_replace_all_with_one
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_child = new_node( new_token 99, :text => 'x' )
    
    t.replace_children(0, 2, new_child)
    t.inspect.should == '(a x)'
    t.sanity_check
  end
  
  def test_replace_all_with_two
    t = new_node( new_token 99, :text => 'a' )
    t.add_child new_node( new_token 99, :text => 'b' )
    t.add_child new_node( new_token 99, :text => 'c' )
    t.add_child new_node( new_token 99, :text => 'd' )
    
    new_children = @adaptor.create_flat_list
    new_children.add_child new_node( new_token 99, :text => 'x' )
    new_children.add_child new_node( new_token 99, :text => 'y' )
    
    t.replace_children(0, 1, new_children)
    t.inspect.should == '(a x y d)'
    t.sanity_check
  end
  
  def new_token(type, opts = {})
    opts[:type] = type
    CommonToken.create(opts)
  end
  def new_node(token)
    CommonTree.new(token)
  end
end


class TestTreeContext < Test::Unit::TestCase
  TOKEN_NAMES = %w(
    <invalid> <EOR> <DOWN> <UP> VEC ASSIGN PRINT
    PLUS MULT DOT ID INT WS '[' ',' ']'
  )
  Tokens = TokenScheme.build( TOKEN_NAMES )
  
  def setup
    @wizard = Wizard.new( :token_scheme => Tokens )
  end
  
  def teardown
    # after-each-test code
  end
  
  # vvvvvvvv tests vvvvvvvvv
  
  def test_simple_parent
    tree = @wizard.create(
      "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))"
    )
    labels = @wizard.match( tree,
      "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))"
    )
    
    assert_kind_of( Hash, labels )
    @wizard.in_context?( labels.fetch( 'x' ), 'VEC' ).should be_true
  end
  
  def test_no_parent
    tree = @wizard.create(
      '(PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3])))'
    )
    
    labels = @wizard.match( tree, "(%x:PRINT (MULT ID (VEC INT INT INT)))" )
    assert_kind_of( Hash, labels )
    @wizard.in_context?( labels.fetch( 'x' ), 'VEC' ).should be_false
  end
  
  def test_parent_with_wildcard
    tree = @wizard.create(
      "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))"
    )
    
    labels = @wizard.match( tree,
      "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))"
    )
    assert_kind_of( Hash, labels )
    node = labels.fetch( 'x' )
    @wizard.in_context?( node, 'VEC ...' ).should be_true
  end
end
Back to Top