PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/Ruby/test/unit/test-streams.rb

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