# -*- coding: utf-8 -*- import unittest from textwrap import dedent from docutils import nodes from docutils.utils import new_document from docutils.readers import Reader from docutils.core import publish_parts from commonmark import Parser from recommonmark.parser import CommonMarkParser class TestParsing(unittest.TestCase): def assertParses(self, source, expected, alt=False): # noqa parser = CommonMarkParser() parser.parse(dedent(source), new_document('')) self.maxDiff = None self.assertMultiLineEqual( dedent(expected).lstrip(), dedent(parser.document.asdom().toprettyxml(indent=' ')), ) def test_heading(self): self.assertParses( """ # Heading 1 ## Heading 2 Body """, """
Heading 1
Heading 2 Body
""" ) def test_heading_inline(self): self.assertParses( """# Heading *foo*""", """
Heading \n\ <emphasis>foo</emphasis>
""" ) def test_paragraph(self): self.assertParses( """This is a paragraph""", """ This is a paragraph """ ) self.assertParses( """This is a paragraph *foo***bar**""", """ This is a paragraph \n\ foo bar """ ) self.assertParses( """ This is a paragraph This is a new line """, """ This is a paragraph This is a new line """ ) def test_entities(self): self.assertParses( u""" © """, u""" © """ ) def test_links(self): self.assertParses( """ This is a [link](http://example.com) """, """ This is a \n\ link """ ) self.assertParses( """ This is a [link][example] [example]: http://example.com "Example" """, """ This is a \n\ link """ ) self.assertParses( """ This is a [link][example] [example]: http://example.com """, """ This is a \n\ link """ ) self.assertParses( """ """, """ http://example.com """ ) self.assertParses( """ [link](/foo) """, """ link """ ) self.assertParses( """ [link](foo) """, """ link """ ) self.assertParses( """ **[link](foo)** """, """ link """ ) def test_known_schemes(self): self.assertParses( """ [https link](https://example.com) [http link](http://example.com) [mailto link](mailto:admin@example.com) [custom scheme](custom:example.com) [ref link](path/to/file:heading) [ref link with spaces]() """, """ https link http link mailto link custom scheme ref link ref link with spaces """ ) pass def test_image(self): self.assertParses( """ ![foo "handle quotes"](/url "title") """, """ foo "handle quotes" """ ) def test_inline_code(self): self.assertParses( """ This is `code` right? """, """ This is \n\ code right? """ ) def test_bullet_list(self): self.assertParses( """ * List item 1 * List item 2 * List item 3 """, """ List item 1 List item 2 List item 3 """ ) self.assertParses( """ * [List item 1](/1) * [List item 2](/2) * [List item 3](/3) """, """ List item 1 List item 2 List item 3 """ ) def test_enumerated_list(self): self.assertParses( """ 1. List item 1 2. List item 2 3. List item 3 """, """ List item 1 List item 2 List item 3 """ ) def test_code(self): self.assertParses( """ Code: #!/bin/sh python """, """ Code: #!/bin/sh python """ ) def test_block_quote(self): self.assertParses( """ > Here is a quoted list: > \n\ > * Item 1 > * Item 2 """, """ Here is a quoted list: Item 1 Item 2 """ ) def test_horizontal_rule(self): self.assertParses( """ Foo ---- Bar """, """ Foo Bar """ ) def test_html_inline(self): self.assertParses( """ Foo Blink """, """ Foo <blink> Blink </blink> """ ) def test_html_block(self): self.assertParses( """ Foo
Blink
""", """ Foo <div> <blink>Blink</blink> </div> """ ) def test_eval(self): self.assertParses( """ ```eval_rst .. image:: figure.png ``` """, """ \ .. image:: figure.png """ ) def test_linebreak(self): self.assertParses( """ line1 line2 """, """ line1 <br /> line2 """ ) if __name__ == '__main__': unittest.main()