1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# SPDX-FileCopyrightText: 2021 Emmanuele Bassi
#
# SPDX-License-Identifier: GPL-3.0-or-later OR Apache-2.0
import xml.etree.ElementTree as ET
import os
import unittest
from gidocgen import gir, mdext, utils
class TestProcessLanguage(unittest.TestCase):
def test_process_language(self):
self.assertEqual(utils.process_language(None), "plain")
self.assertEqual(utils.process_language('<!-- language="C" -->'), "c")
self.assertEqual(utils.process_language(' <!-- language="xml" -->'), "xml")
self.assertEqual(utils.process_language('<!-- language="plain" -->'), "plain")
class TestLinkGenerator(unittest.TestCase):
@classmethod
def setUpClass(cls):
paths = []
paths.extend([os.path.join(os.getcwd(), "tests/data/gir")])
paths.extend(utils.default_search_paths())
parser = gir.GirParser(search_paths=paths, error=False)
parser.parse(os.path.join(os.getcwd(), "tests/data/gir", "GObject-2.0.gir"))
cls._repository = parser.get_repository()
@classmethod
def tearDownClass(cls):
cls._repository = None
def test_link_re(self):
"""
Test the link regular expression.
"""
text = "Some text [type@GObject.Value] other text"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
self.assertEqual(res.group('fragment'), 'type')
self.assertEqual(res.group('endpoint'), 'GObject.Value')
self.assertIsNone(res.group('anchor'))
self.assertIsNone(res.group('text'))
text = "Some text [with some text][type@GObject.Binding] other text"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
self.assertEqual(res.group('fragment'), 'type')
self.assertEqual(res.group('endpoint'), 'GObject.Binding')
self.assertIsNone(res.group('anchor'))
self.assertEqual(res.group('text'), '[with some text]')
text = "Some text [struct@GLib.Variant#serialized-data-memory] other text"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
self.assertEqual(res.group('fragment'), 'struct')
self.assertEqual(res.group('endpoint'), 'GLib.Variant')
self.assertEqual(res.group('anchor'), '#serialized-data-memory')
self.assertIsNone(res.group('text'))
def test_link_generator(self):
"""
Test LinkGenerator
"""
text = "Some text [with some, amazing, text][type@GObject.Binding#text] other text"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
fragment = res.group('fragment')
endpoint = res.group('endpoint')
anchor = res.group('anchor')
alt_text = res.group('text')
link = utils.LinkGenerator(line=text, start=res.start(), end=res.end(),
namespace=self._repository.namespace,
fragment=fragment, endpoint=endpoint, anchor=anchor,
text=alt_text)
self.assertIsNotNone(link)
root = ET.fromstring(str(link))
self.assertEqual(root.tag, 'a')
self.assertIn('href', root.attrib)
self.assertEqual(root.attrib['href'], 'class.Binding.html#text')
self.assertEqual(root.text, 'with some, amazing, text')
def test_link_error(self):
"""
Check that the LinkGenerator errors out when we expect it to.
"""
checks = [
"An [invalid fragment][enum@GObject.BindingFlags]",
"An [unknown namespace][class@InvalidNamespace.Object]",
"An [unknown fragment][foo@GObject.Object]",
"An [unknown type][type@GObject.Unknown]",
"An [unknown identifier][id@unknown_symbol]",
"An [unknown component][type@GObject.Object.Foo]",
]
for idx, c in enumerate(checks):
with self.subTest(msg=f"Link '{c}' should fail", idx=idx):
with self.assertRaises(utils.LinkParseError):
res = utils.LINK_RE.search(c)
self.assertIsNotNone(res)
utils.LinkGenerator(line=c, start=res.start(), end=res.end(),
namespace=self._repository.namespace,
fragment=res.group('fragment'),
endpoint=res.group('endpoint'),
text=res.group('text'),
do_raise=True)
def test_link_enum(self):
"""
Check that the enum types link to the corresponding item.
"""
text = "A value of [flags@GObject.BindingFlags]"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
fragment = res.group('fragment')
endpoint = res.group('endpoint')
alt_text = res.group('text')
link = utils.LinkGenerator(line=text, start=res.start(), end=res.end(),
namespace=self._repository.namespace,
fragment=fragment, endpoint=endpoint, text=alt_text)
self.assertIsNotNone(link)
root = ET.fromstring(str(link))
self.assertEqual(root.tag, 'a')
self.assertIn('href', root.attrib)
self.assertEqual(root.attrib['href'], 'flags.BindingFlags.html')
text = "A value of [flags@GObject.BindingFlags.SYNC_CREATE]"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
fragment = res.group('fragment')
endpoint = res.group('endpoint')
alt_text = res.group('text')
link = utils.LinkGenerator(line=text, start=res.start(), end=res.end(),
namespace=self._repository.namespace,
fragment=fragment, endpoint=endpoint, text=alt_text)
self.assertIsNotNone(link)
root = ET.fromstring(str(link))
self.assertEqual(root.tag, 'a')
self.assertIn('href', root.attrib)
self.assertEqual(root.attrib['href'], 'flags.BindingFlags.html#sync-create')
text = "A value of [flags@GObject.BindingFlags.INVALID_NAME]"
res = utils.LINK_RE.search(text)
self.assertIsNotNone(res)
fragment = res.group('fragment')
endpoint = res.group('endpoint')
alt_text = res.group('text')
with self.assertRaises(utils.LinkParseError):
utils.LinkGenerator(line=text, start=res.start(), end=res.end(),
namespace=self._repository.namespace,
fragment=fragment, endpoint=endpoint, text=alt_text,
do_raise=True)
class TestGtkDocExtension(unittest.TestCase):
def test_gtkdoc_sigils(self):
self.assertTrue(mdext.process_gtkdoc_sigils("will emit the #GCancellable::cancelled signal."),
"will emit the `GCancellable::cancelled` signal.")
self.assertTrue(mdext.process_gtkdoc_sigils("If @cancellable is %NULL,"),
"If `cancellable` is `NULL`,")
self.assertTrue(mdext.process_gtkdoc_sigils("A #GCancellable object."),
"A `GCancellable` object.")
self.assertTrue(mdext.process_gtkdoc_sigils("are two helper functions: g_cancellable_connect() and"),
"are two helper functions: `g_cancellable_connect()` and")
self.assertTrue(mdext.process_gtkdoc_sigils("#GDBusProxy:g-connection must be %NULL and will be set to the"),
"`GDBusProxy:g-connection` must be `NULL` and will be set to the")
self.assertTrue(mdext.process_gtkdoc_sigils("#GDBusProxy:g-name-owner. Connect to the #GObject::notify signal"),
"`GDBusProxy:g-name-owner`. Connect to the `GObject::notify` signal")
class TestDotRenderer(unittest.TestCase):
def test_render_dot(self):
graph_definition = "digraph G { a -> b; }"
svg_data = utils.render_dot(graph_definition, output_format="svg")
self.assertTrue("<svg" in svg_data)
self.assertTrue("</svg>" in svg_data)
self.assertTrue("<?xml" not in svg_data)
self.assertTrue("DOCTYPE" not in svg_data)
|