PageRenderTime 112ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 38ms app.codeStats 1ms

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