1
1
# ruff: noqa: E501
2
2
"""Tests for docutils roles."""
3
3
import typing as t
4
-
4
+ from docutils import nodes
5
5
import pytest
6
6
from django .template import Context , Template
7
-
7
+ from docutils . parsers . rst . states import Inliner
8
8
from django_docutils .lib .roles import (
9
9
register_django_docutils_roles ,
10
10
register_role_mapping ,
@@ -51,6 +51,58 @@ def test_register_django_docutils_roles() -> None:
51
51
register_django_docutils_roles ()
52
52
53
53
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
+
54
106
def test_register_role_mapping () -> None :
55
107
"""Assertions for register_role_mapping()."""
56
108
register_role_mapping ({})
@@ -68,6 +120,19 @@ def test_register_role_mapping() -> None:
68
120
}
69
121
)
70
122
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
+
71
136
72
137
GH_ROLE_TESTS : list [RoleContentFixture ] = [
73
138
RoleContentFixture (
0 commit comments