1#!/usr/bin/ruby 2# encoding: utf-8 3 4require 'antlr3' 5require 'test/unit' 6require 'spec' 7 8 9include ANTLR3 10 11class TestStringStream < Test::Unit::TestCase 12 def setup 13 @stream = StringStream.new( "oh\nhey!\n" ) 14 end 15 16 def test_size 17 @stream.size.should == 8 18 end 19 20 def test_index 21 @stream.index.should == 0 22 end 23 24 def test_consume 25 @stream.consume # o 26 @stream.index.should == 1 27 @stream.column.should == 1 28 @stream.line.should == 1 29 30 @stream.consume # h 31 @stream.index.should == 2 32 @stream.column.should == 2 33 @stream.line.should == 1 34 35 @stream.consume # \n 36 @stream.index.should == 3 37 @stream.column.should == 0 38 @stream.line.should == 2 39 40 @stream.consume # h 41 @stream.index.should == 4 42 @stream.column.should == 1 43 @stream.line.should == 2 44 45 @stream.consume # e 46 @stream.index.should == 5 47 @stream.column.should == 2 48 @stream.line.should == 2 49 50 @stream.consume # y 51 @stream.index.should == 6 52 @stream.column.should == 3 53 @stream.line.should == 2 54 55 @stream.consume # ! 56 @stream.index.should == 7 57 @stream.column.should == 4 58 @stream.line.should == 2 59 60 @stream.consume # \n 61 @stream.index.should == 8 62 @stream.column.should == 0 63 @stream.line.should == 3 64 65 @stream.consume # EOF 66 @stream.index.should == 8 67 @stream.column.should == 0 68 @stream.line.should == 3 69 70 @stream.consume # EOF 71 @stream.index.should == 8 72 @stream.column.should == 0 73 @stream.line.should == 3 74 end 75 76 def test_reset 77 2.times { @stream.consume } 78 @stream.reset 79 @stream.index.should == 0 80 @stream.line.should == 1 81 @stream.column.should == 0 82 @stream.peek(1).should == ?o.ord 83 end 84 85 def test_look 86 @stream.look(1).should == 'o' 87 @stream.look(2).should == 'h' 88 @stream.look(3).should == "\n" 89 @stream.peek(1).should == ?o.ord 90 @stream.peek(2).should == ?h.ord 91 @stream.peek(3).should == ?\n.ord 92 93 6.times { @stream.consume } 94 @stream.look(1).should == '!' 95 @stream.look(2).should == "\n" 96 @stream.look(3).should be_nil 97 @stream.peek(1).should == ?!.ord 98 @stream.peek(2).should == ?\n.ord 99 @stream.peek(3).should == EOF 100 end 101 102 def test_substring 103 @stream.substring(0,0).should == 'o' 104 @stream.substring(0,1).should == 'oh' 105 @stream.substring(0,8).should == "oh\nhey!\n" 106 @stream.substring(3,6).should == "hey!" 107 end 108 109 def test_seek_forward 110 @stream.seek(3) 111 @stream.index.should == 3 112 @stream.line.should == 2 113 @stream.column.should == 0 114 @stream.peek(1).should == ?h.ord 115 end 116 117 def test_mark 118 @stream.seek(4) 119 marker = @stream.mark 120 marker.should == 1 121 122 2.times { @stream.consume } 123 marker = @stream.mark 124 125 marker.should == 2 126 end 127 128 def test_release_last 129 @stream.seek(4) 130 marker1 = @stream.mark 131 132 2.times { @stream.consume } 133 marker2 = @stream.mark 134 135 @stream.release 136 @stream.mark_depth.should == 2 137 @stream.release 138 @stream.mark_depth.should == 1 139 end 140 141 def test_release_nested 142 @stream.seek(4) 143 marker1 = @stream.mark() 144 145 @stream.consume() 146 marker2 = @stream.mark() 147 148 @stream.consume() 149 marker3 = @stream.mark() 150 151 @stream.release(marker2) 152 @stream.mark_depth.should == 2 153 154 end 155 156 def test_rewind_last 157 @stream.seek(4) 158 159 marker = @stream.mark 160 @stream.consume 161 @stream.consume 162 163 @stream.rewind 164 @stream.mark_depth.should == 1 165 @stream.index.should == 4 166 @stream.line.should == 2 167 @stream.column.should == 1 168 @stream.peek(1).should == ?e.ord 169 170 end 171 172 def test_through 173 @stream.through( 2 ).should == 'oh' 174 @stream.through( -2 ).should == '' 175 @stream.seek( 5 ) 176 @stream.through( 0 ).should == '' 177 @stream.through( 1 ).should == 'y' 178 @stream.through( -2 ).should == 'he' 179 @stream.through( 5 ).should == "y!\n" 180 end 181 182 def test_rewind_nested 183 @stream.seek(4) 184 marker1 = @stream.mark() 185 186 @stream.consume 187 marker2 = @stream.mark 188 189 @stream.consume 190 marker3 = @stream.mark 191 192 @stream.rewind(marker2) 193 @stream.mark_depth.should == 2 194 @stream.index().should == 5 195 @stream.line.should == 2 196 @stream.column.should == 2 197 @stream.peek(1).should == ?y.ord 198 end 199end 200 201class TestFileStream < Test::Unit::TestCase 202 203 204 def test_no_encoding 205 206 path = File.join(File.dirname(__FILE__), 'sample-input/file-stream-1') 207 @stream = FileStream.new(path) 208 209 @stream.seek(4) 210 marker1 = @stream.mark() 211 212 @stream.consume() 213 marker2 = @stream.mark() 214 215 @stream.consume() 216 marker3 = @stream.mark() 217 218 @stream.rewind(marker2) 219 @stream.index().should == 5 220 @stream.line.should == 2 221 @stream.column.should == 1 222 @stream.mark_depth.should == 2 223 @stream.look(1).should == 'a' 224 @stream.peek(1).should == ?a.ord 225 end 226 227 def test_encoded 228 229 end 230end 231 232class TestInputStream < Test::Unit::TestCase 233 def test_no_encoding 234 235 end 236 237 def test_encoded 238 239 end 240end 241 242class TestCommonTokenStream < Test::Unit::TestCase 243 class MockSource 244 include ANTLR3::TokenSource 245 attr_accessor :tokens 246 def initialize 247 @tokens = [] 248 end 249 def next_token 250 @tokens.shift 251 end 252 end 253 254 # vvvvvvvv tests vvvvvvvvv 255 def test_init 256 @source = MockSource.new 257 @stream = CommonTokenStream.new( @source ) 258 @stream.position.should == 0 259 end 260 261 def test_rebuild 262 @source1 = MockSource.new 263 @source2 = MockSource.new 264 @source2.tokens << new_token( 10, :channel => ANTLR3::HIDDEN ) << new_token( 11 ) 265 @stream = CommonTokenStream.new( @source1 ) 266 267 @stream.position.should == 0 268 @stream.tokens.length.should == 0 269 270 @stream.rebuild( @source2 ) 271 @stream.token_source.should == @source2 272 @stream.position.should == 1 273 @stream.tokens.should have( 2 ).things 274 end 275 276 def test_look_empty_source 277 @source = MockSource.new 278 @stream = CommonTokenStream.new(@source) 279 @stream.look.should == ANTLR3::EOF_TOKEN 280 end 281 282 def test_look1 283 @source = MockSource.new 284 @source.tokens << new_token(12) 285 @stream = CommonTokenStream.new(@source) 286 @stream.look(1).type.should == 12 287 end 288 289 def test_look1_with_hidden 290 # FIX 291 @source = MockSource.new 292 @source.tokens << new_token(12, :channel => ANTLR3::HIDDEN_CHANNEL) << 293 new_token(13) 294 @stream = CommonTokenStream.new(@source) 295 @stream.look(1).type.should == 13 296 end 297 298 def test_look2_beyond_end 299 @source = MockSource.new 300 @source.tokens << new_token(12) << 301 new_token(13, :channel => ANTLR3::HIDDEN_CHANNEL) 302 303 @stream = CommonTokenStream.new(@source) 304 @stream.look(2).type.should == EOF 305 end 306 307 def test_look_negative 308 @source = MockSource.new 309 @source.tokens << new_token(12) << new_token(13) 310 @stream = CommonTokenStream.new(@source) 311 @stream.consume 312 313 @stream.look(-1).type.should == 12 314 end 315 316 def test_lb1 317 @source = MockSource.new 318 @source.tokens << new_token(12) << new_token(13) 319 @stream = CommonTokenStream.new(@source) 320 321 @stream.consume 322 @stream.look(-1).type.should == 12 323 end 324 325 def test_look_zero 326 # FIX 327 @source = MockSource.new 328 @source.tokens << new_token(12) << new_token(13) 329 @stream = CommonTokenStream.new(@source) 330 @stream.look(0).should == nil 331 end 332 333 def test_lb_beyond_begin 334 @source = MockSource.new 335 @source.tokens << new_token(10) << 336 new_token(11, :channel => HIDDEN_CHANNEL) << 337 new_token(12, :channel => HIDDEN_CHANNEL) << 338 new_token(13) 339 @stream = CommonTokenStream.new(@source) 340 341 @stream.look(-1).should == nil 342 2.times { @stream.consume } 343 @stream.look(-3).should == nil 344 end 345 346 def test_fill_buffer 347 @source = MockSource.new 348 @source.tokens << new_token(12) << new_token(13) << new_token(14) << new_token(EOF) 349 @stream = CommonTokenStream.new(@source) 350 351 @stream.instance_variable_get(:@tokens).length.should == 3 352 @stream.tokens[0].type.should == 12 353 @stream.tokens[1].type.should == 13 354 @stream.tokens[2].type.should == 14 355 end 356 357 def test_consume 358 @source = MockSource.new 359 @source.tokens << new_token(12) << new_token(13) << new_token(EOF) 360 @stream = CommonTokenStream.new(@source) 361 @stream.peek.should == 12 362 @stream.consume 363 @stream.peek.should == 13 364 @stream.consume 365 @stream.peek.should == EOF 366 @stream.consume 367 @stream.peek.should == EOF 368 end 369 370 def test_seek 371 @source = MockSource.new 372 @source.tokens << new_token(12) << new_token(13) << new_token(EOF) 373 @stream = CommonTokenStream.new(@source) 374 375 @stream.peek(1).should == 12 376 @stream.seek(2).peek.should == EOF 377 @stream.seek(0).peek.should == 12 378 @stream.seek(-3).position.should == 0 379 @stream.seek(10).position.should == 2 380 end 381 382 def test_mark_rewind 383 @source = MockSource.new 384 @source.tokens << new_token(12) << new_token(13) << new_token(EOF) 385 @stream = CommonTokenStream.new(@source) 386 @stream.consume 387 marker = @stream.mark 388 @stream.consume 389 @stream.rewind(marker) 390 @stream.peek(1).should == 13 391 end 392 393 def test_to_string 394 @source = MockSource.new 395 @source.tokens << new_token(12, 'foo') << 396 new_token(13, 'bar') << new_token(14, 'gnurz') << 397 new_token(15, 'blarz') 398 @stream = CommonTokenStream.new(@source) 399 @stream.to_s.should == "foobargnurzblarz" 400 @stream.to_s(1,2).should == 'bargnurz' 401 @stream.to_s(@stream[1], @stream[-2]).should == 'bargnurz' 402 end 403 404 def new_token(type, opts = {}) 405 fields = {} 406 case type 407 when Hash then fields.update(type) 408 else 409 fields[:type] = type 410 end 411 case opts 412 when Hash then fields.update(opts) 413 when String then fields[:text] = opts 414 end 415 CommonToken.create(fields) 416 end 417 418end 419 420 421__END__ 422teststreams.py | LN | STATUS 423----------------------------------------------+-----+-------------- 424class TestStringStream(unittest.TestCase) | 009 | [x] 425 def testSize(self) | 012 | [x] 426 def testIndex(self) | 020 | [x] 427 def testConsume(self) | 028 | [x] 428 def testReset(self) | 079 | [x] 429 def testLA(self) | 094 | [x] 430 def testSubstring(self) | 111 | [x] 431 def testSeekForward(self) | 122 | [x] 432 def testMark(self) | 150 | [x] 433 def testReleaseLast(self) | 167 | [x] 434 def testReleaseNested(self) | 186 | [x] 435 def testRewindLast(self) | 204 | [x] 436 def testRewindNested(self) | 223 | [x] 437class TestFileStream(unittest.TestCase) | 245 | [o] 438 def testNoEncoding(self) | 249 | [x] 439 def testEncoded(self) | 272 | [ ] 440class TestInputStream(unittest.TestCase) | 296 | [ ] 441 def testNoEncoding(self) | 299 | [ ] 442 def testEncoded(self) | 322 | [ ] 443class TestCommonTokenStream(unittest.TestCase)| 345 | [ ] 444 def setUp(self) | 348 | [x] 445 def testInit(self) | 369 | [x] 446 def testSetTokenSource(self) | 376 | [x] 447 def testLTEmptySource(self) | 385 | [x] 448 def testLT1(self) | 394 | [x] 449 def testLT1WithHidden(self) | 407 | [x] 450 def testLT2BeyondEnd(self) | 424 | [x] 451 def testLTNegative(self) | 442 | [x] 452 def testLB1(self) | 461 | [x] 453 def testLTZero(self) | 479 | [x] 454 def testLBBeyondBegin(self) | 496 | [x] 455 def testFillBuffer(self) | 523 | [x] 456 def testConsume(self) | 551 | [x] 457 def testSeek(self) | 579 | [x] 458 def testMarkRewind(self) | 604 | [x] 459 def testToString(self) | 631 | [x] 460 461