/source/ruby-1.9.2-p180/test/rdoc/test_rdoc_markup_parser.rb

https://github.com/cparedes/omnibus · Ruby · 1345 lines · 1061 code · 283 blank · 1 comment · 6 complexity · 876d122df800902ff1389f698bf25094 MD5 · raw file

  1. require 'pp'
  2. require 'rubygems'
  3. require 'minitest/autorun'
  4. require 'rdoc/markup'
  5. require 'rdoc/markup/to_test'
  6. class TestRDocMarkupParser < MiniTest::Unit::TestCase
  7. def setup
  8. @RM = RDoc::Markup
  9. @RMP = @RM::Parser
  10. end
  11. def mu_pp(obj)
  12. s = ''
  13. s = PP.pp obj, s
  14. s = s.force_encoding(Encoding.default_external) if defined? Encoding
  15. s.chomp
  16. end
  17. def test_build_heading
  18. parser = @RMP.new
  19. parser.tokens.replace [
  20. [:TEXT, 'heading three', 4, 0],
  21. [:NEWLINE, "\n", 17, 0],
  22. ]
  23. assert_equal @RM::Heading.new(3, 'heading three'), parser.build_heading(3)
  24. end
  25. def test_get
  26. parser = util_parser
  27. assert_equal [:HEADER, 1, 0, 0], parser.get
  28. assert_equal 7, parser.tokens.length
  29. end
  30. def test_parse_bullet
  31. str = <<-STR
  32. * l1
  33. * l2
  34. STR
  35. expected = [
  36. @RM::List.new(:BULLET, *[
  37. @RM::ListItem.new(nil,
  38. @RM::Paragraph.new('l1')),
  39. @RM::ListItem.new(nil,
  40. @RM::Paragraph.new('l2'))])]
  41. assert_equal expected, @RMP.parse(str).parts
  42. end
  43. def test_parse_bullet_verbatim_heading
  44. str = <<-STR
  45. * l1
  46. v
  47. = H
  48. STR
  49. expected = [
  50. @RM::List.new(:BULLET, *[
  51. @RM::ListItem.new(nil,
  52. @RM::Paragraph.new('l1'),
  53. @RM::Verbatim.new(' ', 'v', "\n"))]),
  54. @RM::Heading.new(1, 'H')]
  55. assert_equal expected, @RMP.parse(str).parts
  56. end
  57. def test_parse_bullet_heading
  58. str = <<-STR
  59. * = l1
  60. STR
  61. expected = [
  62. @RM::List.new(:BULLET, *[
  63. @RM::ListItem.new(nil,
  64. @RM::Heading.new(1, 'l1'))])]
  65. assert_equal expected, @RMP.parse(str).parts
  66. end
  67. def test_parse_bullet_indent
  68. str = <<-STR
  69. * l1
  70. * l1.1
  71. * l2
  72. STR
  73. expected = [
  74. @RM::List.new(:BULLET, *[
  75. @RM::ListItem.new(nil,
  76. @RM::Paragraph.new('l1'),
  77. @RM::List.new(:BULLET, *[
  78. @RM::ListItem.new(nil,
  79. @RM::Paragraph.new('l1.1'))])),
  80. @RM::ListItem.new(nil,
  81. @RM::Paragraph.new('l2'))])]
  82. assert_equal expected, @RMP.parse(str).parts
  83. end
  84. def test_parse_bullet_paragraph
  85. str = <<-STR
  86. now is
  87. * l1
  88. * l2
  89. the time
  90. STR
  91. expected = [
  92. @RM::Paragraph.new('now is'),
  93. @RM::List.new(:BULLET, *[
  94. @RM::ListItem.new(nil,
  95. @RM::Paragraph.new('l1')),
  96. @RM::ListItem.new(nil,
  97. @RM::Paragraph.new('l2')),
  98. ]),
  99. @RM::Paragraph.new('the time'),
  100. ]
  101. assert_equal expected, @RMP.parse(str).parts
  102. end
  103. def test_parse_bullet_multiline
  104. str = <<-STR
  105. * l1
  106. l1+
  107. * l2
  108. STR
  109. expected = [
  110. @RM::List.new(:BULLET, *[
  111. @RM::ListItem.new(nil,
  112. @RM::Paragraph.new('l1', 'l1+')),
  113. @RM::ListItem.new(nil,
  114. @RM::Paragraph.new('l2')),
  115. ]),
  116. ]
  117. assert_equal expected, @RMP.parse(str).parts
  118. end
  119. def test_parse_bullet_multiparagraph
  120. str = <<-STR
  121. * l1
  122. l1+
  123. STR
  124. expected = [
  125. @RM::List.new(:BULLET, *[
  126. @RM::ListItem.new(nil,
  127. @RM::Paragraph.new('l1'),
  128. @RM::BlankLine.new,
  129. @RM::Paragraph.new('l1+')),
  130. ]),
  131. ]
  132. assert_equal expected, @RMP.parse(str).parts
  133. end
  134. def test_parse_bullet_indent_verbatim
  135. str = <<-STR
  136. * l1
  137. * l1.1
  138. text
  139. code
  140. code
  141. text
  142. * l2
  143. STR
  144. expected = [
  145. @RM::List.new(:BULLET, *[
  146. @RM::ListItem.new(nil,
  147. @RM::Paragraph.new('l1'),
  148. @RM::List.new(:BULLET, *[
  149. @RM::ListItem.new(nil,
  150. @RM::Paragraph.new('l1.1', 'text'),
  151. @RM::Verbatim.new(' ', 'code', "\n",
  152. ' ', 'code', "\n"),
  153. @RM::Paragraph.new('text'))])),
  154. @RM::ListItem.new(nil,
  155. @RM::Paragraph.new('l2'))])]
  156. assert_equal expected, @RMP.parse(str).parts
  157. end
  158. def test_parse_dash
  159. str = <<-STR
  160. - one
  161. - two
  162. STR
  163. expected = [
  164. @RM::List.new(:BULLET, *[
  165. @RM::ListItem.new(nil,
  166. @RM::Paragraph.new('one')),
  167. @RM::ListItem.new(nil,
  168. @RM::Paragraph.new('two'))])]
  169. assert_equal expected, @RMP.parse(str).parts
  170. end
  171. def test_parse_heading
  172. str = '= heading one'
  173. expected = [
  174. @RM::Heading.new(1, 'heading one')]
  175. assert_equal expected, @RMP.parse(str).parts
  176. end
  177. def test_parse_heading_three
  178. str = '=== heading three'
  179. expected = [
  180. @RM::Heading.new(3, 'heading three')]
  181. assert_equal expected, @RMP.parse(str).parts
  182. end
  183. def test_parse_heading_bullet
  184. str = '= * heading one'
  185. expected = [
  186. @RM::Heading.new(1, '* heading one')]
  187. assert_equal expected, @RMP.parse(str).parts
  188. end
  189. def test_parse_heading_heading
  190. str = '= ='
  191. expected = [
  192. @RM::Heading.new(1, '=')]
  193. assert_equal expected, @RMP.parse(str).parts
  194. end
  195. def test_parse_heading_lalpha
  196. str = '= b. heading one'
  197. expected = [
  198. @RM::Heading.new(1, 'b. heading one')]
  199. assert_equal expected, @RMP.parse(str).parts
  200. end
  201. def test_parse_heading_label
  202. str = '= [heading one]'
  203. expected = [
  204. @RM::Heading.new(1, '[heading one]')]
  205. assert_equal expected, @RMP.parse(str).parts
  206. end
  207. def test_parse_heading_note
  208. str = '= heading one::'
  209. expected = [
  210. @RM::Heading.new(1, 'heading one::')]
  211. assert_equal expected, @RMP.parse(str).parts
  212. end
  213. def test_parse_heading_number
  214. str = '= 5. heading one'
  215. expected = [
  216. @RM::Heading.new(1, '5. heading one')]
  217. assert_equal expected, @RMP.parse(str).parts
  218. end
  219. def test_parse_heading_ualpha
  220. str = '= B. heading one'
  221. expected = [
  222. @RM::Heading.new(1, 'B. heading one')]
  223. assert_equal expected, @RMP.parse(str).parts
  224. end
  225. def test_parse_label
  226. str = <<-STR
  227. [one] item one
  228. [two] item two
  229. STR
  230. expected = [
  231. @RM::List.new(:LABEL, *[
  232. @RM::ListItem.new('one',
  233. @RM::Paragraph.new('item one')),
  234. @RM::ListItem.new('two',
  235. @RM::Paragraph.new('item two'))])]
  236. assert_equal expected, @RMP.parse(str).parts
  237. end
  238. def test_parse_label_bullet
  239. str = <<-STR
  240. [cat] l1
  241. * l1.1
  242. [dog] l2
  243. STR
  244. expected = [
  245. @RM::List.new(:LABEL, *[
  246. @RM::ListItem.new('cat',
  247. @RM::Paragraph.new('l1'),
  248. @RM::List.new(:BULLET, *[
  249. @RM::ListItem.new(nil,
  250. @RM::Paragraph.new('l1.1'))])),
  251. @RM::ListItem.new('dog',
  252. @RM::Paragraph.new('l2'))])]
  253. assert_equal expected, @RMP.parse(str).parts
  254. end
  255. def test_parse_label_multiline
  256. str = <<-STR
  257. [cat] l1
  258. continuation
  259. [dog] l2
  260. STR
  261. expected = [
  262. @RM::List.new(:LABEL, *[
  263. @RM::ListItem.new('cat',
  264. @RM::Paragraph.new('l1', 'continuation')),
  265. @RM::ListItem.new('dog',
  266. @RM::Paragraph.new('l2'))])]
  267. assert_equal expected, @RMP.parse(str).parts
  268. end
  269. def test_parse_label_newline
  270. str = <<-STR
  271. [one]
  272. item one
  273. [two]
  274. item two
  275. STR
  276. expected = [
  277. @RM::List.new(:LABEL, *[
  278. @RM::ListItem.new('one',
  279. @RM::Paragraph.new('item one')),
  280. @RM::ListItem.new('two',
  281. @RM::Paragraph.new('item two')),
  282. ])]
  283. assert_equal expected, @RMP.parse(str).parts
  284. end
  285. def test_parse_lalpha
  286. str = <<-STR
  287. a. l1
  288. b. l2
  289. STR
  290. expected = [
  291. @RM::List.new(:LALPHA, *[
  292. @RM::ListItem.new(nil,
  293. @RM::Paragraph.new('l1')),
  294. @RM::ListItem.new(nil,
  295. @RM::Paragraph.new('l2'))])]
  296. assert_equal expected, @RMP.parse(str).parts
  297. end
  298. def test_parse_lalpha_ualpha
  299. str = <<-STR
  300. a. l1
  301. b. l2
  302. A. l3
  303. A. l4
  304. STR
  305. expected = [
  306. @RM::List.new(:LALPHA, *[
  307. @RM::ListItem.new(nil,
  308. @RM::Paragraph.new('l1')),
  309. @RM::ListItem.new(nil,
  310. @RM::Paragraph.new('l2'))]),
  311. @RM::List.new(:UALPHA, *[
  312. @RM::ListItem.new(nil,
  313. @RM::Paragraph.new('l3')),
  314. @RM::ListItem.new(nil,
  315. @RM::Paragraph.new('l4'))])]
  316. assert_equal expected, @RMP.parse(str).parts
  317. end
  318. def test_parse_list_verbatim
  319. str = <<-STR
  320. * one
  321. verb1
  322. verb2
  323. * two
  324. STR
  325. expected = [
  326. @RM::List.new(:BULLET, *[
  327. @RM::ListItem.new(nil,
  328. @RM::Paragraph.new('one'),
  329. @RM::Verbatim.new(' ', 'verb1', "\n",
  330. ' ', 'verb2', "\n")),
  331. @RM::ListItem.new(nil,
  332. @RM::Paragraph.new('two'))])]
  333. assert_equal expected, @RMP.parse(str).parts
  334. end
  335. def test_parse_lists
  336. str = <<-STR
  337. now is
  338. * l1
  339. 1. n1
  340. 2. n2
  341. * l2
  342. the time
  343. STR
  344. expected = [
  345. @RM::Paragraph.new('now is'),
  346. @RM::List.new(:BULLET, *[
  347. @RM::ListItem.new(nil,
  348. @RM::Paragraph.new('l1'))]),
  349. @RM::List.new(:NUMBER, *[
  350. @RM::ListItem.new(nil,
  351. @RM::Paragraph.new('n1')),
  352. @RM::ListItem.new(nil,
  353. @RM::Paragraph.new('n2'))]),
  354. @RM::List.new(:BULLET, *[
  355. @RM::ListItem.new(nil,
  356. @RM::Paragraph.new('l2'))]),
  357. @RM::Paragraph.new('the time')]
  358. assert_equal expected, @RMP.parse(str).parts
  359. end
  360. def test_parse_note
  361. str = <<-STR
  362. one:: item one
  363. two:: item two
  364. STR
  365. expected = [
  366. @RM::List.new(:NOTE, *[
  367. @RM::ListItem.new('one',
  368. @RM::Paragraph.new('item one')),
  369. @RM::ListItem.new('two',
  370. @RM::Paragraph.new('item two'))])]
  371. assert_equal expected, @RMP.parse(str).parts
  372. end
  373. def test_parse_note_empty
  374. str = <<-STR
  375. one::
  376. two::
  377. STR
  378. expected = [
  379. @RM::List.new(:NOTE, *[
  380. @RM::ListItem.new('one',
  381. @RM::BlankLine.new),
  382. @RM::ListItem.new('two',
  383. @RM::BlankLine.new)])]
  384. assert_equal expected, @RMP.parse(str).parts
  385. end
  386. def test_parse_note_note
  387. str = <<-STR
  388. one:: two::
  389. STR
  390. expected = [
  391. @RM::List.new(:NOTE, *[
  392. @RM::ListItem.new('one',
  393. @RM::List.new(:NOTE, *[
  394. @RM::ListItem.new('two',
  395. @RM::BlankLine.new)]))])]
  396. assert_equal expected, @RMP.parse(str).parts
  397. end
  398. def test_parse_number_bullet
  399. str = <<-STR
  400. 1. l1
  401. * l1.1
  402. 2. l2
  403. STR
  404. expected = [
  405. @RM::List.new(:NUMBER, *[
  406. @RM::ListItem.new(nil,
  407. @RM::Paragraph.new('l1'),
  408. @RM::List.new(:BULLET, *[
  409. @RM::ListItem.new(nil,
  410. @RM::Paragraph.new('l1.1'))])),
  411. @RM::ListItem.new(nil,
  412. @RM::Paragraph.new('l2'))])]
  413. assert_equal expected, @RMP.parse(str).parts
  414. end
  415. def test_parse_paragraph
  416. str = <<-STR
  417. now is the time
  418. for all good men
  419. STR
  420. expected = [
  421. @RM::Paragraph.new('now is the time'),
  422. @RM::BlankLine.new,
  423. @RM::Paragraph.new('for all good men')]
  424. assert_equal expected, @RMP.parse(str).parts
  425. end
  426. def test_parse_paragraph_multiline
  427. str = "now is the time\nfor all good men"
  428. expected = @RM::Paragraph.new 'now is the time for all good men'
  429. assert_equal [expected], @RMP.parse(str).parts
  430. end
  431. def test_parse_paragraph_verbatim
  432. str = <<-STR
  433. now is the time
  434. code _line_ here
  435. for all good men
  436. STR
  437. expected = [
  438. @RM::Paragraph.new('now is the time'),
  439. @RM::Verbatim.new(' ', 'code _line_ here', "\n"),
  440. @RM::Paragraph.new('for all good men'),
  441. ]
  442. assert_equal expected, @RMP.parse(str).parts
  443. end
  444. def test_parse_ualpha
  445. str = <<-STR
  446. A. l1
  447. B. l2
  448. STR
  449. expected = [
  450. @RM::List.new(:UALPHA, *[
  451. @RM::ListItem.new(nil,
  452. @RM::Paragraph.new('l1')),
  453. @RM::ListItem.new(nil,
  454. @RM::Paragraph.new('l2'))])]
  455. assert_equal expected, @RMP.parse(str).parts
  456. end
  457. def test_parse_verbatim
  458. str = <<-STR
  459. now is
  460. code
  461. the time
  462. STR
  463. expected = [
  464. @RM::Paragraph.new('now is'),
  465. @RM::Verbatim.new(' ', 'code', "\n"),
  466. @RM::Paragraph.new('the time'),
  467. ]
  468. assert_equal expected, @RMP.parse(str).parts
  469. end
  470. def test_parse_verbatim_bullet
  471. str = <<-STR
  472. * blah
  473. STR
  474. expected = [
  475. @RM::Verbatim.new(' ', '*', ' ', 'blah', "\n")]
  476. assert_equal expected, @RMP.parse(str).parts
  477. end
  478. def test_parse_verbatim_fold
  479. str = <<-STR
  480. now is
  481. code
  482. code1
  483. the time
  484. STR
  485. expected = [
  486. @RM::Paragraph.new('now is'),
  487. @RM::Verbatim.new(' ', 'code', "\n",
  488. "\n",
  489. ' ', 'code1', "\n"),
  490. @RM::Paragraph.new('the time'),
  491. ]
  492. assert_equal expected, @RMP.parse(str).parts
  493. end
  494. def test_parse_verbatim_heading
  495. str = <<-STR
  496. text
  497. === heading three
  498. STR
  499. expected = [
  500. @RM::Paragraph.new('text'),
  501. @RM::Verbatim.new(' ', '===', ' ', 'heading three', "\n")]
  502. assert_equal expected, @RMP.parse(str).parts
  503. end
  504. def test_parse_verbatim_heading2
  505. str = "text\n code\n=== heading three"
  506. expected = [
  507. @RM::Paragraph.new('text'),
  508. @RM::Verbatim.new(' ', 'code', "\n"),
  509. @RM::Heading.new(3, 'heading three')]
  510. assert_equal expected, @RMP.parse(str).parts
  511. end
  512. def test_parse_verbatim_label
  513. str = <<-STR
  514. [blah] blah
  515. STR
  516. expected = [
  517. @RM::Verbatim.new(' ', '[blah]', ' ', 'blah', "\n")]
  518. assert_equal expected, @RMP.parse(str).parts
  519. end
  520. def test_parse_verbatim_lalpha
  521. str = <<-STR
  522. b. blah
  523. STR
  524. expected = [
  525. @RM::Verbatim.new(' ', 'b.', ' ', 'blah', "\n")]
  526. assert_equal expected, @RMP.parse(str).parts
  527. end
  528. def test_parse_verbatim_markup_example
  529. str = <<-STR
  530. text
  531. code
  532. === heading three
  533. STR
  534. expected = [
  535. @RM::Paragraph.new('text'),
  536. @RM::Verbatim.new(' ', 'code', "\n",
  537. ' ', '===', ' ', 'heading three', "\n")]
  538. assert_equal expected, @RMP.parse(str).parts
  539. end
  540. def test_parse_verbatim_merge
  541. str = <<-STR
  542. now is
  543. code
  544. code1
  545. the time
  546. STR
  547. expected = [
  548. @RM::Paragraph.new('now is'),
  549. @RM::Verbatim.new(' ', 'code', "\n",
  550. "\n",
  551. ' ', 'code1', "\n"),
  552. @RM::Paragraph.new('the time'),
  553. ]
  554. assert_equal expected, @RMP.parse(str).parts
  555. end
  556. def test_parse_verbatim_merge2
  557. str = <<-STR
  558. now is
  559. code
  560. code1
  561. code2
  562. the time
  563. STR
  564. expected = [
  565. @RM::Paragraph.new('now is'),
  566. @RM::Verbatim.new(' ', 'code', "\n",
  567. "\n",
  568. ' ', 'code1', "\n",
  569. "\n",
  570. ' ', 'code2', "\n"),
  571. @RM::Paragraph.new('the time'),
  572. ]
  573. assert_equal expected, @RMP.parse(str).parts
  574. end
  575. def test_parse_verbatim_multiline
  576. str = <<-STR
  577. now is
  578. code
  579. code1
  580. the time
  581. STR
  582. expected = [
  583. @RM::Paragraph.new('now is'),
  584. @RM::Verbatim.new(' ', 'code', "\n",
  585. ' ', 'code1', "\n"),
  586. @RM::Paragraph.new('the time'),
  587. ]
  588. assert_equal expected, @RMP.parse(str).parts
  589. end
  590. def test_parse_verbatim_multilevel
  591. str = <<-STR
  592. now is the time
  593. code
  594. more code
  595. for all good men
  596. STR
  597. expected = [
  598. @RM::Paragraph.new('now is the time'),
  599. @RM::Verbatim.new(' ', 'code', "\n",
  600. ' ', 'more code', "\n"),
  601. @RM::Paragraph.new('for all good men'),
  602. ]
  603. assert_equal expected, @RMP.parse(str).parts
  604. end
  605. def test_parse_verbatim_note
  606. str = <<-STR
  607. blah:: blah
  608. STR
  609. expected = [
  610. @RM::Verbatim.new(' ', 'blah::', ' ', 'blah', "\n")]
  611. assert_equal expected, @RMP.parse(str).parts
  612. end
  613. def test_parse_verbatim_number
  614. str = <<-STR
  615. 2. blah
  616. STR
  617. expected = [
  618. @RM::Verbatim.new(' ', '2.', ' ', 'blah', "\n")]
  619. assert_equal expected, @RMP.parse(str).parts
  620. end
  621. def test_parse_verbatim_rule
  622. str = <<-STR
  623. text
  624. --- lib/blah.rb.orig
  625. +++ lib/blah.rb
  626. STR
  627. expected = [
  628. @RM::Paragraph.new('text'),
  629. @RM::BlankLine.new,
  630. @RM::Verbatim.new(' ', '---', ' ', 'lib/blah.rb.orig', "\n",
  631. ' ', '+++', ' ', 'lib/blah.rb', "\n")]
  632. assert_equal expected, @RMP.parse(str).parts
  633. end
  634. def test_parse_verbatim_rule2
  635. str = <<-STR.chomp
  636. text
  637. ---
  638. STR
  639. expected = [
  640. @RM::Paragraph.new('text'),
  641. @RM::BlankLine.new,
  642. @RM::Verbatim.new(' ', '---', '')]
  643. assert_equal expected, @RMP.parse(str).parts
  644. end
  645. def test_parse_verbatim_trim
  646. str = <<-STR
  647. now is
  648. code
  649. code1
  650. the time
  651. STR
  652. expected = [
  653. @RM::Paragraph.new('now is'),
  654. @RM::Verbatim.new(' ', 'code', "\n",
  655. "\n",
  656. ' ', 'code1', "\n"),
  657. @RM::Paragraph.new('the time'),
  658. ]
  659. assert_equal expected, @RMP.parse(str).parts
  660. end
  661. def test_parse_verbatim_ualpha
  662. str = <<-STR
  663. B. blah
  664. STR
  665. expected = [
  666. @RM::Verbatim.new(' ', 'B.', ' ', 'blah', "\n")]
  667. assert_equal expected, @RMP.parse(str).parts
  668. end
  669. def test_parse_whitespace
  670. expected = [
  671. @RM::Paragraph.new('hello'),
  672. ]
  673. assert_equal expected, @RMP.parse('hello').parts
  674. expected = [
  675. @RM::Verbatim.new(' ', 'hello '),
  676. ]
  677. assert_equal expected, @RMP.parse(' hello ').parts
  678. expected = [
  679. @RM::Verbatim.new(' ', 'hello '),
  680. ]
  681. assert_equal expected, @RMP.parse(" hello ").parts
  682. expected = [
  683. @RM::Paragraph.new('1'),
  684. @RM::Verbatim.new(' ', '2', "\n",
  685. ' ', '3'),
  686. ]
  687. assert_equal expected, @RMP.parse("1\n 2\n 3").parts
  688. expected = [
  689. @RM::Verbatim.new(' ', '1', "\n",
  690. ' ', '2', "\n",
  691. ' ', '3'),
  692. ]
  693. assert_equal expected, @RMP.parse(" 1\n 2\n 3").parts
  694. expected = [
  695. @RM::Paragraph.new('1'),
  696. @RM::Verbatim.new(' ', '2', "\n",
  697. ' ', '3', "\n"),
  698. @RM::Paragraph.new('1'),
  699. @RM::Verbatim.new(' ', '2'),
  700. ]
  701. assert_equal expected, @RMP.parse("1\n 2\n 3\n1\n 2").parts
  702. expected = [
  703. @RM::Verbatim.new(' ', '1', "\n",
  704. ' ', '2', "\n",
  705. ' ', '3', "\n",
  706. ' ', '1', "\n",
  707. ' ', '2'),
  708. ]
  709. assert_equal expected, @RMP.parse(" 1\n 2\n 3\n 1\n 2").parts
  710. expected = [
  711. @RM::Verbatim.new(' ', '1', "\n",
  712. ' ', '2', "\n",
  713. "\n",
  714. ' ', '3'),
  715. ]
  716. assert_equal expected, @RMP.parse(" 1\n 2\n\n 3").parts
  717. end
  718. def test_peek_token
  719. parser = util_parser
  720. assert_equal [:HEADER, 1, 0, 0], parser.peek_token
  721. assert_equal 8, parser.tokens.length
  722. end
  723. def test_skip
  724. parser = util_parser
  725. assert_equal [:HEADER, 1, 0, 0], parser.skip(:HEADER)
  726. assert_equal [:TEXT, 'Heading', 2, 0], parser.get
  727. assert_equal [:NEWLINE, "\n", 9, 0], parser.peek_token
  728. assert_raises RDoc::Markup::Parser::ParseError do
  729. parser.skip :NONE
  730. end
  731. assert_equal [:NEWLINE, "\n", 9, 0], parser.peek_token
  732. assert_equal nil, parser.skip(:NONE, false)
  733. assert_equal [:NEWLINE, "\n", 9, 0], parser.peek_token
  734. end
  735. def test_tokenize_bullet
  736. str = <<-STR
  737. * l1
  738. STR
  739. expected = [
  740. [:BULLET, :BULLET, 0, 0],
  741. [:SPACE, 2, 0, 0],
  742. [:TEXT, 'l1', 2, 0],
  743. [:NEWLINE, "\n", 4, 0],
  744. ]
  745. assert_equal expected, @RMP.tokenize(str)
  746. end
  747. def test_tokenize_bullet_indent
  748. str = <<-STR
  749. * l1
  750. * l1.1
  751. STR
  752. expected = [
  753. [:BULLET, :BULLET, 0, 0],
  754. [:SPACE, 2, 0, 0],
  755. [:TEXT, 'l1', 2, 0],
  756. [:NEWLINE, "\n", 4, 0],
  757. [:INDENT, 2, 0, 1],
  758. [:BULLET, :BULLET, 2, 1],
  759. [:SPACE, 2, 2, 1],
  760. [:TEXT, 'l1.1', 4, 1],
  761. [:NEWLINE, "\n", 8, 1],
  762. ]
  763. assert_equal expected, @RMP.tokenize(str)
  764. end
  765. def test_tokenize_heading
  766. str = <<-STR
  767. = Heading
  768. == Heading 2
  769. STR
  770. expected = [
  771. [:HEADER, 1, 0, 0],
  772. [:TEXT, 'Heading', 2, 0],
  773. [:NEWLINE, "\n", 9, 0],
  774. [:HEADER, 2, 0, 1],
  775. [:TEXT, 'Heading 2', 3, 1],
  776. [:NEWLINE, "\n", 12, 1],
  777. ]
  778. assert_equal expected, @RMP.tokenize(str)
  779. end
  780. def test_tokenize_heading_heading
  781. str = <<-STR
  782. = =
  783. STR
  784. expected = [
  785. [:HEADER, 1, 0, 0],
  786. [:TEXT, '=', 2, 0],
  787. [:NEWLINE, "\n", 3, 0],
  788. ]
  789. assert_equal expected, @RMP.tokenize(str)
  790. end
  791. def test_tokenize_heading_no_space
  792. str = <<-STR
  793. =Heading
  794. ==Heading 2
  795. STR
  796. expected = [
  797. [:HEADER, 1, 0, 0],
  798. [:TEXT, 'Heading', 1, 0],
  799. [:NEWLINE, "\n", 8, 0],
  800. [:HEADER, 2, 0, 1],
  801. [:TEXT, 'Heading 2', 2, 1],
  802. [:NEWLINE, "\n", 11, 1],
  803. ]
  804. assert_equal expected, @RMP.tokenize(str)
  805. end
  806. def test_tokenize_label
  807. str = <<-STR
  808. [cat] l1
  809. [dog] l1.1
  810. STR
  811. expected = [
  812. [:LABEL, 'cat', 0, 0],
  813. [:SPACE, 6, 0, 0],
  814. [:TEXT, 'l1', 6, 0],
  815. [:NEWLINE, "\n", 8, 0],
  816. [:LABEL, 'dog', 0, 1],
  817. [:SPACE, 6, 0, 1],
  818. [:TEXT, 'l1.1', 6, 1],
  819. [:NEWLINE, "\n", 10, 1],
  820. ]
  821. assert_equal expected, @RMP.tokenize(str)
  822. end
  823. def test_tokenize_label_note
  824. str = <<-STR
  825. [label]
  826. note::
  827. STR
  828. expected = [
  829. [:LABEL, 'label', 0, 0],
  830. [:SPACE, 7, 0, 0],
  831. [:NEWLINE, "\n", 7, 0],
  832. [:INDENT, 2, 0, 1],
  833. [:NOTE, 'note', 2, 1],
  834. [:SPACE, 6, 2, 1],
  835. [:NEWLINE, "\n", 8, 1],
  836. ]
  837. assert_equal expected, @RMP.tokenize(str)
  838. end
  839. def test_tokenize_lalpha
  840. str = <<-STR
  841. a. l1
  842. b. l1.1
  843. STR
  844. expected = [
  845. [:LALPHA, 'a', 0, 0],
  846. [:SPACE, 3, 0, 0],
  847. [:TEXT, 'l1', 3, 0],
  848. [:NEWLINE, "\n", 5, 0],
  849. [:LALPHA, 'b', 0, 1],
  850. [:SPACE, 3, 0, 1],
  851. [:TEXT, 'l1.1', 3, 1],
  852. [:NEWLINE, "\n", 7, 1],
  853. ]
  854. assert_equal expected, @RMP.tokenize(str)
  855. end
  856. def test_tokenize_note
  857. str = <<-STR
  858. cat:: l1
  859. dog:: l1.1
  860. STR
  861. expected = [
  862. [:NOTE, 'cat', 0, 0],
  863. [:SPACE, 6, 0, 0],
  864. [:TEXT, 'l1', 6, 0],
  865. [:NEWLINE, "\n", 8, 0],
  866. [:NOTE, 'dog', 0, 1],
  867. [:SPACE, 6, 0, 1],
  868. [:TEXT, 'l1.1', 6, 1],
  869. [:NEWLINE, "\n", 10, 1],
  870. ]
  871. assert_equal expected, @RMP.tokenize(str)
  872. end
  873. def test_tokenize_note_empty
  874. str = <<-STR
  875. cat::
  876. dog::
  877. STR
  878. expected = [
  879. [:NOTE, 'cat', 0, 0],
  880. [:SPACE, 5, 0, 0],
  881. [:NEWLINE, "\n", 5, 0],
  882. [:NOTE, 'dog', 0, 1],
  883. [:SPACE, 5, 0, 1],
  884. [:NEWLINE, "\n", 5, 1],
  885. ]
  886. assert_equal expected, @RMP.tokenize(str)
  887. end
  888. def test_tokenize_note_not
  889. str = <<-STR
  890. Cat::Dog
  891. STR
  892. expected = [
  893. [:TEXT, 'Cat::Dog', 0, 0],
  894. [:NEWLINE, "\n", 8, 0],
  895. ]
  896. assert_equal expected, @RMP.tokenize(str)
  897. end
  898. def test_tokenize_number
  899. str = <<-STR
  900. 1. l1
  901. 2. l1.1
  902. STR
  903. expected = [
  904. [:NUMBER, '1', 0, 0],
  905. [:SPACE, 3, 0, 0],
  906. [:TEXT, 'l1', 3, 0],
  907. [:NEWLINE, "\n", 5, 0],
  908. [:NUMBER, '2', 0, 1],
  909. [:SPACE, 3, 0, 1],
  910. [:TEXT, 'l1.1', 3, 1],
  911. [:NEWLINE, "\n", 7, 1],
  912. ]
  913. assert_equal expected, @RMP.tokenize(str)
  914. end
  915. def test_tokenize_number_period
  916. str = <<-STR
  917. 1. blah blah blah
  918. l.
  919. 2. blah blah blah blah
  920. d.
  921. STR
  922. expected = [
  923. [:NUMBER, "1", 0, 0],
  924. [:SPACE, 3, 0, 0],
  925. [:TEXT, "blah blah blah", 3, 0],
  926. [:NEWLINE, "\n", 17, 0],
  927. [:INDENT, 3, 0, 1],
  928. [:TEXT, "l.", 3, 1],
  929. [:NEWLINE, "\n", 5, 1],
  930. [:NUMBER, "2", 0, 2],
  931. [:SPACE, 3, 0, 2],
  932. [:TEXT, "blah blah blah blah", 3, 2],
  933. [:NEWLINE, "\n", 22, 2],
  934. [:INDENT, 3, 0, 3],
  935. [:TEXT, "d.", 3, 3],
  936. [:NEWLINE, "\n", 5, 3]
  937. ]
  938. assert_equal expected, @RMP.tokenize(str)
  939. end
  940. def test_tokenize_number_period_continue
  941. str = <<-STR
  942. 1. blah blah blah
  943. l. more stuff
  944. 2. blah blah blah blah
  945. d. other stuff
  946. STR
  947. expected = [
  948. [:NUMBER, "1", 0, 0],
  949. [:SPACE, 3, 0, 0],
  950. [:TEXT, "blah blah blah", 3, 0],
  951. [:NEWLINE, "\n", 17, 0],
  952. [:INDENT, 3, 0, 1],
  953. [:LALPHA, "l", 3, 1],
  954. [:SPACE, 4, 3, 1],
  955. [:TEXT, "more stuff", 7, 1],
  956. [:NEWLINE, "\n", 17, 1],
  957. [:NUMBER, "2", 0, 2],
  958. [:SPACE, 3, 0, 2],
  959. [:TEXT, "blah blah blah blah", 3, 2],
  960. [:NEWLINE, "\n", 22, 2],
  961. [:INDENT, 3, 0, 3],
  962. [:LALPHA, "d", 3, 3],
  963. [:SPACE, 3, 3, 3],
  964. [:TEXT, "other stuff", 6, 3],
  965. [:NEWLINE, "\n", 17, 3]
  966. ]
  967. assert_equal expected, @RMP.tokenize(str)
  968. end
  969. def test_tokenize_paragraphs
  970. str = <<-STR
  971. now is
  972. the time
  973. for all
  974. STR
  975. expected = [
  976. [:TEXT, 'now is', 0, 0],
  977. [:NEWLINE, "\n", 6, 0],
  978. [:TEXT, 'the time', 0, 1],
  979. [:NEWLINE, "\n", 8, 1],
  980. [:NEWLINE, "\n", 0, 2],
  981. [:TEXT, 'for all', 0, 3],
  982. [:NEWLINE, "\n", 7, 3],
  983. ]
  984. assert_equal expected, @RMP.tokenize(str)
  985. end
  986. def test_tokenize_rule
  987. str = <<-STR
  988. ---
  989. --- blah ---
  990. STR
  991. expected = [
  992. [:RULE, 1, 0, 0],
  993. [:NEWLINE, "\n", 4, 0],
  994. [:NEWLINE, "\n", 0, 1],
  995. [:TEXT, "--- blah ---", 0, 2],
  996. [:NEWLINE, "\n", 12, 2],
  997. ]
  998. assert_equal expected, @RMP.tokenize(str)
  999. end
  1000. def test_tokenize_ualpha
  1001. str = <<-STR
  1002. A. l1
  1003. B. l1.1
  1004. STR
  1005. expected = [
  1006. [:UALPHA, 'A', 0, 0],
  1007. [:SPACE, 3, 0, 0],
  1008. [:TEXT, 'l1', 3, 0],
  1009. [:NEWLINE, "\n", 5, 0],
  1010. [:UALPHA, 'B', 0, 1],
  1011. [:SPACE, 3, 0, 1],
  1012. [:TEXT, 'l1.1', 3, 1],
  1013. [:NEWLINE, "\n", 7, 1],
  1014. ]
  1015. assert_equal expected, @RMP.tokenize(str)
  1016. end
  1017. def test_tokenize_verbatim_heading
  1018. str = <<-STR
  1019. Example heading:
  1020. === heading three
  1021. STR
  1022. expected = [
  1023. [:TEXT, 'Example heading:', 0, 0],
  1024. [:NEWLINE, "\n", 16, 0],
  1025. [:NEWLINE, "\n", 0, 1],
  1026. [:INDENT, 3, 0, 2],
  1027. [:HEADER, 3, 3, 2],
  1028. [:TEXT, 'heading three', 7, 2],
  1029. [:NEWLINE, "\n", 20, 2],
  1030. ]
  1031. assert_equal expected, @RMP.tokenize(str)
  1032. end
  1033. # HACK move to Verbatim test case
  1034. def test_verbatim_normalize
  1035. v = @RM::Verbatim.new ' ', 'foo', "\n", "\n", "\n", ' ', 'bar', "\n"
  1036. v.normalize
  1037. assert_equal [' ', 'foo', "\n", "\n", ' ', 'bar', "\n"], v.parts
  1038. v = @RM::Verbatim.new ' ', 'foo', "\n", "\n"
  1039. v.normalize
  1040. assert_equal [' ', 'foo', "\n"], v.parts
  1041. end
  1042. def test_unget
  1043. parser = util_parser
  1044. parser.get
  1045. parser.unget
  1046. assert_equal [:HEADER, 1, 0, 0], parser.peek_token
  1047. assert_raises @RMP::Error do
  1048. parser.unget
  1049. end
  1050. assert_equal 8, parser.tokens.length
  1051. end
  1052. def util_parser
  1053. str = <<-STR
  1054. = Heading
  1055. Some text here
  1056. some more text over here
  1057. STR
  1058. @parser = @RMP.new
  1059. @parser.tokenize str
  1060. @parser
  1061. end
  1062. end