PageRenderTime 91ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/parsers/doc_markdown_parser.test.php

http://github.com/CakeDC/markup_parsers
PHP | 709 lines | 469 code | 116 blank | 124 comment | 0 complexity | 6bf9d21147bce5f9c225ed310db8545b MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. App::import('Lib', array('MarkupParsers.DocMarkdownParser'));
  12. /**
  13. * HtmlParser test case
  14. *
  15. * @package markup_parsers
  16. * @subpackage markup_parsers.tests.cases.libs
  17. */
  18. class DocMarkdownParserTest extends CakeTestCase {
  19. /**
  20. * setUp method
  21. *
  22. * @return void
  23. */
  24. public function setup() {
  25. $this->Parser = new DocMarkdownParser();
  26. }
  27. /**
  28. * tearDown method
  29. *
  30. * @return void
  31. */
  32. public function tearDown() {
  33. ClassRegistry::flush();
  34. unset($this->Parser);
  35. }
  36. /**
  37. * testParse
  38. *
  39. * @return void
  40. * @access public
  41. */
  42. public function testParse() {
  43. $testText = <<<TEXT
  44. # test #
  45. * One
  46. * Two
  47. * Three
  48. TEXT;
  49. $expected = <<<HTML
  50. <h1>test</h1>
  51. <ul>
  52. <li>One</li>
  53. <li>Two</li>
  54. <li>Three</li>
  55. </ul>
  56. <p></p>
  57. HTML;
  58. $result = $this->Parser->parse($testText);
  59. $this->assertEqual($result, $expected);
  60. }
  61. /**
  62. * test emphasis and bold elements.
  63. *
  64. * @return void
  65. */
  66. function testEmphasisAndBold() {
  67. $text = 'Normal text *emphasis text* normal *emphasis* normal.';
  68. $result = $this->Parser->parse($text);
  69. $expected = '<p>Normal text <em>emphasis text</em> normal <em>emphasis</em> normal.</p>';
  70. $this->assertEqual($result, $expected);
  71. $text = 'Normal text **bold** normal *emphasis* normal.';
  72. $result = $this->Parser->parse($text);
  73. $expected = '<p>Normal text <strong>bold</strong> normal <em>emphasis</em> normal.</p>';
  74. $this->assertEqual($result, $expected);
  75. $text = 'Normal text ***bold*** normal *emphasis* normal.';
  76. $result = $this->Parser->parse($text);
  77. $expected = '<p>Normal text <strong><em>bold</em></strong> normal <em>emphasis</em> normal.</p>';
  78. $this->assertEqual($result, $expected);
  79. $text = 'Normal text _emphasis text_ normal _emphasis_ normal.';
  80. $result = $this->Parser->parse($text);
  81. $expected = '<p>Normal text <em>emphasis text</em> normal <em>emphasis</em> normal.</p>';
  82. $this->assertEqual($result, $expected);
  83. $text = 'Normal text __bold__ normal _emphasis_ normal.';
  84. $result = $this->Parser->parse($text);
  85. $expected = '<p>Normal text <strong>bold</strong> normal <em>emphasis</em> normal.</p>';
  86. $this->assertEqual($result, $expected);
  87. $text = 'Normal text ___bold___ normal _emphasis_ normal.';
  88. $result = $this->Parser->parse($text);
  89. $expected = '<p>Normal text <strong><em>bold</em></strong> normal <em>emphasis</em> normal.</p>';
  90. $this->assertEqual($result, $expected);
  91. }
  92. /**
  93. * test inline code elements.
  94. *
  95. * @return void
  96. */
  97. function testInlineCode() {
  98. $text = 'Normal text `code text` normal `code` normal.';
  99. $result = $this->Parser->parse($text);
  100. $expected = '<p>Normal text <code>code text</code> normal <code>code</code> normal.</p>';
  101. $this->assertEqual($result, $expected);
  102. $text = 'Normal text ``code text` normal `code`` normal.';
  103. $result = $this->Parser->parse($text);
  104. $expected = '<p>Normal text <code>code text` normal `code</code> normal.</p>';
  105. $this->assertEqual($result, $expected);
  106. $text = 'Normal text ``code text` < > & normal `code`` normal.';
  107. $result = $this->Parser->parse($text);
  108. $expected = '<p>Normal text <code>code text` &lt; > &amp; normal `code</code> normal.</p>';
  109. $this->assertEqual($result, $expected);
  110. $text = 'Normal text ``code text some_variable_here_code text`` normal.';
  111. $result = $this->Parser->parse($text);
  112. $expected = '<p>Normal text <code>code text some_variable_here_code text</code> normal.</p>';
  113. $this->assertEqual($result, $expected);
  114. }
  115. /**
  116. * test inline code elements.
  117. *
  118. * @return void
  119. */
  120. function testAutoLink() {
  121. $text = 'Normal text www.foo.com normal code normal.';
  122. $result = $this->Parser->parse($text);
  123. $expected = '<p>Normal text <a href="http://www.foo.com">www.foo.com</a> normal code normal.</p>';
  124. $this->assertEqual($result, $expected);
  125. $text = 'Normal text www.foo.com/page/foo:bar normal code normal.';
  126. $result = $this->Parser->parse($text);
  127. $expected = '<p>Normal text <a href="http://www.foo.com/page/foo:bar">www.foo.com/page/foo:bar</a> normal code normal.</p>';
  128. $this->assertEqual($result, $expected);
  129. $text = 'Normal text http://www.foo.com normal code normal.';
  130. $result = $this->Parser->parse($text);
  131. $expected = '<p>Normal text <a href="http://www.foo.com">http://www.foo.com</a> normal code normal.</p>';
  132. $this->assertEqual($result, $expected);
  133. }
  134. /**
  135. * testAutoLinksInLists
  136. *
  137. * @return void
  138. */
  139. function testAutoLinksInLists() {
  140. $text = <<<TEXT
  141. * [1] http://cakephp.lighthouseapp.com/projects/42648/changelog-1-3-5
  142. * [2] http://github.com/cakephp/cakephp/downloads
  143. * [3] http://cakephp.lighthouseapp.com/projects/42648
  144. * [4] http://cakedc.com
  145. TEXT;
  146. $result = $this->Parser->parse($text);
  147. preg_match_all('/<a href=/', $result, $matches);
  148. $this->assertIdentical(count($matches[0]), 4);
  149. }
  150. /**
  151. * test inline links
  152. *
  153. * @return void
  154. */
  155. function testInlineLinks() {
  156. $text = 'Normal text [test link](http://www.foo.com) normal code normal.';
  157. $result = $this->Parser->parse($text);
  158. $expected = '<p>Normal text <a href="http://www.foo.com">test link</a> normal code normal.</p>';
  159. $this->assertEqual($result, $expected);
  160. $text = 'Normal text [test link](http://www.foo.com "some title") normal code normal.';
  161. $result = $this->Parser->parse($text);
  162. $expected = '<p>Normal text <a href="http://www.foo.com" title="some title">test link</a> normal code normal.</p>';
  163. $this->assertEqual($result, $expected);
  164. }
  165. /**
  166. * test entity conversion
  167. *
  168. * @return void
  169. */
  170. function testEntityConversion() {
  171. $text = 'Normal < text [test link](http://www.foo.com) normal & code normal.';
  172. $result = $this->Parser->parse($text);
  173. $expected = '<p>Normal &lt; text <a href="http://www.foo.com">test link</a> normal &amp; code normal.</p>';
  174. $this->assertEqual($result, $expected);
  175. }
  176. /**
  177. * Test Headings
  178. *
  179. * @return void
  180. */
  181. function testHeadings() {
  182. $text = <<<TEXT
  183. # H1
  184. ## H2 ##
  185. ### heading 3
  186. #### heading 4
  187. ##### Imbalanced ##
  188. ######## There is no heading 8
  189. TEXT;
  190. $result = $this->Parser->parse($text);
  191. $expected = <<<HTML
  192. <h1>H1</h1>
  193. <h2>H2</h2>
  194. <h3>heading 3</h3>
  195. <h4>heading 4</h4>
  196. <h5>Imbalanced</h5>
  197. <h6>There is no heading 8</h6>
  198. HTML;
  199. $this->assertEqual($result, $expected);
  200. }
  201. /**
  202. * test horizontal rules.
  203. *
  204. * @return void
  205. */
  206. function testHorizontalRule() {
  207. $expected = <<<HTML
  208. <p>this is some</p>
  209. <hr />
  210. <p>text</p>
  211. HTML;
  212. foreach (array('-', '*', '_') as $char) {
  213. $text = <<<TEXT
  214. this is some
  215. {$char}{$char}{$char}
  216. text
  217. TEXT;
  218. $result = $this->Parser->parse($text);
  219. $this->assertEqual($result, $expected);
  220. $text = <<<TEXT
  221. this is some
  222. {$char} {$char} {$char}
  223. text
  224. TEXT;
  225. $result = $this->Parser->parse($text);
  226. $this->assertEqual($result, $expected);
  227. $text = <<<TEXT
  228. this is some
  229. {$char}{$char}{$char}{$char}{$char}{$char}
  230. text
  231. TEXT;
  232. $result = $this->Parser->parse($text);
  233. $this->assertEqual($result, $expected);
  234. }
  235. }
  236. /**
  237. * test multiline code blocks
  238. *
  239. * @return void
  240. */
  241. function testCodeBlockWithDelimiters() {
  242. $text = <<<TEXT
  243. this is some
  244. @@@
  245. function test() {
  246. echo '<test>';
  247. }
  248. @@@
  249. more text
  250. TEXT;
  251. $expected = <<<HTML
  252. <p>this is some</p>
  253. <pre><code>function test() {
  254. echo '&lt;test&gt;';
  255. }</code></pre>
  256. <p>more text</p>
  257. HTML;
  258. $result = $this->Parser->parse($text);
  259. $this->assertEqual($result, $expected);
  260. $text = <<<TEXT
  261. this is some
  262. {{{
  263. function test() {
  264. echo '<test>';
  265. }
  266. }}}
  267. more text
  268. TEXT;
  269. $expected = <<<HTML
  270. <p>this is some</p>
  271. <pre><code>function test() {
  272. echo '&lt;test&gt;';
  273. }</code></pre>
  274. <p>more text</p>
  275. HTML;
  276. $result = $this->Parser->parse($text);
  277. $this->assertEqual($result, $expected);
  278. }
  279. /**
  280. * test two code blocks with delimiters.
  281. *
  282. * @return void
  283. */
  284. function testMultipleCodeBlocksWithDelimiters() {
  285. $text = <<<TEXT
  286. this is some
  287. {{{
  288. function test() {
  289. echo '<test>';
  290. }
  291. }}}
  292. more text goes here.
  293. {{{
  294. function test() {
  295. echo '<test>';
  296. }
  297. }}}
  298. Additional text
  299. TEXT;
  300. $expected = <<<HTML
  301. <p>this is some</p>
  302. <pre><code>function test() {
  303. echo '&lt;test&gt;';
  304. }</code></pre>
  305. <p>more text goes here.</p>
  306. <pre><code>function test() {
  307. echo '&lt;test&gt;';
  308. }</code></pre>
  309. <p>Additional text</p>
  310. HTML;
  311. $result = $this->Parser->parse($text);
  312. $this->assertEqual($result, $expected);
  313. }
  314. /**
  315. * test that code blocks work with no newlines
  316. *
  317. * @return void
  318. */
  319. function testCodeBlockNoNewLines() {
  320. $text = <<<TEXT
  321. this is some
  322. {{{ Router::connectNamed(false, array('default' => true)); }}}
  323. more text
  324. TEXT;
  325. $expected = <<<HTML
  326. <p>this is some</p>
  327. <pre><code>Router::connectNamed(false, array('default' =&gt; true));</code></pre>
  328. <p>more text</p>
  329. HTML;
  330. $result = $this->Parser->parse($text);
  331. $this->assertEqual($result, $expected);
  332. }
  333. /**
  334. * test indented code blocks
  335. *
  336. * @return void
  337. */
  338. function testCodeBlockWithIndents() {
  339. $text = <<<TEXT
  340. this is some
  341. function test() {
  342. echo '<test>';
  343. }
  344. more text
  345. TEXT;
  346. $expected = <<<HTML
  347. <p>this is some</p>
  348. <pre><code>function test() {
  349. echo '&lt;test&gt;';
  350. }</code></pre>
  351. <p>more text</p>
  352. HTML;
  353. $result = $this->Parser->parse($text);
  354. $this->assertEqual($result, $expected);
  355. $text = <<<TEXT
  356. this is some
  357. function test() {
  358. echo '<test>';
  359. }
  360. more text
  361. TEXT;
  362. $expected = <<<HTML
  363. <p>this is some</p>
  364. <pre><code>function test() {
  365. echo '&lt;test&gt;';
  366. }</code></pre>
  367. <p>more text</p>
  368. HTML;
  369. $result = $this->Parser->parse($text);
  370. $this->assertEqual($result, $expected);
  371. $text = <<<TEXT
  372. this is some
  373. function test() {
  374. echo '<test>';
  375. }
  376. \$foo->bar();
  377. more text
  378. TEXT;
  379. $expected = <<<HTML
  380. <p>this is some</p>
  381. <pre><code>function test() {
  382. echo '&lt;test&gt;';
  383. }
  384. \$foo-&gt;bar();</code></pre>
  385. <p>more text</p>
  386. HTML;
  387. $result = $this->Parser->parse($text);
  388. $this->assertEqual($result, $expected);
  389. }
  390. /**
  391. * Test simple ordered list parsing
  392. *
  393. * @return void
  394. */
  395. function testSimpleOrderedList() {
  396. $text = <<<TEXT
  397. Some text here.
  398. - Line 1
  399. - Line 2
  400. - Line 3
  401. more text
  402. TEXT;
  403. $expected = <<<HTML
  404. <p>Some text here.</p>
  405. <ul>
  406. <li>Line 1</li>
  407. <li>Line 2</li>
  408. <li>Line 3</li>
  409. </ul>
  410. <p>more text</p>
  411. HTML;
  412. $result = $this->Parser->parse($text);
  413. $this->assertEqual($result, $expected);
  414. $text = <<<TEXT
  415. Some text here.
  416. - Line `with code`
  417. + Line 2
  418. * Line **bold**
  419. more text
  420. TEXT;
  421. $expected = <<<HTML
  422. <p>Some text here.</p>
  423. <ul>
  424. <li>Line <code>with code</code></li>
  425. <li>Line 2</li>
  426. <li>Line <strong>bold</strong></li>
  427. </ul>
  428. <p>more text</p>
  429. HTML;
  430. $result = $this->Parser->parse($text);
  431. $this->assertEqual($result, $expected);
  432. }
  433. /**
  434. * test that lists ending on the last line of the text are handled properly
  435. *
  436. * @return void
  437. */
  438. function testUnorderedListAtEndOfText() {
  439. $text = <<<TEXT
  440. ### Attributes:
  441. - `empty` - If true, the empty select option is shown. If a string,
  442. that string is displayed as the empty element.
  443. - this is another line
  444. TEXT;
  445. $expected = <<<HTML
  446. <h3>Attributes:</h3>
  447. <ul>
  448. <li><code>empty</code> - If true, the empty select option is shown. If a string,
  449. that string is displayed as the empty element.</li>
  450. <li>this is another line</li>
  451. </ul>
  452. <p></p>
  453. HTML;
  454. $result = $this->Parser->parse($text);
  455. $this->assertEqual($result, $expected);
  456. }
  457. /**
  458. * Test simple ordered list parsing
  459. *
  460. * @return void
  461. */
  462. function testSimpleUnorderedList() {
  463. $text = <<<TEXT
  464. Some text here.
  465. 1. Line 1
  466. 2. Line 2
  467. 3. Line 3
  468. more text
  469. TEXT;
  470. $expected = <<<HTML
  471. <p>Some text here.</p>
  472. <ol>
  473. <li>Line 1</li>
  474. <li>Line 2</li>
  475. <li>Line 3</li>
  476. </ol>
  477. <p>more text</p>
  478. HTML;
  479. $result = $this->Parser->parse($text);
  480. $this->assertEqual($result, $expected);
  481. $text = <<<TEXT
  482. Some text here.
  483. 8. Line `with code`
  484. 100. Line 2
  485. 5. Line **bold**
  486. more text
  487. TEXT;
  488. $expected = <<<HTML
  489. <p>Some text here.</p>
  490. <ol>
  491. <li>Line <code>with code</code></li>
  492. <li>Line 2</li>
  493. <li>Line <strong>bold</strong></li>
  494. </ol>
  495. <p>more text</p>
  496. HTML;
  497. $result = $this->Parser->parse($text);
  498. $this->assertEqual($result, $expected);
  499. }
  500. /**
  501. * test nested one line lists
  502. *
  503. * @return void
  504. */
  505. function testNestedLists() {
  506. $text = <<<TEXT
  507. Some text here.
  508. - Line 1
  509. - Indented 1
  510. - Indented 2
  511. - Indented 3
  512. - Line 2
  513. - Line 3
  514. more text
  515. TEXT;
  516. $expected = <<<HTML
  517. <p>Some text here.</p>
  518. <ul>
  519. <li>Line 1
  520. <ul>
  521. <li>Indented 1</li>
  522. <li>Indented 2</li>
  523. <li>Indented 3</li>
  524. </ul></li>
  525. <li>Line 2</li>
  526. <li>Line 3</li>
  527. </ul>
  528. <p>more text</p>
  529. HTML;
  530. $result = $this->Parser->parse($text);
  531. $this->assertEqual($result, $expected);
  532. $text = <<<TEXT
  533. Some text here.
  534. - Line 1
  535. - Indented 1
  536. - Indented 2
  537. - Indented 3
  538. - Line 2
  539. - Line 3
  540. more text
  541. TEXT;
  542. $expected = <<<HTML
  543. <p>Some text here.</p>
  544. <ul>
  545. <li>Line 1
  546. <ul>
  547. <li>Indented 1</li>
  548. <li>Indented 2
  549. <ul>
  550. <li>Indented 3</li>
  551. </ul></li>
  552. </ul></li>
  553. <li>Line 2</li>
  554. <li>Line 3</li>
  555. </ul>
  556. <p>more text</p>
  557. HTML;
  558. $result = $this->Parser->parse($text);
  559. $this->assertEqual($result, $expected);
  560. }
  561. /**
  562. * test mixed lists.
  563. *
  564. * @return void
  565. */
  566. function testMixedList() {
  567. $text = <<<TEXT
  568. Some text here.
  569. - Line 1
  570. 1. Indented 1
  571. 2. Indented 2
  572. - Line 2
  573. - Line 3
  574. more text
  575. TEXT;
  576. $expected = <<<HTML
  577. <p>Some text here.</p>
  578. <ul>
  579. <li>Line 1
  580. <ol>
  581. <li>Indented 1</li>
  582. <li>Indented 2</li>
  583. </ol></li>
  584. <li>Line 2</li>
  585. <li>Line 3</li>
  586. </ul>
  587. <p>more text</p>
  588. HTML;
  589. $result = $this->Parser->parse($text);
  590. $this->assertEqual($result, $expected);
  591. }
  592. }
  593. ?>