Merge: lib/markdown2: introduce a new Markdown engine
[nit.git] / lib / markdown2 / tests / test_markdown_md.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 # Tests for markdown rendering to markdown
16 module test_markdown_md is test
17
18 import test_markdown
19 import markdown_md_rendering
20
21 abstract class TestMarkdownMd
22 super TestMarkdown
23
24 var md_renderer = new MarkdownRenderer
25
26 fun md_to_md(md: String): String do
27 var doc = md_parser.parse(md)
28 doc.debug
29 return md_renderer.render(doc)
30 end
31 end
32
33 class TestMdHeadings
34 super TestMarkdownMd
35 test
36
37 fun test_no_trailings is test do
38 var md = """# foo\n## foo\n### foo\n#### foo\n##### foo\n###### foo\n"""
39 var exp = """# foo\n\n## foo\n\n### foo\n\n#### foo\n\n##### foo\n\n###### foo\n"""
40 assert md_to_md(md) == exp
41 end
42
43 fun test_trailings is test do
44 var md = """# foo #\n## foo ##\n### foo ###\n#### foo ####\n##### foo #####\n"""
45 var exp = """# foo #\n\n## foo ##\n\n### foo ###\n\n#### foo ####\n\n##### foo #####\n"""
46 assert md_to_md(md) == exp
47 end
48
49 fun test_setext is test do
50 var md = """Foo *bar*\n=========\nFoo *bar*\n---------\n"""
51 var exp = """Foo *bar*\n=========\n\nFoo *bar*\n---------\n"""
52 assert md_to_md(md) == exp
53 end
54 end
55
56 class TestMdBlockQuotes
57 super TestMarkdownMd
58 test
59
60 fun test191 is test do
61 var md = """> # Foo\n> bar\n> baz\n"""
62 assert md_to_md(md) == md
63 end
64
65 fun test197 is test do
66 var md = """> foo\n---\n"""
67 var exp = """> foo\n\n---\n"""
68 assert md_to_md(md) == exp
69 end
70
71 fun test198 is test do
72 var md = """> - foo\n- bar\n"""
73 var exp = """> - foo\n\n- bar\n"""
74 assert md_to_md(md) == exp
75 end
76
77 fun test206 is test do
78 var md = """> foo\n> bar\n"""
79 assert md_to_md(md) == md
80 end
81
82 fun test213 is test do
83 var md = """> > > foo\n> bar\n"""
84 var exp = """> > > foo\n> > > bar\n"""
85 assert md_to_md(md) == exp
86 end
87 end
88
89 class TestMdLists
90 super TestMarkdownMd
91 test
92
93 fun test264 is test do
94 var md = """- foo\n- bar\n+ baz\n"""
95 var exp = """- foo\n- bar\n\n+ baz\n"""
96 assert md_to_md(md) == exp
97 end
98
99 fun test265 is test do
100 var md = """1. foo\n2. bar\n3) baz\n"""
101 var exp = """1. foo\n2. bar\n\n3) baz\n"""
102 assert md_to_md(md) == exp
103 end
104
105 fun test270 is test do
106 var md = """- foo\n - bar\n - baz\n\n bim\n"""
107 assert md_to_md(md) == md
108 end
109
110 fun test273 is test do
111 var md = """- a\n - b\n - c\n - d\n - e\n - f\n- g\n"""
112 var exp = """- a\n- b\n- c\n- d\n- e\n- f\n- g\n"""
113 assert md_to_md(md) == exp
114 end
115
116 fun test274 is test do
117 var md = """1. a\n\n 2. b\n\n 3. c\n"""
118 var exp = """1. a\n\n2. b\n\n3. c\n"""
119 assert md_to_md(md) == exp
120 end
121
122 fun test289 is test do
123 var md = """- a\n - b\n - c\n\n- d\n - e\n - f\n"""
124 var exp = """- a\n\n - b\n - c\n\n- d\n\n - e\n - f\n"""
125 assert md_to_md(md) == exp
126 end
127 end
128
129 class TestMdkListItems
130 super TestMarkdownMd
131 test
132
133 fun test217 is test do
134 var md = """1. A paragraph\n with two lines.\n\n indented code\n\n > A block quote.\n"""
135 var exp = """1. A paragraph\n with two lines.\n\n indented code\n\n > A block quote.\n"""
136 assert md_to_md(md) == exp
137 end
138
139 fun test219 is test do
140 var md = """- one\n\n two\n"""
141 assert md_to_md(md) == md
142 end
143
144 fun test221 is test do
145 var md = """ - one\n\n two\n"""
146 var exp = """- one\n\n two\n"""
147 assert md_to_md(md) == exp
148 end
149
150 # FIXME
151 # fun test223 is test do
152 # var md = """>>- one\n>>\n > > two\n"""
153 # var exp = """> > - one\n> >\n> > two\n"""
154 # assert md_to_md(md) == exp
155 # end
156
157 fun test225 is test do
158 var md = """- foo\n\n bar\n"""
159 assert md_to_md(md) == md
160 end
161
162 fun test228 is test do
163 var md = """123456789. ok\n"""
164 assert md_to_md(md) == md
165 end
166
167 fun test230 is test do
168 var md = """0. ok\n"""
169 assert md_to_md(md) == md
170 end
171
172 fun test246 is test do
173 var md = """1. foo\n2.\n3. bar\n"""
174 assert md_to_md(md) == md
175 end
176
177 fun test254 is test do
178 var md = """ 1. A paragraph\n with two lines.\n"""
179 var exp = """1. A paragraph\n with two lines.\n"""
180 assert md_to_md(md) == exp
181 end
182
183 # FIXME
184 # fun test255 is test do
185 # var md = """> 1. > Blockquote\n> continued here.\n"""
186 # var exp = """> 1. > Blockquote\n > continued here.\n"""
187 # assert md_to_md(md) == exp
188 # end
189 end
190
191 class TestMdFencedCodeBlocks
192 super TestMarkdownMd
193 test
194
195 fun test88 is test do
196 var md = """```\nfoo\n```\n"""
197 assert md_to_md(md) == md
198 end
199
200 fun test92 is test do
201 var md = """~~~\nfoo\n~~~\n"""
202 assert md_to_md(md) == md
203 end
204
205 fun test111 is test do
206 var md = """```ruby\ndef foo(x)\n return 3\nend\n```\n"""
207 assert md_to_md(md) == md
208 end
209
210 fun test112 is test do
211 var md = """~~~~~~\nSome markdown:\n~~~\n**hello**\n~~~\n~~~~~~\n"""
212 assert md_to_md(md) == md
213 end
214 end
215
216 class TestMdIndentedCodeBlocks
217 super TestMarkdownMd
218 test
219
220 fun test75 is test do
221 var md = """ a code block\n"""
222 assert md_to_md(md) == md
223 end
224
225 fun test76 is test do
226 var md = """ a simple\n indented code block\n"""
227 assert md_to_md(md) == md
228 end
229
230 fun test80 is test do
231 var md = """ chunk1\n\n chunk2\n \n \n \n chunk3\n"""
232 assert md_to_md(md) == """ chunk1\n\n chunk2\n\n\n\n chunk3\n"""
233 end
234
235 fun test85 is test do
236 var md = """ foo\n bar\n"""
237 assert md_to_md(md) == md
238 end
239
240 fun test87 is test do
241 var md = """\t\tfoo \n"""
242 assert md_to_md(md) == md
243 end
244 end
245
246 class TestMdThematicBreaks
247 super TestMarkdownMd
248 test
249
250 fun test13 is test do
251 var md = """***\n---\n___\n"""
252 var exp = """***\n\n---\n\n___\n"""
253 assert md_to_md(md) == exp
254 end
255
256 fun test17 is test do
257 var md = """ ***\n ***\n ***\n"""
258 var exp = """***\n\n***\n\n***\n"""
259 assert md_to_md(md) == exp
260 end
261
262 fun test20 is test do
263 var md = """_____________________________________\n"""
264 assert md_to_md(md) == md
265 end
266
267 fun test21 is test do
268 var md = """ - - -\n"""
269 var exp = """- - -\n"""
270 assert md_to_md(md) == exp
271 end
272
273 fun test22 is test do
274 var md = """ ** * ** * ** * **\n"""
275 var exp = """** * ** * ** * **\n"""
276 assert md_to_md(md) == exp
277 end
278
279 fun test23 is test do
280 var md = """- - - -\n"""
281 assert md_to_md(md) == md
282 end
283 end
284
285 class TestMdParagraphs
286 super TestMarkdownMd
287 test
288
289 fun test182 is test do
290 var md = """aaa\n\nbbb\n"""
291 assert md_to_md(md) == md
292 end
293
294 fun test183 is test do
295 var md = """aaa\nbbb\n\nccc\nddd\n"""
296 assert md_to_md(md) == md
297 end
298
299 fun test186 is test do
300 var md = """aaa\n bbb\n ccc\n"""
301 var exp = """aaa\nbbb\nccc\n"""
302 assert md_to_md(md) == exp
303 end
304
305 fun test187 is test do
306 var md = """ aaa\nbbb\n"""
307 var exp = """aaa\nbbb\n"""
308 assert md_to_md(md) == exp
309 end
310 end
311
312 class TestMdHTMLBlocks
313 super TestMarkdownMd
314 test
315
316 fun test116 is test do
317 var md = """<table><tr><td>\n<pre>\n*Hello*,\n\n_world_.\n</pre>\n\n</td></tr></table>\n"""
318 assert md_to_md(md) == md
319 end
320
321 fun test119 is test do
322 var md = """</div>\n*foo*\n"""
323 assert md_to_md(md) == md
324 end
325
326 fun test120 is test do
327 var md = """<DIV CLASS="foo">\n\n*Markdown*\n\n</DIV>\n"""
328 assert md_to_md(md) == md
329 end
330
331 fun test121 is test do
332 var md = """<div id="foo"\n class="bar">\n</div>\n"""
333 assert md_to_md(md) == md
334 end
335
336 fun test124 is test do
337 var md = """<div id="foo"\n*hi*\n"""
338 assert md_to_md(md) == md
339 end
340
341 fun test137 is test do
342 var md = """<pre language="haskell"><code>\nimport Text.HTML.TagSoup\n\nmain :: IO ()\nmain = print $ parseTags tags\n</code></pre>\n"""
343 assert md_to_md(md) == md
344 end
345
346 fun test138 is test do
347 var md = """<script type="text/javascript">\n// JavaScript example\n\ndocument.getElementById("demo").innerHTML = "Hello JavaScript!";\n</script>\n"""
348 assert md_to_md(md) == md
349 end
350
351 fun test139 is test do
352 var md = """<style\n type="text/css">\nh1 {color:red;}\n\np {color:blue;}\n</style>\n"""
353 assert md_to_md(md) == md
354 end
355
356 fun test149 is test do
357 var md = """<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\n"""
358 assert md_to_md(md) == md
359 end
360
361 fun test150 is test do
362 var md = """ <!-- foo -->\n\n <!-- foo -->\n"""
363 assert md_to_md(md) == md
364 end
365 end
366
367 # Inlines
368
369 class TestMdLinks
370 super TestMarkdownMd
371 test
372
373 fun test_autolink is test do
374 var md = """<http://foo.bar.baz/test?q=hello&id=22&boolean>\n"""
375 assert md_to_md(md) == md
376 end
377
378 fun test_automail is test do
379 var md = """<MAILTO:FOO@BAR.BAZ>\n"""
380 assert md_to_md(md) == md
381 end
382
383 fun test462 is test do
384 var md = """[link](/uri "title")\n"""
385 assert md_to_md(md) == md
386 end
387
388 fun test463 is test do
389 var md = """[link](/uri)\n"""
390 assert md_to_md(md) == md
391 end
392
393 fun test464 is test do
394 var md = """[link]()\n"""
395 assert md_to_md(md) == md
396 end
397
398 fun test467 is test do
399 var md = """[link](</my%20uri>)\n"""
400 assert md_to_md(md) == md
401 end
402
403 fun test483 is test do
404 var md = """[link](/url 'title "and" title')\n"""
405 assert md_to_md(md) == """[link](/url "title \\"and\\" title")\n"""
406 end
407
408 fun test490 is test do
409 var md = """[link *foo **bar** `#`*](/uri)\n"""
410 assert md_to_md(md) == md
411 end
412 end
413
414 class TestMdImages
415 super TestMarkdownMd
416 test
417
418 fun test546 is test do
419 var md = """![foo](/url "title")\n"""
420 assert md_to_md(md) == md
421 end
422
423 fun test552 is test do
424 var md = """![foo](train.jpg)\n"""
425 assert md_to_md(md) == md
426 end
427
428 fun test554 is test do
429 var md = """![foo](<url>)\n"""
430 assert md_to_md(md) == md
431 end
432
433 fun test555 is test do
434 var md = """![foo](train.jpg)\n"""
435 assert md_to_md(md) == md
436 end
437 end
438
439 class TestMdCodeSpans
440 super TestMarkdownMd
441 test
442
443 fun test316 is test do
444 var md = """`foo`\n"""
445 assert md_to_md(md) == md
446 end
447
448 fun test319 is test do
449 var md = """``foo``\n"""
450 assert md_to_md(md) == md
451 end
452
453 fun test332 is test do
454 var md = """`foo``bar``\n"""
455 assert md_to_md(md) == md
456 end
457 end
458
459 class TestMdEmphasisAndStrongEmphasis
460 super TestMarkdownMd
461 test
462
463 fun test333 is test do
464 var md = """*foo bar*\n"""
465 assert md_to_md(md) == md
466 end
467
468 fun test335 is test do
469 var md = """a*"foo"*\n"""
470 assert md_to_md(md) == md
471 end
472
473 fun test336 is test do
474 var md = """* a *\n"""
475 assert md_to_md(md) == md
476 end
477
478 fun test351 is test do
479 var md = """*(*foo*)*\n"""
480 assert md_to_md(md) == md
481 end
482
483 fun test360 is test do
484 var md = """**foo bar**\n"""
485 assert md_to_md(md) == md
486 end
487
488 fun test361 is test do
489 var md = """** foo bar**\n"""
490 assert md_to_md(md) == md
491 end
492
493 fun test364 is test do
494 var md = """__foo bar__\n"""
495 assert md_to_md(md) == md
496 end
497
498 fun test393 is test do
499 var md = """*foo**bar**baz*\n"""
500 assert md_to_md(md) == md
501 end
502 end
503
504 class TestMdLineBreaks
505 super TestMarkdownMd
506 test
507
508 fun test609 is test do
509 var md = """foo\\\nbaz\n"""
510 assert md_to_md(md) == md
511 end
512
513 fun test612 is test do
514 var md = """foo\\\n bar\n"""
515 var exp = """foo\\\nbar\n"""
516 assert md_to_md(md) == exp
517 end
518
519 fun test613 is test do
520 var md = """*foo \nbar*\n"""
521 assert md_to_md(md) == md
522 end
523
524 fun test619 is test do
525 var md = """foo\\\n"""
526 assert md_to_md(md) == md
527 end
528
529 fun test623 is test do
530 var md = """foo\nbaz\n"""
531 assert md_to_md(md) == md
532 end
533 end
534
535 class TestMdRawHTML
536 super TestMarkdownMd
537 test
538
539 fun test590 is test do
540 var md = """<a foo="bar" bam = 'baz <em>"</em>'\n_boolean zoop:33=zoop:33 />\n"""
541 assert md_to_md(md) == md
542 end
543
544 fun test591 is test do
545 var md = """Foo <responsive-image src="foo.jpg" />\n"""
546 assert md_to_md(md) == md
547 end
548
549 fun test596 is test do
550 var md = """<a href='bar'title=title>\n"""
551 assert md_to_md(md) == md
552 end
553
554 fun test599 is test do
555 var md = """foo <!-- this is a\ncomment - with hyphen -->\n"""
556 assert md_to_md(md) == md
557 end
558
559 fun test601 is test do
560 var md = """foo <!--> foo -->\n\nfoo <!-- foo--->\n"""
561 assert md_to_md(md) == md
562 end
563
564 fun test602 is test do
565 var md = """foo <?php echo $a; ?>\n"""
566 assert md_to_md(md) == md
567 end
568
569 fun test604 is test do
570 var md = """foo <![CDATA[>&<]]>\n"""
571 assert md_to_md(md) == md
572 end
573
574 fun test606 is test do
575 var md = """foo <a href="\\*">\n"""
576 assert md_to_md(md) == md
577 end
578 end
579
580 class TestMdTabs
581 super TestMarkdownMd
582 test
583
584 fun test1 is test do
585 var md = """\tfoo\tbaz\t\tbim\n"""
586 assert md_to_md(md) == md
587 end
588
589 fun test2 is test do
590 var md = """ \tfoo\tbaz\t\tbim\n"""
591 var exp = """ foo\tbaz\t\tbim\n"""
592 assert md_to_md(md) == exp
593 end
594
595 fun test3 is test do
596 var md = """ a\ta\n ὐ\ta\n"""
597 assert md_to_md(md) == md
598 end
599
600 fun test4 is test do
601 var md = """ - foo\n\n\tbar\n"""
602 var exp = """- foo\n\n bar\n"""
603 assert md_to_md(md) == exp
604 end
605
606 fun test8 is test do
607 var md = """ foo\n\tbar\n"""
608 var exp = """ foo\n bar\n"""
609 assert md_to_md(md) == exp
610 end
611
612 fun test9 is test do
613 var md = """ - foo\n - bar\n\t - baz\n"""
614 var exp = """- foo\n - bar\n - baz\n"""
615 assert md_to_md(md) == exp
616 end
617
618 fun test10 is test do
619 var md = """#\tFoo\n"""
620 var exp = """# Foo\n"""
621 assert md_to_md(md) == exp
622 end
623
624 fun test11 is test do
625 var md = """*\t*\t*\t\n"""
626 assert md_to_md(md) == md
627 end
628 end
629
630 class TestMdBackslashEscapes
631 super TestMarkdownMd
632 test
633
634 fun test292 is test do
635 var md = """\\\t\\A\\a\\ \\3\\φ\\«\n"""
636 assert md_to_md(md) == md
637 end
638
639 fun test295 is test do
640 var md = """foo\\\nbar\n"""
641 assert md_to_md(md) == md
642 end
643
644 fun test297 is test do
645 var md = """ \\[\\]\n"""
646 assert md_to_md(md) == md
647 end
648
649 fun test298 is test do
650 var md = """~~~\n\\[\\]\n~~~\n"""
651 assert md_to_md(md) == md
652 end
653
654 fun test299 is test do
655 var md = """<http://example.com?find=\\*>\n"""
656 assert md_to_md(md) == md
657 end
658
659 fun test300 is test do
660 var md = """<a href="/bar\\/)">\n"""
661 assert md_to_md(md) == md
662 end
663 end