PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/test-unicode-stream.rb

http://github.com/ohboyohboyohboy/antlr3
Ruby | 186 lines | 143 code | 39 blank | 4 comment | 71 complexity | 40034606c6d27144044fc8a4fa92783e MD5 | raw file
  1. #!/usr/bin/ruby
  2. # encoding: utf-8
  3. require 'antlr3'
  4. require 'antlr3/streams/unicode'
  5. require 'test/unit'
  6. require 'spec'
  7. include ANTLR3
  8. class TestUnicodeStream < Test::Unit::TestCase
  9. def setup
  10. # Ť a ť \n 9 8 ° \n
  11. # [ 171, 97, 187, 10, 57, 56, 176, 10]
  12. @stream = UnicodeStream.new( "Ťať\n98°\n" )
  13. end
  14. def test_size
  15. @stream.size.should == 8
  16. end
  17. def test_index
  18. @stream.index.should == 0
  19. end
  20. def test_consume
  21. @stream.consume # Ť
  22. @stream.index.should == 1
  23. @stream.column.should == 1
  24. @stream.line.should == 1
  25. @stream.consume # a
  26. @stream.index.should == 2
  27. @stream.column.should == 2
  28. @stream.line.should == 1
  29. @stream.consume # ť
  30. @stream.index.should == 3
  31. @stream.column.should == 3
  32. @stream.line.should == 1
  33. @stream.consume # \n
  34. @stream.index.should == 4
  35. @stream.column.should == 0
  36. @stream.line.should == 2
  37. @stream.consume # 9
  38. @stream.index.should == 5
  39. @stream.column.should == 1
  40. @stream.line.should == 2
  41. @stream.consume # 8
  42. @stream.index.should == 6
  43. @stream.column.should == 2
  44. @stream.line.should == 2
  45. @stream.consume # °
  46. @stream.index.should == 7
  47. @stream.column.should == 3
  48. @stream.line.should == 2
  49. @stream.consume # \n
  50. @stream.index.should == 8
  51. @stream.column.should == 0
  52. @stream.line.should == 3
  53. @stream.consume # EOF
  54. @stream.index.should == 8
  55. @stream.column.should == 0
  56. @stream.line.should == 3
  57. @stream.consume # EOF
  58. @stream.index.should == 8
  59. @stream.column.should == 0
  60. @stream.line.should == 3
  61. end
  62. def test_reset
  63. 2.times { @stream.consume }
  64. @stream.reset
  65. @stream.index.should == 0
  66. @stream.line.should == 1
  67. @stream.column.should == 0
  68. @stream.peek(1).should == 171 # code point for Ť
  69. end
  70. def test_look
  71. @stream.look(1).should == 'Ť'
  72. @stream.look(2).should == 'a'
  73. @stream.look(3).should == "ť"
  74. @stream.peek(1).should == 171
  75. @stream.peek(2).should == 97
  76. @stream.peek(3).should == 187
  77. 6.times { @stream.consume }
  78. @stream.look(1).should == '°'
  79. @stream.look(2).should == "\n"
  80. @stream.look(3).should be_nil
  81. @stream.peek(1).should == 176
  82. @stream.peek(2).should == 10
  83. @stream.peek(3).should == EOF
  84. end
  85. def test_substring
  86. @stream.substring(0,0).should == 'Ť'
  87. @stream.substring(0,1).should == 'Ťa'
  88. @stream.substring(0,10).should == "Ťať\n98°\n"
  89. @stream.substring(3,6).should == "\n98°"
  90. end
  91. def test_seek_forward
  92. @stream.seek(4)
  93. @stream.index.should == 4
  94. @stream.line.should == 2
  95. @stream.column.should == 0
  96. @stream.peek(1).should == 57
  97. end
  98. def test_mark
  99. @stream.seek(4)
  100. marker = @stream.mark
  101. marker.should == 1
  102. 2.times { @stream.consume }
  103. marker = @stream.mark
  104. marker.should == 2
  105. end
  106. def test_release_last
  107. @stream.seek(4)
  108. marker1 = @stream.mark
  109. 2.times { @stream.consume }
  110. marker2 = @stream.mark
  111. @stream.release
  112. @stream.mark_depth.should == 2
  113. @stream.release
  114. @stream.mark_depth.should == 1
  115. end
  116. def test_release_nested
  117. @stream.seek(4)
  118. marker1 = @stream.mark()
  119. @stream.consume()
  120. marker2 = @stream.mark()
  121. @stream.consume()
  122. marker3 = @stream.mark()
  123. @stream.release(marker2)
  124. @stream.mark_depth.should == 2
  125. end
  126. def test_rewind_last
  127. @stream.seek(4)
  128. marker = @stream.mark
  129. @stream.consume
  130. @stream.consume
  131. @stream.rewind
  132. @stream.mark_depth.should == 1
  133. @stream.index.should == 4
  134. @stream.line.should == 2
  135. @stream.column.should == 0
  136. @stream.peek(1).should == 57
  137. end
  138. def test_through
  139. @stream.through( 2 ).should == 'Ťa'
  140. @stream.through( -2 ).should == ''
  141. @stream.seek( 4 )
  142. @stream.through( 0 ).should == ''
  143. @stream.through( 1 ).should == '9'
  144. @stream.through( -2 ).should == "ť\n"
  145. @stream.through( 5 ).should == "98°\n"
  146. end
  147. end