1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Tests for markdown nodes location
16 module test_markdown_location
is test
20 abstract class TestMarkdownLocation
23 redef var md_parser
do
25 parser
.github_mode
= true
26 parser
.wikilinks_mode
= true
30 fun md_to_loc
(md
: String): String do
31 var node
= parse_md
(md
)
32 var v
= new TestMarkdownLocationVisitor
38 class TestMarkdownLocationVisitor
41 var buffer
= new Buffer
44 fun print_loc
(node
: MdNode) do
45 buffer
.append
"{" " * indent}{node.class_name}: {node.location}\n"
51 redef fun visit
(node
) do print_loc
(node
)
54 class TestLocationOutput
55 super TestMarkdownLocation
58 fun test_atx_headings1
is test
do
82 assert md_to_loc
(md
) == loc
85 fun test_atx_headings_with_trailings
is test
do
109 assert md_to_loc
(md
) == loc
112 fun test_settext_headings
is test
do
127 assert md_to_loc
(md
) == loc
130 fun test_indented_code_spaces
is test
do
137 MdDocument: 1,1--3,15
138 MdIndentedCodeBlock: 1,1--3,15
140 assert md_to_loc
(md
) == loc
143 fun test_indented_code_tabs
is test
do
150 MdDocument: 1,1--3,15
151 MdIndentedCodeBlock: 1,1--3,15
153 assert md_to_loc
(md
) == loc
156 fun test_fenced_code
is test
do
166 MdFencedCodeBlock: 1,1--5,3
168 assert md_to_loc
(md
) == loc
171 fun test_thematic_breaks
is test
do
181 MdThematicBreak: 1,1--1,3
182 MdThematicBreak: 3,1--3,5
183 MdThematicBreak: 5,1--5,5
185 assert md_to_loc
(md
) == loc
188 fun test_html_blocks1
is test
do
190 <p><a href="foo">bar</a></p>
193 <a href="foo">bar</a>
197 MdDocument: 1,1--5,12
198 MdHtmlBlock: 1,1--1,28
199 MdHtmlBlock: 3,1--5,12
201 assert md_to_loc
(md
) == loc
204 fun test_paragraph1
is test
do
215 MdDocument: 1,1--7,19
216 MdParagraph: 1,1--1,11
218 MdParagraph: 3,1--4,6
220 MdSoftLineBreak: 3,7--3,7
222 MdParagraph: 6,1--7,19
224 MdSoftLineBreak: 6,10--6,10
227 assert md_to_loc
(md
) == loc
230 fun test_blockquotes
is test
do
237 MdBlockQuote: 1,1--2,5
238 MdParagraph: 1,3--2,5
240 MdSoftLineBreak: 1,6--1,6
243 assert md_to_loc
(md
) == loc
246 fun test_blockquotes_nested
is test
do
254 MdBlockQuote: 1,1--3,7
255 MdParagraph: 1,3--1,5
257 MdBlockQuote: 2,3--3,7
258 MdParagraph: 2,5--3,7
260 MdSoftLineBreak: 2,8--2,8
263 assert md_to_loc
(md
) == loc
266 fun test_blockquotes_headings
is test
do
272 MdDocument: 1,1--2,15
273 MdBlockQuote: 1,1--2,15
279 assert md_to_loc
(md
) == loc
282 fun test_blockquotes_thematic_breaks
is test
do
289 MdBlockQuote: 1,1--2,7
290 MdThematicBreak: 1,3--1,5
291 MdThematicBreak: 2,3--2,7
293 assert md_to_loc
(md
) == loc
296 fun test_blockquotes_indented_code
is test
do
302 MdDocument: 1,1--2,12
303 MdBlockQuote: 1,1--2,12
304 MdIndentedCodeBlock: 1,3--2,12
306 assert md_to_loc
(md
) == loc
309 fun test_blockquotes_fenced_code
is test
do
318 MdBlockQuote: 1,1--4,5
319 MdFencedCodeBlock: 1,3--4,5
321 assert md_to_loc
(md
) == loc
324 fun test_blockquotes_list
is test
do
330 MdDocument: 1,1--2,10
331 MdBlockQuote: 1,1--2,10
332 MdUnorderedList: 1,3--2,10
333 MdListItem: 1,3--1,10
334 MdParagraph: 1,5--1,10
336 MdListItem: 2,3--2,10
337 MdParagraph: 2,5--2,10
340 assert md_to_loc
(md
) == loc
343 fun test_unordered_lists
is test
do
350 MdUnorderedList: 1,1--2,8
352 MdParagraph: 1,3--1,8
355 MdParagraph: 2,3--2,8
358 assert md_to_loc
(md
) == loc
361 fun test_ordered_lists
is test
do
368 MdOrderedList: 1,1--2,9
370 MdParagraph: 1,4--1,9
373 MdParagraph: 2,4--2,9
376 assert md_to_loc
(md
) == loc
379 fun test_list_headings
is test
do
385 MdDocument: 1,1--2,15
386 MdUnorderedList: 1,1--2,15
387 MdListItem: 1,1--1,11
390 MdListItem: 2,1--2,15
394 assert md_to_loc
(md
) == loc
397 fun test_list_thematic_breaks
is test
do
404 MdUnorderedList: 1,1--2,7
406 MdThematicBreak: 1,3--1,5
408 MdThematicBreak: 2,3--2,7
410 assert md_to_loc
(md
) == loc
413 fun test_list_indented_codes
is test
do
419 MdDocument: 1,1--2,12
420 MdUnorderedList: 1,1--2,12
421 MdListItem: 1,1--1,12
422 MdIndentedCodeBlock: 1,3--1,12
423 MdListItem: 2,1--2,12
424 MdIndentedCodeBlock: 2,3--2,12
426 assert md_to_loc
(md
) == loc
429 fun test_list_fenced_codes
is test
do
438 MdUnorderedList: 1,1--4,5
440 MdFencedCodeBlock: 1,3--4,5
442 assert md_to_loc
(md
) == loc
445 fun test_list_blockquotes
is test
do
451 MdDocument: 1,1--2,10
452 MdUnorderedList: 1,1--2,10
453 MdListItem: 1,1--2,10
454 MdBlockQuote: 1,3--2,10
455 MdParagraph: 1,5--2,10
457 MdSoftLineBreak: 1,11--1,11
460 assert md_to_loc
(md
) == loc
463 fun test_list_pars
is test
do
472 MdUnorderedList: 1,1--4,8
474 MdParagraph: 1,3--2,8
476 MdSoftLineBreak: 1,9--1,9
479 MdParagraph: 4,3--4,8
482 assert md_to_loc
(md
) == loc
485 fun test_list_nested
is test
do
493 MdUnorderedList: 1,1--3,7
495 MdParagraph: 1,3--1,5
497 MdUnorderedList: 2,3--3,7
499 MdParagraph: 2,5--2,7
502 MdParagraph: 3,5--3,7
505 assert md_to_loc
(md
) == loc
508 fun test_emphasis
is test
do
510 An *emphasis* and a **strong emphasis**.
513 MdDocument: 1,1--1,40
514 MdParagraph: 1,1--1,40
516 MdEmphasis: 1,4--1,13
519 MdStrongEmphasis: 1,21--1,39
523 assert md_to_loc
(md
) == loc
526 fun test_emphasis_nested
is test
do
528 Another ***emphasis***.
531 MdDocument: 1,1--1,23
532 MdParagraph: 1,1--1,23
534 MdEmphasis: 1,9--1,22
535 MdStrongEmphasis: 1,10--1,21
539 assert md_to_loc
(md
) == loc
542 fun test_emphasis_nested2
is test
do
544 Another ****emphasis****.
547 MdDocument: 1,1--1,25
548 MdParagraph: 1,1--1,25
550 MdStrongEmphasis: 1,9--1,24
551 MdStrongEmphasis: 1,11--1,22
555 assert md_to_loc
(md
) == loc
558 fun test_emphasis_nested3
is test
do
560 Another *****emphasis*****.
563 MdDocument: 1,1--1,27
564 MdParagraph: 1,1--1,27
566 MdEmphasis: 1,9--1,26
567 MdStrongEmphasis: 1,10--1,25
568 MdStrongEmphasis: 1,12--1,23
572 assert md_to_loc
(md
) == loc
575 fun test_emphasis_bad
is test
do
577 Another ___ emphasis ___.
580 MdDocument: 1,1--1,25
581 MdParagraph: 1,1--1,25
584 assert md_to_loc
(md
) == loc
587 fun test_emphasis_bad2
is test
do
592 MdDocument: 1,1--1,19
593 MdParagraph: 1,1--1,19
596 assert md_to_loc
(md
) == loc
599 fun test_inline_code
is test
do
601 A `code` and another ``one``.
604 MdDocument: 1,1--1,29
605 MdParagraph: 1,1--1,29
612 assert md_to_loc
(md
) == loc
615 fun test_inline_code_bad
is test
do
617 A `code and another ``one``.
620 MdDocument: 1,1--1,28
621 MdParagraph: 1,1--1,28
626 assert md_to_loc
(md
) == loc
629 fun test_inline_autolink
is test
do
631 An <http://autolink>.
634 MdDocument: 1,1--1,21
635 MdParagraph: 1,1--1,21
641 assert md_to_loc
(md
) == loc
644 fun test_inline_autolink_bad
is test
do
649 MdDocument: 1,1--1,20
650 MdParagraph: 1,1--1,20
653 assert md_to_loc
(md
) == loc
656 fun test_inline_autolink_bad2
is test
do
661 MdDocument: 1,1--1,20
662 MdParagraph: 1,1--1,20
665 assert md_to_loc
(md
) == loc
668 fun test_inline_automail
is test
do
670 An <me.foo+@bar.baz>.
673 MdDocument: 1,1--1,21
674 MdParagraph: 1,1--1,21
680 assert md_to_loc
(md
) == loc
683 fun test_inline_link
is test
do
688 MdDocument: 1,1--1,15
689 MdParagraph: 1,1--1,15
695 assert md_to_loc
(md
) == loc
698 fun test_inline_link_with_title
is test
do
700 A [link](url/ "title").
703 MdDocument: 1,1--1,23
704 MdParagraph: 1,1--1,23
710 assert md_to_loc
(md
) == loc
713 fun test_inline_link_with_content
is test
do
715 A [`code` link](url/).
718 MdDocument: 1,1--1,22
719 MdParagraph: 1,1--1,22
726 assert md_to_loc
(md
) == loc
729 fun test_inline_link_bad
is test
do
734 MdDocument: 1,1--1,14
735 MdParagraph: 1,1--1,14
738 assert md_to_loc
(md
) == loc
741 fun test_inline_image
is test
do
746 MdDocument: 1,1--1,15
747 MdParagraph: 1,1--1,15
753 assert md_to_loc
(md
) == loc
756 fun test_inline_image_bad
is test
do
761 MdDocument: 1,1--1,14
762 MdParagraph: 1,1--1,14
765 assert md_to_loc
(md
) == loc
768 fun test_inline_link_ref
is test
do
775 MdDocument: 1,1--1,11
776 MdParagraph: 1,1--1,11
782 assert md_to_loc
(md
) == loc
785 fun test_inline_link_ref2
is test
do
792 MdDocument: 1,1--1,14
793 MdParagraph: 1,1--1,14
799 assert md_to_loc
(md
) == loc
802 fun test_inline_link_ref_bad
is test
do
809 MdDocument: 1,1--1,15
810 MdParagraph: 1,1--1,15
813 assert md_to_loc
(md
) == loc
816 fun test_inline_html
is test
do
818 An <br /> break line.
821 MdDocument: 1,1--1,21
822 MdParagraph: 1,1--1,21
824 MdHtmlInline: 1,4--1,9
827 assert md_to_loc
(md
) == loc
831 fun test_inline_html2
is test
do
833 An <a href="link">*emph*</a>.
836 MdDocument: 1,1--1,29
837 MdParagraph: 1,1--1,29
839 MdHtmlInline: 1,4--1,18
840 MdEmphasis: 1,19--1,24
842 MdHtmlInline: 1,25--1,28
845 assert md_to_loc
(md
) == loc
848 fun test_inline_escape
is test
do
850 A text with \\"escaped chars\\".
853 MdDocument: 1,1--1,30
854 MdParagraph: 1,1--1,30
857 assert md_to_loc
(md
) == loc
860 fun test_inline_soft_break
is test
do
866 MdDocument: 1,1--2,13
867 MdParagraph: 1,1--2,13
869 MdSoftLineBreak: 1,12--1,12
872 assert md_to_loc
(md
) == loc
875 fun test_inline_soft_break2
is test
do
876 var md
= """A text with \na hard break.\n"""
878 MdDocument: 1,1--2,13
879 MdParagraph: 1,1--2,13
881 MdSoftLineBreak: 1,12--1,13
884 assert md_to_loc
(md
) == loc
887 fun test_inline_hard_break
is test
do
893 MdDocument: 1,1--2,13
894 MdParagraph: 1,1--2,13
896 MdHardLineBreak: 1,12--1,13
899 assert md_to_loc
(md
) == loc
902 fun test_inline_hard_break2
is test
do
903 var md
= """A text with \na hard break.\n"""
905 MdDocument: 1,1--2,13
906 MdParagraph: 1,1--2,13
908 MdHardLineBreak: 1,12--1,14
911 assert md_to_loc
(md
) == loc