| 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 "&" 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 "'<" in output |
| 73 |
assert "&" 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}}]}, "¶"), |
| 82 |
], |
| 83 |
indirect=["ext_markdown"], |
| 84 |
) |
| 85 |
def test_no_double_toc(ext_markdown: Markdown, expect_permalink: str) -> None: |
| 86 |
"""Assert that the 'toc' extension doesn't apply its modification twice.""" |
| 87 |
output = ext_markdown.convert( |
| 88 |
dedent( |
| 89 |
""" |
| 90 |
# aa |
| 91 |
|
| 92 |
::: tests.fixtures.headings |
| 93 |
options: |
| 94 |
show_root_toc_entry: false |
| 95 |
|
| 96 |
# bb |
| 97 |
""", |
| 98 |
), |
| 99 |
) |
| 100 |
assert output.count(expect_permalink) == 5 |
| 101 |
assert 'id="tests.fixtures.headings--foo"' in output |
| 102 |
assert ext_markdown.toc_tokens == [ # ty: ignore[unresolved-attribute] |
| 103 |
{ |
| 104 |
"level": 1, |
| 105 |
"id": "aa", |
| 106 |
"html": "aa", |
| 107 |
"name": "aa", |
| 108 |
"data-toc-label": "", |
| 109 |
"children": [ |
| 110 |
{ |
| 111 |
"level": 2, |
| 112 |
"id": "tests.fixtures.headings--foo", |
| 113 |
"html": "Foo", |
| 114 |
"name": "Foo", |
| 115 |
"data-toc-label": "", |
| 116 |
"children": [ |
| 117 |
{ |
| 118 |
"level": 4, |
| 119 |
"id": "tests.fixtures.headings--bar", |
| 120 |
"html": "Bar", |
| 121 |
"name": "Bar", |
| 122 |
"data-toc-label": "", |
| 123 |
"children": [ |
| 124 |
{ |
| 125 |
"level": 6, |
| 126 |
"id": "tests.fixtures.headings--baz", |
| 127 |
"html": "Baz", |
| 128 |
"name": "Baz", |
| 129 |
"data-toc-label": "", |
| 130 |
"children": [], |
| 131 |
}, |
| 132 |
], |
| 133 |
}, |
| 134 |
], |
| 135 |
}, |
| 136 |
], |
| 137 |
}, |
| 138 |
{ |
| 139 |
"level": 1, |
| 140 |
"id": "bb", |
| 141 |
"html": "bb", |
| 142 |
"name": "bb", |
| 143 |
"data-toc-label": "", |
| 144 |
"children": [], |
| 145 |
}, |
| 146 |
] |
| 147 |
|
| 148 |
|
| 149 |
def test_use_custom_handler(ext_markdown: Markdown) -> None: |
| 150 |
"""Assert that we use the custom handler declared in an individual autodoc instruction.""" |
| 151 |
with pytest.raises(ModuleNotFoundError): |
| 152 |
ext_markdown.convert("::: tests.fixtures.headings\n handler: not_here") |
| 153 |
|
| 154 |
|
| 155 |
def test_register_every_identifier_alias(plugin: MkdocstringsPlugin, ext_markdown: Markdown) -> None: |
| 156 |
"""Assert that we don't preemptively register all identifiers of a rendered object.""" |
| 157 |
handler = plugin._handlers.get_handler("python") # ty: ignore[unresolved-attribute] |
| 158 |
ids = ("id1", "id2", "id3") |
| 159 |
handler.get_aliases = lambda _: ids # ty: ignore[invalid-assignment] |
| 160 |
autorefs = ext_markdown.parser.blockprocessors["mkdocstrings"]._autorefs |
| 161 |
|
| 162 |
class Page: |
| 163 |
url = "foo" |
| 164 |
|
| 165 |
autorefs.current_page = Page() |
| 166 |
ext_markdown.convert("::: tests.fixtures.headings") |
| 167 |
for identifier in ids: |
| 168 |
assert identifier in autorefs._secondary_url_map |
| 169 |
|
| 170 |
|
| 171 |
def test_use_options_yaml_key(ext_markdown: Markdown) -> None: |
| 172 |
"""Check that using the 'options' YAML key works as expected.""" |
| 173 |
assert "h1" in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 1") |
| 174 |
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 2") |
| 175 |
|
| 176 |
|
| 177 |
def test_use_yaml_options_after_blank_line(ext_markdown: Markdown) -> None: |
| 178 |
"""Check that YAML options are detected even after a blank line.""" |
| 179 |
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n\n options:\n heading_level: 2") |
| 180 |
|
| 181 |
|
| 182 |
@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"admonition": {}}]}], indirect=["ext_markdown"]) |
| 183 |
def test_removing_duplicated_headings(ext_markdown: Markdown) -> None: |
| 184 |
"""Assert duplicated headings are removed from the output.""" |
| 185 |
output = ext_markdown.convert( |
| 186 |
dedent( |
| 187 |
""" |
| 188 |
::: tests.fixtures.headings_many.heading_1 |
| 189 |
|
| 190 |
!!! note |
| 191 |
|
| 192 |
::: tests.fixtures.headings_many.heading_2 |
| 193 |
|
| 194 |
::: tests.fixtures.headings_many.heading_3 |
| 195 |
""", |
| 196 |
), |
| 197 |
) |
| 198 |
assert output.count(">Heading one<") == 1 |
| 199 |
assert output.count(">Heading two<") == 1 |
| 200 |
assert output.count(">Heading three<") == 1 |
| 201 |
|
| 202 |
|
| 203 |
def _assert_contains_in_order(items: list[str], string: str) -> None: |
| 204 |
index = 0 |
| 205 |
for item in items: |
| 206 |
assert item in string[index:] |
| 207 |
index = string.index(item, index) + len(item) |
| 208 |
|
| 209 |
|
| 210 |
@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"attr_list": {}}]}], indirect=["ext_markdown"]) |
| 211 |
def test_backup_of_anchors(ext_markdown: Markdown) -> None: |
| 212 |
"""Anchors with empty `href` are backed up.""" |
| 213 |
output = ext_markdown.convert("::: tests.fixtures.markdown_anchors") |
| 214 |
|
| 215 |
# Anchors with id and no href have been backed up and updated. |
| 216 |
_assert_contains_in_order( |
| 217 |
[ |
| 218 |
'id="anchor"', |
| 219 |
'id="tests.fixtures.markdown_anchors--anchor"', |
| 220 |
'id="heading-anchor-1"', |
| 221 |
'id="tests.fixtures.markdown_anchors--heading-anchor-1"', |
| 222 |
'id="heading-anchor-2"', |
| 223 |
'id="tests.fixtures.markdown_anchors--heading-anchor-2"', |
| 224 |
'id="heading-anchor-3"', |
| 225 |
'id="tests.fixtures.markdown_anchors--heading-anchor-3"', |
| 226 |
], |
| 227 |
output, |
| 228 |
) |
| 229 |
|
| 230 |
# Anchors with href and with or without id have been updated but not backed up. |
| 231 |
_assert_contains_in_order( |
| 232 |
[ |
| 233 |
'id="tests.fixtures.markdown_anchors--with-id"', |
| 234 |
], |
| 235 |
output, |
| 236 |
) |
| 237 |
assert 'id="with-id"' not in output |
| 238 |
|
| 239 |
_assert_contains_in_order( |
| 240 |
[ |
| 241 |
'href="#tests.fixtures.markdown_anchors--has-href1"', |
| 242 |
'href="#tests.fixtures.markdown_anchors--has-href2"', |
| 243 |
], |
| 244 |
output, |
| 245 |
) |
| 246 |
assert 'href="#has-href1"' not in output |
| 247 |
assert 'href="#has-href2"' not in output |