| 1 |
"""Test the non-trivial features in the :mod:`sphinx.addnodes` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from sphinx import addnodes |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from collections.abc import Iterator |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def sig_elements() -> Iterator[set[type[addnodes.desc_sig_element]]]: |
| 17 |
"""Fixture returning the current ``addnodes.SIG_ELEMENTS`` set.""" |
| 18 |
original = addnodes.SIG_ELEMENTS.copy() # safe copy of the current nodes |
| 19 |
yield {*addnodes.SIG_ELEMENTS} # temporary value to use during tests |
| 20 |
addnodes.SIG_ELEMENTS = original # restore the previous value |
| 21 |
|
| 22 |
|
| 23 |
def test_desc_sig_element_nodes( |
| 24 |
sig_elements: set[type[addnodes.desc_sig_element]], |
| 25 |
) -> None: |
| 26 |
"""Test the registration of ``desc_sig_element`` subclasses.""" |
| 27 |
# expected desc_sig_* node classes (must be declared *after* reloading |
| 28 |
# the module since otherwise the objects are not the correct ones) |
| 29 |
EXPECTED_SIG_ELEMENTS = { |
| 30 |
addnodes.desc_sig_space, |
| 31 |
addnodes.desc_sig_name, |
| 32 |
addnodes.desc_sig_operator, |
| 33 |
addnodes.desc_sig_punctuation, |
| 34 |
addnodes.desc_sig_keyword, |
| 35 |
addnodes.desc_sig_keyword_type, |
| 36 |
addnodes.desc_sig_literal_number, |
| 37 |
addnodes.desc_sig_literal_string, |
| 38 |
addnodes.desc_sig_literal_char, |
| 39 |
} |
| 40 |
|
| 41 |
assert addnodes.SIG_ELEMENTS == EXPECTED_SIG_ELEMENTS |
| 42 |
|
| 43 |
# create a built-in custom desc_sig_element (added to SIG_ELEMENTS) |
| 44 |
class BuiltInSigElementLikeNode(addnodes.desc_sig_element, _sig_element=True): |
| 45 |
pass |
| 46 |
|
| 47 |
# create a custom desc_sig_element (implicitly not added to SIG_ELEMENTS) |
| 48 |
class Custom1SigElementLikeNode(addnodes.desc_sig_element): |
| 49 |
pass |
| 50 |
|
| 51 |
# create a custom desc_sig_element (explicitly not added to SIG_ELEMENTS) |
| 52 |
class Custom2SigElementLikeNode(addnodes.desc_sig_element, _sig_element=False): |
| 53 |
pass |
| 54 |
|
| 55 |
assert BuiltInSigElementLikeNode in addnodes.SIG_ELEMENTS |
| 56 |
assert Custom1SigElementLikeNode not in addnodes.SIG_ELEMENTS |
| 57 |
assert Custom2SigElementLikeNode not in addnodes.SIG_ELEMENTS |