android_external_antlr /antlr-3.4/runtime/Ruby/test/unit/test-streams.rb

Language Ruby Lines 461
MD5 Hash 0e2a60373868f8be83a0a06d76ec5698 Estimated Cost $9,539 (why?)
Repository https://bitbucket.org/cyanogenmod/android_external_antlr.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
#!/usr/bin/ruby
# encoding: utf-8

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


include ANTLR3

class TestStringStream < Test::Unit::TestCase
  def setup
    @stream = StringStream.new( "oh\nhey!\n" )
  end
  
  def test_size
    @stream.size.should == 8
  end
  
  def test_index
    @stream.index.should == 0
  end
  
  def test_consume
    @stream.consume # o
    @stream.index.should == 1
    @stream.column.should == 1
    @stream.line.should == 1
    
    @stream.consume  # h
    @stream.index.should == 2
    @stream.column.should == 2
    @stream.line.should == 1
    
    @stream.consume # \n
    @stream.index.should == 3
    @stream.column.should == 0
    @stream.line.should == 2
    
    @stream.consume #  h
    @stream.index.should == 4
    @stream.column.should == 1
    @stream.line.should == 2
    
    @stream.consume # e
    @stream.index.should == 5
    @stream.column.should == 2
    @stream.line.should == 2
    
    @stream.consume # y
    @stream.index.should == 6
    @stream.column.should == 3
    @stream.line.should == 2
    
    @stream.consume # !
    @stream.index.should == 7
    @stream.column.should == 4
    @stream.line.should == 2
    
    @stream.consume # \n
    @stream.index.should == 8
    @stream.column.should == 0
    @stream.line.should == 3
    
    @stream.consume # EOF
    @stream.index.should == 8
    @stream.column.should == 0
    @stream.line.should == 3
    
    @stream.consume # EOF
    @stream.index.should == 8
    @stream.column.should == 0
    @stream.line.should == 3
  end
  
  def test_reset
    2.times { @stream.consume }
    @stream.reset
    @stream.index.should == 0
    @stream.line.should == 1
    @stream.column.should == 0
    @stream.peek(1).should == ?o.ord
  end
  
  def test_look
    @stream.look(1).should == 'o'
    @stream.look(2).should == 'h'
    @stream.look(3).should == "\n"
    @stream.peek(1).should == ?o.ord
    @stream.peek(2).should == ?h.ord
    @stream.peek(3).should == ?\n.ord
    
    6.times { @stream.consume }
    @stream.look(1).should == '!'
    @stream.look(2).should == "\n"
    @stream.look(3).should be_nil
    @stream.peek(1).should == ?!.ord
    @stream.peek(2).should == ?\n.ord
    @stream.peek(3).should == EOF
  end
  
  def test_substring
    @stream.substring(0,0).should == 'o'
    @stream.substring(0,1).should == 'oh'
    @stream.substring(0,8).should == "oh\nhey!\n"
    @stream.substring(3,6).should == "hey!"
  end
  
  def test_seek_forward
    @stream.seek(3)
    @stream.index.should == 3
    @stream.line.should == 2
    @stream.column.should == 0
    @stream.peek(1).should == ?h.ord
  end
  
  def test_mark
    @stream.seek(4)
    marker = @stream.mark
    marker.should == 1
    
    2.times { @stream.consume }
    marker = @stream.mark
    
    marker.should == 2
  end
  
  def test_release_last
    @stream.seek(4)
    marker1 = @stream.mark
    
    2.times { @stream.consume }
    marker2 = @stream.mark
    
    @stream.release
    @stream.mark_depth.should == 2
    @stream.release
    @stream.mark_depth.should == 1
  end
  
  def test_release_nested
    @stream.seek(4)
    marker1 = @stream.mark()
    
    @stream.consume()
    marker2 = @stream.mark()
    
    @stream.consume()
    marker3 = @stream.mark()
    
    @stream.release(marker2)
    @stream.mark_depth.should == 2

  end
  
  def test_rewind_last
    @stream.seek(4)

    marker = @stream.mark
    @stream.consume
    @stream.consume

    @stream.rewind
    @stream.mark_depth.should == 1
    @stream.index.should == 4
    @stream.line.should == 2
    @stream.column.should == 1
    @stream.peek(1).should == ?e.ord
    
  end

  def test_through
    @stream.through( 2 ).should == 'oh'
    @stream.through( -2 ).should == ''
    @stream.seek( 5 )
    @stream.through( 0 ).should == ''
    @stream.through( 1 ).should == 'y'
    @stream.through( -2 ).should == 'he'
    @stream.through( 5 ).should == "y!\n"
  end
  
  def test_rewind_nested
    @stream.seek(4)
    marker1 = @stream.mark()
    
    @stream.consume
    marker2 = @stream.mark
    
    @stream.consume
    marker3 = @stream.mark
    
    @stream.rewind(marker2)
    @stream.mark_depth.should == 2
    @stream.index().should == 5
    @stream.line.should == 2
    @stream.column.should == 2
    @stream.peek(1).should == ?y.ord    
  end
