File: cpp_nesting.py

package info (click to toggle)
exhale 0.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,616 kB
  • sloc: python: 9,057; cpp: 1,260; javascript: 915; f90: 29; ansic: 18; makefile: 16
file content (282 lines) | stat: -rw-r--r-- 11,425 bytes parent folder | download
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -*- coding: utf8 -*-
########################################################################################
# This file is part of exhale.  Copyright (c) 2017-2024, Stephen McDowell.             #
# Full BSD 3-Clause license available here:                                            #
#                                                                                      #
#                https://github.com/svenevs/exhale/blob/master/LICENSE                 #
########################################################################################
"""
Tests for the ``cpp_nesting`` project.
"""

from __future__ import unicode_literals

import os.path as osp
import re
import subprocess
from textwrap import dedent

from exhale.utils import heading_mark

from testing.base import ExhaleTestCase
from testing.decorators import confoverrides, no_cleanup
from testing.hierarchies import                                                        \
    class_hierarchy, compare_class_hierarchy, compare_file_hierarchy, file,            \
    file_hierarchy, page


# NOTE: See setUp / tearDown in CPPNestingPages below.  That file is being
# generated to test the page hierarchy, but we don't want it included in the
# tests here which assert the page hierarchy being empty.
# TODO: assert that the page hierarchy is actually empty.
@confoverrides(exhale_args={
    "exhaleDoxygenStdin": dedent("""\
        INPUT            = ../include
        EXCLUDE_PATTERNS = */page_town_rock*.hpp
    """)})
class CPPNesting(ExhaleTestCase):
    """
    Primary test class for project ``cpp_nesting``.
    """

    test_project = "cpp_nesting"
    """.. testproject:: cpp_nesting"""

    def test_hierarchies(self):
        """Verify the class and file hierarchies."""
        compare_class_hierarchy(self, class_hierarchy(self.class_hierarchy_dict()))
        compare_file_hierarchy(self, file_hierarchy(self.file_hierarchy_dict()))
        assert len(self.app.exhale_root.pages) == 0  # pages got excluded

    @confoverrides(exhale_args={"doxygenStripFromPath": "../include"})
    def test_hierarchies_stripped(self):
        """
        Verify the class and file hierarchies with ``doxygenStripFromPath=../include``.
        """
        compare_class_hierarchy(self, class_hierarchy(self.class_hierarchy_dict()))
        # dirty hack to pop off the first include/ directory without needing to know
        # the actual object that is the first and only key
        file_hierarchy_dict = self.file_hierarchy_dict()
        for key in file_hierarchy_dict:
            no_include = file_hierarchy_dict[key]
            break
        compare_file_hierarchy(self, file_hierarchy(no_include))


