lib/markdown: merge processor and emitter
[nit.git] / lib / markdown / test_markdown.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Test suites for module `markdown`
16 module test_markdown is test
17
18 intrude import markdown
19
20 class TestMarkdownProcessor
21 test
22
23 fun test_process_empty is test do
24 var test = ""
25 var exp = ""
26 var res = test.md_to_html.write_to_string
27 assert res == exp
28 end
29
30 fun test_process_tabs is test do
31 var test = """
32 some code
33 """
34 var exp = """<pre><code>some code
35 </code></pre>
36 """
37 var res = test.md_to_html.write_to_string
38 assert res == exp
39 end
40
41
42 fun test_process_par1 is test do
43 var test = "test"
44 var exp = "<p>test</p>\n"
45 var res = test.md_to_html.write_to_string
46 assert res == exp
47 end
48
49 fun test_process_par2 is test do
50 var test = """
51 line1
52 line2
53
54 line3 line4
55
56 line5"""
57 var exp = """
58 <p>line1
59 line2</p>
60 <p>line3 line4</p>
61 <p>line5</p>
62 """
63 var res = test.md_to_html.write_to_string
64 assert res == exp
65 end
66
67 fun test_process_par3 is test do
68 var test = """
69 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
70 Aliquam hendrerit mi posuere lectus.
71 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
72
73 Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
74 id sem consectetuer libero luctus adipiscing.
75 """
76 var exp = """
77 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
78 Aliquam hendrerit mi posuere lectus.
79 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
80 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
81 id sem consectetuer libero luctus adipiscing.</p>
82 """
83 var res = test.md_to_html.write_to_string
84 assert res == exp
85 end
86
87 fun test_process_headings_1 is test do
88 var test = """
89 This is a H1
90 =============
91
92 This is a H2
93 -------------
94 """
95 var exp = """
96 <h1 id="This_is_a_H1">This is a H1</h1>
97 <h2 id="This_is_a_H2">This is a H2</h2>
98 """
99 var res = test.md_to_html.write_to_string
100 assert res == exp
101 end
102
103 fun test_process_headings_2 is test do
104 var test = """
105 # This is a H1
106
107 ## This is a H2
108 ###### This is a H6
109 """
110 var exp = """
111 <h1 id="This_is_a_H1">This is a H1</h1>
112 <h2 id="This_is_a_H2">This is a H2</h2>
113 <h6 id="This_is_a_H6">This is a H6</h6>
114 """
115 var res = test.md_to_html.write_to_string
116 assert res == exp
117 end
118
119 fun test_process_headings_3 is test do
120 var test = """
121 # This is a H1 #
122
123 ## This is a H2 ##
124
125 ### This is a H3 ######
126 """
127 var exp = """
128 <h1 id="This_is_a_H1">This is a H1</h1>
129 <h2 id="This_is_a_H2">This is a H2</h2>
130 <h3 id="This_is_a_H3">This is a H3</h3>
131 """
132 var res = test.md_to_html.write_to_string
133 assert res == exp
134 end
135
136 fun test_process_hr is test do
137 var test = """
138 * * *
139
140 ***
141
142 *****
143
144 - - -
145
146 ---------------------------------------
147 """
148 var exp = "<hr/>\n<hr/>\n<hr/>\n<hr/>\n<hr/>\n"
149 var res = test.md_to_html.write_to_string
150 assert res == exp
151 end
152
153 fun test_process_bquote1 is test do
154 var test = """
155 > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
156 > consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
157 > Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
158 >
159 > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
160 > id sem consectetuer libero luctus adipiscing.
161 """
162 var exp = """<blockquote>
163 <p>This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
164 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
165 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
166 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
167 id sem consectetuer libero luctus adipiscing.</p>
168 </blockquote>
169 """
170 var res = test.md_to_html.write_to_string
171 assert res == exp
172 end
173
174 fun test_process_bquote2 is test do
175 var test = """
176 > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
177 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
178 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
179
180 > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
181 id sem consectetuer libero luctus adipiscing.
182 """
183 var exp = """<blockquote>
184 <p>This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
185 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
186 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
187 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
188 id sem consectetuer libero luctus adipiscing.</p>
189 </blockquote>
190 """
191 var res = test.md_to_html.write_to_string
192 assert res == exp
193 end
194
195 fun test_process_bquote3 is test do
196 var test = """
197 > This is the first level of quoting.
198 >
199 > > This is nested blockquote.
200 >
201 > Back to the first level.
202 """
203 var exp = """<blockquote>
204 <p>This is the first level of quoting.</p>
205 <blockquote>
206 <p>This is nested blockquote.</p>
207 </blockquote>
208 <p>Back to the first level.</p>
209 </blockquote>
210 """
211 var res = test.md_to_html.write_to_string
212 assert res == exp
213 end
214
215 fun test_process_list1 is test do
216 var test = """
217 * Red
218 * Green
219 * Blue
220 """
221 var exp = """<ul>
222 <li>Red</li>
223 <li>Green</li>
224 <li>Blue</li>
225 </ul>
226 """
227 var res = test.md_to_html.write_to_string
228 assert res == exp
229 end
230
231 fun test_process_list2 is test do
232 var test = """
233 + Red
234 + Green
235 + Blue
236 """
237 var exp = """<ul>
238 <li>Red</li>
239 <li>Green</li>
240 <li>Blue</li>
241 </ul>
242 """
243 var res = test.md_to_html.write_to_string
244 assert res == exp
245 end
246
247 fun test_process_list3 is test do
248 var test = """
249 - Red
250 - Green
251 - Blue
252 """
253 var exp = """<ul>
254 <li>Red</li>
255 <li>Green</li>
256 <li>Blue</li>
257 </ul>
258 """
259 var res = test.md_to_html.write_to_string
260 assert res == exp
261 end
262
263 fun test_process_list4 is test do
264 var test = """
265 1. Bird
266 2. McHale
267 3. Parish
268 """
269 var exp = """<ol>
270 <li>Bird</li>
271 <li>McHale</li>
272 <li>Parish</li>
273 </ol>
274 """
275 var res = test.md_to_html.write_to_string
276 assert res == exp
277 end
278
279 fun test_process_list5 is test do
280 var test = """
281 3. Bird
282 1. McHale
283 8. Parish
284 """
285 var exp = """<ol>
286 <li>Bird</li>
287 <li>McHale</li>
288 <li>Parish</li>
289 </ol>
290 """
291 var res = test.md_to_html.write_to_string
292 assert res == exp
293 end
294
295 fun test_process_list6 is test do
296 var test = """
297 * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
298 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
299 viverra nec, fringilla in, laoreet vitae, risus.
300 * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
301 Suspendisse id sem consectetuer libero luctus adipiscing.
302 """
303 var exp = """
304 <ul>
305 <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
306 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
307 viverra nec, fringilla in, laoreet vitae, risus.</li>
308 <li>Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
309 Suspendisse id sem consectetuer libero luctus adipiscing.</li>
310 </ul>
311 """
312 var res = test.md_to_html.write_to_string
313 assert res == exp
314 end
315
316 fun test_process_list7 is test do
317 var test = """
318 * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
319 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
320 viverra nec, fringilla in, laoreet vitae, risus.
321 * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
322 Suspendisse id sem consectetuer libero luctus adipiscing.
323 """
324 var exp = """
325 <ul>
326 <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
327 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
328 viverra nec, fringilla in, laoreet vitae, risus.</li>
329 <li>Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
330 Suspendisse id sem consectetuer libero luctus adipiscing.</li>
331 </ul>
332 """
333 var res = test.md_to_html.write_to_string
334 assert res == exp
335 end
336
337 fun test_process_list8 is test do
338 var test = """
339 * Bird
340
341 * Magic
342 """
343 var exp = """
344 <ul>
345 <li><p>Bird</p>
346 </li>
347 <li><p>Magic</p>
348 </li>
349 </ul>
350 """
351 var res = test.md_to_html.write_to_string
352 assert res == exp
353 end
354
355 fun test_process_list9 is test do
356 var test = """
357 1. This is a list item with two paragraphs. Lorem ipsum dolor
358 sit amet, consectetuer adipiscing elit. Aliquam hendrerit
359 mi posuere lectus.
360
361 Vestibulum enim wisi, viverra nec, fringilla in, laoreet
362 vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
363 sit amet velit.
364
365 2. Suspendisse id sem consectetuer libero luctus adipiscing.
366 """
367 var exp = """
368 <ol>
369 <li><p>This is a list item with two paragraphs. Lorem ipsum dolor
370 sit amet, consectetuer adipiscing elit. Aliquam hendrerit
371 mi posuere lectus.</p>
372 <p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet
373 vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
374 sit amet velit.</p>
375 </li>
376 <li><p>Suspendisse id sem consectetuer libero luctus adipiscing.</p>
377 </li>
378 </ol>
379 """
380 var res = test.md_to_html.write_to_string
381 assert res == exp
382 end
383
384 fun test_process_list10 is test do
385 var test = """
386 * This is a list item with two paragraphs.
387
388 This is the second paragraph in the list item. You're
389 only required to indent the first line. Lorem ipsum dolor
390 sit amet, consectetuer adipiscing elit.
391
392 * Another item in the same list.
393 """
394 var exp = """
395 <ul>
396 <li><p>This is a list item with two paragraphs.</p>
397 <p>This is the second paragraph in the list item. You're
398 only required to indent the first line. Lorem ipsum dolor
399 sit amet, consectetuer adipiscing elit.</p>
400 </li>
401 <li><p>Another item in the same list.</p>
402 </li>
403 </ul>
404 """
405 var res = test.md_to_html.write_to_string
406 assert res == exp
407 end
408
409 fun test_process_list11 is test do
410 var test = """
411 This is a paragraph
412 * and this is not a list
413 """
414 var exp = """
415 <p>This is a paragraph
416 * and this is not a list</p>
417 """
418 var proc = new MarkdownProcessor
419 proc.ext_mode = false
420 var res = proc.process(test).write_to_string
421 assert res == exp
422 end
423
424 fun test_process_list_ext is test do
425 var test = """
426 This is a paragraph
427 * and this is not a list
428 """
429 var exp = """
430 <p>This is a paragraph</p>
431 <ul>
432 <li>and this is not a list</li>
433 </ul>
434 """
435 var res = test.md_to_html.write_to_string
436 assert res == exp
437 end
438
439 fun test_process_code1 is test do
440 var test = """
441 This is a normal paragraph:
442
443 This is a code block.
444 """
445 var exp = """<p>This is a normal paragraph:</p>
446 <pre><code>This is a code block.
447 </code></pre>
448 """
449 var res = test.md_to_html.write_to_string
450 assert res == exp
451 end
452
453 fun test_process_code2 is test do
454 var test = """
455 Here is an example of AppleScript:
456
457 tell application "Foo"
458 beep
459 end tell
460
461 <div class="footer">
462 &copy; 2004 Foo Corporation
463 </div>
464 """
465 var exp = """
466 <p>Here is an example of AppleScript:</p>
467 <pre><code>tell application "Foo"
468 beep
469 end tell
470
471 &lt;div class="footer"&gt;
472 &amp;copy; 2004 Foo Corporation
473 &lt;/div&gt;
474 </code></pre>
475 """
476 var res = test.md_to_html.write_to_string
477 assert res == exp
478 end
479
480 fun test_process_code_ext1 is test do
481 var test = """
482 Here is an example of AppleScript:
483 ~~~
484 tell application "Foo"
485 beep
486 end tell
487
488 <div class="footer">
489 &copy; 2004 Foo Corporation
490 </div>
491 ~~~
492 """
493 var exp = """
494 <p>Here is an example of AppleScript:</p>
495 <pre><code>tell application "Foo"
496 beep
497 end tell
498
499 &lt;div class="footer"&gt;
500 &amp;copy; 2004 Foo Corporation
501 &lt;/div&gt;
502 </code></pre>
503 """
504 var res = test.md_to_html.write_to_string
505 assert res == exp
506 end
507
508 fun test_process_code_ext2 is test do
509 var test = """
510 Here is an example of AppleScript:
511 ```
512 tell application "Foo"
513 beep
514 end tell
515
516 <div class="footer">
517 &copy; 2004 Foo Corporation
518 </div>
519 ```
520 """
521 var exp = """
522 <p>Here is an example of AppleScript:</p>
523 <pre><code>tell application "Foo"
524 beep
525 end tell
526
527 &lt;div class="footer"&gt;
528 &amp;copy; 2004 Foo Corporation
529 &lt;/div&gt;
530 </code></pre>
531 """
532 var res = test.md_to_html.write_to_string
533 assert res == exp
534 end
535
536 fun test_process_code_ext3 is test do
537 var proc = new MarkdownProcessor
538 proc.ext_mode = false
539
540 var test = """
541 Here is an example of AppleScript:
542 beep
543 """
544 var exp = """
545 <p>Here is an example of AppleScript:
546 beep</p>
547 """
548 var res = proc.process(test).write_to_string
549 assert res == exp
550 end
551
552 fun test_process_code_ext4 is test do
553 var test = """
554 Here is an example of AppleScript:
555 beep
556 """
557 var exp = """
558 <p>Here is an example of AppleScript:</p>
559 <pre><code>beep
560 </code></pre>
561 """
562 var res = test.md_to_html.write_to_string
563 assert res == exp
564 end
565
566 fun test_process_code_ext5 is test do
567 var test = """
568 ```nit
569 print "Hello World!"
570 ```
571 """
572 var exp = """
573 <pre class="nit"><code>print "Hello World!"
574 </code></pre>
575 """
576 var res = test.md_to_html.write_to_string
577 assert res == exp
578 end
579
580 fun test_process_code_ext6 is test do
581 var test = """
582 ~~~
583 print "Hello"
584 ~~~
585 ~~~
586 print "World"
587 ~~~
588 """
589 var exp = """
590 <pre><code>print "Hello"
591 </code></pre>
592 <pre><code>print "World"
593 </code></pre>
594 """
595 var res = test.md_to_html.write_to_string
596 assert res == exp
597 end
598
599 fun test_process_code_ext7 is test do
600 var test = """
601 ~~~
602 print "Hello"
603 ~~~
604 ~~~
605 print "World"
606 ~~~
607 """
608 var exp = """
609 <pre><code>print "Hello"
610 </code></pre>
611 <pre><code>print "World"
612 </code></pre>
613 """
614 var res = test.md_to_html.write_to_string
615 assert res == exp
616 end
617
618 fun test_process_nesting1 is test do
619 var test = """
620 > ## This is a header.
621 >
622 > 1. This is the first list item.
623 > 2. This is the second list item.
624 >
625 > Here's some example code:
626 >
627 > return shell_exec("echo $input | $markdown_script");
628 """
629 var exp = """
630 <blockquote>
631 <h2 id="This_is_a_header.">This is a header.</h2>
632 <ol>
633 <li>This is the first list item.</li>
634 <li>This is the second list item.</li>
635 </ol>
636 <p>Here's some example code:</p>
637 <pre><code>return shell_exec("echo $input | $markdown_script");
638 </code></pre>
639 </blockquote>
640 """
641 var res = test.md_to_html.write_to_string
642 assert res == exp
643 end
644
645 fun test_process_nesting2 is test do
646 var test = """
647 * A list item with a blockquote:
648
649 > This is a blockquote
650 > inside a list item.
651 """
652 var exp = """
653 <ul>
654 <li><p>A list item with a blockquote:</p>
655 <blockquote>
656 <p>This is a blockquote
657 inside a list item.</p>
658 </blockquote>
659 </li>
660 </ul>
661 """
662 var res = test.md_to_html.write_to_string
663 assert res == exp
664 end
665
666 fun test_process_nesting3 is test do
667 var test = """
668 * A list item with a code block:
669
670 <code goes here>
671 """
672 var exp = """
673 <ul>
674 <li><p>A list item with a code block:</p>
675 <pre><code>&lt;code goes here&gt;
676 </code></pre>
677 </li>
678 </ul>
679 """
680 var res = test.md_to_html.write_to_string
681 assert res == exp
682 end
683
684 fun test_process_nesting4 is test do
685 var test = """
686 * Tab
687 * Tab
688 * Tab
689 """
690 var exp = """
691 <ul>
692 <li>Tab<ul>
693 <li>Tab<ul>
694 <li>Tab</li>
695 </ul>
696 </li>
697 </ul>
698 </li>
699 </ul>
700 """
701 var res = test.md_to_html.write_to_string
702 assert res == exp
703 end
704
705 # TODO
706 # fun test_process_nesting5 is test do
707 # var test = """
708 # * this
709 #
710 # * sub
711 #
712 # that
713 # """
714 # var exp = """
715 # <ul>
716 # <li><p>this</p>
717 # <ul>
718 # <li>sub</li>
719 # </ul>
720 # <p>that</p>
721 # </li>
722 # </ul>
723 # """
724 # var res = test.md_to_html.write_to_string
725 # assert res == exp
726 # end
727
728 fun test_process_emph1 is test do
729 var test = """
730 *single asterisks*
731
732 _single underscores_
733
734 **double asterisks**
735
736 __double underscores__
737 """
738 var exp = """<p><em>single asterisks</em></p>
739 <p><em>single underscores</em></p>
740 <p><strong>double asterisks</strong></p>
741 <p><strong>double underscores</strong></p>
742 """
743 var res = test.md_to_html.write_to_string
744 assert res == exp
745 end
746
747 fun test_process_emph2 is test do
748 var test = "un*frigging*believable"
749 var exp = "<p>un<em>frigging</em>believable</p>\n"
750 var res = test.md_to_html.write_to_string
751 assert res == exp
752 end
753
754 fun test_process_emph3 is test do
755 var proc = new MarkdownProcessor
756 proc.ext_mode = false
757 var test = "Con_cat_this"
758 var exp = "<p>Con<em>cat</em>this</p>\n"
759 var res = proc.process(test).write_to_string
760 assert res == exp
761 end
762
763 fun test_process_emph_ext is test do
764 var test = "Con_cat_this"
765 var exp = "<p>Con_cat_this</p>\n"
766 var res = test.md_to_html.write_to_string
767 assert res == exp
768 end
769
770 fun test_process_xml1 is test do
771 var test = """
772 This is a regular paragraph.
773
774 <table>
775 <tr>
776 <td>Foo</td>
777 </tr>
778 </table>
779
780 This is another regular paragraph.
781 """
782 var exp = """
783 <p>This is a regular paragraph.</p>
784 <table>
785 <tr>
786 <td>Foo</td>
787 </tr>
788 </table>
789 <p>This is another regular paragraph.</p>
790 """
791 var res = test.md_to_html.write_to_string
792 assert res == exp
793 end
794
795 fun test_process_xml2 is test do
796 var test = """
797 This is an image <img src="foo/bar" alt="baz"/> in a regular paragraph.
798 """
799 var exp = """<p>This is an image <img src="foo/bar" alt="baz" /> in a regular paragraph.</p>
800 """
801 var res = test.md_to_html.write_to_string
802 assert res == exp
803 end
804
805 fun test_process_xml3 is test do
806 var test = """
807 <div style=">"/>
808 """
809 var exp = """
810 <div style=">"/>
811 """
812 var res = test.md_to_html.write_to_string
813 assert res == exp
814 end
815
816 fun test_process_xml4 is test do
817 var test = """
818 <p>This is an example of a block element that should be escaped.</p>
819 <p>Idem for the second paragraph.</p>
820 """
821 var exp = test
822 var res = test.md_to_html.write_to_string
823 assert res == exp
824 end
825
826 fun test_process_xml5 is test do
827 var test = """
828 # Some more XML tests
829
830 <p>This is an example of a block element that should be escaped.</p>
831 <p>Idem for the second paragraph.</p>
832
833 With a *md paragraph*!
834 """
835 var exp = """
836 <h1 id="Some_more_XML_tests">Some more XML tests</h1>
837 <p>This is an example of a block element that should be escaped.</p>
838 <p>Idem for the second paragraph.</p>
839 <p>With a <em>md paragraph</em>!</p>
840 """
841 var res = test.md_to_html.write_to_string
842 print res
843 assert res == exp
844 end
845
846 fun test_process_span_code1 is test do
847 var test = "Use the `printf()` function."
848 var exp = "<p>Use the <code>printf()</code> function.</p>\n"
849 var res = test.md_to_html.write_to_string
850 assert res == exp
851 end
852
853 fun test_process_span_code2 is test do
854 var test = "``There is a literal backtick (`) here.``"
855 var exp = "<p><code>There is a literal backtick (`) here.</code></p>\n"
856 var res = test.md_to_html.write_to_string
857 assert res == exp
858 end
859
860 fun test_process_span_code3 is test do
861 var test = """
862 A single backtick in a code span: `` ` ``
863
864 A backtick-delimited string in a code span: `` `foo` ``
865 """
866 var exp = """
867 <p>A single backtick in a code span: <code>`</code></p>
868 <p>A backtick-delimited string in a code span: <code>`foo`</code></p>
869 """
870 var res = test.md_to_html.write_to_string
871 assert res == exp
872 end
873
874 fun test_process_span_code4 is test do
875 var test = "Please don't use any `<blink>` tags."
876 var exp = "<p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>\n"
877 var res = test.md_to_html.write_to_string
878 assert res == exp
879 end
880
881 fun test_process_span_code5 is test do
882 var test = "`&#8212;` is the decimal-encoded equivalent of `&mdash;`."
883 var exp = "<p><code>&amp;#8212;</code> is the decimal-encoded equivalent of <code>&amp;mdash;</code>.</p>\n"
884 var res = test.md_to_html.write_to_string
885 assert res == exp
886 end
887
888 fun test_process_escape1 is test do
889 var test = "\\*this text is surrounded by literal asterisks\\*"
890 var exp = "<p>*this text is surrounded by literal asterisks*</p>\n"
891 var res = test.md_to_html.write_to_string
892 assert res == exp
893 end
894
895 fun test_process_escape2 is test do
896 var test = "1986\\. What a great season."
897 var exp = "<p>1986. What a great season.</p>\n"
898 var res = test.md_to_html.write_to_string
899 assert res == exp
900 end
901
902 fun test_process_escape3 is test do
903 var test = "Ben & Lux"
904 var exp = "<p>Ben &amp; Lux</p>\n"
905 var res = test.md_to_html.write_to_string
906 assert res == exp
907 end
908
909 fun test_process_link1 is test do
910 var test = """
911 This is [an example](http://example.com/ "Title") inline link.
912
913 [This link](http://example.net/) has no title attribute.
914 """
915 var exp = """<p>This is <a href="http://example.com/" title="Title">an example</a> inline link.</p>
916 <p><a href="http://example.net/">This link</a> has no title attribute.</p>
917 """
918 var res = test.md_to_html.write_to_string
919 assert res == exp
920 end
921
922 fun test_process_link2 is test do
923 var test = "See my [About](/about/) page for details."
924 var exp = "<p>See my <a href=\"/about/\">About</a> page for details.</p>\n"
925 var res = test.md_to_html.write_to_string
926 assert res == exp
927 end
928
929 fun test_process_link3 is test do
930 var test = """
931 This is [an example][id] reference-style link.
932
933 This is [an example] [id] reference-style link.
934
935 Some lorem ipsum
936
937 [id]: http://example.com/ "Optional Title Here"
938
939 Some other lipsum
940 """
941 var exp = """
942 <p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>
943 <p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>
944 <p>Some lorem ipsum</p>
945 <p>Some other lipsum</p>
946 """
947 var res = test.md_to_html.write_to_string
948 assert res == exp
949 end
950
951 fun test_process_link4 is test do
952 var test = """
953 This is multiple examples: [foo][1], [bar][2], [baz][3].
954
955 [1]: http://example.com/ "Optional Title Here"
956 [2]: http://example.com/ 'Optional Title Here'
957 [3]: http://example.com/ (Optional Title Here)
958 """
959 var exp = """
960 <p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">baz</a>.</p>
961 """
962 var res = test.md_to_html.write_to_string
963 assert res == exp
964 end
965
966 fun test_process_link5 is test do
967 var test = """
968 This is multiple examples: [foo][a], [bar][A], [a].
969
970 [a]: http://example.com/ "Optional Title Here"
971 """
972 var exp = """<p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">a</a>.</p>
973 """
974 var res = test.md_to_html.write_to_string
975 assert res == exp
976 end
977
978 fun test_process_link6 is test do
979 var test = """
980 I get 10 times more traffic from [Google][] than from [Yahoo][] or [MSN][].
981
982 [Google]: http://google.com/ "Google"
983 [Yahoo]: http://search.yahoo.com/ "Yahoo Search"
984 [MSN]: http://search.msn.com/ "MSN Search"
985 """
986 var exp = """<p>I get 10 times more traffic from <a href="http://google.com/" title="Google">Google</a> than from <a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>
987 """
988 var res = test.md_to_html.write_to_string
989 assert res == exp
990 end
991
992 fun test_process_link7 is test do
993 var test = """
994 Visit [Daring Fireball][] for more information.
995
996 [Daring Fireball]: http://daringfireball.net/
997 """
998 var exp = """<p>Visit <a href="http://daringfireball.net/">Daring Fireball</a> for more information.</p>
999 """
1000 var res = test.md_to_html.write_to_string
1001 assert res == exp
1002 end
1003
1004 fun test_process_link8 is test do
1005 var test = """
1006 This one has a [line
1007 break].
1008
1009 This one has a [line
1010 break] with a line-ending space.
1011
1012 [line break]: /foo
1013 """
1014 var exp = """
1015 <p>This one has a <a href="/foo">line
1016 break</a>.</p>
1017 <p>This one has a <a href="/foo">line
1018 break</a> with a line-ending space.</p>
1019 """
1020 var res = test.md_to_html.write_to_string
1021 assert res == exp
1022 end
1023
1024 # FIXME unignore test once escape strings fixed
1025 # fun test_process_link9 is test do
1026 # var test = """
1027 # Foo [bar][].
1028 #
1029 # Foo [bar](/url/ "Title with \"quotes\" inside").
1030 #
1031 #
1032 # [bar]: /url/ "Title with \"quotes\" inside"
1033 # """
1034 # var exp = """
1035 # <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
1036 # <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
1037 # """
1038 # var res = test.md_to_html.write_to_string
1039 # assert res == exp
1040 # end
1041
1042 fun test_process_img1 is test do
1043 var test = """
1044 ![Alt text](/path/to/img.jpg)
1045
1046 ![Alt text](/path/to/img.jpg "Optional title")
1047 """
1048 var exp = """<p><img src="/path/to/img.jpg" alt="Alt text"/></p>
1049 <p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title"/></p>
1050 """
1051 var res = test.md_to_html.write_to_string
1052 assert res == exp
1053 end
1054
1055 fun test_process_img2 is test do
1056 var test = """
1057 ![Alt text][id]
1058
1059 [id]: url/to/image "Optional title attribute"
1060 """
1061 var exp = """<p><img src="url/to/image" alt="Alt text" title="Optional title attribute"/></p>
1062 """
1063 var res = test.md_to_html.write_to_string
1064 assert res == exp
1065 end
1066
1067 fun test_process_strike is test do
1068 var proc = new MarkdownProcessor
1069 proc.ext_mode = false
1070 var test = "This is how you ~~strike text~~"
1071 var exp = "<p>This is how you ~~strike text~~</p>\n"
1072 var res = proc.process(test).write_to_string
1073 assert exp == res
1074 end
1075
1076 fun test_process_strike_ext is test do
1077 var test = "This is how you ~~strike text~~"
1078 var exp = "<p>This is how you <del>strike text</del></p>\n"
1079 var res = test.md_to_html.write_to_string
1080 assert exp == res
1081 end
1082
1083 fun test_escape_bad_html is test do
1084 var test = "-1 if < , +1 if > and 0 otherwise"
1085 var exp = "<p>-1 if &lt; , +1 if > and 0 otherwise</p>\n"
1086 var res = test.md_to_html.write_to_string
1087 assert exp == res
1088 end
1089
1090 fun test_daring_encoding is test do
1091 var test = """
1092 AT&T has an ampersand in their name.
1093
1094 AT&amp;T is another way to write it.
1095
1096 This & that.
1097
1098 4 < 5.
1099
1100 6 > 5.
1101
1102 Here's a [link] [1] with an ampersand in the URL.
1103
1104 Here's a link with an amersand in the link text: [AT&T] [2].
1105
1106 Here's an inline [link](/script?foo=1&bar=2).
1107
1108 Here's an inline [link](</script?foo=1&bar=2>).
1109
1110
1111 [1]: http://example.com/?foo=1&bar=2
1112 [2]: http://att.com/ "AT&T"
1113 """
1114
1115 var exp = """
1116 <p>AT&amp;T has an ampersand in their name.</p>
1117 <p>AT&amp;T is another way to write it.</p>
1118 <p>This &amp; that.</p>
1119 <p>4 &lt; 5.</p>
1120 <p>6 > 5.</p>
1121 <p>Here's a <a href="http://example.com/?foo=1&amp;bar=2">link</a> with an ampersand in the URL.</p>
1122 <p>Here's a link with an amersand in the link text: <a href="http://att.com/" title="AT&amp;T">AT&amp;T</a>.</p>
1123 <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
1124 <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
1125 """
1126 var res = test.md_to_html.write_to_string
1127 assert res == exp
1128
1129 end
1130
1131 fun test_daring_autolinks is test do
1132 var test = """
1133 Link: <http://example.com/>.
1134
1135 With an ampersand: <http://example.com/?foo=1&bar=2>
1136
1137 * In a list?
1138 * <http://example.com/>
1139 * It should.
1140
1141 > Blockquoted: <http://example.com/>
1142
1143 Auto-links should not occur here: `<http://example.com/>`
1144
1145 or here: <http://example.com/>
1146 """
1147
1148 var exp = """
1149 <p>Link: <a href="http://example.com/">http://example.com/</a>.</p>
1150 <p>With an ampersand: <a href="http://example.com/?foo=1&amp;bar=2">http://example.com/?foo=1&amp;bar=2</a></p>
1151 <ul>
1152 <li>In a list?</li>
1153 <li><a href="http://example.com/">http://example.com/</a></li>
1154 <li>It should.</li>
1155 </ul>
1156 <blockquote>
1157 <p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
1158 </blockquote>
1159 <p>Auto-links should not occur here: <code>&lt;http://example.com/&gt;</code></p>
1160 <pre><code>or here: &lt;http://example.com/&gt;
1161 </code></pre>
1162 """
1163 var res = test.md_to_html.write_to_string
1164 assert res == exp
1165 end
1166
1167 fun test_daring_escape is test do
1168 var test = """
1169 These should all get escaped:
1170
1171 Backslash: \\
1172
1173 Backtick: \`
1174
1175 Asterisk: \*
1176
1177 Underscore: \_
1178
1179 Left brace: \{
1180
1181 Right brace: \}
1182
1183 Left bracket: \[
1184
1185 Right bracket: \]
1186
1187 Left paren: \(
1188
1189 Right paren: \)
1190
1191 Greater-than: \>
1192
1193 Hash: \#
1194
1195 Period: \.
1196
1197 Bang: \!
1198
1199 Plus: \+
1200
1201 Minus: \-
1202
1203
1204 These should not, because they occur within a code block:
1205
1206 Backslash: \\
1207
1208 Backtick: \`
1209
1210 Asterisk: \*
1211
1212 Underscore: \_
1213
1214 Left brace: \{
1215
1216 Right brace: \}
1217
1218 Left bracket: \[
1219
1220 Right bracket: \]
1221
1222 Left paren: \(
1223
1224 Right paren: \)
1225
1226 Greater-than: \>
1227
1228 Hash: \#
1229
1230 Period: \.
1231
1232 Bang: \!
1233
1234 Plus: \+
1235
1236 Minus: \-
1237
1238 Nor should these, which occur in code spans:
1239
1240 Backslash: `\\`
1241
1242 Backtick: `` \` ``
1243
1244 Asterisk: `\*`
1245
1246 Underscore: `\_`
1247
1248 Left brace: `\{`
1249
1250 Right brace: `\}`
1251
1252 Left bracket: `\[`
1253
1254 Right bracket: `\]`
1255
1256 Left paren: `\(`
1257
1258 Right paren: `\)`
1259
1260 Greater-than: `\>`
1261
1262 Hash: `\#`
1263
1264 Period: `\.`
1265
1266 Bang: `\!`
1267
1268 Plus: `\+`
1269
1270 Minus: `\-`
1271
1272 These should get escaped, even though they're matching pairs for
1273 other Markdown constructs:
1274
1275 \\\*asterisks\\\*
1276
1277 \\\_underscores\\\_
1278
1279 \\\`backticks\\\`
1280
1281 This is a code span with a literal backslash-backtick sequence: `` \` ``
1282
1283 This is a tag with unescaped backticks <span attr='`ticks`'>bar</span>.
1284
1285 This is a tag with backslashes <span attr='\\\\backslashes\\\\'>bar</span>.
1286 """
1287 var exp = """
1288 <p>These should all get escaped:</p>
1289 <p>Backslash: \\</p>
1290 <p>Backtick: \`</p>
1291 <p>Asterisk: \*</p>
1292 <p>Underscore: \_</p>
1293 <p>Left brace: \{</p>
1294 <p>Right brace: \}</p>
1295 <p>Left bracket: \[</p>
1296 <p>Right bracket: \]</p>
1297 <p>Left paren: \(</p>
1298 <p>Right paren: \)</p>
1299 <p>Greater-than: \></p>
1300 <p>Hash: \#</p>
1301 <p>Period: \.</p>
1302 <p>Bang: \!</p>
1303 <p>Plus: \+</p>
1304 <p>Minus: \-</p>
1305 <p>These should not, because they occur within a code block:</p>
1306 <pre><code>Backslash: \\
1307
1308 Backtick: \`
1309
1310 Asterisk: \*
1311
1312 Underscore: \_
1313
1314 Left brace: \{
1315
1316 Right brace: \}
1317
1318 Left bracket: \[
1319
1320 Right bracket: \]
1321
1322 Left paren: \(
1323
1324 Right paren: \)
1325
1326 Greater-than: \&gt;
1327
1328 Hash: \#
1329
1330 Period: \.
1331
1332 Bang: \!
1333
1334 Plus: \+
1335
1336 Minus: \-
1337 </code></pre>
1338 <p>Nor should these, which occur in code spans:</p>
1339 <p>Backslash: <code>\\</code></p>
1340 <p>Backtick: <code>\`</code></p>
1341 <p>Asterisk: <code>\*</code></p>
1342 <p>Underscore: <code>\_</code></p>
1343 <p>Left brace: <code>\{</code></p>
1344 <p>Right brace: <code>\}</code></p>
1345 <p>Left bracket: <code>\[</code></p>
1346 <p>Right bracket: <code>\]</code></p>
1347 <p>Left paren: <code>\(</code></p>
1348 <p>Right paren: <code>\)</code></p>
1349 <p>Greater-than: <code>\&gt;</code></p>
1350 <p>Hash: <code>\#</code></p>
1351 <p>Period: <code>\.</code></p>
1352 <p>Bang: <code>\!</code></p>
1353 <p>Plus: <code>\+</code></p>
1354 <p>Minus: <code>\-</code></p>
1355 <p>These should get escaped, even though they're matching pairs for
1356 other Markdown constructs:</p>
1357 <p>*asterisks*</p>
1358 <p>_underscores_</p>
1359 <p>`backticks`</p>
1360 <p>This is a code span with a literal backslash-backtick sequence: <code>\`</code></p>
1361 <p>This is a tag with unescaped backticks <span attr='`ticks`'>bar</span>.</p>
1362 <p>This is a tag with backslashes <span attr='\\\\backslashes\\\\'>bar</span>.</p>
1363 """
1364
1365 var res = test.md_to_html.write_to_string
1366 assert res == exp
1367 end
1368
1369 fun test_daring_blockquotes is test do
1370 var test = """
1371 > Example:
1372 >
1373 > sub status {
1374 > print "working";
1375 > }
1376 >
1377 > Or:
1378 >
1379 > sub status {
1380 > return "working";
1381 > }
1382 """
1383
1384 var exp = """
1385 <blockquote>
1386 <p>Example:</p>
1387 <pre><code>sub status {
1388 print "working";
1389 }
1390 </code></pre>
1391 <p>Or:</p>
1392 <pre><code>sub status {
1393 return "working";
1394 }
1395 </code></pre>
1396 </blockquote>
1397 """
1398 var res = test.md_to_html.write_to_string
1399 assert res == exp
1400 end
1401
1402 fun test_daring_code_blocks is test do
1403 var test = """
1404 code block on the first line
1405
1406 Regular text.
1407
1408 code block indented by spaces
1409
1410 Regular text.
1411
1412 the lines in this block
1413 all contain trailing spaces
1414
1415 Regular Text.
1416
1417 code block on the last line
1418 """
1419
1420 var exp = """
1421 <pre><code>code block on the first line
1422 </code></pre>
1423 <p>Regular text.</p>
1424 <pre><code>code block indented by spaces
1425 </code></pre>
1426 <p>Regular text.</p>
1427 <pre><code>the lines in this block
1428 all contain trailing spaces
1429 </code></pre>
1430 <p>Regular Text.</p>
1431 <pre><code>code block on the last line
1432 </code></pre>
1433 """
1434 var res = test.md_to_html.write_to_string
1435 assert res == exp
1436 end
1437
1438 fun test_daring_code_spans is test do
1439 var test = """
1440 `<test a="` content of attribute `">`
1441
1442 Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span>
1443
1444 Here's how you put `` `backticks` `` in a code span.
1445 """
1446
1447 var exp = """
1448 <p><code>&lt;test a="</code> content of attribute <code>"&gt;</code></p>
1449 <p>Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span></p>
1450 <p>Here's how you put <code>`backticks`</code> in a code span.</p>
1451 """
1452 var res = test.md_to_html.write_to_string
1453 assert res == exp
1454 end
1455
1456 fun test_daring_pars is test do
1457 var proc = new MarkdownProcessor
1458 proc.ext_mode = false
1459
1460 var test = """
1461 In Markdown 1.0.0 and earlier. Version
1462 8. This line turns into a list item.
1463 Because a hard-wrapped line in the
1464 middle of a paragraph looked like a
1465 list item.
1466
1467 Here's one with a bullet.
1468 * criminey.
1469 """
1470
1471 var exp = """
1472 <p>In Markdown 1.0.0 and earlier. Version
1473 8. This line turns into a list item.
1474 Because a hard-wrapped line in the
1475 middle of a paragraph looked like a
1476 list item.</p>
1477 <p>Here's one with a bullet.
1478 * criminey.</p>
1479 """
1480 var res = proc.process(test).write_to_string
1481 assert res == exp
1482 end
1483
1484 fun test_daring_rules is test do
1485 var test = """
1486 Dashes:
1487
1488 ---
1489
1490 ---
1491
1492 ---
1493
1494 ---
1495
1496 ---
1497
1498 - - -
1499
1500 - - -
1501
1502 - - -
1503
1504 - - -
1505
1506 - - -
1507
1508
1509 Asterisks:
1510
1511 ***
1512
1513 ***
1514
1515 ***
1516
1517 ***
1518
1519 ***
1520
1521 * * *
1522
1523 * * *
1524
1525 * * *
1526
1527 * * *
1528
1529 * * *
1530
1531
1532 Underscores:
1533
1534 ___
1535
1536 ___
1537
1538 ___
1539
1540 ___
1541
1542 ___
1543
1544 _ _ _
1545
1546 _ _ _
1547
1548 _ _ _
1549
1550 _ _ _
1551
1552 _ _ _
1553 """
1554
1555 var exp = """
1556 <p>Dashes:</p>
1557 <hr/>
1558 <hr/>
1559 <hr/>
1560 <hr/>
1561 <pre><code>---
1562 </code></pre>
1563 <hr/>
1564 <hr/>
1565 <hr/>
1566 <hr/>
1567 <pre><code>- - -
1568 </code></pre>
1569 <p>Asterisks:</p>
1570 <hr/>
1571 <hr/>
1572 <hr/>
1573 <hr/>
1574 <pre><code>***
1575 </code></pre>
1576 <hr/>
1577 <hr/>
1578 <hr/>
1579 <hr/>
1580 <pre><code>* * *
1581 </code></pre>
1582 <p>Underscores:</p>
1583 <hr/>
1584 <hr/>
1585 <hr/>
1586 <hr/>
1587 <pre><code>___
1588 </code></pre>
1589 <hr/>
1590 <hr/>
1591 <hr/>
1592 <hr/>
1593 <pre><code>_ _ _
1594 </code></pre>
1595 """
1596 var res = test.md_to_html.write_to_string
1597 assert res == exp
1598 end
1599
1600 fun test_daring_images is test do
1601 var test = """
1602 ![Alt text](/path/to/img.jpg)
1603
1604 ![Alt text](/path/to/img.jpg "Optional title")
1605
1606 Inline within a paragraph: [alt text](/url/).
1607
1608 ![alt text](/url/ "title preceded by two spaces")
1609
1610 ![alt text](/url/ "title has spaces afterward" )
1611
1612 ![alt text](</url/>)
1613
1614 ![alt text](</url/> "with a title").
1615
1616 ![Empty]()
1617
1618 ![this is a stupid URL](http://example.com/(parens).jpg)
1619
1620
1621 ![alt text][foo]
1622
1623 [foo]: /url/
1624
1625 ![alt text][bar]
1626
1627 [bar]: /url/ "Title here"
1628 """
1629
1630 var exp = """
1631 <p><img src="/path/to/img.jpg" alt="Alt text"/></p>
1632 <p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title"/></p>
1633 <p>Inline within a paragraph: <a href="/url/">alt text</a>.</p>
1634 <p><img src="/url/" alt="alt text" title="title preceded by two spaces"/></p>
1635 <p><img src="/url/" alt="alt text" title="title has spaces afterward"/></p>
1636 <p><img src="/url/" alt="alt text"/></p>
1637 <p><img src="/url/" alt="alt text" title="with a title"/>.</p>
1638 <p><img src="" alt="Empty"/></p>
1639 <p><img src="http://example.com/(parens).jpg" alt="this is a stupid URL"/></p>
1640 <p><img src="/url/" alt="alt text"/></p>
1641 <p><img src="/url/" alt="alt text" title="Title here"/></p>
1642 """
1643 var res = test.md_to_html.write_to_string
1644 assert res == exp
1645 end
1646
1647 fun test_daring_inline_html1 is test do
1648 var test = """
1649 Here's a simple block:
1650
1651 <div>
1652 foo
1653 </div>
1654
1655 This should be a code block, though:
1656
1657 <div>
1658 foo
1659 </div>
1660
1661 As should this:
1662
1663 <div>foo</div>
1664
1665 Now, nested:
1666
1667 <div>
1668 <div>
1669 <div>
1670 foo
1671 </div>
1672 </div>
1673 </div>
1674
1675 This should just be an HTML comment:
1676
1677 <!-- Comment -->
1678
1679 Multiline:
1680
1681 <!--
1682 Blah
1683 Blah
1684 -->
1685
1686 Code block:
1687
1688 <!-- Comment -->
1689
1690 Just plain comment, with trailing spaces on the line:
1691
1692 <!-- foo -->
1693
1694 Code:
1695
1696 <hr />
1697
1698 Hr's:
1699
1700 <hr>
1701
1702 <hr/>
1703
1704 <hr />
1705
1706 <hr>
1707
1708 <hr/>
1709
1710 <hr />
1711
1712 <hr class="foo" id="bar" />
1713
1714 <hr class="foo" id="bar"/>
1715
1716 <hr class="foo" id="bar" >
1717 """
1718
1719 var exp = """
1720 <p>Here's a simple block:</p>
1721 <div>
1722 foo
1723 </div>
1724 <p>This should be a code block, though:</p>
1725 <pre><code>&lt;div&gt;
1726 foo
1727 &lt;/div&gt;
1728 </code></pre>
1729 <p>As should this:</p>
1730 <pre><code>&lt;div&gt;foo&lt;/div&gt;
1731 </code></pre>
1732 <p>Now, nested:</p>
1733 <div>
1734 <div>
1735 <div>
1736 foo
1737 </div>
1738 </div>
1739 </div>
1740 <p>This should just be an HTML comment:</p>
1741 <!-- Comment -->
1742 <p>Multiline:</p>
1743 <!--
1744 Blah
1745 Blah
1746 -->
1747 <p>Code block:</p>
1748 <pre><code>&lt;!-- Comment --&gt;
1749 </code></pre>
1750 <p>Just plain comment, with trailing spaces on the line:</p>
1751 <!-- foo -->
1752 <p>Code:</p>
1753 <pre><code>&lt;hr /&gt;
1754 </code></pre>
1755 <p>Hr's:</p>
1756 <hr>
1757 <hr/>
1758 <hr />
1759 <hr>
1760 <hr/>
1761 <hr />
1762 <hr class="foo" id="bar" />
1763 <hr class="foo" id="bar"/>
1764 <hr class="foo" id="bar" >
1765 """
1766 var res = test.md_to_html.write_to_string
1767 assert res == exp
1768 end
1769
1770 fun test_daring_inline_html2 is test do
1771 var test = """
1772 Simple block on one line:
1773
1774 <div>foo</div>
1775
1776 And nested without indentation:
1777
1778 <div>
1779 <div>
1780 <div>
1781 foo
1782 </div>
1783 <div style=">"/>
1784 </div>
1785 <div>bar</div>
1786 </div>
1787
1788 And with attributes:
1789
1790 <div>
1791 <div id="foo">
1792 </div>
1793 </div>
1794
1795 This was broken in 1.0.2b7:
1796
1797 <div class="inlinepage">
1798 <div class="toggleableend">
1799 foo
1800 </div>
1801 </div>
1802 """
1803
1804 var exp = """
1805 <p>Simple block on one line:</p>
1806 <div>foo</div>
1807 <p>And nested without indentation:</p>
1808 <div>
1809 <div>
1810 <div>
1811 foo
1812 </div>
1813 <div style=">"/>
1814 </div>
1815 <div>bar</div>
1816 </div>
1817 <p>And with attributes:</p>
1818 <div>
1819 <div id="foo">
1820 </div>
1821 </div>
1822 <p>This was broken in 1.0.2b7:</p>
1823 <div class="inlinepage">
1824 <div class="toggleableend">
1825 foo
1826 </div>
1827 </div>
1828 """
1829 var res = test.md_to_html.write_to_string
1830 assert res == exp
1831 end
1832
1833 fun test_daring_inline_html3 is test do
1834 var test = """
1835 Paragraph one.
1836
1837 <!-- This is a simple comment -->
1838
1839 <!--
1840 This is another comment.
1841 -->
1842
1843 Paragraph two.
1844
1845 <!-- one comment block -- -- with two comments -->
1846
1847 The end.
1848 """
1849
1850 var exp = """
1851 <p>Paragraph one.</p>
1852 <!-- This is a simple comment -->
1853 <!--
1854 This is another comment.
1855 -->
1856 <p>Paragraph two.</p>
1857 <!-- one comment block -- -- with two comments -->
1858 <p>The end.</p>
1859 """
1860 var res = test.md_to_html.write_to_string
1861 assert res == exp
1862 end
1863
1864 fun test_daring_links1 is test do
1865 var test = """
1866 Just a [URL](/url/).
1867
1868 [URL and title](/url/ "title").
1869
1870 [URL and title](/url/ "title preceded by two spaces").
1871
1872 [URL and title](/url/ "title preceded by a tab").
1873
1874 [URL and title](/url/ "title has spaces afterward" ).
1875
1876 [URL wrapped in angle brackets](</url/>).
1877
1878 [URL w/ angle brackets + title](</url/> "Here's the title").
1879
1880 [Empty]().
1881
1882 [With parens in the URL](http://en.wikipedia.org/wiki/WIMP_(computing))
1883
1884 (With outer parens and [parens in url](/foo(bar)))
1885
1886
1887 [With parens in the URL](/foo(bar) "and a title")
1888
1889 (With outer parens and [parens in url](/foo(bar) "and a title"))
1890 """
1891
1892 var exp = """
1893 <p>Just a <a href="/url/">URL</a>.</p>
1894 <p><a href="/url/" title="title">URL and title</a>.</p>
1895 <p><a href="/url/" title="title preceded by two spaces">URL and title</a>.</p>
1896 <p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
1897 <p><a href="/url/" title="title has spaces afterward">URL and title</a>.</p>
1898 <p><a href="/url/">URL wrapped in angle brackets</a>.</p>
1899 <p><a href="/url/" title="Here&apos;s the title">URL w/ angle brackets + title</a>.</p>
1900 <p><a href="">Empty</a>.</p>
1901 <p><a href="http://en.wikipedia.org/wiki/WIMP_(computing)">With parens in the URL</a></p>
1902 <p>(With outer parens and <a href="/foo(bar)">parens in url</a>)</p>
1903 <p><a href="/foo(bar)" title="and a title">With parens in the URL</a></p>
1904 <p>(With outer parens and <a href="/foo(bar)" title="and a title">parens in url</a>)</p>
1905 """
1906 var res = test.md_to_html.write_to_string
1907 assert res == exp
1908 end
1909
1910 fun test_daring_links2 is test do
1911 var test = """
1912 Foo [bar] [1].
1913
1914 Foo [bar][1].
1915
1916 Foo [bar]
1917 [1].
1918
1919 [1]: /url/ "Title"
1920
1921
1922 With [embedded [brackets]] [b].
1923
1924
1925 Indented [once][].
1926
1927 Indented [twice][].
1928
1929 Indented [thrice][].
1930
1931 Indented [four][] times.
1932
1933 [once]: /url
1934
1935 [twice]: /url
1936
1937 [thrice]: /url
1938
1939 [four]: /url
1940
1941
1942 [b]: /url/
1943
1944 * * *
1945
1946 [this] [this] should work
1947
1948 So should [this][this].
1949
1950 And [this] [].
1951
1952 And [this][].
1953
1954 And [this].
1955
1956 But not [that] [].
1957
1958 Nor [that][].
1959
1960 Nor [that].
1961
1962 [Something in brackets like [this][] should work]
1963
1964 [Same with [this].]
1965
1966 In this case, [this](/somethingelse/) points to something else.
1967
1968 Backslashing should suppress \\\[this] and [this\\\].
1969
1970 [this]: foo
1971
1972
1973 * * *
1974
1975 Here's one where the [link
1976 breaks] across lines.
1977
1978 Here's another where the [link
1979 breaks] across lines, but with a line-ending space.
1980
1981
1982 [link breaks]: /url/
1983 """
1984
1985 var exp = """
1986 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1987 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1988 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1989 <p>With <a href="/url/">embedded [brackets]</a>.</p>
1990 <p>Indented <a href="/url">once</a>.</p>
1991 <p>Indented <a href="/url">twice</a>.</p>
1992 <p>Indented <a href="/url">thrice</a>.</p>
1993 <p>Indented [four][] times.</p>
1994 <pre><code>[four]: /url
1995 </code></pre>
1996 <hr/>
1997 <p><a href="foo">this</a> should work</p>
1998 <p>So should <a href="foo">this</a>.</p>
1999 <p>And <a href="foo">this</a>.</p>
2000 <p>And <a href="foo">this</a>.</p>
2001 <p>And <a href="foo">this</a>.</p>
2002 <p>But not [that] [].</p>
2003 <p>Nor [that][].</p>
2004 <p>Nor [that].</p>
2005 <p>[Something in brackets like <a href="foo">this</a> should work]</p>
2006 <p>[Same with <a href="foo">this</a>.]</p>
2007 <p>In this case, <a href="/somethingelse/">this</a> points to something else.</p>
2008 <p>Backslashing should suppress [this] and [this].</p>
2009 <hr/>
2010 <p>Here's one where the <a href="/url/">link
2011 breaks</a> across lines.</p>
2012 <p>Here's another where the <a href="/url/">link
2013 breaks</a> across lines, but with a line-ending space.</p>
2014 """
2015 var res = test.md_to_html.write_to_string
2016 assert res == exp
2017 end
2018
2019 fun test_daring_links3 is test do
2020 var test = """
2021 This is the [simple case].
2022
2023 [simple case]: /simple
2024
2025
2026
2027 This one has a [line
2028 break].
2029
2030 This one has a [line
2031 break] with a line-ending space.
2032
2033 [line break]: /foo
2034
2035
2036 [this] [that] and the [other]
2037
2038 [this]: /this
2039 [that]: /that
2040 [other]: /other
2041 """
2042
2043 var exp = """
2044 <p>This is the <a href="/simple">simple case</a>.</p>
2045 <p>This one has a <a href="/foo">line
2046 break</a>.</p>
2047 <p>This one has a <a href="/foo">line
2048 break</a> with a line-ending space.</p>
2049 <p><a href="/that">this</a> and the <a href="/other">other</a></p>
2050 """
2051 var res = test.md_to_html.write_to_string
2052 assert res == exp
2053 end
2054
2055 fun test_daring_nested is test do
2056 var test = """
2057 > foo
2058 >
2059 > > bar
2060 >
2061 > foo
2062 """
2063
2064 var exp = """
2065 <blockquote>
2066 <p>foo</p>
2067 <blockquote>
2068 <p>bar</p>
2069 </blockquote>
2070 <p>foo</p>
2071 </blockquote>
2072 """
2073 var res = test.md_to_html.write_to_string
2074 assert res == exp
2075 end
2076
2077 fun test_daring_list is test do
2078 var test = """
2079 ## Unordered
2080
2081 Asterisks tight:
2082
2083 * asterisk 1
2084 * asterisk 2
2085 * asterisk 3
2086
2087
2088 Asterisks loose:
2089
2090 * asterisk 1
2091
2092 * asterisk 2
2093
2094 * asterisk 3
2095
2096 * * *
2097
2098 Pluses tight:
2099
2100 + Plus 1
2101 + Plus 2
2102 + Plus 3
2103
2104
2105 Pluses loose:
2106
2107 + Plus 1
2108
2109 + Plus 2
2110
2111 + Plus 3
2112
2113 * * *
2114
2115
2116 Minuses tight:
2117
2118 - Minus 1
2119 - Minus 2
2120 - Minus 3
2121
2122
2123 Minuses loose:
2124
2125 - Minus 1
2126
2127 - Minus 2
2128
2129 - Minus 3
2130
2131
2132 ## Ordered
2133
2134 Tight:
2135
2136 1. First
2137 2. Second
2138 3. Third
2139
2140 and:
2141
2142 1. One
2143 2. Two
2144 3. Three
2145
2146
2147 Loose using tabs:
2148
2149 1. First
2150
2151 2. Second
2152
2153 3. Third
2154
2155 and using spaces:
2156
2157 1. One
2158
2159 2. Two
2160
2161 3. Three
2162
2163 Multiple paragraphs:
2164
2165 1. Item 1, graf one.
2166
2167 Item 2. graf two. The quick brown fox jumped over the lazy dog's
2168 back.
2169
2170 2. Item 2.
2171
2172 3. Item 3.
2173
2174
2175
2176 ## Nested
2177
2178 * Tab
2179 * Tab
2180 * Tab
2181
2182 Here's another:
2183
2184 1. First
2185 2. Second:
2186 * Fee
2187 * Fie
2188 * Foe
2189 3. Third
2190
2191 Same thing but with paragraphs:
2192
2193 1. First
2194
2195 2. Second:
2196 * Fee
2197 * Fie
2198 * Foe
2199
2200 3. Third
2201 """
2202
2203 var exp = """
2204 <h2 id="Unordered">Unordered</h2>
2205 <p>Asterisks tight:</p>
2206 <ul>
2207 <li>asterisk 1</li>
2208 <li>asterisk 2</li>
2209 <li>asterisk 3</li>
2210 </ul>
2211 <p>Asterisks loose:</p>
2212 <ul>
2213 <li><p>asterisk 1</p>
2214 </li>
2215 <li><p>asterisk 2</p>
2216 </li>
2217 <li><p>asterisk 3</p>
2218 </li>
2219 </ul>
2220 <hr/>
2221 <p>Pluses tight:</p>
2222 <ul>
2223 <li>Plus 1</li>
2224 <li>Plus 2</li>
2225 <li>Plus 3</li>
2226 </ul>
2227 <p>Pluses loose:</p>
2228 <ul>
2229 <li><p>Plus 1</p>
2230 </li>
2231 <li><p>Plus 2</p>
2232 </li>
2233 <li><p>Plus 3</p>
2234 </li>
2235 </ul>
2236 <hr/>
2237 <p>Minuses tight:</p>
2238 <ul>
2239 <li>Minus 1</li>
2240 <li>Minus 2</li>
2241 <li>Minus 3</li>
2242 </ul>
2243 <p>Minuses loose:</p>
2244 <ul>
2245 <li><p>Minus 1</p>
2246 </li>
2247 <li><p>Minus 2</p>
2248 </li>
2249 <li><p>Minus 3</p>
2250 </li>
2251 </ul>
2252 <h2 id="Ordered">Ordered</h2>
2253 <p>Tight:</p>
2254 <ol>
2255 <li>First</li>
2256 <li>Second</li>
2257 <li>Third</li>
2258 </ol>
2259 <p>and:</p>
2260 <ol>
2261 <li>One</li>
2262 <li>Two</li>
2263 <li>Three</li>
2264 </ol>
2265 <p>Loose using tabs:</p>
2266 <ol>
2267 <li><p>First</p>
2268 </li>
2269 <li><p>Second</p>
2270 </li>
2271 <li><p>Third</p>
2272 </li>
2273 </ol>
2274 <p>and using spaces:</p>
2275 <ol>
2276 <li><p>One</p>
2277 </li>
2278 <li><p>Two</p>
2279 </li>
2280 <li><p>Three</p>
2281 </li>
2282 </ol>
2283 <p>Multiple paragraphs:</p>
2284 <ol>
2285 <li><p>Item 1, graf one.</p>
2286 <p>Item 2. graf two. The quick brown fox jumped over the lazy dog's
2287 back.</p>
2288 </li>
2289 <li><p>Item 2.</p>
2290 </li>
2291 <li><p>Item 3.</p>
2292 </li>
2293 </ol>
2294 <h2 id="Nested">Nested</h2>
2295 <ul>
2296 <li>Tab<ul>
2297 <li>Tab<ul>
2298 <li>Tab</li>
2299 </ul>
2300 </li>
2301 </ul>
2302 </li>
2303 </ul>
2304 <p>Here's another:</p>
2305 <ol>
2306 <li>First</li>
2307 <li>Second:<ul>
2308 <li>Fee</li>
2309 <li>Fie</li>
2310 <li>Foe</li>
2311 </ul>
2312 </li>
2313 <li>Third</li>
2314 </ol>
2315 <p>Same thing but with paragraphs:</p>
2316 <ol>
2317 <li><p>First</p>
2318 </li>
2319 <li><p>Second:</p>
2320 <ul>
2321 <li>Fee</li>
2322 <li>Fie</li>
2323 <li>Foe</li>
2324 </ul>
2325 </li>
2326 <li><p>Third</p>
2327 </li>
2328 </ol>
2329 """
2330 var res = test.md_to_html.write_to_string
2331 assert res == exp
2332 end
2333
2334 fun test_daring_strong_em is test do
2335 var test = """
2336 ***This is strong and em.***
2337
2338 So is ***this*** word.
2339
2340 ___This is strong and em.___
2341
2342 So is ___this___ word.
2343 """
2344
2345 var exp = """
2346 <p><strong><em>This is strong and em.</em></strong></p>
2347 <p>So is <strong><em>this</em></strong> word.</p>
2348 <p><strong><em>This is strong and em.</em></strong></p>
2349 <p>So is <strong><em>this</em></strong> word.</p>
2350 """
2351 var res = test.md_to_html.write_to_string
2352 assert res == exp
2353 end
2354
2355 fun test_daring_tabs is test do
2356 var test = """
2357 + this is a list item
2358 indented with tabs
2359
2360 + this is a list item
2361 indented with spaces
2362
2363 Code:
2364
2365 this code block is indented by one tab
2366
2367 And:
2368
2369 this code block is indented by two tabs
2370
2371 And:
2372
2373 + this is an example list item
2374 indented with tabs
2375
2376 + this is an example list item
2377 indented with spaces
2378 """
2379
2380 var exp = """
2381 <ul>
2382 <li><p>this is a list item
2383 indented with tabs</p>
2384 </li>
2385 <li><p>this is a list item
2386 indented with spaces</p>
2387 </li>
2388 </ul>
2389 <p>Code:</p>
2390 <pre><code>this code block is indented by one tab
2391 </code></pre>
2392 <p>And:</p>
2393 <pre><code> this code block is indented by two tabs
2394 </code></pre>
2395 <p>And:</p>
2396 <pre><code>+ this is an example list item
2397 indented with tabs
2398
2399 + this is an example list item
2400 indented with spaces
2401 </code></pre>
2402 """
2403 var res = test.md_to_html.write_to_string
2404 assert res == exp
2405 end
2406
2407 fun test_daring_tidyness is test do
2408 var test = """
2409 > A list within a blockquote:
2410 >
2411 > * asterisk 1
2412 > * asterisk 2
2413 > * asterisk 3
2414 """
2415
2416 var exp = """
2417 <blockquote>
2418 <p>A list within a blockquote:</p>
2419 <ul>
2420 <li>asterisk 1</li>
2421 <li>asterisk 2</li>
2422 <li>asterisk 3</li>
2423 </ul>
2424 </blockquote>
2425 """
2426 var res = test.md_to_html.write_to_string
2427 assert res == exp
2428 end
2429
2430
2431 end
2432
2433 class TestBlock
2434 test
2435
2436 # A dummy location for testing purposes.
2437 var loc = new MDLocation(0, 0, 0, 0)
2438
2439 fun test_has_blocks is test do
2440 var subject = new MDBlock(loc)
2441 assert not subject.has_blocks
2442 subject.first_block = new MDBlock(loc)
2443 assert subject.has_blocks
2444 end
2445
2446 fun test_count_blocks is test do
2447 var subject = new MDBlock(loc)
2448 assert subject.count_blocks == 0
2449 subject.first_block = new MDBlock(loc)
2450 assert subject.count_blocks == 1
2451 subject.first_block.next = new MDBlock(loc)
2452 assert subject.count_blocks == 2
2453 end
2454
2455 fun test_has_lines is test do
2456 var subject = new MDBlock(loc)
2457 assert not subject.has_lines
2458 subject.first_line = new MDLine(loc, "")
2459 assert subject.has_lines
2460 end
2461
2462 fun test_count_lines is test do
2463 var subject = new MDBlock(loc)
2464 assert subject.count_lines == 0
2465 subject.first_line = new MDLine(loc, "")
2466 assert subject.count_lines == 1
2467 subject.first_line.next = new MDLine(loc, "")
2468 assert subject.count_lines == 2
2469 end
2470
2471 fun test_split is test do
2472 var line1 = new MDLine(loc, "line1")
2473 var line2 = new MDLine(loc, "line2")
2474 var line3 = new MDLine(loc, "line3")
2475 var subject = new MDBlock(loc)
2476 subject.add_line line1
2477 subject.add_line line2
2478 subject.add_line line3
2479 var block = subject.split(line2)
2480 assert subject.count_blocks == 1
2481 assert subject.count_lines == 1
2482 assert subject.first_line == line3
2483 assert block.count_blocks == 0
2484 assert block.count_lines == 2
2485 assert block.first_line == line1
2486 assert block.last_line == line2
2487 end
2488
2489 fun test_add_line is test do
2490 var subject = new MDBlock(loc)
2491 assert subject.count_lines == 0
2492 subject.add_line new MDLine(loc, "")
2493 assert subject.count_lines == 1
2494 subject.add_line new MDLine(loc, "")
2495 assert subject.count_lines == 2
2496 end
2497
2498 fun test_remove_line is test do
2499 var line1 = new MDLine(loc, "line1")
2500 var line2 = new MDLine(loc, "line2")
2501 var line3 = new MDLine(loc, "line3")
2502 var subject = new MDBlock(loc)
2503 subject.add_line line1
2504 subject.add_line line2
2505 subject.add_line line3
2506 subject.remove_line(line2)
2507 assert subject.count_lines == 2
2508 subject.remove_line(line1)
2509 assert subject.count_lines == 1
2510 assert subject.first_line == line3
2511 assert subject.last_line == line3
2512 end
2513
2514 fun test_transform_headline1 is test do
2515 var subject = new MDBlock(loc)
2516 var kind = new BlockHeadline(subject)
2517 subject.add_line new MDLine(loc, " # Title 1 ")
2518 kind.transform_headline(subject)
2519 assert kind.depth == 1
2520 assert subject.first_line.value == "Title 1"
2521 end
2522
2523 fun test_transform_headline2 is test do
2524 var subject = new MDBlock(loc)
2525 var kind = new BlockHeadline(subject)
2526 subject.add_line new MDLine(loc, " #####Title 5 ")
2527 kind.transform_headline(subject)
2528 assert kind.depth == 5
2529 assert subject.first_line.value == "Title 5"
2530 end
2531
2532 fun test_remove_quote_prefix is test do
2533 var subject = new MDBlock(loc)
2534 var kind = new BlockQuote(subject)
2535 subject.add_line new MDLine(loc, " > line 1")
2536 subject.add_line new MDLine(loc, " > line 2")
2537 subject.add_line new MDLine(loc, " > line 3")
2538 kind.remove_block_quote_prefix(subject)
2539 assert subject.first_line.value == "line 1"
2540 assert subject.first_line.next.value == "line 2"
2541 assert subject.first_line.next.next.value == "line 3"
2542 end
2543
2544 fun test_remove_leading_empty_lines_1 is test do
2545 var block = new MDBlock(loc)
2546 block.add_line new MDLine(loc, "")
2547 block.add_line new MDLine(loc, "")
2548 block.add_line new MDLine(loc, "")
2549 block.add_line new MDLine(loc, "")
2550 block.add_line new MDLine(loc, " text")
2551 block.add_line new MDLine(loc, "")
2552 assert block.remove_leading_empty_lines
2553 assert block.first_line.value == " text"
2554 end
2555
2556 fun test_remove_leading_empty_lines_2 is test do
2557 var block = new MDBlock(loc)
2558 block.add_line new MDLine(loc, " text")
2559 block.remove_leading_empty_lines
2560 assert block.first_line.value == " text"
2561 end
2562
2563 fun test_remove_trailing_empty_lines_1 is test do
2564 var block = new MDBlock(loc)
2565 block.add_line new MDLine(loc, "")
2566 block.add_line new MDLine(loc, "text")
2567 block.add_line new MDLine(loc, "")
2568 block.add_line new MDLine(loc, "")
2569 block.add_line new MDLine(loc, "")
2570 block.add_line new MDLine(loc, "")
2571 assert block.remove_trailing_empty_lines
2572 assert block.last_line.value == "text"
2573 end
2574
2575 fun test_remove_trailing_empty_lines_2 is test do
2576 var block = new MDBlock(loc)
2577 block.add_line new MDLine(loc, "text ")
2578 assert not block.remove_trailing_empty_lines
2579 assert block.last_line.value == "text "
2580 end
2581
2582 fun test_remove_surrounding_empty_lines is test do
2583 var block = new MDBlock(loc)
2584 block.add_line new MDLine(loc, "")
2585 block.add_line new MDLine(loc, "text")
2586 block.add_line new MDLine(loc, "")
2587 block.add_line new MDLine(loc, "")
2588 block.add_line new MDLine(loc, "")
2589 block.add_line new MDLine(loc, "")
2590 assert block.remove_surrounding_empty_lines
2591 assert block.first_line.value == "text"
2592 assert block.last_line.value == "text"
2593 end
2594 end
2595
2596 class TestLine
2597 test
2598
2599 # A dummy location for testing purposes.
2600 var loc = new MDLocation(0, 0, 0, 0)
2601
2602 var subject: MDLine
2603
2604 fun test_is_empty is test do
2605 subject = new MDLine(loc, "")
2606 assert subject.is_empty
2607 subject = new MDLine(loc, " ")
2608 assert subject.is_empty
2609 subject = new MDLine(loc, "test")
2610 assert not subject.is_empty
2611 subject = new MDLine(loc, " test")
2612 assert not subject.is_empty
2613 end
2614
2615 fun test_leading is test do
2616 subject = new MDLine(loc, "")
2617 assert subject.leading == 0
2618 subject = new MDLine(loc, " ")
2619 assert subject.leading == 4
2620 subject = new MDLine(loc, "test")
2621 assert subject.leading == 0
2622 subject = new MDLine(loc, " test")
2623 assert subject.leading == 4
2624 end
2625
2626 fun test_trailing is test do
2627 subject = new MDLine(loc, "")
2628 assert subject.trailing == 0
2629 subject = new MDLine(loc, " ")
2630 assert subject.trailing == 0
2631 subject = new MDLine(loc, "test ")
2632 assert subject.trailing == 3
2633 subject = new MDLine(loc, " test ")
2634 assert subject.trailing == 1
2635 end
2636
2637 fun test_line_type is test do
2638 var v = new MarkdownProcessor
2639 subject = new MDLine(loc, "")
2640 assert v.line_kind(subject) isa LineEmpty
2641 subject = new MDLine(loc, " ")
2642 assert v.line_kind(subject) isa LineEmpty
2643 subject = new MDLine(loc, "text ")
2644 assert v.line_kind(subject) isa LineOther
2645 subject = new MDLine(loc, " # Title")
2646 assert v.line_kind(subject) isa LineHeadline
2647 subject = new MDLine(loc, " ### Title")
2648 assert v.line_kind(subject) isa LineHeadline
2649 subject = new MDLine(loc, " code")
2650 assert v.line_kind(subject) isa LineCode
2651 subject = new MDLine(loc, " Title ")
2652 subject.next = new MDLine(loc, "== ")
2653 assert v.line_kind(subject) isa LineHeadline1
2654 subject = new MDLine(loc, " Title ")
2655 subject.next = new MDLine(loc, "-- ")
2656 assert v.line_kind(subject) isa LineHeadline2
2657 subject = new MDLine(loc, " * * * ")
2658 assert v.line_kind(subject) isa LineHR
2659 subject = new MDLine(loc, " *** ")
2660 assert v.line_kind(subject) isa LineHR
2661 subject = new MDLine(loc, "- -- ")
2662 assert v.line_kind(subject) isa LineHR
2663 subject = new MDLine(loc, "--------- ")
2664 assert v.line_kind(subject) isa LineHR
2665 subject = new MDLine(loc, " >")
2666 assert v.line_kind(subject) isa LineBlockquote
2667 subject = new MDLine(loc, "<p></p>")
2668 assert v.line_kind(subject) isa LineXML
2669 subject = new MDLine(loc, "<p>")
2670 assert v.line_kind(subject) isa LineOther
2671 subject = new MDLine(loc, " * foo")
2672 assert v.line_kind(subject) isa LineUList
2673 subject = new MDLine(loc, "- foo")
2674 assert v.line_kind(subject) isa LineUList
2675 subject = new MDLine(loc, "+ foo")
2676 assert v.line_kind(subject) isa LineUList
2677 subject = new MDLine(loc, "1. foo")
2678 assert v.line_kind(subject) isa LineOList
2679 subject = new MDLine(loc, " 11111. foo")
2680 assert v.line_kind(subject) isa LineOList
2681 end
2682
2683 fun test_line_type_ext is test do
2684 var v = new MarkdownProcessor
2685 subject = new MDLine(loc, " ~~~")
2686 assert v.line_kind(subject) isa LineFence
2687 subject = new MDLine(loc, " ```")
2688 assert v.line_kind(subject) isa LineFence
2689 subject = new MDLine(loc, "~~~raw")
2690 assert v.line_kind(subject) isa LineFence
2691 end
2692
2693 fun test_count_chars is test do
2694 subject = new MDLine(loc, "")
2695 assert subject.count_chars('*') == 0
2696 subject = new MDLine(loc, "* ")
2697 assert subject.count_chars('*') == 1
2698 subject = new MDLine(loc, " * text")
2699 assert subject.count_chars('*') == 0
2700 subject = new MDLine(loc, " * * *")
2701 assert subject.count_chars('*') == 3
2702 subject = new MDLine(loc, "text ** ")
2703 assert subject.count_chars('*') == 0
2704 end
2705
2706 fun test_count_chars_start is test do
2707 subject = new MDLine(loc, "")
2708 assert subject.count_chars_start('*') == 0
2709 subject = new MDLine(loc, "* ")
2710 assert subject.count_chars_start('*') == 1
2711 subject = new MDLine(loc, " * text")
2712 assert subject.count_chars_start('*') == 1
2713 subject = new MDLine(loc, " * * * text")
2714 assert subject.count_chars_start('*') == 3
2715 subject = new MDLine(loc, "text ** ")
2716 assert subject.count_chars_start('*') == 0
2717 end
2718 end
2719
2720 class TestHTMLDecorator
2721 test
2722
2723 fun test_headlines is test do
2724 var test = """
2725 # **a**
2726 ## a.a
2727 ### a.a.b
2728 ### a.a.b
2729 ## a.b
2730 # [b](test)
2731 ## b.a
2732 ### b.a.c
2733 ## b.b
2734 ## b.c
2735 # c
2736 """
2737 var proc = new MarkdownProcessor
2738 var decorator = proc.decorator.as(HTMLDecorator)
2739 proc.process(test)
2740 var res = ""
2741 for id, headline in decorator.headlines do
2742 res += "{headline.title}:{id}\n"
2743 end
2744 var exp = """
2745 **a**:a
2746 a.a:a.a
2747 a.a.b:a.a.b
2748 a.a.b:a.a.b_1
2749 a.b:a.b
2750 [b](test):btest
2751 b.a:b.a
2752 b.a.c:b.a.c
2753 b.b:b.b
2754 b.c:b.c
2755 c:c
2756 """
2757 assert res == exp
2758 end
2759 end
2760
2761 class TestTokenLocation
2762 test
2763
2764 fun test_token_location1 is test do
2765 var string = "**Hello** `World`"
2766 var stack = [
2767 "TokenStrongStar at 1,1--1,1",
2768 "TokenStrongStar at 1,8--1,8",
2769 "TokenCodeSingle at 1,11--1,11",
2770 "TokenCodeSingle at 1,17--1,17"]
2771 (new TestTokenProcessor(stack)).process(string)
2772 end
2773
2774 fun test_token_location2 is test do
2775 var string = "**Hello**\n`World`\n*Bonjour*\n[le monde]()"
2776 var stack = [
2777 "TokenStrongStar at 1,1--1,1",
2778 "TokenStrongStar at 1,8--1,8",
2779 "TokenCodeSingle at 2,1--2,1",
2780 "TokenCodeSingle at 2,7--2,7",
2781 "TokenEmStar at 3,1--3,1",
2782 "TokenEmStar at 3,9--3,9",
2783 "TokenLink at 4,1--4,1"]
2784 (new TestTokenProcessor(stack)).process(string)
2785 end
2786
2787 fun test_token_location3 is test do
2788 var string = """**Hello**
2789 `World`
2790 *Bonjour*
2791 [le monde]()"""
2792 var stack = [
2793 "TokenStrongStar at 1,1--1,1",
2794 "TokenStrongStar at 1,8--1,8",
2795 "TokenCodeSingle at 2,1--2,1",
2796 "TokenCodeSingle at 2,7--2,7",
2797 "TokenEmStar at 3,1--3,1",
2798 "TokenEmStar at 3,9--3,9",
2799 "TokenLink at 4,1--4,1"]
2800 (new TestTokenProcessor(stack)).process(string)
2801 end
2802
2803 fun test_token_location4 is test do
2804 var string = "**Hello**\n\n`World`"
2805 var stack = [
2806 "TokenStrongStar at 1,1--1,1",
2807 "TokenStrongStar at 1,8--1,8",
2808 "TokenCodeSingle at 3,1--3,1",
2809 "TokenCodeSingle at 3,7--3,7"]
2810 (new TestTokenProcessor(stack)).process(string)
2811 end
2812
2813 fun test_token_location5 is test do
2814 var string = "# *Title1*\n\n# *Title2*"
2815 var stack = [
2816 "TokenEmStar at 1,3--1,3",
2817 "TokenEmStar at 1,10--1,10",
2818 "TokenEmStar at 3,3--3,3",
2819 "TokenEmStar at 3,10--3,10"]
2820 (new TestTokenProcessor(stack)).process(string)
2821 end
2822 end
2823
2824 class TestTokenProcessor
2825 super MarkdownProcessor
2826
2827 var test_stack: Array[String]
2828
2829 redef fun token_at(input, pos) do
2830 var token = super
2831 if token isa TokenNone then return token
2832 var res = "{token.class_name} at {token.location or else "?"}"
2833 var exp = test_stack.shift
2834 print ""
2835 print "EXP {exp}"
2836 print "RES {res}"
2837 assert exp == res
2838 return token
2839 end
2840 end
2841
2842 class TestBlockLocation
2843 test
2844
2845 var proc = new MarkdownProcessor
2846
2847 fun test_block_location1 is test do
2848 var stack = [
2849 "BlockHeadline: 1,1--1,8",
2850 "BlockListItem: 2,1--2,6",
2851 "BlockListItem: 3,1--3,5"
2852 ]
2853 var string =
2854 "# Title\n* li1\n* li2"
2855 proc.decorator = new TestBlockDecorator(stack)
2856 proc.process(string)
2857 end
2858
2859 fun test_block_location2 is test do
2860 var stack = [
2861 "BlockHeadline: 1,1--1,11",
2862 "BlockFence: 3,1--5,4",
2863 "BlockListItem: 7,1--7,7",
2864 "BlockListItem: 8,1--8,6"]
2865 var string ="""#### Title
2866
2867 ~~~fence
2868 some code
2869 ~~~
2870
2871 1. li1
2872 1. li2"""
2873 proc.decorator = new TestBlockDecorator(stack)
2874 proc.process(string)
2875 end
2876
2877 fun test_block_location3 is test do
2878 var stack = [
2879 "BlockHeadline: 1,1--1,8",
2880 "BlockHeadline: 3,1--3,10"]
2881 var string ="""# Title\n\n## Title 2"""
2882 proc.decorator = new TestBlockDecorator(stack)
2883 proc.process(string)
2884 end
2885 end
2886
2887 class TestBlockDecorator
2888 super HTMLDecorator
2889
2890 var stack: Array[String]
2891
2892 redef fun add_headline(v, block) do
2893 super
2894 check_res(block)
2895 end
2896
2897 redef fun add_listitem(v, block) do
2898 super
2899 check_res(block)
2900 end
2901
2902 redef fun add_blockquote(v, block) do
2903 super
2904 check_res(block)
2905 end
2906
2907 redef fun add_code(v, block) do
2908 super
2909 check_res(block)
2910 end
2911
2912 fun check_res(block: Block) do
2913 var res = "{block.class_name}: {block.block.location}"
2914 var exp = stack.shift
2915 assert res == exp
2916 end
2917 end