Ejemplo n.º 1
0
class TestGtkDocExtension(unittest.TestCase):

    def setUp(self):
        self.doc_database = DocDatabase()
        self.link_resolver = LinkResolver(self.doc_database)
        self.link_resolver.add_link(Link("here.com", "foo", "foo"))
        self.link_resolver.add_link(Link("there.org", "there", "Test::test"))
        self.link_resolver.add_link(Link("wherever.biz", "wherever", "bar"))
        self.link_resolver.add_link(Link("whenever.net", "whenever", "Test"))
        self.link_resolver.add_link(Link("somewhere.me",
                                         "somewhere",
                                         "Test.baz"))
        self.link_resolver.add_link(Link("elsewhere.co",
                                         "elsewhere",
                                         "org.dbus.func"))

    def assertOutputs(self, inp, expected):
        ast, diagnostics = cmark.gtkdoc_to_ast(inp, self.link_resolver)
        out = cmark.ast_to_html(ast, self.link_resolver)[0]
        self.assertEqual(out, expected)
        return ast, diagnostics

    def test_existing_link(self):
        inp = u"this : #foo is a link !"
        self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')

    def test_modified_link(self):
        inp = u"this : #foo is a link !"
        ast, _ = self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')
        self.link_resolver.upsert_link(
            Link("there.com", "ze_foo", "foo"),
            overwrite_ref=True)
        out = cmark.ast_to_html(ast, self.link_resolver)[0]
        self.assertEqual(
            out,
            u'<p>this : <a href="there.com">ze_foo</a> is a link !</p>\n')

    def test_syntax_boundaries(self):
        # Make sure we don't parse type links inside words
        inp = u"this : yo#foo is a link !"
        self.assertOutputs(
            inp,
            u'<p>this : yo#foo is a link !</p>\n')

        # Make sure the function link syntax doesn't take precedence
        # over classic links.
        inp = u"this is [a link]() however"
        self.assertOutputs(
            inp,
            u'<p>this is <a href="">a link</a> however</p>\n')

        # Make sure we respect code blocks
        inp = u"And `this a code block`()"
        self.assertOutputs(
            inp,
            u'<p>And <code>this a code block</code>()</p>\n')

        inp = u"And `this #too`"
        self.assertOutputs(
            inp,
            u'<p>And <code>this #too</code></p>\n')

        # Boundaries should be acceptable here
        inp = u"bar()"
        self.assertOutputs(
            inp,
            u'<p><a href="wherever.biz">wherever</a></p>\n')

        inp = u"Linking to #Test: cool"
        self.assertOutputs(
            inp,
            u'<p>Linking to <a href="whenever.net">whenever</a>: cool</p>\n')

    def test_dbus_function_link(self):
        inp = u"org.dbus.func()\n"
        self.assertOutputs(
            inp,
            u'<p><a href="elsewhere.co">elsewhere</a></p>\n')

    def test_struct_field_link(self):
        inp = u"Linking to #Test.baz yo"
        self.assertOutputs(
            inp,
            u'<p>Linking to <a href="somewhere.me">somewhere</a> yo</p>\n')

    def test_qualified_links(self):
        inp = u' #Test::test is a link'
        self.assertOutputs(
            inp,
            u'<p><a href="there.org">there</a> is a link</p>\n')

    def test_param_no_match(self):
        inp = u'Should@not@match please'
        self.assertOutputs(
            inp,
            u'<p>Should@not@match please</p>\n')

    def test_param_ref(self):
        inp = u'Should @match please'
        self.assertOutputs(
            inp,
            u'<p>Should <em>match</em> please</p>\n')

    def test_preserve_links(self):
        inp = u'Should preserve [](http://this_link.com)'
        self.assertOutputs(
            inp,
            u'<p>Should preserve <a href="http://this_link.com"></a></p>\n')

    def test_preserve_anchor_links(self):
        inp = u'Should preserve [](#this-anchor-link)'
        self.assertOutputs(
            inp,
            u'<p>Should preserve <a href="#this-anchor-link"></a></p>\n')

    def test_wrong_link(self):
        inp = u'this #does_not_exist'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>this does_not_exist</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘does_not_exist’'))
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 5)

    def test_link_parsing_context(self):
        inp = u'A %NULL-terminated thing'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>A NULL-terminated thing</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘NULL’'))
        inp = u'A #Object::dashed-signal'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>A Object::dashed-signal</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘Object::dashed-signal’'))

    def test_wrong_function_link(self):
        inp = u'does_not_exist()'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>does_not_exist</p>\n')

        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘does_not_exist’'))
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 0)

    def test_wrong_multiline_link(self):
        inp = (u'a #wrong_link\n\n'
               'and #another_wrong_link\n'
               'and then #yet_another_wrong_link')
        _, diagnostics = cmark.gtkdoc_to_ast(inp, self.link_resolver)
        self.assertEqual(len(diagnostics), 3)
        diag = diagnostics[0]
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 2)
        diag = diagnostics[1]
        self.assertEqual(diag.lineno, 2)
        self.assertEqual(diag.column, 4)
        diag = diagnostics[2]
        self.assertEqual(diag.lineno, 3)
        self.assertEqual(diag.column, 9)
