PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/rdoc/test_rdoc_markup_parser.rb

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