antlr3 /runtime/Ruby/test/unit/test-tree-wizard.rb

Language Ruby Lines 495
MD5 Hash 18328420c75b3c73c074824dc5720c44 Estimated Cost $9,593 (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
#!/usr/bin/ruby
# encoding: utf-8

require 'antlr3'
require 'antlr3/tree/wizard'
require 'test/unit'
require 'spec'

include ANTLR3
include ANTLR3::AST

class TestPatternLexer < Test::Unit::TestCase
  
  # vvvvvvvv tests vvvvvvvvv
  
  def test_open
    lexer = Wizard::PatternLexer.new( '(' )
    type = lexer.next_token
    assert_equal( type, :open )
    assert_equal( lexer.text, '' )
    assert_equal( lexer.error, false )
  end
  
  def test_close
    lexer = Wizard::PatternLexer.new(')')
    type = lexer.next_token
    assert_equal(type, :close)
    assert_equal(lexer.text, '')
    assert_equal(lexer.error, false)
  end
  
  def test_percent
    lexer = Wizard::PatternLexer.new('%')
    type = lexer.next_token
    assert_equal(type, :percent)
    assert_equal(lexer.text, '')
    assert_equal(lexer.error, false)
  end
  
  def test_dot
    lexer = Wizard::PatternLexer.new('.')
    type = lexer.next_token
    assert_equal(type, :dot)
    assert_equal(lexer.text, '')
    assert_equal(lexer.error, false)
  end
  
  def test_eof
    lexer = Wizard::PatternLexer.new(" \n \r \t ")
    type = lexer.next_token
    assert_equal(type, EOF)
    assert_equal(lexer.text, '')
    assert_equal(lexer.error, false)
  end
  
  def test_id
    lexer = Wizard::PatternLexer.new('__whatever_1__')
    type = lexer.next_token
    assert_equal(:identifier, type)
    assert_equal('__whatever_1__', lexer.text)
    assert( !(lexer.error) )
  end
  
  def test_arg
    lexer = Wizard::PatternLexer.new('[ \]bla\n]')
    type = lexer.next_token
    assert_equal(type, :argument)
    assert_equal(' ]bla\n', lexer.text)
    assert( !(lexer.error) )
  end
  
  def test_error
    lexer = Wizard::PatternLexer.new("1")
    type = lexer.next_token
    assert_equal(type, EOF)
    assert_equal(lexer.text, '')
    assert_equal(lexer.error, true)
  end
  
end


class TestPatternParser < Test::Unit::TestCase
  Tokens = TokenScheme.build %w(A B C D E ID VAR)
  include Tokens
  
  def setup
    @adaptor = CommonTreeAdaptor.new( Tokens.token_class )
    @pattern_adaptor = Wizard::PatternAdaptor.new( Tokens.token_class )
    @wizard = Wizard.new( :adaptor => @adaptor, :token_scheme => Tokens )
  end
  
  # vvvvvvvv tests vvvvvvvvv
  def test_single_node
    tree = Wizard::PatternParser.parse( 'ID', Tokens, @adaptor )
    
    assert_instance_of(CommonTree, tree)
    assert_equal( ID, tree.type )
    assert_equal( 'ID', tree.text )
  end
  
  def test_single_node_with_arg
    tree = Wizard::PatternParser.parse( 'ID[foo]', Tokens, @adaptor )
    
    assert_instance_of( CommonTree, tree )
    assert_equal( ID, tree.type )
    assert_equal( 'foo', tree.text )
  end
  
  def test_single_level_tree
    tree = Wizard::PatternParser.parse( '(A B)', Tokens, @adaptor )
    
    assert_instance_of( CommonTree, tree )
    assert_equal(A, tree.type)
    assert_equal('A', tree.text)
    assert_equal(tree.child_count, 1)
    assert_equal(tree.child(0).type, B)
    assert_equal(tree.child(0).text, 'B')
  end
  
  def test_nil
    tree = Wizard::PatternParser.parse( 'nil', Tokens, @adaptor )
    
    assert_instance_of(CommonTree, tree)
    assert_equal(0, tree.type)
    assert_nil tree.text
  end
  
  def test_wildcard
    tree = Wizard::PatternParser.parse( '(.)', Tokens, @adaptor )
    assert_instance_of( Wizard::WildcardPattern, tree )
  end
  
  def test_label
    tree = Wizard::PatternParser.parse( '(%a:A)', Tokens, @pattern_adaptor )
    assert_instance_of(Wizard::Pattern, tree)
    assert_equal('a', tree.label)
  end
  
  def test_error_1
    tree = Wizard::PatternParser.parse( ')', Tokens, @adaptor )
    assert_nil tree
  end
  
  def test_error_2
    tree = Wizard::PatternParser.parse( '()', Tokens, @adaptor )
    assert_nil tree
  end
  
  def test_error_3
    tree = Wizard::PatternParser.parse( '(A ])', Tokens, @adaptor )
    assert_nil tree
  end
  
end


class TestTreeWizard < Test::Unit::TestCase
  Tokens = TokenScheme.build %w(A B C D E ID VAR)
  include Tokens

  def setup
    @adaptor = CommonTreeAdaptor.new( Tokens.token_class )
    @wizard = Wizard.new( :adaptor => @adaptor, :token_scheme => Tokens )
  end
  
  def create_wizard( tokens )
    Wizard.new( :tokens => tokens )
  end
  
  # vvvvvvvv tests vvvvvvvvv
  def test_init
    @wizard = Wizard.new( :tokens => %w(A B), :adaptor => @adaptor )
    
    assert_equal( @wizard.adaptor, @adaptor )
    assert_kind_of( ANTLR3::TokenScheme, @wizard.token_scheme )
  end
  
  def test_single_node
    t = @wizard.create("ID")
    assert_equal(t.inspect, 'ID')
  end
  
  def test_single_node_with_arg
    t = @wizard.create("ID[foo]")
    
    assert_equal(t.inspect, 'foo')
  end
  
  def test_single_node_tree
    t = @wizard.create("(A)")
    assert_equal(t.inspect, 'A')
  end
  
  def test_single_level_tree
    t = @wizard.create("(A B C D)")
    assert_equal(t.inspect, '(A B C D)')
  end
  
  def test_list_tree
    t = @wizard.create("(nil A B C)")
    assert_equal(t.inspect, 'A B C')
  end
  
  def test_invalid_list_tree
    t = @wizard.create("A B C")
    assert_nil t
  end
  
  def test_double_level_tree
    t = @wizard.create("(A (B C) (B D) E)")
    assert_equal(t.inspect, "(A (B C) (B D) E)")
  end
  
  SIMPLIFY_MAP = lambda do |imap|
    Hash[
      imap.map { |type, nodes| [type, nodes.map { |n| n.to_s }] }
    ]
  end
  
  def test_single_node_index
    tree = @wizard.create("ID")
    index_map = SIMPLIFY_MAP[@wizard.index(tree)]
    
    assert_equal(index_map, ID => %w(ID))
  end
  
  
  def test_no_repeats_index
    tree = @wizard.create("(A B C D)")
    index_map = SIMPLIFY_MAP[@wizard.index(tree)]
    
    assert_equal(index_map,
        D => %w(D), B => %w(B),
        C => %w(C), A => %w(A)
    )
  end
  
  def test_repeats_index
    tree = @wizard.create("(A B (A C B) B D D)")
    index_map = SIMPLIFY_MAP[@wizard.index(tree)]
    
    assert_equal(index_map,
        D => %w(D D), B => %w(B B B),
        C => %w(C), A => %w(A A)
    )
  end
  
  
  def test_no_repeats_visit
    tree = @wizard.create("(A B C D)")
    
    elements = []
    @wizard.visit( tree, B ) do |node, parent, child_index, labels|
      elements << node.to_s
    end
    
    assert_equal( %w(B), elements )
  end
  
  
  def test_no_repeats_visit2
    tree = @wizard.create("(A B (A C B) B D D)")
    
    elements = []
    @wizard.visit( tree, C ) do |node, parent, child_index, labels|
      elements << node.to_s
    end
    
    assert_equal(%w(C), elements)
  end
  
  
  def test_repeats_visit
    tree = @wizard.create("(A B (A C B) B D D)")
    
    elements = []
    @wizard.visit( tree, B ) do |node, parent, child_index, labels|
      elements << node.to_s
    end
    
    assert_equal(%w(B B B), elements)
  end
  
  
  def test_repeats_visit2
    tree = @wizard.create("(A B (A C B) B D D)")
    
    elements = []
    @wizard.visit( tree, A ) do |node, parent, child_index, labels|
      elements << node.to_s
    end
    
    assert_equal(%w(A A), elements)
  end
  
  def context(node, parent, index)
    '%s@%s[%d]' % [node.to_s, (parent || 'nil').to_s, index]
  end
  
  def test_repeats_visit_with_context
    tree = @wizard.create("(A B (A C B) B D D)")
    
    elements = []
    @wizard.visit( tree, B ) do |node, parent, child_index, labels|
      elements << context(node, parent, child_index)
    end
    
    assert_equal(['B@A[0]', 'B@A[1]', 'B@A[2]'], elements)
  end
  
  
  def test_repeats_visit_with_null_parent_and_context
    tree = @wizard.create("(A B (A C B) B D D)")
    
    elements = []
    @wizard.visit( tree, A ) do |node, parent, child_index, labels|
      elements << context(node, parent, child_index)
    end
    
    assert_equal(['A@nil[-1]', 'A@A[1]'], elements)
  end
  
  def test_visit_pattern
    tree = @wizard.create("(A B C (A B) D)")
    
    elements = []
    @wizard.visit(tree, '(A B)') do |node, parent, child_index, labels|
      elements << node.to_s
    end
    
    assert_equal(%w(A), elements)
  end
  
  
  def test_visit_pattern_multiple
    tree = @wizard.create("(A B C (A B) (D (A B)))")
    
    elements = []
    @wizard.visit(tree, '(A B)') do |node, parent, child_index, labels|
      elements << context(node, parent, child_index)
    end
    
    assert_equal( %w(A@A[2] A@D[0]) , elements )
  end
  
  def labeled_context(node, parent, index, labels, *names)
    suffix = names.map { |n| labels[n].to_s }.join('&')
    '%s@%s[%d]%s' % [node.to_s, (parent || 'nil').to_s, index, suffix]
  end
    
  def test_visit_pattern_multiple_with_labels
    tree = @wizard.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))")
    
    elements = []
    @wizard.visit(tree, '(%a:A %b:B)') do |node, parent, child_index, labels|
      elements << labeled_context(node, parent, child_index, labels, 'a', 'b')
    end
    
    assert_equal( ['foo@A[2]foo&bar', 'big@D[0]big&dog'] , elements )
  end
  
  
  def test_match
    tree = @wizard.create("(A B C)")
    assert @wizard.match(tree, "(A B C)")
  end
  
  def test_match_single_node
    tree = @wizard.create('A')
    assert @wizard.match(tree, 'A')
  end
  
  def test_match_single_node_fails
    tree = @wizard.create('A')
    assert( !(@wizard.match(tree, 'B')) )
  end
  
  
  def test_match_flat_tree
    tree = @wizard.create('(nil A B C)')
    assert @wizard.match(tree, '(nil A B C)')
  end
  
  def test_match_flat_tree_fails
    tree = @wizard.create('(nil A B C)')
    assert( !(@wizard.match(tree, '(nil A B)')) )
  end

  def test_match_flat_tree_fails2
    tree = @wizard.create('(nil A B C)')
    assert( !(@wizard.match(tree, '(nil A B A)')) )
  end
  
  def test_wildcard
    tree = @wizard.create('(A B C)')
    assert @wizard.match(tree, '(A . .)')
  end
  
  def test_match_with_text
    tree = @wizard.create('(A B[foo] C[bar])')
    assert @wizard.match(tree, '(A B[foo] C)')
  end
  
  def test_match_with_text_fails
    tree = @wizard.create('(A B C)')
    assert( !(@wizard.match(tree, '(A[foo] B C)')) )
  end
  
  def test_match_labels
    tree = @wizard.create('(A B C)')
    labels = @wizard.match( tree, '(%a:A %b:B %c:C)' )
    
    assert_equal('A', labels['a'].to_s)
    assert_equal('B', labels['b'].to_s)
    assert_equal('C', labels['c'].to_s)
  end
  
  def test_match_with_wildcard_labels
    tree = @wizard.create('(A B C)')
    labels = @wizard.match(tree, '(A %b:. %c:.)')
    assert_kind_of( Hash, labels )
    assert_equal('B', labels['b'].to_s)
    assert_equal('C', labels['c'].to_s)
  end
  
  
  def test_match_labels_and_test_text
    tree = @wizard.create('(A B[foo] C)')
    labels = @wizard.match( tree, '(%a:A %b:B[foo] %c:C)' )
    assert_kind_of( Hash, labels )
    assert_equal('A', labels['a'].to_s)
    assert_equal('foo', labels['b'].to_s)
    assert_equal('C', labels['c'].to_s)
  end
  
  def test_match_labels_in_nested_tree
    tree = @wizard.create('(A (B C) (D E))')
    labels = @wizard.match( tree, '(%a:A (%b:B %c:C) (%d:D %e:E))' )
    assert_kind_of( Hash, labels )
    assert_equal('A', labels['a'].to_s)
    assert_equal('B', labels['b'].to_s)
    assert_equal('C', labels['c'].to_s)
    assert_equal('D', labels['d'].to_s)
    assert_equal('E', labels['e'].to_s)
  end
  
  
  def test_equals
    tree1 = @wizard.create("(A B C)")
    tree2 = @wizard.create("(A B C)")
    assert @wizard.equals(tree1, tree2)
  end
  
  
  def test_equals_with_text
    tree1 = @wizard.create("(A B[foo] C)")
    tree2 = @wizard.create("(A B[foo] C)")
    assert @wizard.equals(tree1, tree2)
  end
  
  
  def test_equals_with_mismatched_text
    tree1 = @wizard.create("(A B[foo] C)")
    tree2 = @wizard.create("(A B C)")
    assert( !(@wizard.equals(tree1, tree2)) )
  end
  
  
  def test_equals_with_mismatched_list
    tree1 = @wizard.create("(A B C)")
    tree2 = @wizard.create("(A B A)")
    assert( !(@wizard.equals(tree1, tree2)) )
  end
  
  def test_equals_with_mismatched_list_length
    tree1 = @wizard.create("(A B C)")
    tree2 = @wizard.create("(A B)")
    assert( !(@wizard.equals(tree1, tree2)) )
  end
  
  def test_find_pattern
    tree = @wizard.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))")
    subtrees = @wizard.find(tree, "(A B)").map { |t| t.to_s }
    assert_equal(%w(foo big), subtrees)
  end
  
  def test_find_token_type
    tree = @wizard.create("(A B C (A[foo] B[bar]) (D (A[big] B[dog])))")
    subtrees = @wizard.find( tree, A ).map { |t| t.to_s }
    assert_equal(%w(A foo big), subtrees)
  end
end
Back to Top