Ejemplo n.º 2
0
class TestGtkDocExtension(unittest.TestCase):

    def setUp(self):
        self.database = Database(None)
        self.link_resolver = LinkResolver(self.database)
        self.link_resolver.add_link(Link("here.com", "foo", "foo"))
        self.link_resolver.add_link(Link("there.org", "there", "Test::test"))
        self.link_resolver.add_link(Link("wherever.biz", "wherever", "bar"))
        self.link_resolver.add_link(Link("whenever.net", "whenever", "Test"))
        self.link_resolver.add_link(Link("somewhere.me",
                                         "somewhere",
                                         "Test.baz"))
        self.link_resolver.add_link(Link("elsewhere.co",
                                         "elsewhere",
                                         "org.dbus.func"))

    def assertOutputs(self, inp, expected):
        ast, diagnostics = cmark.gtkdoc_to_ast(inp, self.link_resolver)
        out = cmark.ast_to_html(ast, self.link_resolver)[0]
        self.assertEqual(out, expected)
        return ast, diagnostics

    def test_existing_link(self):
        inp = u"this : #foo is a link !"
        self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')

    def test_modified_link(self):
        inp = u"this : #foo is a link !"
        ast, _ = self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')
        self.link_resolver.upsert_link(
            Link("there.com", "ze_foo", "foo"),
            overwrite_ref=True)
        out = cmark.ast_to_html(ast, self.link_resolver)[0]
        self.assertEqual(
            out,
            u'<p>this : <a href="there.com">ze_foo</a> is a link !</p>\n')

    def test_code_block(self):
        inp = u"|[\nfoo\n]|"
        self.assertOutputs(
            inp, '<pre><code>foo\n</code></pre>\n')

    def test_titled_code_block(self):
        inp = u"|[\nfoo\n]| something"
        self.assertOutputs(
            inp, '<pre><code>foo\n</code></pre>\n<p>something</p>\n')
        inp = u"|[\nfoo\n ]|something"
        self.assertOutputs(
            inp, '<pre><code>foo\n</code></pre>\n<p>something</p>\n')

    def test_syntax_boundaries(self):
        # Make sure we don't parse type links inside words
        inp = u"this : yo#foo is a link !"
        self.assertOutputs(
            inp,
            u'<p>this : yo#foo is a link !</p>\n')

        # Make sure the function link syntax doesn't take precedence
        # over classic links.
        inp = u"this is [a link]() however"
        self.assertOutputs(
            inp,
            u'<p>this is <a href="">a link</a> however</p>\n')

        # Make sure we respect code blocks
        inp = u"And `this a code block`()"
        self.assertOutputs(
            inp,
            u'<p>And <code>this a code block</code>()</p>\n')

        inp = u"And `this #too`"
        self.assertOutputs(
            inp,
            u'<p>And <code>this #too</code></p>\n')

        # Boundaries should be acceptable here
        inp = u"bar()"
        self.assertOutputs(
            inp,
            u'<p><a href="wherever.biz">wherever</a></p>\n')

        inp = u"Linking to #Test: cool"
        self.assertOutputs(
            inp,
            u'<p>Linking to <a href="whenever.net">whenever</a>: cool</p>\n')

    def test_dbus_function_link(self):
        inp = u"org.dbus.func()\n"
        self.assertOutputs(
            inp,
            u'<p><a href="elsewhere.co">elsewhere</a></p>\n')

    def test_struct_field_link(self):
        inp = u"Linking to #Test.baz yo"
        self.assertOutputs(
            inp,
            u'<p>Linking to <a href="somewhere.me">somewhere</a> yo</p>\n')

    def test_qualified_links(self):
        inp = u' #Test::test is a link'
        self.assertOutputs(
            inp,
            u'<p><a href="there.org">there</a> is a link</p>\n')

    def test_param_no_match(self):
        inp = u'Should@not@match please'
        self.assertOutputs(
            inp,
            u'<p>Should@not@match please</p>\n')

    def test_param_ref(self):
        inp = u'Should @match please'
        self.assertOutputs(
            inp,
            u'<p>Should <em>match</em> please</p>\n')

    def test_preserve_links(self):
        inp = u'Should preserve [](http://this_link.com)'
        self.assertOutputs(
            inp,
            u'<p>Should preserve <a href="http://this_link.com"></a></p>\n')

    def test_preserve_anchor_links(self):
        inp = u'Should preserve [](#this-anchor-link)'
        self.assertOutputs(
            inp,
            u'<p>Should preserve <a href="#this-anchor-link"></a></p>\n')

    def test_wrong_link(self):
        inp = u'this #does_not_exist'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>this does_not_exist</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘does_not_exist’'))
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 5)

    def test_link_parsing_context(self):
        inp = u'A %NULL-terminated thing'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>A NULL-terminated thing</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘NULL’'))
        inp = u'A #Object::dashed-signal'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>A Object::dashed-signal</p>\n')
        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘Object::dashed-signal’'))

    def test_wrong_function_link(self):
        inp = u'does_not_exist()'
        _, diagnostics = self.assertOutputs(
            inp,
            u'<p>does_not_exist</p>\n')

        self.assertEqual(len(diagnostics), 1)
        diag = diagnostics[0]
        self.assertEqual(
            diag.message,
            (u'Trying to link to non-existing symbol ‘does_not_exist’'))
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 0)

    def test_wrong_multiline_link(self):
        inp = (u'a #wrong_link\n\n'
               'and #another_wrong_link\n'
               'and then #yet_another_wrong_link')
        _, diagnostics = cmark.gtkdoc_to_ast(inp, self.link_resolver)
        self.assertEqual(len(diagnostics), 3)
        diag = diagnostics[0]
        self.assertEqual(diag.lineno, 0)
        self.assertEqual(diag.column, 2)
        diag = diagnostics[1]
        self.assertEqual(diag.lineno, 2)
        self.assertEqual(diag.column, 4)
        diag = diagnostics[2]
        self.assertEqual(diag.lineno, 3)
        self.assertEqual(diag.column, 9)