class CPPNestingPages(ExhaleTestCase):
    """
    Primary test class for project ``doxygenpage``.

    .. note::

        Setup for this test is particularly fickle.  There are only two test
        cases because there are only two file paths we generate.  Order of tests
        is not guaranteed, so each test needs to exclude it's alternate.  Eww.
    """

    test_project = "cpp_nesting"
    """.. testproject:: cpp_nesting"""

    def get_gen_page(self, page):
        """Return the generated node's rst document text as a string."""
        gen_file = osp.join(page.root_owner.root_directory, page.file_name)
        with open(gen_file, "r") as f:
            return f.read()

    def check_gen_page(self, page):
        """Verify expected link, title, and directive are on the page."""
        # NOTE: should *never* be called with p.refid == "indexpage", that is
        # not generated.
        page_contents = self.get_gen_page(page)
        link = "_page_{refid}".format(refid=page.refid)
        title = "{title}\n{underline}".format(
            title=page.title, underline=heading_mark(page.title, "="))
        directive = ".. doxygenpage:: {refid}".format(refid=page.refid)
        assert link in page_contents
        assert title in page_contents
        assert directive in page_contents

    def check_most_pages(self, uses_mainpage=True, main_refid="indexpage"):
        r"""Check the majority of the page generation.

        **Parameters**
            uses_mainpage (:class:`python:bool`)
                Whether or not ``\mainpage`` is used.

            main_refid (:class:`python:str`)
                If ``uses_mainpage`` is ``False``, then whatever ``\mainpage``
                was replaced to ``\page {main_refid}``.
        """
        # Checks that the right files are included or not included in the library
        # root document.  Specifically, whether these are found:
        #
        #     - .. include:: page_index.rst from doxygen \mainpage
        #     - .. include:: page_view_hierarchy.rst (from any \page not \mainpage)
        self.test_common()

        # Make sure nothing changed with the class / file hierarchies.
        compare_class_hierarchy(self, class_hierarchy(self.class_hierarchy_dict()))
        file_hierarchy_dict = self.file_hierarchy_dict()
        include_dir = set(file_hierarchy_dict.keys()).pop()
        # TODO: make this a parameter if we add more than two tests...
        if uses_mainpage:
            f_name = "page_town_rock.hpp"
            outer_page = "index"
        else:
            f_name = "page_town_rock_alt.hpp"
            outer_page = "overview"

        # In doxygen 1.8.x no pages appear, but in 1.9.x they exist and are required to
        # be added in order for tests to pass.
        doxygen_proc = subprocess.run(
            ["doxygen", "--version"], capture_output=True, check=True)
        doxygen_stdout = doxygen_proc.stdout.decode("utf8").strip()
        match = re.match(
            r"(\d+)\.(\d+)\.(\d+)", doxygen_proc.stdout.decode("utf-8").strip())
        try:
            major, minor, patch = [int(g) for g in match.groups()]
        except Exception as e:
            self.fail(
                f"Could not obtain doxygen version number from {doxygen_stdout}: {e}")

        if (major, minor) < (1, 9):
            page_hierarchy = {}
        else:
            page_hierarchy = {
                page(outer_page): {
                    page("intro"): {
                        page("more_nesting"): {},
                        page("more_nesting_redux"): {
                            page("more_nesting_redux_again"): {},
                            page("more_nesting_redux_again_again"): {}
                        }
                    },
                    page("advanced"): {}
                },
            }
        file_hierarchy_dict[include_dir][file(f_name)] = page_hierarchy

        compare_file_hierarchy(self, file_hierarchy(file_hierarchy_dict))

        # Validate the misc page hierarchy details.
        exhale_root = self.app.exhale_root
        all_pages = [p for p in exhale_root.all_nodes if p.kind == "page"]
        assert len(all_pages) == 7

        # \mainpage A simple manual
        # - \subpage intro
        # - \subpage advanced "Advanced usage"
        assert len(exhale_root.pages) == 1
        index = exhale_root.pages[0]
        assert index.refid == main_refid
        if not uses_mainpage:
            self.check_gen_page(index)
        assert len(index.children) == 2
        # NOTE: sort should keep intro in front of advanced.
        index_children = sorted(index.children)
        assert index_children[0].refid == "intro"
        assert index_children[1].refid == "advanced"

        # \page intro Introduction
        # - \subpage more_nesting
        # - \subpage more_nesting_redux
        intro = index_children[0]
        assert len(intro.children) == 2
        assert intro.children[0].refid == "more_nesting"
        assert intro.children[1].refid == "more_nesting_redux"
        self.check_gen_page(intro)

        # \page advanced Advanced Usage
        advanced = index_children[1]
        assert len(advanced.children) == 0
        self.check_gen_page(advanced)

        # \page more_nesting More Information
        mn = intro.children[0]
        assert len(mn.children) == 0
        self.check_gen_page(mn)

        # \page more_nesting_redux Even More Information
        mnr = intro.children[1]
        # - \subpage more_nesting_redux_again
        # - \subpage more_nesting_redux_again_again
        assert len(mnr.children) == 2
        assert mnr.children[0].refid == "more_nesting_redux_again"
        assert mnr.children[1].refid == "more_nesting_redux_again_again"
        self.check_gen_page(mnr)

        # \page more_nesting_redux_again Too Much Information
        mnra = mnr.children[0]
        assert len(mnra.children) == 0
        self.check_gen_page(mnra)

        # \page more_nesting_redux_again_again Way Too Much Information
        mnraa = mnr.children[1]
        assert len(mnraa.children) == 0
        self.check_gen_page(mnraa)

    @confoverrides(exhale_args={
        "rootFileTitle": "",
        "exhaleDoxygenStdin": dedent("""\
            INPUT            = ../include
            EXCLUDE_PATTERNS = */page_town_rock_alt.hpp
        """)})
    def test_hierarchies_primary_mainpage(self):
        """Verify the class, file, and page hierarchies."""
        self.check_most_pages(uses_mainpage=True)
        # Last but not least, we expect the hierarchy to *not* have indexpage.
        expected_page_hierarchy = dedent(r"""
            Page Hierarchy
            --------------

            - :ref:`page_intro`
                - :ref:`page_more_nesting`
                - :ref:`page_more_nesting_redux`
                    - :ref:`page_more_nesting_redux_again`
                    - :ref:`page_more_nesting_redux_again_again`
            - :ref:`page_advanced`
        """)
        with open(self.app.exhale_root.page_hierarchy_file, "r") as phf:
            assert expected_page_hierarchy in phf.read()

    @confoverrides(exhale_args={
        "exhaleDoxygenStdin": dedent("""\
            INPUT            = ../include
            EXCLUDE_PATTERNS = */page_town_rock.hpp
        """)})
    def test_hierarchies_primary_no_mainpage(self):
        """Verify the class, file, and page hierarchies."""
        self.check_most_pages(uses_mainpage=False, main_refid="overview")

        # Last but not least, we expect the hierarchy to *not* have indexpage.
        expected_page_hierarchy = dedent(r"""
            Page Hierarchy
            --------------

            - :ref:`page_overview`
                - :ref:`page_intro`
                    - :ref:`page_more_nesting`
                    - :ref:`page_more_nesting_redux`
                        - :ref:`page_more_nesting_redux_again`
                        - :ref:`page_more_nesting_redux_again_again`
                - :ref:`page_advanced`
        """)
        with open(self.app.exhale_root.page_hierarchy_file, "r") as phf:
            assert expected_page_hierarchy in phf.read()

    @no_cleanup
    @confoverrides(exhale_args={
        "rootFileTitle": "",
        "exhaleDoxygenStdin": dedent("""\
            INPUT            = ../include
            EXCLUDE_PATTERNS = */page_town_rock_alt.hpp
        """)})
    def test_html_output(self):
        """
        Verify exhale builds a project.

        This is not really a test.  But it can be helpful to view a test project build,
        in the ``testing/projects/cpp_nesting`` folder you can open
        ``docs_CPPNestingPages_test_html_output/_build/html/index.html`` to view.
        """
        self.app.build()