[gtk-doc] mkhtml2: refactor convert() to make it testable



commit 7b92047665289cb2623da59180adfaadcebedcfa
Author: Stefan Sauer <ensonic users sf net>
Date:   Tue Feb 12 19:28:56 2019 +0100

    mkhtml2: refactor convert() to make it testable
    
    Separate logic from io and start a new test suite for converters.

 gtkdoc/mkhtml2.py | 32 ++++++++++++++++++--------------
 tests/mkhtml2.py  | 39 +++++++++++++++++++++++++++++++++++----
 2 files changed, 53 insertions(+), 18 deletions(-)
---
diff --git a/gtkdoc/mkhtml2.py b/gtkdoc/mkhtml2.py
index 945bfd4..fc458af 100644
--- a/gtkdoc/mkhtml2.py
+++ b/gtkdoc/mkhtml2.py
@@ -1630,6 +1630,22 @@ def generate_nav_nodes(files, node):
     return nav
 
 
+def convert_content(module, files, node, src_lang):
+    converter = convert_chunks.get(node.name)
+    if converter is None:
+        logging.warning('Add chunk converter for "%s"', node.name)
+        return []
+
+    ctx = {
+        'module': module,
+        'files': files,
+        'node': node,
+        'src-lang': src_lang,
+    }
+    ctx.update(generate_nav_nodes(files, node))
+    return converter(ctx)
+
+
 def convert(out_dir, module, files, node, src_lang):
     """Convert the docbook chunks to a html file.
 
@@ -1642,20 +1658,8 @@ def convert(out_dir, module, files, node, src_lang):
     logging.info('Writing: %s', node.filename)
     with open(os.path.join(out_dir, node.filename), 'wt',
               newline='\n', encoding='utf-8') as html:
-        ctx = {
-            'module': module,
-            'files': files,
-            'node': node,
-            'src-lang': src_lang,
-        }
-        ctx.update(generate_nav_nodes(files, node))
-
-        converter = convert_chunks.get(node.name)
-        if converter is not None:
-            for line in converter(ctx):
-                html.write(line)
-        else:
-            logging.warning('Add chunk converter for "%s"', node.name)
+        for line in convert_content(module, files, node, src_lang):
+            html.write(line)
 
 
 def create_devhelp2_toc(node):
diff --git a/tests/mkhtml2.py b/tests/mkhtml2.py
index da9dd2b..bb201e1 100755
--- a/tests/mkhtml2.py
+++ b/tests/mkhtml2.py
@@ -145,10 +145,10 @@ class TestDevhelp(unittest.TestCase):
 
 class TestNavNodes(unittest.TestCase):
 
-    def setUp(self):
-        logging.basicConfig(
-            level=logging.INFO,
-            format='%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(levelname)s:%(message)s')
+    # def setUp(self):
+    #     logging.basicConfig(
+    #         level=logging.INFO,
+    #         format='%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(levelname)s:%(message)s')
 
     def chunk_db(self, xml):
         root = etree.XML(xml)
@@ -185,5 +185,36 @@ class TestNavNodes(unittest.TestCase):
         self.assertIn('nav_next', nav)
 
 
+class TestConverter(unittest.TestCase):
+
+    xml_book = textwrap.dedent("""\
+        <book>
+          <bookinfo>
+            <title>test Reference Manual</title>
+          </bookinfo>
+        </book>""")
+
+    # def setUp(self):
+    #     logging.basicConfig(
+    #         level=logging.INFO,
+    #         format='%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(levelname)s:%(message)s')
+
+    def convert(self, xml, ix):
+        root = etree.XML(xml)
+        files = mkhtml2.chunk(root, 'test')
+        nodes = [f for f in PreOrderIter(files) if f.anchor is None]
+        return '\n'.join(mkhtml2.convert_content('test', nodes, nodes[ix], 'c'))
+
+    def test_convert_bool_is_html(self):
+        html = self.convert(self.xml_book, 0)
+        self.assertIn('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">', html)
+        self.assertIn('<html>', html)
+        self.assertIn('</html>', html)
+
+    def test_convert_bool_has_title(self):
+        html = self.convert(self.xml_book, 0)
+        self.assertIn('<title>test Reference Manual</title>', html)
+
+
 if __name__ == '__main__':
     unittest.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]