Skip to content

Commit d7ef0da

Browse files
committed
tests(register_role_mapping): Add Sphinx-like role
1 parent 421a588 commit d7ef0da

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

tests/test_docutils_roles.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# ruff: noqa: E501
22
"""Tests for docutils roles."""
33
import typing as t
4-
4+
from docutils import nodes
55
import pytest
66
from django.template import Context, Template
7-
7+
from docutils.parsers.rst.states import Inliner
88
from django_docutils.lib.roles import (
99
register_django_docutils_roles,
1010
register_role_mapping,
@@ -51,6 +51,58 @@ def test_register_django_docutils_roles() -> None:
5151
register_django_docutils_roles()
5252

5353

54+
class SphinxLikeRole:
55+
"""A base class copied from SphinxRole for testing class-based roles.
56+
57+
This class provides helper methods for Sphinx-like roles.
58+
59+
.. note:: The subclasses of this class might not work with docutils.
60+
This class is strongly coupled with Sphinx.
61+
"""
62+
63+
name: str #: The role name actually used in the document.
64+
rawtext: str #: A string containing the entire interpreted text input.
65+
text: str #: The interpreted text content.
66+
lineno: int #: The line number where the interpreted text begins.
67+
inliner: Inliner #: The ``docutils.parsers.rst.states.Inliner`` object.
68+
#: A dictionary of directive options for customisation
69+
#: (from the "role" directive).
70+
options: dict[str, t.Any]
71+
#: A list of strings, the directive content for customisation
72+
#: (from the "role" directive).
73+
content: t.Sequence[str]
74+
75+
def __call__(
76+
self,
77+
name: str,
78+
rawtext: str,
79+
text: str,
80+
lineno: int,
81+
inliner: Inliner,
82+
options: dict[str, t.Any] | None = None,
83+
content: t.Sequence[str] = (),
84+
) -> tuple[list[nodes.Node], list[t.Any]]:
85+
"""Return example class-based role."""
86+
self.rawtext = rawtext
87+
self.text = text
88+
self.lineno = lineno
89+
self.inliner = inliner
90+
self.options = options if options is not None else {}
91+
self.content = content
92+
93+
# guess role type
94+
if name:
95+
self.name = name.lower()
96+
return self.run()
97+
98+
def run(self) -> tuple[list[nodes.Node], list[t.Any]]:
99+
"""Run docutils role."""
100+
raise NotImplementedError
101+
102+
103+
MySphinxLikeRole = SphinxLikeRole()
104+
105+
54106
def test_register_role_mapping() -> None:
55107
"""Assertions for register_role_mapping()."""
56108
register_role_mapping({})
@@ -68,6 +120,19 @@ def test_register_role_mapping() -> None:
68120
}
69121
)
70122

123+
register_role_mapping(
124+
{
125+
"gh": (
126+
"tests.test_docutils_roles.MySphinxLikeRole",
127+
{
128+
"lowercase": True,
129+
"innernodeclass": "docutils.nodes.inline",
130+
"warn_dangling": True,
131+
},
132+
),
133+
}
134+
)
135+
71136

72137
GH_ROLE_TESTS: list[RoleContentFixture] = [
73138
RoleContentFixture(

0 commit comments

Comments
 (0)