|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Pins the API entries #2234 adds for members that had a docstring but no directive. |
| 3 | +
|
| 4 | +* ``Protocol.context`` -- a public property of every protocol, listed on the |
| 5 | + ``Protocol`` autoclass and in the ``ProtocolBase`` property group beside its |
| 6 | + siblings. |
| 7 | +* ``IPX._next_exthdr_depth`` -- an override of the |
| 8 | + ``Internet._next_exthdr_depth`` hook, which ``internet.rst`` documents, so the |
| 9 | + override is contract too. |
| 10 | +* ``held_body``, ``FlagsField`` and ``PACKET_FLAGS`` in |
| 11 | + :mod:`pcapkit.protocols.schema.misc.pcapng` -- listed beside the length |
| 12 | + callbacks, the option fields and the ``autodata`` constants respectively. |
| 13 | +
|
| 14 | +Each entry is checked as declared on its page and as naming a real object, the |
| 15 | +textual precondition a plain (non-nitpicky) build would not notice losing. |
| 16 | +
|
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import importlib |
| 22 | +import pathlib |
| 23 | +import re |
| 24 | +import unittest |
| 25 | + |
| 26 | +ROOT = pathlib.Path(__file__).resolve().parents[2] |
| 27 | +API = ROOT / 'docs' / 'source' / 'pcapkit' / 'protocols' |
| 28 | + |
| 29 | +#: The pcapng schema module, whose helpers ``pcapng.rst`` documents. |
| 30 | +SCHEMA = 'pcapkit.protocols.schema.misc.pcapng' |
| 31 | + |
| 32 | + |
| 33 | +def _block(text: 'str', directive: 'str') -> 'str': |
| 34 | + """The body of the top-level ``directive`` in ``text``, up to the next unindented line.""" |
| 35 | + lines = text.splitlines() |
| 36 | + start = lines.index(directive) |
| 37 | + body = [] |
| 38 | + for line in lines[start + 1:]: |
| 39 | + if line and not line[0].isspace(): |
| 40 | + break |
| 41 | + body.append(line) |
| 42 | + return '\n'.join(body) |
| 43 | + |
| 44 | + |
| 45 | +def _group(block: 'str', directive: 'str') -> 'list[str]': |
| 46 | + """The names of a grouped ``directive`` (e.g. ``py:property``) inside ``block``, options left out.""" |
| 47 | + match = re.search(rf'^( *)\.\. {re.escape(directive)}:: (\S+)\n((?:\1 +[^:\s]\S*\n)*)', block + '\n', re.M) |
| 48 | + if match is None: |
| 49 | + return [] |
| 50 | + return [match.group(2)] + match.group(3).split() |
| 51 | + |
| 52 | + |
| 53 | +class ProtocolContextTests(unittest.TestCase): |
| 54 | + """``context`` is documented on ``Protocol`` and linked from ``ProtocolBase``.""" |
| 55 | + |
| 56 | + text = (API / 'protocol.rst').read_text(encoding='utf-8') |
| 57 | + |
| 58 | + def test_protocol_autoclass_lists_context(self) -> 'None': |
| 59 | + """Listed after ``schema``, the property it follows in the class.""" |
| 60 | + block = _block(self.text, '.. autoclass:: pcapkit.protocols.protocol.Protocol') |
| 61 | + self.assertIn(' .. autoproperty:: schema\n .. autoproperty:: context\n', block) |
| 62 | + |
| 63 | + def test_protocolbase_group_lists_and_links_context(self) -> 'None': |
| 64 | + """In the property group, and linked to its ``Protocol`` entry.""" |
| 65 | + block = _block(self.text, '.. autoclass:: pcapkit.protocols.protocol.ProtocolBase') |
| 66 | + self.assertIn('context', _group(block, 'py:property')) |
| 67 | + self.assertIn(':attr:`~Protocol.context`', block) |
| 68 | + |
| 69 | + def test_context_is_a_property_of_protocolbase(self) -> 'None': |
| 70 | + """The entry names a real property, defined where the group says.""" |
| 71 | + from pcapkit.protocols.protocol import ProtocolBase # pylint: disable=import-outside-toplevel |
| 72 | + self.assertIsInstance(ProtocolBase.__dict__.get('context'), property) |
| 73 | + |
| 74 | + |
| 75 | +class IPXNextExthdrDepthTests(unittest.TestCase): |
| 76 | + """The ``_next_exthdr_depth`` override on ``IPX`` has an entry.""" |
| 77 | + |
| 78 | + def test_ipx_autoclass_lists_the_override(self) -> 'None': |
| 79 | + """Listed on the ``IPX`` autoclass beside ``_make_data``.""" |
| 80 | + text = (API / 'internet' / 'ipx.rst').read_text(encoding='utf-8') |
| 81 | + block = _block(text, '.. autoclass:: pcapkit.protocols.internet.ipx.IPX') |
| 82 | + self.assertIn(' .. automethod:: _next_exthdr_depth\n', block) |
| 83 | + |
| 84 | + def test_the_hook_it_overrides_is_documented(self) -> 'None': |
| 85 | + """The base hook has its own entry, which makes the override contract.""" |
| 86 | + text = (API / 'internet' / 'internet.rst').read_text(encoding='utf-8') |
| 87 | + self.assertIn(' .. automethod:: _next_exthdr_depth\n', text) |
| 88 | + |
| 89 | + def test_ipx_defines_its_own_override(self) -> 'None': |
| 90 | + """The entry documents the method ``IPX`` defines, not an inherited one.""" |
| 91 | + from pcapkit.protocols.internet.ipx import IPX # pylint: disable=import-outside-toplevel |
| 92 | + self.assertIn('_next_exthdr_depth', IPX.__dict__) |
| 93 | + |
| 94 | + |
| 95 | +class PCAPNGSchemaHelperTests(unittest.TestCase): |
| 96 | + """``held_body``, ``FlagsField`` and ``PACKET_FLAGS`` have entries on ``pcapng.rst``.""" |
| 97 | + |
| 98 | + text = (API / 'misc' / 'pcapng.rst').read_text(encoding='utf-8') |
| 99 | + |
| 100 | + #: Each name, and the directive that documents it. |
| 101 | + ENTRIES = { |
| 102 | + 'held_body': f'.. autofunction:: {SCHEMA}.held_body\n', |
| 103 | + 'FlagsField': f'.. autoclass:: {SCHEMA}.FlagsField\n :members:\n :show-inheritance:\n', |
| 104 | + 'PACKET_FLAGS': f'.. autodata:: {SCHEMA}.PACKET_FLAGS\n :no-value:\n', |
| 105 | + } |
| 106 | + |
| 107 | + def test_each_entry_is_declared(self) -> 'None': |
| 108 | + """Each directive appears once, with the options its siblings carry.""" |
| 109 | + for name, directive in self.ENTRIES.items(): |
| 110 | + with self.subTest(name=name): |
| 111 | + self.assertEqual(self.text.count(directive), 1) |
| 112 | + |
| 113 | + def test_each_entry_names_a_real_object(self) -> 'None': |
| 114 | + """Each documented name exists in the schema module.""" |
| 115 | + module = importlib.import_module(SCHEMA) |
| 116 | + for name in self.ENTRIES: |
| 117 | + with self.subTest(name=name): |
| 118 | + self.assertTrue(hasattr(module, name)) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + unittest.main() |
0 commit comments