Debian Codegraph @

tests/test_extension.py

raw ◀ ▶
1 Debian patch touch this file
1 """Tests for the extension module."""
2
3 from __future__ import annotations
4
5 import re
6 from textwrap import dedent
7 from typing import TYPE_CHECKING
8
9 import pytest
10
11 if TYPE_CHECKING:
12 from markdown import Markdown
13
14 from mkdocstrings import MkdocstringsPlugin
15
16
17 @pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"footnotes": {}}]}], indirect=["ext_markdown"])
18 def test_multiple_footnotes(ext_markdown: Markdown) -> None:
19 """Assert footnotes don't get added to subsequent docstrings."""
20 output = ext_markdown.convert(
21 dedent(
22 """
23 Top.[^aaa]
24
25 ::: tests.fixtures.footnotes.func_a
26
27 ::: tests.fixtures.footnotes.func_b
28
29 ::: tests.fixtures.footnotes.func_c
30
31 [^aaa]: Top footnote
32 """,
33 ),
34 )
35 assert output.count("Footnote A") == 1
36 assert output.count("Footnote B") == 1
37 assert output.count("Top footnote") == 1
38
39
40 def test_markdown_heading_level(ext_markdown: Markdown) -> None:
41 """Assert that Markdown headings' level doesn't exceed heading_level."""
42 output = ext_markdown.convert("::: tests.fixtures.headings\n options:\n show_root_heading: true")
43 assert ">Foo</h3>" in output
44 assert ">Bar</h5>" in output
45 assert ">Baz</h6>" in output
46
47
48 def test_keeps_preceding_text(ext_markdown: Markdown) -> None:
49 """Assert that autodoc is recognized in the middle of a block and preceding text is kept."""
50 output = ext_markdown.convert("**preceding**\n::: tests.fixtures.headings")
51 assert "<strong>preceding</strong>" in output
52 assert ">Foo</h2>" in output
53 assert ":::" not in output
54
55
56 def test_reference_inside_autodoc(ext_markdown: Markdown) -> None:
57 """Assert cross-reference Markdown extension works correctly."""
58 output = ext_markdown.convert("::: tests.fixtures.cross_reference")
59 assert re.search(r"Link to <.*something\.Else.*>something\.Else<.*>\.", output)
60
61
62 def test_quote_inside_annotation(ext_markdown: Markdown) -> None:
63 """Assert that inline highlighting doesn't double-escape HTML."""
64 output = ext_markdown.convert("::: tests.fixtures.string_annotation.Foo")
65 assert ";hi&" in output
66 assert "&amp;" not in output
67
68
69 def test_html_inside_heading(ext_markdown: Markdown) -> None:
70 """Assert that headings don't double-escape HTML."""
71 output = ext_markdown.convert("::: tests.fixtures.html_tokens")
72 assert "&#39;&lt;" in output
73 assert "&amp;" not in output
74
75
76 @pytest.mark.parametrize(
77 ("ext_markdown", "expect_permalink"),
78 [
79 ({"markdown_extensions": [{"toc": {"permalink": "@@@"}}]}, "@@@"),
80 ({"markdown_extensions": [{"toc": {"permalink": "TeSt"}}]}, "TeSt"),
81 ({"markdown_extensions": [{"toc": {"permalink": True}}]}, "&para;"),
82 ],
83 indirect=["ext_markdown"],
84 )
•85 @pytest.mark.xfail(reason="The test do sometimes fail...")
86 def test_no_double_toc(ext_markdown: Markdown, expect_permalink: str) -> None:
87 """Assert that the 'toc' extension doesn't apply its modification twice."""
88 output = ext_markdown.convert(
89 dedent(
90 """
91 # aa
92
93 ::: tests.fixtures.headings
94 options:
95 show_root_toc_entry: false
96
97 # bb
98 """,
99 ),
100 )
101 assert output.count(expect_permalink) == 5
102 assert 'id="tests.fixtures.headings--foo"' in output
103 assert ext_markdown.toc_tokens == [ # ty: ignore[unresolved-attribute]
104 {
105 "level": 1,
106 "id": "aa",
107 "html": "aa",
108 "name": "aa",
109 "data-toc-label": "",
110 "children": [
111 {
112 "level": 2,
113 "id": "tests.fixtures.headings--foo",
114 "html": "Foo",
115 "name": "Foo",
116 "data-toc-label": "",
117 "children": [
118 {
119 "level": 4,
120 "id": "tests.fixtures.headings--bar",
121 "html": "Bar",
122 "name": "Bar",
123 "data-toc-label": "",
124 "children": [
125 {
126 "level": 6,
127 "id": "tests.fixtures.headings--baz",
128 "html": "Baz",
129 "name": "Baz",
130 "data-toc-label": "",
131 "children": [],
132 },
133 ],
134 },
135 ],
136 },
137 ],
138 },
139 {
140 "level": 1,
141 "id": "bb",
142 "html": "bb",
143 "name": "bb",
144 "data-toc-label": "",
145 "children": [],
146 },
147 ]
148
149
150 def test_use_custom_handler(ext_markdown: Markdown) -> None:
151 """Assert that we use the custom handler declared in an individual autodoc instruction."""
152 with pytest.raises(ModuleNotFoundError):
153 ext_markdown.convert("::: tests.fixtures.headings\n handler: not_here")
154
155
156 def test_register_every_identifier_alias(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> None:
157 """Assert that we don't preemptively register all identifiers of a rendered object."""
158 handler = plugin._handlers.get_handler("python") # ty: ignore[unresolved-attribute]
159 ids = ("id1", "id2", "id3")
160 handler.get_aliases = lambda _: ids # ty: ignore[invalid-assignment]
161 autorefs = ext_markdown.parser.blockprocessors["mkdocstrings"]._autorefs
162
163 class Page:
164 url = "foo"
165
166 autorefs.current_page = Page()
167 ext_markdown.convert("::: tests.fixtures.headings")
168 for identifier in ids:
169 assert identifier in autorefs._secondary_url_map
170
171
172 def test_use_options_yaml_key(ext_markdown: Markdown) -> None:
173 """Check that using the 'options' YAML key works as expected."""
174 assert "h1" in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 1")
175 assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 2")
176
177
178 def test_use_yaml_options_after_blank_line(ext_markdown: Markdown) -> None:
179 """Check that YAML options are detected even after a blank line."""
180 assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n\n options:\n heading_level: 2")
181
182
183 @pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"admonition": {}}]}], indirect=["ext_markdown"])
184 def test_removing_duplicated_headings(ext_markdown: Markdown) -> None:
185 """Assert duplicated headings are removed from the output."""
186 output = ext_markdown.convert(
187 dedent(
188 """
189 ::: tests.fixtures.headings_many.heading_1
190
191 !!! note
192
193 ::: tests.fixtures.headings_many.heading_2
194
195 ::: tests.fixtures.headings_many.heading_3
196 """,
197 ),
198 )
199 assert output.count(">Heading one<") == 1
200 assert output.count(">Heading two<") == 1
201 assert output.count(">Heading three<") == 1
202
203
204 def _assert_contains_in_order(items: list[str], string: str) -> None:
205 index = 0
206 for item in items:
207 assert item in string[index:]
208 index = string.index(item, index) + len(item)
209
210
211 @pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"attr_list": {}}]}], indirect=["ext_markdown"])
212 def test_backup_of_anchors(ext_markdown: Markdown) -> None:
213 """Anchors with empty `href` are backed up."""
214 output = ext_markdown.convert("::: tests.fixtures.markdown_anchors")
215
216 # Anchors with id and no href have been backed up and updated.
217 _assert_contains_in_order(
218 [
219 'id="anchor"',
220 'id="tests.fixtures.markdown_anchors--anchor"',
221 'id="heading-anchor-1"',
222 'id="tests.fixtures.markdown_anchors--heading-anchor-1"',
223 'id="heading-anchor-2"',
224 'id="tests.fixtures.markdown_anchors--heading-anchor-2"',
225 'id="heading-anchor-3"',
226 'id="tests.fixtures.markdown_anchors--heading-anchor-3"',
227 ],
228 output,
229 )
230
231 # Anchors with href and with or without id have been updated but not backed up.
232 _assert_contains_in_order(
233 [
234 'id="tests.fixtures.markdown_anchors--with-id"',
235 ],
236 output,
237 )
238 assert 'id="with-id"' not in output
239
240 _assert_contains_in_order(
241 [
242 'href="#tests.fixtures.markdown_anchors--has-href1"',
243 'href="#tests.fixtures.markdown_anchors--has-href2"',
244 ],
245 output,
246 )
247 assert 'href="#has-href1"' not in output
248 assert 'href="#has-href2"' not in output