end

class TestFileStream < Test::Unit::TestCase
  
  
  def test_no_encoding
    
    path = File.join(File.dirname(__FILE__), 'sample-input/file-stream-1')
    @stream = FileStream.new(path)
    
    @stream.seek(4)
    marker1 = @stream.mark()
    
    @stream.consume()
    marker2 = @stream.mark()
    
    @stream.consume()
    marker3 = @stream.mark()
    
    @stream.rewind(marker2)
    @stream.index().should == 5
    @stream.line.should == 2
    @stream.column.should == 1
    @stream.mark_depth.should == 2
    @stream.look(1).should == 'a'
    @stream.peek(1).should == ?a.ord
  end
  
  def test_encoded
    
  end
end

class TestInputStream < Test::Unit::TestCase
  def test_no_encoding
    
  end
  
  def test_encoded
    
  end
end

class TestCommonTokenStream < Test::Unit::TestCase
  class MockSource
    include ANTLR3::TokenSource
    attr_accessor :tokens
    def initialize
      @tokens = []
    end
    def next_token
      @tokens.shift
    end
  end
  
  # vvvvvvvv tests vvvvvvvvv
  def test_init
    @source = MockSource.new
    @stream = CommonTokenStream.new( @source )
    @stream.position.should == 0
  end
  
  def test_rebuild
    @source1 = MockSource.new
    @source2 = MockSource.new
    @source2.tokens << new_token( 10, :channel => ANTLR3::HIDDEN ) << new_token( 11 )
    @stream = CommonTokenStream.new( @source1 )
    
    @stream.position.should == 0
    @stream.tokens.length.should == 0
    
    @stream.rebuild( @source2 )
    @stream.token_source.should == @source2
    @stream.position.should == 1
    @stream.tokens.should have( 2 ).things
  end
  
  def test_look_empty_source
    @source = MockSource.new
    @stream = CommonTokenStream.new(@source)
    @stream.look.should == ANTLR3::EOF_TOKEN
  end
  
  def test_look1
    @source = MockSource.new
    @source.tokens << new_token(12)
    @stream = CommonTokenStream.new(@source)
    @stream.look(1).type.should == 12
  end
  
  def test_look1_with_hidden
    # FIX
    @source = MockSource.new
    @source.tokens << new_token(12, :channel => ANTLR3::HIDDEN_CHANNEL) <<
      new_token(13)
    @stream = CommonTokenStream.new(@source)
    @stream.look(1).type.should == 13
  end
  
  def test_look2_beyond_end
    @source = MockSource.new
    @source.tokens << new_token(12) <<
      new_token(13, :channel => ANTLR3::HIDDEN_CHANNEL)
    
    @stream = CommonTokenStream.new(@source)
    @stream.look(2).type.should == EOF
  end
  
  def test_look_negative
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13)
    @stream = CommonTokenStream.new(@source)
    @stream.consume
    
    @stream.look(-1).type.should == 12
  end
  
  def test_lb1
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13)
    @stream = CommonTokenStream.new(@source)
    
    @stream.consume
    @stream.look(-1).type.should == 12
  end
  
  def test_look_zero
    # FIX
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13)
    @stream = CommonTokenStream.new(@source)
    @stream.look(0).should == nil
  end
  
  def test_lb_beyond_begin
    @source = MockSource.new
    @source.tokens << new_token(10) <<
      new_token(11, :channel => HIDDEN_CHANNEL) <<
      new_token(12, :channel => HIDDEN_CHANNEL) <<
      new_token(13)
    @stream = CommonTokenStream.new(@source)
    
    @stream.look(-1).should == nil
    2.times { @stream.consume }
    @stream.look(-3).should == nil
  end
  
  def test_fill_buffer
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13) <<  new_token(14) << new_token(EOF)
    @stream = CommonTokenStream.new(@source)
    
    @stream.instance_variable_get(:@tokens).length.should == 3
    @stream.tokens[0].type.should == 12
    @stream.tokens[1].type.should == 13
    @stream.tokens[2].type.should == 14
  end
  
  def test_consume
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13) << new_token(EOF)
    @stream = CommonTokenStream.new(@source)
    @stream.peek.should == 12
    @stream.consume
    @stream.peek.should == 13
    @stream.consume
    @stream.peek.should == EOF
    @stream.consume
    @stream.peek.should == EOF
  end
  
  def test_seek
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13) << new_token(EOF)
    @stream = CommonTokenStream.new(@source)
    
    @stream.peek(1).should == 12
    @stream.seek(2).peek.should == EOF
    @stream.seek(0).peek.should == 12
    @stream.seek(-3).position.should == 0
    @stream.seek(10).position.should == 2
  end
  
  def test_mark_rewind
    @source = MockSource.new
    @source.tokens << new_token(12) << new_token(13) << new_token(EOF)
    @stream = CommonTokenStream.new(@source)
    @stream.consume
    marker = @stream.mark
    @stream.consume
    @stream.rewind(marker)
    @stream.peek(1).should == 13
  end
  
  def test_to_string
    @source = MockSource.new
    @source.tokens << new_token(12, 'foo') <<
      new_token(13, 'bar') << new_token(14, 'gnurz') <<
      new_token(15, 'blarz')
    @stream = CommonTokenStream.new(@source)
    @stream.to_s.should == "foobargnurzblarz"
    @stream.to_s(1,2).should == 'bargnurz'
    @stream.to_s(@stream[1], @stream[-2]).should == 'bargnurz'
  end

  def new_token(type, opts = {})
    fields = {}
    case type
    when Hash then fields.update(type)
    else
      fields[:type] = type
    end
    case opts
    when Hash then fields.update(opts)
    when String then fields[:text] = opts
    end
    CommonToken.create(fields)
  end
  
