[gtk-doc] tests: refactor devhelp2 generation to make the code testable



commit d1fcf2febace14a58224ac721a28c1b6e530ce17
Author: Stefan Sauer <ensonic users sf net>
Date:   Wed Feb 6 21:42:29 2019 +0100

    tests: refactor devhelp2 generation to make the code testable
    
    Add a few tests for the devhelp2 output.

 gtkdoc/mkhtml2.py | 93 ++++++++++++++++++++++++++++++-------------------------
 tests/mkhtml2.py  | 57 ++++++++++++++++++++++++++++------
 2 files changed, 97 insertions(+), 53 deletions(-)
---
diff --git a/gtkdoc/mkhtml2.py b/gtkdoc/mkhtml2.py
index 1a776e1..abcf267 100644
--- a/gtkdoc/mkhtml2.py
+++ b/gtkdoc/mkhtml2.py
@@ -1700,54 +1700,61 @@ def create_devhelp2_refsect3_keyword(node, base_link, title, name):
         create_devhelp2_condition_attribs(node))
 
 
-def create_devhelp2(out_dir, module, xml, files):
-    with open(os.path.join(out_dir, module + '.devhelp2'), 'wt',
-              newline='\n', encoding='utf-8') as idx:
-        bookinfo_nodes = xml.xpath('/book/bookinfo')
-        title = ''
-        if bookinfo_nodes is not None:
-            bookinfo = bookinfo_nodes[0]
-            title = bookinfo.xpath('./title/text()')[0]
-            online_url = bookinfo.xpath('./releaseinfo/ulink[@role="online-location"]/@url')[0]
-            # TODO: support author too (see devhelp2.xsl)
-        # TODO: fixxref uses '--src-lang' to set the language
-        result = [
-            """<?xml version="1.0" encoding="utf-8" standalone="no"?>
-<book xmlns="http://www.devhelp.net/book"; title="%s" link="index.html" author="" name="%s" version="2" 
language="c" online="%s">
+def create_devhelp2_content(module, xml, files):
+    title = ''
+    online_attr = ''
+    bookinfo_nodes = xml.xpath('/book/bookinfo')
+    if len(bookinfo_nodes):
+        bookinfo = bookinfo_nodes[0]
+        title = bookinfo.xpath('./title/text()')[0]
+        online_url = bookinfo.xpath('./releaseinfo/ulink[@role="online-location"]/@url')[0]
+        if online_url:
+            online_attr = ' online="' + online_url + '"'
+        # TODO: support author too (see devhelp2.xsl)
+    # TODO: fixxref uses '--src-lang' to set the language, we have this in options too
+    result = [
+        """<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<book xmlns="http://www.devhelp.net/book"; title="%s" link="index.html" author="" name="%s" version="2" 
language="c"%s>
   <chapters>
-""" % (title, module, online_url)
-        ]
-        # toc
-        result.extend(create_devhelp2_toc(files[0].root))
-        result.append("""  </chapters>
+""" % (title, module, online_attr)
+    ]
+    # toc
+    result.extend(create_devhelp2_toc(files[0].root))
+    result.append("""  </chapters>
   <functions>
 """)
-        # keywords from all refsect2 and refsect3
-        refsect2 = etree.XPath('//refsect2[@role]')
-        refsect3_enum = 
etree.XPath('refsect3[@role="enum_members"]/informaltable/tgroup/tbody/row[@role="constant"]')
-        refsect3_enum_details = etree.XPath('entry[@role="enum_member_name"]/para')
-        refsect3_struct = 
etree.XPath('refsect3[@role="struct_members"]/informaltable/tgroup/tbody/row[@role="member"]')
-        refsect3_struct_details = etree.XPath('entry[@role="struct_member_name"]/para/structfield')
-        for node in files:
-            base_link = node.filename + '#'
-            refsect2_nodes = refsect2(node.xml)
-            for refsect2_node in refsect2_nodes:
-                result.append(create_devhelp2_refsect2_keyword(refsect2_node, base_link))
-                refsect3_nodes = refsect3_enum(refsect2_node)
-                for refsect3_node in refsect3_nodes:
-                    details_node = refsect3_enum_details(refsect3_node)[0]
-                    name = details_node.attrib['id']
-                    result.append(create_devhelp2_refsect3_keyword(refsect3_node, base_link, 
details_node.text, name))
-                refsect3_nodes = refsect3_struct(refsect2_node)
-                for refsect3_node in refsect3_nodes:
-                    details_node = refsect3_struct_details(refsect3_node)[0]
-                    name = details_node.attrib['id']
-                    result.append(create_devhelp2_refsect3_keyword(refsect3_node, base_link, name, name))
-
-        result.append("""  </functions>
+    # keywords from all refsect2 and refsect3
+    refsect2 = etree.XPath('//refsect2[@role]')
+    refsect3_enum = 
etree.XPath('refsect3[@role="enum_members"]/informaltable/tgroup/tbody/row[@role="constant"]')
+    refsect3_enum_details = etree.XPath('entry[@role="enum_member_name"]/para')
+    refsect3_struct = 
etree.XPath('refsect3[@role="struct_members"]/informaltable/tgroup/tbody/row[@role="member"]')
+    refsect3_struct_details = etree.XPath('entry[@role="struct_member_name"]/para/structfield')
+    for node in files:
+        base_link = node.filename + '#'
+        refsect2_nodes = refsect2(node.xml)
+        for refsect2_node in refsect2_nodes:
+            result.append(create_devhelp2_refsect2_keyword(refsect2_node, base_link))
+            refsect3_nodes = refsect3_enum(refsect2_node)
+            for refsect3_node in refsect3_nodes:
+                details_node = refsect3_enum_details(refsect3_node)[0]
+                name = details_node.attrib['id']
+                result.append(create_devhelp2_refsect3_keyword(refsect3_node, base_link, details_node.text, 
name))
+            refsect3_nodes = refsect3_struct(refsect2_node)
+            for refsect3_node in refsect3_nodes:
+                details_node = refsect3_struct_details(refsect3_node)[0]
+                name = details_node.attrib['id']
+                result.append(create_devhelp2_refsect3_keyword(refsect3_node, base_link, name, name))
+
+    result.append("""  </functions>
 </book>
 """)
-        for line in result:
+    return result
+
+
+def create_devhelp2(out_dir, module, xml, files):
+    with open(os.path.join(out_dir, module + '.devhelp2'), 'wt',
+              newline='\n', encoding='utf-8') as idx:
+        for line in create_devhelp2_content(module, xml, files):
             idx.write(line)
 
 
diff --git a/tests/mkhtml2.py b/tests/mkhtml2.py
index ba4d96c..bf7802b 100755
--- a/tests/mkhtml2.py
+++ b/tests/mkhtml2.py
@@ -30,10 +30,10 @@ from gtkdoc import mkhtml2
 
 class TestChunking(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 test_chunk_only_root_gives_single_chunk(self):
         root = etree.XML('<book />')
@@ -63,12 +63,12 @@ class TestChunking(unittest.TestCase):
         self.assertEqual(2, len(descendants))
 
 
-class TestXrefs(unittest.TestCase):
+class TestDataExtraction(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)
@@ -103,7 +103,44 @@ class TestXrefs(unittest.TestCase):
             </book>"""))
         mkhtml2.build_glossary(files)
         self.assertIn('API', mkhtml2.glossary)
-        self.assertEquals('Application Programming Interface', mkhtml2.glossary['API'])
+        self.assertEqual('Application Programming Interface', mkhtml2.glossary['API'])
+
+
+class TestDevhelp(unittest.TestCase):
+
+    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)
+        files = mkhtml2.chunk(root, 'test')
+        return root, [f for f in PreOrderIter(files) if f.anchor is None]
+
+    def test_create_devhelp_without_bookinfo(self):
+        root, files = self.chunk_db(textwrap.dedent("""\
+            <book>
+              <chapter id="chap1"><title>Intro</title></chapter>
+            </book>"""))
+        devhelp = mkhtml2.create_devhelp2_content('test', root, files)
+        self.assertNotIn('online', devhelp[0])
+
+    def test_create_devhelp_with_bookinfo(self):
+        root, files = self.chunk_db(textwrap.dedent("""\
+            <book>
+              <bookinfo>
+                <title>test Reference Manual</title>
+                <releaseinfo>
+                  The latest version of this documentation can be found on-line at
+                  <ulink role="online-location" 
url="http://www.example.com/tester/index.html";>online-site</ulink>.
+                </releaseinfo>
+              </bookinfo>
+              <chapter id="chap1"><title>Intro</title></chapter>
+            </book>"""))
+        devhelp = mkhtml2.create_devhelp2_content('test', root, files)
+        self.assertIn('online="http://www.example.com/tester/index.html";', devhelp[0])
+        self.assertIn('title="test Reference Manual"', devhelp[0])
 
 
 if __name__ == '__main__':


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