PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/jwalton/antlr3
Ruby | 843 lines | 667 code | 155 blank | 21 comment | 94 complexity | 141eda1a577ae1a2d90ec1916ca3a518 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. include ANTLR3::AST
  8. class TestTreeNodeStream < Test::Unit::TestCase
  9. def setup
  10. @adaptor = CommonTreeAdaptor.new
  11. end
  12. def new_stream(t)
  13. CommonTreeNodeStream.new(t)
  14. end
  15. def test_single_node
  16. t = CommonTree.new(CommonToken.new { |t| t.type = 101 })
  17. stream = new_stream(t)
  18. expecting = '101'
  19. found = nodes_only_string(stream)
  20. found.should == expecting
  21. expecting = '<UNKNOWN: 101>'
  22. found = stream.inspect
  23. found.should == expecting
  24. end
  25. def test_two_children_of_nil_root
  26. v = Class.new(CommonTree) do
  27. def initialize(token = nil, type = nil, x = nil)
  28. @x = x
  29. super(token || (CommonToken.new { |t| t.type = type } if type))
  30. end
  31. def to_s
  32. (@token.text rescue '') + '<V>'
  33. end
  34. end
  35. root_0 = @adaptor.create_flat_list
  36. t = v.new(nil, 101, 2)
  37. u = v.new CommonToken.create(:type => 102, :text => '102')
  38. @adaptor.add_child(root_0, t)
  39. @adaptor.add_child(root_0, u)
  40. assert(root_0.parent.nil?)
  41. root_0.child_index.should == -1
  42. t.child_index.should == 0
  43. u.child_index.should == 1
  44. end
  45. def test_4_nodes
  46. t = CommonTree.new CommonToken[101]
  47. t.add_child( CommonTree.new CommonToken[102] )
  48. t.child(0).add_child(CommonTree.new CommonToken[103])
  49. t.add_child(CommonTree.new CommonToken[104])
  50. stream = new_stream(t)
  51. expecting = "101 102 103 104"
  52. found = nodes_only_string(stream)
  53. found.should == expecting
  54. expecting = "<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> <UP>"
  55. found = stream.inspect
  56. found.should == expecting
  57. end
  58. def test_list
  59. root = CommonTree.new(nil)
  60. t = CommonTree.new CommonToken[101]
  61. t.add_child CommonTree.new(CommonToken[102])
  62. t.child(0).add_child(CommonTree.new(CommonToken[103]))
  63. t.add_child(CommonTree.new(CommonToken[104]))
  64. u = CommonTree.new CommonToken[105]
  65. root.add_child(t)
  66. root.add_child(u)
  67. stream = CommonTreeNodeStream.new(root)
  68. expecting = '101 102 103 104 105'
  69. found = nodes_only_string(stream)
  70. found.should == expecting
  71. expecting = "<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> <UP> <UNKNOWN: 105>"
  72. found = stream.inspect
  73. found.should == expecting
  74. end
  75. def test_flat_list
  76. root = CommonTree.new(nil)
  77. root.add_child CommonTree.new(CommonToken[101])
  78. root.add_child(CommonTree.new(CommonToken[102]))
  79. root.add_child(CommonTree.new(CommonToken[103]))
  80. stream = CommonTreeNodeStream.new( root )
  81. expecting = '101 102 103'
  82. found = nodes_only_string(stream)
  83. found.should == expecting
  84. expecting = '<UNKNOWN: 101> <UNKNOWN: 102> <UNKNOWN: 103>'
  85. found = stream.inspect
  86. found.should == expecting
  87. end
  88. def test_list_with_one_node
  89. root = CommonTree.new(nil)
  90. root.add_child(CommonTree.new(CommonToken[101]))
  91. stream = CommonTreeNodeStream.new(root)
  92. expecting = '101'
  93. found = nodes_only_string(stream)
  94. found.should == expecting
  95. expecting = "<UNKNOWN: 101>"
  96. found = stream.inspect
  97. found.should == expecting
  98. end
  99. def test_a_over_b
  100. t = CommonTree.new(CommonToken[101])
  101. t.add_child(CommonTree.new(CommonToken[102]))
  102. stream = new_stream(t)
  103. expecting = '101 102'
  104. found = nodes_only_string(stream)
  105. found.should == expecting
  106. expecting = '<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <UP>'
  107. found = stream.inspect
  108. found.should == expecting
  109. end
  110. def test_LT
  111. # ^(101 ^(102 103) 104)
  112. t = CommonTree.new CommonToken[101]
  113. t.add_child CommonTree.new(CommonToken[102])
  114. t.child(0).add_child(CommonTree.new(CommonToken[103]))
  115. t.add_child(CommonTree.new(CommonToken[104]))
  116. stream = new_stream(t)
  117. [101, DOWN, 102, DOWN, 103, UP, 104, UP, EOF].each_with_index do |type, index|
  118. stream.look(index + 1).type.should == type
  119. end
  120. stream.look(100).type.should == EOF
  121. end
  122. def test_mark_rewind_entire
  123. # ^(101 ^(102 103 ^(106 107)) 104 105)
  124. r0 = new_node new_token(101)
  125. r1 = new_node new_token(102)
  126. r0.add_child(r1)
  127. r1.add_child(new_node new_token(103))
  128. r2 = new_node new_token(106)
  129. r2.add_child new_node( new_token 107 )
  130. r1.add_child r2
  131. r0.add_child new_node( new_token 104 )
  132. r0.add_child new_node( new_token 105 )
  133. stream = CommonTreeNodeStream.new(r0)
  134. m = stream.mark
  135. 13.times { stream.look(1); stream.consume } # consume until end
  136. stream.look(1).type.should == EOF
  137. stream.look(-1).type.should == UP
  138. stream.rewind(m)
  139. 13.times { stream.look(1); stream.consume } # consume until end
  140. stream.look(1).type.should == EOF
  141. stream.look(-1).type.should == UP
  142. end
  143. def test_mark_rewind_in_middle
  144. # ^(101 ^(102 103 ^(106 107)) 104 105)
  145. r0 = new_node new_token(101)
  146. r1 = new_node new_token(102)
  147. r0.add_child r1
  148. r1.add_child new_node( new_token 103 )
  149. r2 = new_node new_token(106)
  150. r2.add_child new_node( new_token 107 )
  151. r1.add_child r2
  152. r0.add_child new_node( new_token 104 )
  153. r0.add_child new_node( new_token 105 )
  154. stream = CommonTreeNodeStream.new(r0)
  155. 7.times { stream.consume }
  156. stream.look(1).type.should == 107
  157. m = stream.mark
  158. 4.times { stream.consume }
  159. stream.rewind(m)
  160. [107, UP, UP, 104].each do |val|
  161. stream.look(1).type.should == val
  162. stream.consume
  163. end
  164. # past rewind position now
  165. [105, UP].each do |val|
  166. stream.look(1).type.should == val
  167. stream.consume
  168. end
  169. stream.look(1).type.should == EOF
  170. stream.look(-1).type.should == UP
  171. end
  172. def test_mark_rewind_nested
  173. # ^(101 ^(102 103 ^(106 107)) 104 105)
  174. r0 = new_node new_token(101)
  175. r1 = new_node new_token(102)
  176. r0.add_child r1
  177. r1.add_child new_node( new_token 103 )
  178. r2 = new_node new_token(106)
  179. r2.add_child new_node( new_token 107 )
  180. r1.add_child r2
  181. r0.add_child new_node( new_token 104 )
  182. r0.add_child new_node( new_token 105 )
  183. stream = CommonTreeNodeStream.new(r0)
  184. m = stream.mark
  185. 2.times { stream.consume }
  186. m2 = stream.mark
  187. 4.times { stream.consume }
  188. stream.rewind(m2)
  189. stream.look(1).type.should == 102
  190. stream.consume
  191. stream.look(1).type.should == DOWN
  192. stream.consume
  193. stream.rewind(m)
  194. [101, DOWN, 102].each do |val|
  195. stream.look(1).type.should == val
  196. stream.consume
  197. end
  198. stream.look(1).type.should == DOWN
  199. end
  200. def test_seek
  201. # ^(101 ^(102 103 ^(106 107) ) 104 105)
  202. # stream has 7 real + 6 nav nodes
  203. # Sequence of types: 101 DN 102 DN 103 106 DN 107 UP UP 104 105 UP EOF
  204. r0 = new_node new_token(101)
  205. r1 = new_node new_token(102)
  206. r0.add_child r1
  207. r1.add_child new_node( new_token 103 )
  208. r2 = new_node new_token(106)
  209. r2.add_child new_node( new_token 107 )
  210. r1.add_child r2
  211. r0.add_child new_node( new_token 104 )
  212. r0.add_child new_node( new_token 105 )
  213. stream = CommonTreeNodeStream.new(r0)
  214. 3.times { stream.consume }
  215. stream.seek(7)
  216. stream.look(1).type.should == 107
  217. 3.times { stream.consume }
  218. stream.look(1).type.should == 104
  219. end
  220. def test_seek_from_start
  221. r0 = new_node new_token(101)
  222. r1 = new_node new_token(102)
  223. r0.add_child r1
  224. r1.add_child new_node( new_token 103 )
  225. r2 = new_node new_token(106)
  226. r2.add_child new_node( new_token 107 )
  227. r1.add_child r2
  228. r0.add_child new_node( new_token 104 )
  229. r0.add_child new_node( new_token 105 )
  230. stream = CommonTreeNodeStream.new(r0)
  231. stream.seek(7)
  232. stream.look(1).type.should == 107
  233. 3.times { stream.consume }
  234. stream.look(1).type.should == 104
  235. end
  236. def nodes_only_string(nodes)
  237. buffer = []
  238. nodes.size.times do |index|
  239. t = nodes.look(index + 1)
  240. type = nodes.tree_adaptor.type_of(t)
  241. buffer << type.to_s unless type == DOWN or type == UP
  242. end
  243. return buffer.join(' ')
  244. end
  245. def new_token(type, opts = {})
  246. opts[:type] = type
  247. CommonToken.create(opts)
  248. end
  249. def new_node(token)
  250. CommonTree.new(token)
  251. end
  252. end
  253. class TestCommonTreeNodeStream < Test::Unit::TestCase
  254. def setup
  255. # before-each-test code
  256. end
  257. def teardown
  258. # after-each-test code
  259. end
  260. # vvvvvvvv tests vvvvvvvvv
  261. def test_push_pop
  262. r0 = new_node new_token(101)
  263. r1 = new_node new_token(102)
  264. r1.add_child new_node( new_token 103 )
  265. r0.add_child r1
  266. r2 = new_node new_token(104)
  267. r2.add_child new_node( new_token 105 )
  268. r0.add_child r2
  269. r3 = new_node new_token(106)
  270. r3.add_child new_node( new_token 107 )
  271. r0.add_child r3
  272. r0.add_child new_node( new_token 108 )
  273. r0.add_child new_node( new_token 109 )
  274. stream = CommonTreeNodeStream.new(r0)
  275. expecting = '<UNKNOWN: 101> <DOWN> <UNKNOWN: 102> <DOWN> <UNKNOWN: 103> <UP> <UNKNOWN: 104> ' +
  276. '<DOWN> <UNKNOWN: 105> <UP> <UNKNOWN: 106> <DOWN> <UNKNOWN: 107> <UP> ' +
  277. '<UNKNOWN: 108> <UNKNOWN: 109> <UP>'
  278. found = stream.inspect
  279. found.should == expecting
  280. index_of_102 = 2
  281. index_of_107 = 12
  282. index_of_107.times { stream.consume }
  283. stream.look(1).type.should == 107
  284. stream.push(index_of_102)
  285. stream.look(1).type.should == 102
  286. stream.consume
  287. stream.look(1).type.should == DOWN
  288. stream.consume
  289. stream.look(1).type.should == 103
  290. stream.consume
  291. stream.look(1).type.should == UP
  292. stream.pop
  293. stream.look(1).type.should == 107
  294. end
  295. def test_nested_push_pop
  296. r0 = new_node new_token(101)
  297. r1 = new_node new_token(102)
  298. r1.add_child new_node( new_token 103 )
  299. r0.add_child r1
  300. r2 = new_node new_token(104)
  301. r2.add_child new_node( new_token 105 )
  302. r0.add_child r2
  303. r3 = new_node new_token(106)
  304. r3.add_child new_node( new_token 107 )
  305. r0.add_child r3
  306. r0.add_child new_node( new_token 108 )
  307. r0.add_child new_node( new_token 109 )
  308. stream = CommonTreeNodeStream.new(r0)
  309. index_of_102 = 2
  310. index_of_107 = 12
  311. index_of_107.times { stream.consume }
  312. stream.look(1).type.should == 107
  313. stream.push(index_of_102)
  314. [102, DOWN, 103].each do |val|
  315. stream.look(1).type.should == val
  316. stream.consume
  317. end
  318. index_of_104 = 6
  319. stream.push(index_of_104)
  320. [104,DOWN,105].each do |val|
  321. stream.look(1).type.should == val
  322. stream.consume
  323. end
  324. stream.look(1).type.should == UP
  325. stream.pop
  326. stream.look(1).type.should == UP
  327. stream.pop
  328. stream.look(1).type.should == 107
  329. end
  330. def test_push_pop_from_eof
  331. r0 = new_node new_token(101)
  332. r1 = new_node new_token(102)
  333. r1.add_child new_node( new_token 103 )
  334. r0.add_child r1
  335. r2 = new_node new_token(104)
  336. r2.add_child new_node( new_token 105 )
  337. r0.add_child r2
  338. r3 = new_node new_token(106)
  339. r3.add_child new_node( new_token 107 )
  340. r0.add_child r3
  341. r0.add_child new_node( new_token 108 )
  342. r0.add_child new_node( new_token 109 )
  343. stream = CommonTreeNodeStream.new(r0)
  344. stream.consume until stream.peek(1) == EOF
  345. index_of_102 = 2
  346. index_of_104 = 6
  347. stream.look(1).type.should == EOF
  348. stream.push(index_of_102)
  349. [102, DOWN, 103].each do |val|
  350. stream.look(1).type.should == val
  351. stream.consume
  352. end
  353. stream.look(1).type.should == UP
  354. stream.pop
  355. stream.look(1).type.should == EOF
  356. stream.push(index_of_104)
  357. [104, DOWN, 105].each do |val|
  358. stream.look(1).type.should == val
  359. stream.consume
  360. end
  361. stream.look(1).type.should == UP
  362. stream.pop
  363. stream.look(1).type.should == EOF
  364. end
  365. def new_token(type, opts = {})
  366. opts[:type] = type
  367. CommonToken.create(opts)
  368. end
  369. def new_node(token)
  370. CommonTree.new(token)
  371. end
  372. end
  373. class TestCommonTree < Test::Unit::TestCase
  374. def setup
  375. @adaptor = CommonTreeAdaptor.new
  376. end
  377. def teardown
  378. # after-each-test code
  379. end
  380. # vvvvvvvv tests vvvvvvvvv
  381. def test_single_node
  382. t = new_node( new_token 101 )
  383. assert_nil t.parent
  384. t.child_index.should == -1
  385. end
  386. def test_4_nodes
  387. # ^(101 ^(102 103) 104)
  388. r0 = new_node( new_token 101 )
  389. r0.add_child new_node( new_token 102 )
  390. r0.child(0).add_child new_node( new_token 103 )
  391. r0.add_child new_node( new_token 104 )
  392. assert_nil r0.parent
  393. r0.child_index.should == -1
  394. end
  395. def test_list
  396. # ^(nil 101 102 103)
  397. r0 = CommonTree.new(nil)
  398. c0 = new_node( new_token 101 )
  399. r0.add_child c0
  400. c1 = new_node( new_token 102 )
  401. r0.add_child c1
  402. c2 = new_node( new_token 103 )
  403. r0.add_child c2
  404. assert_nil r0.parent
  405. r0.child_index.should == -1
  406. c0.parent.should == r0
  407. c0.child_index.should == 0
  408. c1.parent.should == r0
  409. c1.child_index.should == 1
  410. c2.parent.should == r0
  411. c2.child_index.should == 2
  412. end
  413. def test_list2
  414. # ^(nil 101 102 103)
  415. root = new_node( new_token 5 )
  416. r0 = CommonTree.new(nil)
  417. c0 = new_node( new_token 101 )
  418. r0.add_child c0
  419. c1 = new_node( new_token 102 )
  420. r0.add_child c1
  421. c2 = new_node( new_token 103 )
  422. r0.add_child c2
  423. root.add_child r0
  424. assert_nil root.parent
  425. root.child_index.should == -1
  426. c0.parent.should == root
  427. c0.child_index.should == 0
  428. c1.parent.should == root # note -- actual python tests all use c0 here, which i think might be wrong
  429. c1.child_index.should == 1
  430. c2.parent.should == root # note -- actual python tests all use c0 here, which i think might be wrong
  431. c2.child_index.should == 2
  432. end
  433. def test_add_list_to_exist_children
  434. root = new_node( new_token 5 )
  435. root.add_child new_node( new_token 6 )
  436. r0 = CommonTree.new(nil)
  437. c0 = new_node( new_token 101 )
  438. r0.add_child c0
  439. c1 = new_node( new_token 102 )
  440. r0.add_child c1
  441. c2 = new_node( new_token 103 )
  442. r0.add_child c2
  443. # ^(nil c0=101 c1=102 c2=103)
  444. root.add_child(r0)
  445. assert_nil root.parent
  446. root.child_index.should == -1
  447. c0.parent.should == root
  448. c0.child_index.should == 1
  449. c1.parent.should == root
  450. c1.child_index.should == 2
  451. c2.parent.should == root
  452. c2.child_index.should == 3
  453. end
  454. def test_copy_tree
  455. r0 = new_node( new_token 101 )
  456. r1 = new_node( new_token 102 )
  457. r2 = new_node( new_token 106 )
  458. r0.add_child( r1 )
  459. r1.add_child( new_node( new_token 103 ) )
  460. r2.add_child( new_node( new_token 107 ) )
  461. r1.add_child( r2 )
  462. r0.add_child( new_node( new_token 104 ) )
  463. r0.add_child( new_node( new_token 105 ) )
  464. dup = @adaptor.copy_tree( r0 )
  465. assert_nil dup.parent
  466. dup.child_index.should == -1
  467. dup.sanity_check
  468. end
  469. def test_become_root
  470. new_root = new_node( new_token 5 )
  471. old_root = new_node nil
  472. old_root.add_child( new_node( new_token 101 ) )
  473. old_root.add_child( new_node( new_token 102 ) )
  474. old_root.add_child( new_node( new_token 103 ) )
  475. @adaptor.become_root(new_root, old_root)
  476. new_root.sanity_check
  477. end
  478. def test_become_root2
  479. new_root = new_node( new_token 5 )
  480. old_root = new_node( new_token 101 )
  481. old_root.add_child( new_node( new_token 102 ) )
  482. old_root.add_child( new_node( new_token 103 ) )
  483. @adaptor.become_root(new_root, old_root)
  484. new_root.sanity_check
  485. end
  486. def test_become_root3
  487. new_root = new_node nil
  488. new_root.add_child( new_node( new_token 5 ) )
  489. old_root = new_node nil
  490. old_root.add_child( new_node( new_token 101 ) )
  491. old_root.add_child( new_node( new_token 102 ) )
  492. old_root.add_child( new_node( new_token 103 ) )
  493. @adaptor.become_root(new_root, old_root)
  494. new_root.sanity_check
  495. end
  496. def test_become_root5
  497. new_root = new_node nil
  498. new_root.add_child( new_node( new_token 5 ) )
  499. old_root = new_node( new_token 101 )
  500. old_root.add_child( new_node( new_token 102 ) )
  501. old_root.add_child( new_node( new_token 103 ) )
  502. @adaptor.become_root(new_root, old_root)
  503. new_root.sanity_check
  504. end
  505. def test_become_root6
  506. root_0 = @adaptor.create_flat_list
  507. root_1 = @adaptor.create_flat_list
  508. root_1 = @adaptor.become_root( new_node( new_token 5 ), root_1 )
  509. @adaptor.add_child( root_1, new_node( new_token 6 ) )
  510. @adaptor.add_child( root_0, root_1 )
  511. root_0.sanity_check
  512. end
  513. def test_replace_with_no_children
  514. t = new_node( new_token 101 )
  515. new_child = new_node( new_token 5 )
  516. error = false
  517. assert_raise(IndexError) do
  518. t.replace_children(0, 0, new_child)
  519. end
  520. end
  521. def test_replace_with_one_children
  522. t = new_node( new_token 99, :text => 'a' )
  523. c0 = new_node( new_token 99, :text => 'b' )
  524. t.add_child(c0)
  525. new_child = new_node( new_token 99, :text => 'c' )
  526. t.replace_children(0,0,new_child)
  527. t.inspect.should == '(a c)'
  528. t.sanity_check
  529. end
  530. def test_replace_in_middle
  531. t = new_node( new_token 99, :text => 'a' )
  532. t.add_child new_node( new_token 99, :text => 'b' )
  533. t.add_child new_node( new_token 99, :text => 'c' )
  534. t.add_child new_node( new_token 99, :text => 'd' )
  535. new_child = new_node( new_token 99, :text => 'x' )
  536. t.replace_children(1, 1, new_child)
  537. t.inspect.should == '(a b x d)'
  538. t.sanity_check
  539. end
  540. def test_replace_at_left
  541. t = new_node( new_token 99, :text => 'a' )
  542. t.add_child new_node( new_token 99, :text => 'b' )
  543. t.add_child new_node( new_token 99, :text => 'c' )
  544. t.add_child new_node( new_token 99, :text => 'd' )
  545. new_child = new_node( new_token 99, :text => 'x' )
  546. t.replace_children(0, 0, new_child)
  547. t.inspect.should == '(a x c d)'
  548. t.sanity_check
  549. end
  550. def test_replace_at_left
  551. t = new_node( new_token 99, :text => 'a' )
  552. t.add_child new_node( new_token 99, :text => 'b' )
  553. t.add_child new_node( new_token 99, :text => 'c' )
  554. t.add_child new_node( new_token 99, :text => 'd' )
  555. new_child = new_node( new_token 99, :text => 'x' )
  556. t.replace_children(2, 2, new_child)
  557. t.inspect.should == '(a b c x)'
  558. t.sanity_check
  559. end
  560. def test_replace_one_with_two_at_left
  561. t = new_node( new_token 99, :text => 'a' )
  562. t.add_child new_node( new_token 99, :text => 'b' )
  563. t.add_child new_node( new_token 99, :text => 'c' )
  564. t.add_child new_node( new_token 99, :text => 'd' )
  565. new_children = @adaptor.create_flat_list
  566. new_children.add_child new_node( new_token 99, :text => 'x' )
  567. new_children.add_child new_node( new_token 99, :text => 'y' )
  568. t.replace_children(0, 0, new_children)
  569. t.inspect.should == '(a x y c d)'
  570. t.sanity_check
  571. end
  572. def test_replace_one_with_two_at_right
  573. t = new_node( new_token 99, :text => 'a' )
  574. t.add_child new_node( new_token 99, :text => 'b' )
  575. t.add_child new_node( new_token 99, :text => 'c' )
  576. t.add_child new_node( new_token 99, :text => 'd' )
  577. new_children = @adaptor.create_flat_list
  578. new_children.add_child new_node( new_token 99, :text => 'x' )
  579. new_children.add_child new_node( new_token 99, :text => 'y' )
  580. t.replace_children(2, 2, new_children)
  581. t.inspect.should == '(a b c x y)'
  582. t.sanity_check
  583. end
  584. def test_replace_one_with_two_in_middle
  585. t = new_node( new_token 99, :text => 'a' )
  586. t.add_child new_node( new_token 99, :text => 'b' )
  587. t.add_child new_node( new_token 99, :text => 'c' )
  588. t.add_child new_node( new_token 99, :text => 'd' )
  589. new_children = @adaptor.create_flat_list
  590. new_children.add_child new_node( new_token 99, :text => 'x' )
  591. new_children.add_child new_node( new_token 99, :text => 'y' )
  592. t.replace_children(1, 1, new_children)
  593. t.inspect.should == '(a b x y d)'
  594. t.sanity_check
  595. end
  596. def test_replace_two_with_one_at_left
  597. t = new_node( new_token 99, :text => 'a' )
  598. t.add_child new_node( new_token 99, :text => 'b' )
  599. t.add_child new_node( new_token 99, :text => 'c' )
  600. t.add_child new_node( new_token 99, :text => 'd' )
  601. new_child = new_node( new_token 99, :text => 'x' )
  602. t.replace_children(0, 1, new_child)
  603. t.inspect.should == '(a x d)'
  604. t.sanity_check
  605. end
  606. def test_replace_two_with_one_at_right
  607. t = new_node( new_token 99, :text => 'a' )
  608. t.add_child new_node( new_token 99, :text => 'b' )
  609. t.add_child new_node( new_token 99, :text => 'c' )
  610. t.add_child new_node( new_token 99, :text => 'd' )
  611. new_child = new_node( new_token 99, :text => 'x' )
  612. t.replace_children(1, 2, new_child)
  613. t.inspect.should == '(a b x)'
  614. t.sanity_check
  615. end
  616. def test_replace_all_with_one
  617. t = new_node( new_token 99, :text => 'a' )
  618. t.add_child new_node( new_token 99, :text => 'b' )
  619. t.add_child new_node( new_token 99, :text => 'c' )
  620. t.add_child new_node( new_token 99, :text => 'd' )
  621. new_child = new_node( new_token 99, :text => 'x' )
  622. t.replace_children(0, 2, new_child)
  623. t.inspect.should == '(a x)'
  624. t.sanity_check
  625. end
  626. def test_replace_all_with_two
  627. t = new_node( new_token 99, :text => 'a' )
  628. t.add_child new_node( new_token 99, :text => 'b' )
  629. t.add_child new_node( new_token 99, :text => 'c' )
  630. t.add_child new_node( new_token 99, :text => 'd' )
  631. new_children = @adaptor.create_flat_list
  632. new_children.add_child new_node( new_token 99, :text => 'x' )
  633. new_children.add_child new_node( new_token 99, :text => 'y' )
  634. t.replace_children(0, 1, new_children)
  635. t.inspect.should == '(a x y d)'
  636. t.sanity_check
  637. end
  638. def new_token(type, opts = {})
  639. opts[:type] = type
  640. CommonToken.create(opts)
  641. end
  642. def new_node(token)
  643. CommonTree.new(token)
  644. end
  645. end
  646. class TestTreeContext < Test::Unit::TestCase
  647. TOKEN_NAMES = %w(
  648. <invalid> <EOR> <DOWN> <UP> VEC ASSIGN PRINT
  649. PLUS MULT DOT ID INT WS '[' ',' ']'
  650. )
  651. Tokens = TokenScheme.build( TOKEN_NAMES )
  652. def setup
  653. @wizard = Wizard.new( :token_scheme => Tokens )
  654. end
  655. def teardown
  656. # after-each-test code
  657. end
  658. # vvvvvvvv tests vvvvvvvvv
  659. def test_simple_parent
  660. tree = @wizard.create(
  661. "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))"
  662. )
  663. labels = @wizard.match( tree,
  664. "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))"
  665. )
  666. assert_kind_of( Hash, labels )
  667. @wizard.in_context?( labels.fetch( 'x' ), 'VEC' ).should be_true
  668. end
  669. def test_no_parent
  670. tree = @wizard.create(
  671. '(PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3])))'
  672. )
  673. labels = @wizard.match( tree, "(%x:PRINT (MULT ID (VEC INT INT INT)))" )
  674. assert_kind_of( Hash, labels )
  675. @wizard.in_context?( labels.fetch( 'x' ), 'VEC' ).should be_false
  676. end
  677. def test_parent_with_wildcard
  678. tree = @wizard.create(
  679. "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID[x] (VEC INT[1] INT[2] INT[3]))))"
  680. )
  681. labels = @wizard.match( tree,
  682. "(nil (ASSIGN ID[x] INT[3]) (PRINT (MULT ID (VEC INT %x:INT INT))))"
  683. )
  684. assert_kind_of( Hash, labels )
  685. node = labels.fetch( 'x' )
  686. @wizard.in_context?( node, 'VEC ...' ).should be_true
  687. end
  688. end