end


__END__
teststreams.py                                | LN  | STATUS
----------------------------------------------+-----+--------------
class TestStringStream(unittest.TestCase)     | 009 | [x]
  def testSize(self)                          | 012 | [x]
  def testIndex(self)                         | 020 | [x]
  def testConsume(self)                       | 028 | [x]
  def testReset(self)                         | 079 | [x]
  def testLA(self)                            | 094 | [x]
  def testSubstring(self)                     | 111 | [x]
  def testSeekForward(self)                   | 122 | [x]
  def testMark(self)                          | 150 | [x]
  def testReleaseLast(self)                   | 167 | [x]
  def testReleaseNested(self)                 | 186 | [x]
  def testRewindLast(self)                    | 204 | [x]
  def testRewindNested(self)                  | 223 | [x]
class TestFileStream(unittest.TestCase)       | 245 | [o]
  def testNoEncoding(self)                    | 249 | [x]
  def testEncoded(self)                       | 272 | [ ]
class TestInputStream(unittest.TestCase)      | 296 | [ ]
  def testNoEncoding(self)                    | 299 | [ ]
  def testEncoded(self)                       | 322 | [ ]
class TestCommonTokenStream(unittest.TestCase)| 345 | [ ]
  def setUp(self)                             | 348 | [x]
  def testInit(self)                          | 369 | [x]
  def testSetTokenSource(self)                | 376 | [x]
  def testLTEmptySource(self)                 | 385 | [x]
  def testLT1(self)                           | 394 | [x]
  def testLT1WithHidden(self)                 | 407 | [x]
  def testLT2BeyondEnd(self)                  | 424 | [x]
  def testLTNegative(self)                    | 442 | [x]
  def testLB1(self)                           | 461 | [x]
  def testLTZero(self)                        | 479 | [x]
  def testLBBeyondBegin(self)                 | 496 | [x]
  def testFillBuffer(self)                    | 523 | [x]
  def testConsume(self)                       | 551 | [x]
  def testSeek(self)                          | 579 | [x]
  def testMarkRewind(self)                    | 604 | [x]
  def testToString(self)                      | 631 | [x]
Back to Top