Ejemplo n.º 3
0
class TestGtkDocExtension(unittest.TestCase):

    def setUp(self):
        self.doc_database = DocDatabase()
        self.link_resolver = LinkResolver(self.doc_database)
        self.link_resolver.add_link(Link("here.com", "foo", "foo"))
        self.link_resolver.add_link(Link("there.org", "there", "Test::test"))

    def assertOutputs(self, inp, expected):
        ast = cmark.gtkdoc_to_ast(inp, self.link_resolver)
        out = cmark.ast_to_html(ast, self.link_resolver)
        self.assertEqual(out, expected)
        return ast

    def test_existing_link(self):
        inp = u"this : #foo is a link !"
        self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')

    def test_modified_link(self):
        inp = u"this : #foo is a link !"
        ast = self.assertOutputs(
            inp, '<p>this : <a href="here.com">foo</a> is a link !</p>\n')
        self.link_resolver.upsert_link(
            Link("there.com", "ze_foo", "foo"),
            overwrite_ref=True)
        out = cmark.ast_to_html(ast, self.link_resolver)
        self.assertEqual(
            out,
            u'<p>this : <a href="there.com">ze_foo</a> is a link !</p>\n')

    def test_syntax_boundaries(self):
        # Make sure we don't parse type links inside words
        inp = u"this : yo#foo is a link !"
        self.assertOutputs(
            inp,
            u'<p>this : yo#foo is a link !</p>\n')

        # Make sure the function link syntax doesn't take precedence
        # over classic links.
        inp = u"this is [a link]() however"
        self.assertOutputs(
            inp,
            u'<p>this is <a href="">a link</a> however</p>\n')

        # Make sure we respect code blocks
        inp = u"And `this a code block`()"
        self.assertOutputs(
            inp,
            u'<p>And <code>this a code block</code>()</p>\n')

        inp = u"And `this #too`"
        self.assertOutputs(
            inp,
            u'<p>And <code>this #too</code></p>\n')

        # Boundaries should be acceptable here
        inp = u"function_link()"
        self.assertOutputs(
            inp,
            u'<p><a href="function_link"></a></p>\n')

    def test_qualified_links(self):
        inp = u' #Test::test is a link'
        self.assertOutputs(
            inp,
            u'<p><a href="there.org">there</a> is a link</p>\n')

    def test_param_no_match(self):
        inp = u'Should@not@match please'
        self.assertOutputs(
            inp,
            u'<p>Should@not@match please</p>\n')

    def test_param_ref(self):
        inp = u'Should @match please'
        self.assertOutputs(
            inp,
            u'<p>Should <em>match</em> please</p>\n')